adp-fluent-plugin-graphite 0.0.2 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/adp-fluent-plugin-graphite.gemspec +1 -1
- data/lib/fluent/plugin/out_graphite.rb +82 -100
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 052c0e2d0e66ba05cb94a995f040c436ada3cad27d1386850d2aca4659152189
|
4
|
+
data.tar.gz: d6333c8cbead5da773384aa63149518d1b9fb8dfbac2dbcec35b1127b1688171
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fd9dd5a45669dec38a3d7498070bd7ac2ad56b8a7c9d150668956270bcb8d4c3a0934f26681929277189afdcdded2c8a8583ac3046b5289c781e2ee567c2d54e
|
7
|
+
data.tar.gz: df3548004608c62f59da0cc3cea317ec3115c685dd7cb419b2182d16337d8cca4ce57209d25fcc0f3bd509c303d294db1be4f3437ac70217ea9dff2dcfb2d044
|
@@ -3,7 +3,7 @@ $:.push File.expand_path('../lib', __FILE__)
|
|
3
3
|
|
4
4
|
Gem::Specification.new do |gem|
|
5
5
|
gem.name = 'adp-fluent-plugin-graphite'
|
6
|
-
gem.version = '0.0.
|
6
|
+
gem.version = '0.0.6'
|
7
7
|
gem.authors = ['Satoshi SUZUKI']
|
8
8
|
gem.email = 'studio3104.com@gmail.com'
|
9
9
|
gem.homepage = 'https://github.com/studio3104/fluent-plugin-graphite'
|
@@ -1,116 +1,98 @@
|
|
1
|
-
require 'fluent/
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
end
|
19
|
-
|
20
|
-
def initialize
|
21
|
-
super
|
22
|
-
require 'graphite-api'
|
23
|
-
end
|
24
|
-
|
25
|
-
def start
|
26
|
-
super
|
27
|
-
connect_client!
|
28
|
-
end
|
29
|
-
|
30
|
-
def configure(conf)
|
31
|
-
super
|
32
|
-
|
33
|
-
if !['prefix', 'suffix', 'ignore'].include?(@tag_for)
|
34
|
-
raise Fluent::ConfigError, 'out_graphite: can specify to tag_for only prefix, suffix or ignore'
|
35
|
-
end
|
36
|
-
|
37
|
-
if !@name_keys && !@name_key_pattern
|
38
|
-
raise Fluent::ConfigError, 'out_graphite: missing both of name_keys and name_key_pattern'
|
39
|
-
end
|
40
|
-
if @name_keys && @name_key_pattern
|
41
|
-
raise Fluent::ConfigError, 'out_graphite: cannot specify both of name_keys and name_key_pattern'
|
1
|
+
require 'fluent/plugin/output'
|
2
|
+
|
3
|
+
module Fluent::Plugin
|
4
|
+
class GraphiteOutput < Output
|
5
|
+
Fluent::Plugin.register_output('graphite', self)
|
6
|
+
|
7
|
+
config_param :host, :string
|
8
|
+
config_param :port, :integer, default: 2003
|
9
|
+
config_param :tag_for, :string, default: 'prefix'
|
10
|
+
config_param :name_keys, :string, default: nil
|
11
|
+
config_param :name_key_pattern, :string, default: nil
|
12
|
+
config_param :max_retries, :integer, default: 3
|
13
|
+
|
14
|
+
def initialize
|
15
|
+
super
|
16
|
+
require 'graphite-api'
|
17
|
+
log.info("Initialize graphite plugin")
|
42
18
|
end
|
43
19
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
if @name_key_pattern
|
48
|
-
@name_key_pattern = Regexp.new(@name_key_pattern)
|
20
|
+
def start
|
21
|
+
super
|
22
|
+
connect_client!
|
49
23
|
end
|
50
|
-
# How many times to retry the call if timeout raised
|
51
|
-
@max_retries ||= 3
|
52
|
-
end
|
53
24
|
|
54
|
-
|
55
|
-
|
56
|
-
emit_tag = tag.dup
|
57
|
-
filter_record(emit_tag, time, record)
|
58
|
-
next unless metrics = format_metrics(emit_tag, record)
|
25
|
+
def configure(conf)
|
26
|
+
super
|
59
27
|
|
60
|
-
|
61
|
-
|
28
|
+
if @name_keys
|
29
|
+
@name_keys = @name_keys.split(',')
|
30
|
+
end
|
62
31
|
end
|
63
32
|
|
64
|
-
chain
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
record.select { |k,v| @name_keys.include?(k.to_s) }
|
70
|
-
else # defined @name_key_pattern
|
71
|
-
record.select { |k,v| @name_key_pattern.match(k.to_s) }
|
72
|
-
end
|
33
|
+
def emit(tag, es, chain)
|
34
|
+
es.each do |time, record|
|
35
|
+
emit_tag = tag.dup
|
36
|
+
log.info("Emit graphite plugin: #{record}")
|
37
|
+
next unless metrics = format_metrics(emit_tag, record)
|
73
38
|
|
74
|
-
|
39
|
+
# implemented to immediate call post method in this loop, because graphite-api.gem has the buffers.
|
40
|
+
post(metrics, time)
|
41
|
+
end
|
75
42
|
|
76
|
-
|
77
|
-
|
78
|
-
filtered_record.each do |k, v|
|
79
|
-
key = case @tag_for
|
80
|
-
when 'ignore' then k.to_s
|
81
|
-
when 'prefix' then "#{tag}.#{k}"
|
82
|
-
when 'suffix' then "#{k}.#{tag}"
|
83
|
-
end
|
43
|
+
chain.next
|
44
|
+
end
|
84
45
|
|
85
|
-
|
86
|
-
|
46
|
+
def format_metrics(tag, record)
|
47
|
+
filtered_record = if @name_keys
|
48
|
+
record.select { |k, v| @name_keys.include?(k.to_s) }
|
49
|
+
else
|
50
|
+
# defined @name_key_pattern
|
51
|
+
record.select { |k, v| @name_key_pattern.match(k.to_s) }
|
52
|
+
end
|
53
|
+
|
54
|
+
return nil if filtered_record.empty?
|
55
|
+
|
56
|
+
metrics = {}
|
57
|
+
tag = tag.sub(/\.$/, '') # may include a dot at the end of the emit_tag fluent-mixin-rewrite-tag-name returns. remove it.
|
58
|
+
filtered_record.each do |k, v|
|
59
|
+
key = case @tag_for
|
60
|
+
when 'ignore' then k.to_s
|
61
|
+
when 'prefix' then "#{tag}.#{k}"
|
62
|
+
when 'suffix' then "#{k}.#{tag}"
|
63
|
+
end
|
64
|
+
|
65
|
+
key = key.gsub(/(\s|\/)+/, '_') # cope with in the case of containing symbols or spaces in the key of the record like in_dstat.
|
66
|
+
metrics[key] = v.to_f
|
67
|
+
end
|
68
|
+
metrics
|
87
69
|
end
|
88
|
-
metrics
|
89
|
-
end
|
90
70
|
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
71
|
+
def post(metrics, time)
|
72
|
+
trial ||= 1
|
73
|
+
@client.metrics(metrics, time)
|
74
|
+
log.warn("Sending metrics: #{metrics}")
|
75
|
+
rescue Errno::ETIMEDOUT
|
76
|
+
# after long periods with nothing emitted, the connection will be closed and result in timeout
|
77
|
+
if trial <= @max_retries
|
78
|
+
log.warn "out_graphite: connection timeout to #{@host}:#{@port}. Reconnecting... "
|
79
|
+
trial += 1
|
80
|
+
connect_client!
|
81
|
+
retry
|
82
|
+
else
|
83
|
+
log.error "out_graphite: ERROR: connection timeout to #{@host}:#{@port}. Exceeded max_retries #{@max_retries}"
|
84
|
+
end
|
85
|
+
rescue Errno::ECONNREFUSED
|
86
|
+
log.warn "out_graphite: connection refused by #{@host}:#{@port}"
|
87
|
+
rescue SocketError => se
|
88
|
+
log.warn "out_graphite: socket error by #{@host}:#{@port} :#{se}"
|
89
|
+
rescue StandardError => e
|
90
|
+
log.error "out_graphite: ERROR: #{e}"
|
104
91
|
end
|
105
|
-
rescue Errno::ECONNREFUSED
|
106
|
-
log.warn "out_graphite: connection refused by #{@host}:#{@port}"
|
107
|
-
rescue SocketError => se
|
108
|
-
log.warn "out_graphite: socket error by #{@host}:#{@port} :#{se}"
|
109
|
-
rescue StandardError => e
|
110
|
-
log.error "out_graphite: ERROR: #{e}"
|
111
|
-
end
|
112
92
|
|
113
|
-
|
114
|
-
|
93
|
+
def connect_client!
|
94
|
+
@client = GraphiteAPI.new(graphite: "#{@host}:#{@port}")
|
95
|
+
log.info("starting client")
|
96
|
+
end
|
115
97
|
end
|
116
98
|
end
|