fluent-plugin-light-core 0.1.6 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +2 -0
- data/fluent-plugin-light-core.gemspec +2 -1
- data/lib/fluent/plugin/filter_light_core.rb +75 -44
- data/pkg/fluent-plugin-light-core-0.2.0.gem +0 -0
- data/sample/README.md +10 -0
- data/sample/source.conf +11 -2
- metadata +23 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: af0f4865440862be4488030659ec9798b8eb9261bc2f9b95ee093a0b5de28387
|
4
|
+
data.tar.gz: 5a46ab336bb472b47f6572d5836e6fbbfeea8075e19dfa6a6781d4e938ceddb7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 045f2e53eba3fcf2ac10aa1a625e539bff38f6221c4a776d4c7a8b5250868e8bda1b0c1c59b07da1458c86787f07e6f9d568e4cb73562b7a4d261aaa0a42be1b
|
7
|
+
data.tar.gz: 7272c0c901fd28c3aec6b9241060f8dab6e64f3cc91f40ab7a044f866b951e4b3aa4cc4ac9428029914e7104ca43840abceee5b88cae3e0d0a95928ed337311c
|
data/Gemfile
CHANGED
@@ -3,7 +3,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
3
3
|
|
4
4
|
Gem::Specification.new do |spec|
|
5
5
|
spec.name = "fluent-plugin-light-core"
|
6
|
-
spec.version = "0.1
|
6
|
+
spec.version = "0.2.1"
|
7
7
|
spec.authors = ["LIN LI"]
|
8
8
|
spec.email = ["l.li@alphabets.cn"]
|
9
9
|
|
@@ -24,4 +24,5 @@ Gem::Specification.new do |spec|
|
|
24
24
|
spec.add_development_dependency "rake", "~> 12.0"
|
25
25
|
spec.add_development_dependency "test-unit", "~> 3.0"
|
26
26
|
spec.add_runtime_dependency "fluentd", [">= 0.14.10", "< 2"]
|
27
|
+
spec.add_runtime_dependency "sentry-ruby", [">= 4.1.5", "< 5"]
|
27
28
|
end
|
@@ -14,8 +14,8 @@
|
|
14
14
|
# limitations under the License.
|
15
15
|
|
16
16
|
require 'fluent/plugin/filter'
|
17
|
-
require 'socket'
|
18
17
|
require 'json'
|
18
|
+
require 'sentry-ruby'
|
19
19
|
|
20
20
|
module Fluent
|
21
21
|
module Plugin
|
@@ -23,9 +23,8 @@ module Fluent
|
|
23
23
|
Fluent::Plugin.register_filter('light_core', self)
|
24
24
|
|
25
25
|
# 通知相关配置
|
26
|
-
config_param :
|
27
|
-
config_param :
|
28
|
-
config_param :host, :string, default: '255.255.255.255'
|
26
|
+
config_param :sentry, :bool, default: false
|
27
|
+
config_param :sentry_dsn, :string, default: ''
|
29
28
|
|
30
29
|
# Application 异常 settings
|
31
30
|
config_param :app_stream, :string, default: 'stderr'
|
@@ -35,32 +34,35 @@ module Fluent
|
|
35
34
|
|
36
35
|
# Nginx 异常 settings
|
37
36
|
config_param :lb_stream, :string, default: 'stderr'
|
38
|
-
config_param :lb_code, :array, default: ['500'], value_type: :string
|
39
|
-
config_param :lb_elapsed, :float, default:
|
37
|
+
config_param :lb_code, :array, default: ['400', '500'], value_type: :string
|
38
|
+
config_param :lb_elapsed, :float, default: 3
|
40
39
|
|
41
40
|
# MongoDB 异常 settings
|
42
41
|
config_param :mongo_severity, :array, default: ['F', 'E'], value_type: :string
|
43
42
|
config_param :mongo_querytime, :float, default: 100
|
44
43
|
|
45
|
-
# 初始化
|
44
|
+
# 初始化 Sentry
|
46
45
|
def start
|
47
46
|
super
|
48
47
|
|
49
|
-
if @
|
50
|
-
log.info('init
|
51
|
-
|
52
|
-
|
48
|
+
if @sentry
|
49
|
+
log.info('init sentry')
|
50
|
+
Sentry.init do |config|
|
51
|
+
config.dsn = @sentry_dsn
|
52
|
+
|
53
|
+
# To activate performance monitoring, set one of these options.
|
54
|
+
# We recommend adjusting the value in production:
|
55
|
+
config.traces_sample_rate = 1
|
56
|
+
|
57
|
+
# config.background_worker_threads = 2
|
58
|
+
config.transport.timeout = 10
|
59
|
+
config.transport.open_timeout = 10
|
60
|
+
end
|
53
61
|
end
|
54
62
|
end
|
55
63
|
|
56
|
-
# 清理
|
64
|
+
# 清理
|
57
65
|
def shutdown
|
58
|
-
|
59
|
-
if @notice
|
60
|
-
log.info('close udp connection')
|
61
|
-
@udp.close if @udp
|
62
|
-
end
|
63
|
-
|
64
66
|
super
|
65
67
|
end
|
66
68
|
|
@@ -88,11 +90,12 @@ module Fluent
|
|
88
90
|
|
89
91
|
# Parse the application log
|
90
92
|
def filter_app(tag, time, record)
|
91
|
-
file = record['file'].split('/').last.split('_')
|
92
|
-
log = record['log']
|
93
|
+
file = record['file'].split('/').last.split('_') # Parse log file name
|
94
|
+
log = record['log'] # Get detailed log content
|
93
95
|
|
94
96
|
# Set common items
|
95
|
-
record['environment'] = Socket.gethostname.split('-')[0]
|
97
|
+
# record['environment'] = Socket.gethostname.split('-')[0] # dev | prd
|
98
|
+
record['environment'] = ENV['FLUENTD_ENV'] # dev | prd
|
96
99
|
record['cid'] = file[0] # container id
|
97
100
|
record['cname'] = file[0].split('-')[1] # container name
|
98
101
|
record['ctime'] = record['time'] # container time
|
@@ -146,7 +149,7 @@ module Fluent
|
|
146
149
|
file = record['file'].split('/').last.split('_')
|
147
150
|
log = record['log']
|
148
151
|
|
149
|
-
record['environment'] =
|
152
|
+
record['environment'] = ENV['FLUENTD_ENV']
|
150
153
|
record['cid'] = file[0]
|
151
154
|
record['cname'] = tag
|
152
155
|
record['ctime'] = record['time']
|
@@ -157,7 +160,7 @@ module Fluent
|
|
157
160
|
|
158
161
|
# access log
|
159
162
|
if /^[^ ]+ [^ ]+ [^ ]+ \[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\+\d{2}:\d{2}\]/.match(log)
|
160
|
-
item = /^(?<remote>[^ ]*) (?<host>[^ ]*) (?<user>[^ ]*) \[(?<time>[^\]]*)\] "(?<method>\S+)(?: +(?<path>[^ ]*) +\S*)?" (?<code>[^ ]*) (?<size>[^ ]*)(?: "(?<referer>[^\"]*)" "(?<agent>[^\"]*)" "(?<
|
163
|
+
item = /^(?<remote>[^ ]*) (?<host>[^ ]*) (?<user>[^ ]*) \[(?<time>[^\]]*)\] "(?<method>\S+)(?: +(?<path>[^ ]*) +\S*)?" (?<code>[^ ]*) (?<size>[^ ]*)(?: "(?<referer>[^\"]*)" "(?<agent>[^\"]*)" "(?<elapsed>[^\"]*)" "(?<requestid>[^\"]*)" "(?<sessionid>[^\"]*)")?/.match(log)
|
161
164
|
|
162
165
|
record['remote'] = item[:remote]
|
163
166
|
record['host'] = item[:host]
|
@@ -169,8 +172,9 @@ module Fluent
|
|
169
172
|
record['size'] = format_str(item[:size])
|
170
173
|
record['referer'] = item[:referer]
|
171
174
|
record['agent'] = item[:agent]
|
172
|
-
record['forwarder'] = item[:forwarder]
|
173
175
|
record['elapsed'] = format_str(item[:elapsed])
|
176
|
+
record['requestid'] = item[:requestid]
|
177
|
+
record['sessionid'] = item[:sessionid]
|
174
178
|
|
175
179
|
return record
|
176
180
|
end
|
@@ -211,7 +215,7 @@ module Fluent
|
|
211
215
|
file = record['file'].split('/').last.split('_')
|
212
216
|
log = record['log']
|
213
217
|
|
214
|
-
record['environment'] =
|
218
|
+
record['environment'] = ENV['FLUENTD_ENV']
|
215
219
|
record['cid'] = file[0]
|
216
220
|
record['cname'] = tag
|
217
221
|
record['ctime'] = record['time']
|
@@ -220,6 +224,7 @@ module Fluent
|
|
220
224
|
record.delete('file')
|
221
225
|
record.delete('time')
|
222
226
|
|
227
|
+
# 旧版本日志格式解析
|
223
228
|
if /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}\+\d{4} [A-Z]/.match(log)
|
224
229
|
|
225
230
|
item = /^(?<time>[^ ]*) (?<severity>[A-Z])\s* (?<component>(-|([^ ]*)))\s* \[(?<context>[^\]]*)\]\s* ((?<query>.*) protocol:op_query (?<querytime>[\d\.]+(?=ms))|(?<message>.*))/.match(log)
|
@@ -247,6 +252,30 @@ module Fluent
|
|
247
252
|
end
|
248
253
|
end
|
249
254
|
|
255
|
+
return record
|
256
|
+
else
|
257
|
+
|
258
|
+
# 版本4.4开始,默认日志为json格式
|
259
|
+
item = JSON.parse(log)
|
260
|
+
record['time'] = item['t']['$date']
|
261
|
+
record['severity'] = item['s']
|
262
|
+
record['component'] = item['c']
|
263
|
+
record['context'] = item['ctx']
|
264
|
+
record['identifier'] = item['id']
|
265
|
+
record['message'] = item['msg']
|
266
|
+
|
267
|
+
attributes = item['attr']
|
268
|
+
if attributes
|
269
|
+
record['querytime'] = attributes['durationMillis']
|
270
|
+
record['collection'] = attributes['ns']
|
271
|
+
record['command'] = attributes['command']
|
272
|
+
record['attr'] = attributes
|
273
|
+
end
|
274
|
+
|
275
|
+
record['tags'] = item['tags']
|
276
|
+
record['truncated'] = item['truncated']
|
277
|
+
record['size'] = item['size']
|
278
|
+
|
250
279
|
return record
|
251
280
|
end
|
252
281
|
|
@@ -256,15 +285,18 @@ module Fluent
|
|
256
285
|
# 确认是否发送通知
|
257
286
|
def notice(tag, record)
|
258
287
|
|
259
|
-
unless @
|
288
|
+
unless @sentry
|
260
289
|
return record
|
261
290
|
end
|
262
291
|
|
263
292
|
if tag == 'app'
|
293
|
+
|
294
|
+
# 未使用
|
264
295
|
if @app_stream && record['stream']
|
265
296
|
send(record) if record['stream'] == @app_stream
|
266
297
|
end
|
267
298
|
|
299
|
+
# 未使用
|
268
300
|
if @app_message.length > 0 && record['message']
|
269
301
|
@app_message.each do |pattern|
|
270
302
|
if pattern.match(record['message'])
|
@@ -275,11 +307,13 @@ module Fluent
|
|
275
307
|
end
|
276
308
|
|
277
309
|
if @app_status.length > 0 && record['status']
|
278
|
-
|
310
|
+
message = 'Status code abnormal : ' + record['url']
|
311
|
+
send(tag, message, record) if @app_status.include?(record['status'])
|
279
312
|
end
|
280
313
|
|
281
314
|
if @app_elapsed > 0 && record['elapsed']
|
282
|
-
|
315
|
+
message = 'Slow process : ' + record['url']
|
316
|
+
send(tag, message, record) if record['elapsed'].to_f >= @app_elapsed
|
283
317
|
end
|
284
318
|
|
285
319
|
return record
|
@@ -287,15 +321,17 @@ module Fluent
|
|
287
321
|
|
288
322
|
if tag == 'lb'
|
289
323
|
if @lb_stream && record['stream']
|
290
|
-
send(record) if record['stream'] == @lb_stream
|
324
|
+
send(tag, 'Stderror', record) if record['stream'] == @lb_stream
|
291
325
|
end
|
292
326
|
|
293
327
|
if @lb_code.length > 0 && record['code']
|
294
|
-
|
328
|
+
message = 'Status code abnormal : ' + record['path']
|
329
|
+
send(tag, message, record) if @lb_code.include?(record['code'])
|
295
330
|
end
|
296
331
|
|
297
332
|
if @lb_elapsed > 0 && record['elapsed']
|
298
|
-
|
333
|
+
message = 'Slow request : ' + record['path']
|
334
|
+
send(tag, message, record) if record['elapsed'].to_f >= @lb_elapsed
|
299
335
|
end
|
300
336
|
|
301
337
|
return record
|
@@ -303,11 +339,14 @@ module Fluent
|
|
303
339
|
|
304
340
|
if tag == 'mongo'
|
305
341
|
if @mongo_severity.length > 0 && record['severity']
|
306
|
-
|
342
|
+
message = 'Severity level abnormal : ' + record['severity']
|
343
|
+
send(tag, message, record) if @mongo_severity.include?(record['severity'])
|
307
344
|
end
|
308
345
|
|
309
346
|
if @mongo_querytime > 0 && record['querytime']
|
310
|
-
|
347
|
+
message = 'Slow query'
|
348
|
+
message = message + ' : ' + record['collection'] if record['collection']
|
349
|
+
send(tag, message, record) if record['querytime'].to_f >= @mongo_querytime
|
311
350
|
end
|
312
351
|
|
313
352
|
return record
|
@@ -318,17 +357,9 @@ module Fluent
|
|
318
357
|
end
|
319
358
|
|
320
359
|
# 发送UDP请求
|
321
|
-
def send(record)
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
data = {
|
326
|
-
:query => '/api/log/notice',
|
327
|
-
:params => {
|
328
|
-
:data => record
|
329
|
-
}
|
330
|
-
}
|
331
|
-
@udp.send(data.to_json, 0, Socket.pack_sockaddr_in(@port, @host))
|
360
|
+
def send(tag, message, record)
|
361
|
+
log.debug('send udp notice')
|
362
|
+
Sentry.capture_message(message, :extra => record, :tags => {'log' => tag})
|
332
363
|
end
|
333
364
|
|
334
365
|
# 转数字
|
Binary file
|
data/sample/README.md
CHANGED
@@ -32,3 +32,13 @@ Initialized empty Git repository in /Users/lilin/developer/light/fluent-plugin-l
|
|
32
32
|
|
33
33
|
% rm -f sample/*.pos
|
34
34
|
% fluentd -c sample/source.conf -p lib/fluent/plugin
|
35
|
+
|
36
|
+
## 添加 sentry 依赖
|
37
|
+
|
38
|
+
- 编辑Gemfile
|
39
|
+
|
40
|
+
gem "sentry-ruby"
|
41
|
+
|
42
|
+
- 安装依赖
|
43
|
+
|
44
|
+
gem install sentry-ruby
|
data/sample/source.conf
CHANGED
@@ -14,8 +14,8 @@
|
|
14
14
|
|
15
15
|
<source>
|
16
16
|
@type tail
|
17
|
-
path sample/
|
18
|
-
pos_file sample/source.
|
17
|
+
path sample/ingress-nginx*.log
|
18
|
+
pos_file sample/source.ingress-nginx.pos
|
19
19
|
tag lb
|
20
20
|
format json
|
21
21
|
read_from_head true
|
@@ -79,6 +79,15 @@
|
|
79
79
|
|
80
80
|
<filter **>
|
81
81
|
@type light_core
|
82
|
+
sentry true
|
83
|
+
sentry_dsn https://4447403a2a86437491d307ceb72898e5@sentry.zf.link/4
|
84
|
+
mongo_querytime 300
|
85
|
+
mongo_severity F,E
|
86
|
+
lb_stream stderr
|
87
|
+
lb_code 400,500
|
88
|
+
lb_elapsed 5
|
89
|
+
app_status 500
|
90
|
+
app_elapsed 5
|
82
91
|
</filter>
|
83
92
|
|
84
93
|
# <match app>
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fluent-plugin-light-core
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- LIN LI
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-01
|
11
|
+
date: 2021-02-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -72,6 +72,26 @@ dependencies:
|
|
72
72
|
- - "<"
|
73
73
|
- !ruby/object:Gem::Version
|
74
74
|
version: '2'
|
75
|
+
- !ruby/object:Gem::Dependency
|
76
|
+
name: sentry-ruby
|
77
|
+
requirement: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: 4.1.5
|
82
|
+
- - "<"
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '5'
|
85
|
+
type: :runtime
|
86
|
+
prerelease: false
|
87
|
+
version_requirements: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: 4.1.5
|
92
|
+
- - "<"
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '5'
|
75
95
|
description: light core fluent plugin. support mongodb, nginx and application
|
76
96
|
email:
|
77
97
|
- l.li@alphabets.cn
|
@@ -87,6 +107,7 @@ files:
|
|
87
107
|
- Rakefile
|
88
108
|
- fluent-plugin-light-core.gemspec
|
89
109
|
- lib/fluent/plugin/filter_light_core.rb
|
110
|
+
- pkg/fluent-plugin-light-core-0.2.0.gem
|
90
111
|
- sample/README.md
|
91
112
|
- sample/source.conf
|
92
113
|
- sample/udpsample.rb
|