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