fluent-plugin-match_counter 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/fluent/plugin/match_counter.rb +82 -0
- data/test/test_helper.rb +54 -0
- metadata +127 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 01b4e141cefd46b67981b6e7a738482b690d18e6396e212addece81731867171
|
4
|
+
data.tar.gz: d9ccf51e029aef3a200d3c3dfafd68614b981ad773def796ab033d7a4d83956f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3a71764f5341b669db5d436200eb63281ab4cdb7b6e4f86cfff188a6d88ff7dbb684c2c9264fd3fa7e80536785cdc18da91af99dc614fac223da349ce2961279
|
7
|
+
data.tar.gz: 8fd3a83e13fdc5bb8d39b0ae83a3755d685fd97a7a8622b5a7d8da5568f5b60d2cf5ff434aba3b9601d927c067f5f92b96342a25793d5133e13d92631e0544f4
|
@@ -0,0 +1,82 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
require 'fluent/plugin/filter'
|
4
|
+
|
5
|
+
module Fluent
|
6
|
+
class MatchCounter < Plugin::Filter
|
7
|
+
Fluent::Plugin.register_filter('match_counter', self)
|
8
|
+
|
9
|
+
config_section :match_counter, required: true, multi: true do
|
10
|
+
# REQUIRED
|
11
|
+
desc 'Pattern to be matched'
|
12
|
+
config_param :matcher, :string
|
13
|
+
|
14
|
+
desc 'Name for counter output, "${tag}" will be replaced with record tag'
|
15
|
+
config_param :name, :string
|
16
|
+
|
17
|
+
# OPTIONAL
|
18
|
+
desc 'Input key, where match event is contained; if unset, then search entire event'
|
19
|
+
config_param :event_key, :string, default: nil
|
20
|
+
|
21
|
+
desc 'Use RegExp to match value'
|
22
|
+
config_param :regexp, :bool, default: true
|
23
|
+
|
24
|
+
desc 'Metric type passed to output'
|
25
|
+
config_param :type, :string, default: nil
|
26
|
+
|
27
|
+
desc 'Fields to be merged'
|
28
|
+
config_param :fields, :hash, default: nil
|
29
|
+
end
|
30
|
+
|
31
|
+
def configure(conf)
|
32
|
+
super
|
33
|
+
end
|
34
|
+
|
35
|
+
def filter(tag, time, record)
|
36
|
+
out = []
|
37
|
+
@match_counter.each do |mc|
|
38
|
+
str = \
|
39
|
+
begin
|
40
|
+
ek = mc[:event_key].to_sym
|
41
|
+
next unless ek.nil? || record.has_key?(ek)
|
42
|
+
|
43
|
+
record[ek]
|
44
|
+
rescue
|
45
|
+
record.to_s
|
46
|
+
end
|
47
|
+
|
48
|
+
if !!((mc[:regexp] && Regexp.new(mc[:matcher]).match(str)) || \
|
49
|
+
str.include?(mc[:matcher]))
|
50
|
+
|
51
|
+
name = mc[:name].clone
|
52
|
+
name.gsub!('${tag}', tag) if name.include?('${tag}')
|
53
|
+
|
54
|
+
met = {
|
55
|
+
name: name,
|
56
|
+
value: 1
|
57
|
+
}
|
58
|
+
met[:type] = mc[:type] unless mc[:type].nil?
|
59
|
+
met.merge!(mc[:fields]) unless mc[:fields].nil?
|
60
|
+
|
61
|
+
out << met
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
return nil if out.empty?
|
66
|
+
out
|
67
|
+
end
|
68
|
+
|
69
|
+
def filter_stream(tag, es)
|
70
|
+
new_es = Fluent::MultiEventStream.new
|
71
|
+
es.each do |time, record|
|
72
|
+
begin
|
73
|
+
new_es.add(time, filter(tag, time, record))
|
74
|
+
rescue => e
|
75
|
+
router.emit_error_event(tag, time, record, e)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
new_es
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'bundler/setup'
|
3
|
+
require 'test/unit'
|
4
|
+
require 'pry'
|
5
|
+
|
6
|
+
$LOAD_PATH.unshift File.expand_path('../lib', __dir__)
|
7
|
+
require 'test-unit'
|
8
|
+
require 'fluent/test'
|
9
|
+
require 'fluent/test/helpers'
|
10
|
+
require 'fluent/test/driver/filter'
|
11
|
+
require 'fluent/plugin/match_counter'
|
12
|
+
|
13
|
+
class Fluent::MatchCounterTestHelper < Test::Unit::TestCase
|
14
|
+
def setup
|
15
|
+
super
|
16
|
+
|
17
|
+
Fluent::Test.setup
|
18
|
+
@time = Fluent::Engine.now
|
19
|
+
@tag = 'test'
|
20
|
+
end
|
21
|
+
|
22
|
+
def create_driver(conf)
|
23
|
+
Fluent::Test::Driver::Filter.new(Fluent::MatchCounter).configure(conf)
|
24
|
+
end
|
25
|
+
|
26
|
+
def filter(messages, conf)
|
27
|
+
d = create_driver(conf)
|
28
|
+
|
29
|
+
d.run(default_tag: 'test', start: true, shutdown: false) do
|
30
|
+
messages.each do |message|
|
31
|
+
d.feed(@time, message)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
d.filtered_records
|
36
|
+
end
|
37
|
+
|
38
|
+
def config(s = '')
|
39
|
+
%[
|
40
|
+
<match_counter>
|
41
|
+
matcher "^message.+foo.+$"
|
42
|
+
event_key foo
|
43
|
+
name foo
|
44
|
+
type count
|
45
|
+
</match_counter>
|
46
|
+
<match_counter>
|
47
|
+
matcher bar
|
48
|
+
regexp false
|
49
|
+
event_key bar
|
50
|
+
name bar
|
51
|
+
</match_counter>
|
52
|
+
] + s
|
53
|
+
end
|
54
|
+
end
|
metadata
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fluent-plugin-match_counter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Joshua Mervine
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-12-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: pry
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: test-unit
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: benchmark-ips
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '2.9'
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: 2.9.2
|
65
|
+
type: :development
|
66
|
+
prerelease: false
|
67
|
+
version_requirements: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - "~>"
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '2.9'
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: 2.9.2
|
75
|
+
- !ruby/object:Gem::Dependency
|
76
|
+
name: fluentd
|
77
|
+
requirement: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: 0.14.0
|
82
|
+
- - "<"
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '2'
|
85
|
+
type: :runtime
|
86
|
+
prerelease: false
|
87
|
+
version_requirements: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: 0.14.0
|
92
|
+
- - "<"
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '2'
|
95
|
+
description: FluentD Plugin for counting matched events via a pattern
|
96
|
+
email:
|
97
|
+
- jmervine@users.noreply.github.com
|
98
|
+
executables: []
|
99
|
+
extensions: []
|
100
|
+
extra_rdoc_files: []
|
101
|
+
files:
|
102
|
+
- lib/fluent/plugin/match_counter.rb
|
103
|
+
- test/test_helper.rb
|
104
|
+
homepage:
|
105
|
+
licenses: []
|
106
|
+
metadata: {}
|
107
|
+
post_install_message:
|
108
|
+
rdoc_options: []
|
109
|
+
require_paths:
|
110
|
+
- lib
|
111
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
112
|
+
requirements:
|
113
|
+
- - ">="
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: 2.4.0
|
116
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
117
|
+
requirements:
|
118
|
+
- - ">="
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
requirements: []
|
122
|
+
rubygems_version: 3.1.4
|
123
|
+
signing_key:
|
124
|
+
specification_version: 4
|
125
|
+
summary: FluentD Plugin for counting matched events via a pattern
|
126
|
+
test_files:
|
127
|
+
- test/test_helper.rb
|