fluent-plugin-groupcounter 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +2 -1
- data/README.md +13 -3
- data/fluent-plugin-groupcounter.gemspec +1 -1
- data/lib/fluent/plugin/out_groupcounter.rb +14 -0
- data/spec/out_groupcounter_spec.rb +32 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c8cc4bf62fb23f54f31a2538cdf2ec1218e2970e
|
4
|
+
data.tar.gz: 8fe568adf912877846f3c26c0bb1426c2c0dba34
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 13d484566258d0a0ce4b260baaebdfabb2763dcb5df908c684be4a8577173b6dbd2ddd590bbbf16ac170a080bb4b8a7675eefea15c72eed2152cd8c374b204e8
|
7
|
+
data.tar.gz: 93178c805d0fe9a19a206cfd3098a64cc51685f4daa0fb767a2bec45b25dc77b3f0656f85d075f2157b7b41ec63e83d0f88b05e17d1a0cd5d8e437b854e5461f
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -7,7 +7,7 @@ Fluentd plugin to count like SELECT COUNT(\*) GROUP BY.
|
|
7
7
|
Assume inputs are coming as followings:
|
8
8
|
|
9
9
|
apache.access: {"code":"200", "method":"GET", "path":"/index.html", "reqtime":"1.001" }
|
10
|
-
apache.access: {"code":"
|
10
|
+
apache.access: {"code":"202", "method":"GET", "path":"/foo.html", "reqtime":"2.002" }
|
11
11
|
apache.access: {"code":"200", "method":"GET", "path":"/index.html", "reqtime":"3.003" }
|
12
12
|
|
13
13
|
Think of quering `SELECT COUNT(\*) GROUP BY code,method,path`. Configuration becomes as below:
|
@@ -22,7 +22,7 @@ Think of quering `SELECT COUNT(\*) GROUP BY code,method,path`. Configuration bec
|
|
22
22
|
|
23
23
|
Output becomes like
|
24
24
|
|
25
|
-
groupcounter.apache.access: {"200_GET_/index.html_count":2, "
|
25
|
+
groupcounter.apache.access: {"200_GET_/index.html_count":2, "202_GET_/foo.html_count":1}
|
26
26
|
|
27
27
|
## Parameters
|
28
28
|
|
@@ -34,6 +34,16 @@ Output becomes like
|
|
34
34
|
|
35
35
|
Specify the delimiter to join `group_by_keys`. Default is '_'.
|
36
36
|
|
37
|
+
* pattern\[1-20\]
|
38
|
+
|
39
|
+
Use `patternX` option to apply grouping more roughly. For example, adding a configuration for the above example as below
|
40
|
+
|
41
|
+
pattern1 2xx ^2\d\d
|
42
|
+
|
43
|
+
gives you an ouput like
|
44
|
+
|
45
|
+
groupcounter.apache.access: {"2xx_GET_/index.html_count":3}
|
46
|
+
|
37
47
|
* group\_by\_expression (semi-required)
|
38
48
|
|
39
49
|
Use an expression to group the event record. `group_by_keys` or `group_by_expression` is required.
|
@@ -89,7 +99,7 @@ Output becomes like
|
|
89
99
|
|
90
100
|
gives you an output like
|
91
101
|
|
92
|
-
groupcounter.apache.access: {"200_GET_/index.html_reqtime_max":3.003, "
|
102
|
+
groupcounter.apache.access: {"200_GET_/index.html_reqtime_max":3.003, "202_GET_/foo.html_reqtime_max":2.002}
|
93
103
|
|
94
104
|
* min\_key
|
95
105
|
|
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
|
|
3
3
|
|
4
4
|
Gem::Specification.new do |s|
|
5
5
|
s.name = "fluent-plugin-groupcounter"
|
6
|
-
s.version = "0.2.
|
6
|
+
s.version = "0.2.1"
|
7
7
|
s.authors = ["Ryosuke IWANAGA", "Naotoshi SEO"]
|
8
8
|
s.email = ["@riywo", "sonots@gmail.com"]
|
9
9
|
s.homepage = "https://github.com/riywo/fluent-plugin-groupcounter"
|
@@ -1,6 +1,8 @@
|
|
1
1
|
class Fluent::GroupCounterOutput < Fluent::Output
|
2
2
|
Fluent::Plugin.register_output('groupcounter', self)
|
3
3
|
|
4
|
+
PATTERN_MAX_NUM = 20
|
5
|
+
|
4
6
|
def initialize
|
5
7
|
super
|
6
8
|
require 'pathname'
|
@@ -24,6 +26,7 @@ class Fluent::GroupCounterOutput < Fluent::Output
|
|
24
26
|
config_param :min_suffix, :string, :default => '_min'
|
25
27
|
config_param :avg_suffix, :string, :default => '_avg'
|
26
28
|
config_param :store_file, :string, :default => nil
|
29
|
+
(1..PATTERN_MAX_NUM).each {|i| config_param "pattern#{i}".to_sym, :string, :default => nil }
|
27
30
|
|
28
31
|
attr_accessor :count_interval
|
29
32
|
attr_accessor :counts
|
@@ -69,6 +72,14 @@ class Fluent::GroupCounterOutput < Fluent::Output
|
|
69
72
|
|
70
73
|
@group_by_keys = @group_by_keys.split(',') if @group_by_keys
|
71
74
|
|
75
|
+
@pattern = {}
|
76
|
+
(1..PATTERN_MAX_NUM).each do |i|
|
77
|
+
next unless conf["pattern#{i}"]
|
78
|
+
replace, regexp = conf["pattern#{i}"].split(/ +/, 2)
|
79
|
+
raise Fluent::ConfigError, "pattern#{i} does not contain 2 parameters" unless regexp
|
80
|
+
@pattern[replace] = Regexp.compile(regexp)
|
81
|
+
end
|
82
|
+
|
72
83
|
if @store_file
|
73
84
|
f = Pathname.new(@store_file)
|
74
85
|
if (f.exist? && !f.writable_real?) || (!f.exist? && !f.parent.writable_real?)
|
@@ -231,6 +242,9 @@ class Fluent::GroupCounterOutput < Fluent::Output
|
|
231
242
|
group_key = values.join(@delimiter)
|
232
243
|
end
|
233
244
|
group_key = group_key.to_s.force_encoding('ASCII-8BIT')
|
245
|
+
|
246
|
+
@pattern.each {|replace, regexp| break if group_key.gsub!(regexp, replace) }
|
247
|
+
group_key
|
234
248
|
end
|
235
249
|
|
236
250
|
def sum(a, b)
|
@@ -347,6 +347,38 @@ describe Fluent::GroupCounterOutput do
|
|
347
347
|
it { emit }
|
348
348
|
end
|
349
349
|
|
350
|
+
context 'pattern' do
|
351
|
+
# NOTE: \\d should be just \d in config file
|
352
|
+
let(:config) { CONFIG + %[
|
353
|
+
group_by_expression ${method}_${path.split("?")[0].split("/")[2]}/${code}
|
354
|
+
delimiter _
|
355
|
+
pattern1 201 201$
|
356
|
+
pattern2 2xx 2\\d\\d$
|
357
|
+
pattern3 4xx 4\\d\\d$
|
358
|
+
]}
|
359
|
+
let(:messages) do
|
360
|
+
[
|
361
|
+
{"code" => "200", "method" => "GET", "path" => "/api/people/@me/@self?count=1", "reqtime" => 0.000 },
|
362
|
+
{"code" => "200", "method" => "POST", "path" => "/api/ngword?_method=check", "reqtime" => 1.001 },
|
363
|
+
{"code" => "400", "method" => "GET", "path" => "/api/messages/@me/@outbox", "reqtime" => 2.002 },
|
364
|
+
{"code" => "201", "method" => "GET", "path" => "/api/people/@me/@self", "reqtime" => 3.003 },
|
365
|
+
]
|
366
|
+
end
|
367
|
+
let(:expected) do
|
368
|
+
{
|
369
|
+
"GET_people/201_count"=>1,
|
370
|
+
"GET_people/2xx_count"=>1,
|
371
|
+
"POST_ngword/2xx_count"=>1,
|
372
|
+
"GET_messages/4xx_count"=>1,
|
373
|
+
}
|
374
|
+
end
|
375
|
+
before do
|
376
|
+
Fluent::Engine.stub(:now).and_return(time)
|
377
|
+
Fluent::Engine.should_receive(:emit).with("count.#{tag}", time, expected)
|
378
|
+
end
|
379
|
+
it { emit }
|
380
|
+
end
|
381
|
+
|
350
382
|
end
|
351
383
|
end
|
352
384
|
|