fluent-plugin-label-router 0.2.4 → 0.2.5

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e7a22624dbabbce0daa1bcf3d6845bf15e377d9453a5e733ad6c91b1c9d886d5
4
- data.tar.gz: 5cb0f6eed3e37706bd2f09c0f399acd4fb6dc714aadb7d9879309575904dccfe
3
+ metadata.gz: 7f98b66b8fcb6667342e2c8e30b1c20eaa07f949eab93514cceba98872502d7f
4
+ data.tar.gz: 7316dcea967a1dc2160b7996fdd8718d307809e86ab7900f5195b7b7f8e57b3e
5
5
  SHA512:
6
- metadata.gz: f1240df83181e1e6188121c48dbffc4fbe226c756064155e1457dd2478aae79d6740dcb5b403202fd2ef0d55b7850f1937e6aed9f414dc73615f24f5ae3e0f2a
7
- data.tar.gz: 13093eb11d980427a8bc5540612da4bca7a199a6aa848c2054ec157a930b4db372cd6c6ceed4ec854f783e165bfe7448f6ad53add6409f150460b49f522ea1e4
6
+ metadata.gz: c180a282410db01da6f356598cda074390bcfa56e02fa53d8197605228a5b20679f17af766054de9109906829f6bb8efd43d46a968d8f7ad5a05618a0d41c539
7
+ data.tar.gz: 2b74fcc7c70de9fd1cd06ff9f2d070befebc48544962e2eba828c72a131f79358d2f0f877af332f236da18c409cce52425babf24853af706cacbcafc3d68ddf2
@@ -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-label-router"
6
- spec.version = "0.2.4"
6
+ spec.version = "0.2.5"
7
7
  spec.authors = ["Banzai Cloud"]
8
8
  spec.email = ["info@banzaicloud.com"]
9
9
 
@@ -23,5 +23,6 @@ Gem::Specification.new do |spec|
23
23
  spec.add_development_dependency "bundler", "~> 1.14"
24
24
  spec.add_development_dependency "rake", "~> 12.0"
25
25
  spec.add_development_dependency "test-unit", "~> 3.0"
26
+ spec.add_dependency "prometheus-client", ">= 2.1.0"
26
27
  spec.add_runtime_dependency "fluentd", [">= 0.14.10", "< 2"]
27
28
  end
@@ -11,10 +11,11 @@
11
11
  # distributed under the License is distributed on an "AS IS" BASIS,
12
12
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
13
  # See the License for the specific language governing permissions and
14
- # limitations under the License.
14
+ # limitations under the License
15
15
 
16
16
  require "fluent/plugin/output"
17
- require 'digest/md5'
17
+ require 'prometheus/client'
18
+
18
19
 
19
20
  module Fluent
20
21
  module Plugin
@@ -40,6 +41,8 @@ module Fluent
40
41
  config_param :@label, :string, :default => nil
41
42
  desc "New tag if selectors matched"
42
43
  config_param :tag, :string, :default => ""
44
+ desc "Extra labels for metrics"
45
+ config_param :metrics_labels, :hash, :default => {}
43
46
 
44
47
  config_section :match, param_name: :matches, multi: true do
45
48
  desc "Label definition to match record. Example: app:nginx. You can specify more values as comma separated list: key1:value1,key2:value2"
@@ -56,10 +59,22 @@ module Fluent
56
59
  end
57
60
 
58
61
  class Route
59
- def initialize(matches, tag, router)
62
+ def initialize(rule, router, registry)
60
63
  @router = router
61
- @matches = matches
62
- @tag = tag
64
+ @matches = rule['matches']
65
+ @tag = rule['tag'].to_s
66
+ @label = rule['@label']
67
+ @metrics_labels = (rule['metrics_labels'].map { |k, v| [k.to_sym, v] }.to_h if rule['metrics_labels'])
68
+ @counter = nil
69
+ unless registry.nil?
70
+ @counter = registry.counter(:fluentd_router_records_total, "Total number of events router for the flow")
71
+ end
72
+ end
73
+
74
+ def get_labels
75
+ default = { 'flow': @label }
76
+ labels = default.merge(@metrics_labels)
77
+ labels
63
78
  end
64
79
 
65
80
  # Evaluate selectors
@@ -113,6 +128,8 @@ module Fluent
113
128
  else
114
129
  @router.emit_stream(@tag, es)
115
130
  end
131
+ # increment the counter for a given label set
132
+ @counter&.increment(by: es.size, labels: get_labels)
116
133
  end
117
134
 
118
135
  def match_labels(input, match)
@@ -176,18 +193,19 @@ module Fluent
176
193
 
177
194
  def configure(conf)
178
195
  super
196
+ @registry = (::Prometheus::Client.registry if @metrics)
179
197
  @route_map = Hash.new { |h, k| h[k] = Set.new }
180
198
  @mutex = Mutex.new
181
199
  @routers = []
182
200
  @default_router = nil
183
201
  @routes.each do |rule|
184
202
  route_router = event_emitter_router(rule['@label'])
185
- puts rule
186
- @routers << Route.new(rule.matches, rule.tag.to_s, route_router)
203
+ @routers << Route.new(rule, route_router, @registry)
187
204
  end
188
205
 
189
206
  if @default_route != '' or @default_tag != ''
190
- @default_router = Route.new(nil, @default_tag, event_emitter_router(@default_route))
207
+ default_rule = { 'matches' => nil, 'tag' => @default_tag, '@label' => @default_route}
208
+ @default_router = Route.new(default_rule, event_emitter_router(@default_route), @registry)
191
209
  end
192
210
 
193
211
  @access_to_labels = record_accessor_create("$.kubernetes.labels")
@@ -76,7 +76,7 @@ class LabelRouterOutputTest < Test::Unit::TestCase
76
76
  d = Fluent::Test::Driver::BaseOwner.new(Fluent::Plugin::LabelRouterOutput)
77
77
  d.configure(routing_conf)
78
78
 
79
- r1 = Fluent::Plugin::LabelRouterOutput::Route.new(d.instance.routes[0].matches, d.instance.routes[0].tag,nil)
79
+ r1 = Fluent::Plugin::LabelRouterOutput::Route.new(d.instance.routes[0], nil,nil)
80
80
  # Selector matched: GO
81
81
  assert_equal(true, r1.match?(labels: { 'app' => 'app1' }, namespace: ''))
82
82
  # Exclude match: NO GO
@@ -84,7 +84,7 @@ class LabelRouterOutputTest < Test::Unit::TestCase
84
84
  # Nothing matched: NO GO
85
85
  assert_equal(false, r1.match?(labels: { 'app3' => 'app2' }, namespace: ''))
86
86
 
87
- r2 = Fluent::Plugin::LabelRouterOutput::Route.new(d.instance.routes[1].matches, d.instance.routes[1].tag,nil)
87
+ r2 = Fluent::Plugin::LabelRouterOutput::Route.new(d.instance.routes[1], nil,nil)
88
88
  # Match selector and namespace: GO
89
89
  assert_equal(true, r2.match?(labels: { 'app' => 'app1' }, namespace: 'test'))
90
90
  # Exclude via namespace
@@ -92,12 +92,12 @@ class LabelRouterOutputTest < Test::Unit::TestCase
92
92
  # Nothing matched: NO GO
93
93
  assert_equal(false, r2.match?(labels: { 'app3' => 'app' }, namespace: 'system'))
94
94
 
95
- r3 = Fluent::Plugin::LabelRouterOutput::Route.new(d.instance.routes[2].matches, d.instance.routes[2].tag,nil)
95
+ r3 = Fluent::Plugin::LabelRouterOutput::Route.new(d.instance.routes[2], nil,nil)
96
96
  assert_equal(true, r3.match?(labels: { 'app' => 'nginx' }, namespace: 'dev'))
97
97
  assert_equal(true, r3.match?(labels: { 'app' => 'nginx' }, namespace: 'sandbox'))
98
98
  assert_equal(false, r3.match?(labels: { 'app' => 'nginx2' }, namespace: 'sandbox'))
99
99
 
100
- r4 = Fluent::Plugin::LabelRouterOutput::Route.new(d.instance.routes[3].matches, d.instance.routes[3].tag,nil)
100
+ r4 = Fluent::Plugin::LabelRouterOutput::Route.new(d.instance.routes[3], nil,nil)
101
101
  # Matching container name
102
102
  assert_equal(true, r4.match?(labels: { 'app' => 'nginx' }, namespace: 'dev', container: 'mycontainer'))
103
103
  # Missing container name is equal to wrong container
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-plugin-label-router
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Banzai Cloud
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-13 00:00:00.000000000 Z
11
+ date: 2021-05-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: prometheus-client
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 2.1.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 2.1.0
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: fluentd
57
71
  requirement: !ruby/object:Gem::Requirement