fluent-plugin-grafana-loki 1.2.3 → 1.2.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/fluent/plugin/out_loki.rb +51 -35
  3. metadata +4 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5beface085cb0713b107553e74ad179ce784a93a74312c7bfa8ca65f8d981c04
4
- data.tar.gz: 7559738f0794cbdc0c313a8a6299693d153a6e2dcbd61a4221040e7c7a47917f
3
+ metadata.gz: edc3f7350833b173b7899a6a7edcada2dba818cd5e5c452b633d9e7c93206799
4
+ data.tar.gz: 7cb2464e4a85621f9b546162ca985be8021a9330853e9ce020594b7470c1e3e2
5
5
  SHA512:
6
- metadata.gz: 492a76db23871d115007caeca704db37dc883f5623df5b67c5f0e9d9fe582b5a99cb1af94e7c66d23e2d2e72973ba72ba49e9fb234d27c30ae027ec389951dca
7
- data.tar.gz: b1107f60ef666346e1bdb5b8b5e06809ff3c41ff328c581677d615f3bf447a28b133322ec4667968a2821c83db56fec6646ab9e79feab2038b2da3d02981ec3f
6
+ metadata.gz: edac698c75dc3b600167e85ed20447ffa25a136342c9344dd48dc92b0578b9c6e81d1c7c04c2319d35404d554ee995d00bcb1f69165eefccd328c3a729481459
7
+ data.tar.gz: f0f9e6dce6b997fe598f6abeb2943cb9e9d7ecc072d568bcb2dcd04926c368364d7c361030722e9f39936f7a7b9ee9bf26458b12b0d2c9ee267c5dce636ffef0
@@ -86,17 +86,26 @@ module Fluent
86
86
  @remove_keys_accessors.push(record_accessor_create(key))
87
87
  end
88
88
 
89
- if !@key.nil? && !@cert.nil?
90
- @cert = OpenSSL::X509::Certificate.new(File.read(@cert)) if @cert
91
- @key = OpenSSL::PKey.read(File.read(@key)) if @key
92
-
93
- if !@key.is_a?(OpenSSL::PKey::RSA) && !@key.is_a?(OpenSSL::PKey::DSA)
94
- raise "Unsupported private key type #{key.class}"
95
- end
89
+ if ssl_cert?
90
+ load_ssl
91
+ validate_ssl_key
96
92
  end
97
93
 
98
- if !@ca_cert.nil? && !File.exist?(@ca_cert)
99
- raise "CA certificate file #{@ca_cert} not found"
94
+ raise "CA certificate file #{@ca_cert} not found" if !@ca_cert.nil? && !File.exist?(@ca_cert)
95
+ end
96
+
97
+ def ssl_cert?
98
+ !@key.nil? && !@cert.nil?
99
+ end
100
+
101
+ def load_ssl
102
+ @cert = OpenSSL::X509::Certificate.new(File.read(@cert)) if @cert
103
+ @key = OpenSSL::PKey.read(File.read(@key)) if @key
104
+ end
105
+
106
+ def validate_ssl_key
107
+ if !@key.is_a?(OpenSSL::PKey::RSA) && !@key.is_a?(OpenSSL::PKey::DSA)
108
+ raise "Unsupported private key type #{key.class}"
100
109
  end
101
110
  end
102
111
 
@@ -127,6 +136,24 @@ module Fluent
127
136
  req.add_field('X-Scope-OrgID', @tenant) if @tenant
128
137
  req.body = Yajl.dump(body)
129
138
  req.basic_auth(@username, @password) if @username
139
+
140
+ opts = ssl_opts(uri)
141
+
142
+ log.debug "sending #{req.body.length} bytes to loki"
143
+ res = Net::HTTP.start(uri.hostname, uri.port, **opts) { |http| http.request(req) }
144
+ unless res&.is_a?(Net::HTTPSuccess)
145
+ res_summary = if res
146
+ "#{res.code} #{res.message} #{res.body}"
147
+ else
148
+ 'res=nil'
149
+ end
150
+ log.warn "failed to #{req.method} #{uri} (#{res_summary})"
151
+ log.warn Yajl.dump(body)
152
+
153
+ end
154
+ end
155
+
156
+ def ssl_opts(uri)
130
157
  opts = {
131
158
  use_ssl: uri.scheme == 'https'
132
159
  }
@@ -139,24 +166,12 @@ module Fluent
139
166
  )
140
167
  end
141
168
 
142
- if !@ca_cert.nil?
169
+ unless @ca_cert.nil?
143
170
  opts = opts.merge(
144
171
  ca_file: @ca_cert
145
172
  )
146
173
  end
147
-
148
- log.debug "sending #{req.body.length} bytes to loki"
149
- res = Net::HTTP.start(uri.hostname, uri.port, **opts) { |http| http.request(req) }
150
- unless res&.is_a?(Net::HTTPSuccess)
151
- res_summary = if res
152
- "#{res.code} #{res.message} #{res.body}"
153
- else
154
- 'res=nil'
155
- end
156
- log.warn "failed to #{req.method} #{uri} (#{res_summary})"
157
- log.warn Yajl.dump(body)
158
-
159
- end
174
+ opts
160
175
  end
161
176
 
162
177
  def generic_to_loki(chunk)
@@ -174,17 +189,14 @@ module Fluent
174
189
  false
175
190
  end
176
191
 
177
- def labels_to_protocol(data_labels)
178
- formatted_labels = []
179
-
192
+ def format_labels(data_labels)
193
+ formatted_labels = {}
180
194
  # merge extra_labels with data_labels. If there are collisions extra_labels win.
181
195
  data_labels = {} if data_labels.nil?
182
196
  data_labels = data_labels.merge(@extra_labels)
183
-
184
- data_labels.each do |k, v|
185
- formatted_labels.push(%(#{k}="#{v.gsub('"', '\\"')}")) if v
186
- end
187
- '{' + formatted_labels.join(',') + '}'
197
+ # sanitize label values
198
+ data_labels.each { |k, v| formatted_labels[k] = v.gsub('"', '\\"') }
199
+ formatted_labels
188
200
  end
189
201
 
190
202
  def payload_builder(streams)
@@ -193,15 +205,19 @@ module Fluent
193
205
  # create a stream for each label set.
194
206
  # Additionally sort the entries by timestamp just in case we
195
207
  # got them out of order.
196
- # 'labels' => '{worker="0"}',
208
+ entries = v.sort_by.with_index { |hsh, i| [hsh['ts'], i] }
197
209
  payload.push(
198
- 'labels' => labels_to_protocol(k),
199
- 'entries' => v.sort_by.with_index { |hsh, i| [Time.parse(hsh['ts']), i] }
210
+ 'stream' => format_labels(k),
211
+ 'values' => entries.map { |e| [e['ts'].to_s, e['line']] }
200
212
  )
201
213
  end
202
214
  payload
203
215
  end
204
216
 
217
+ def to_nano(time)
218
+ time.to_i * (10**9) + time.nsec
219
+ end
220
+
205
221
  def record_to_line(record)
206
222
  line = ''
207
223
  if @drop_single_key && record.keys.length == 1
@@ -272,7 +288,7 @@ module Fluent
272
288
  # NOTE: timestamp must include nanoseconds
273
289
  # append to matching chunk_labels key
274
290
  streams[chunk_labels].push(
275
- 'ts' => Time.at(time.to_f).gmtime.iso8601(6),
291
+ 'ts' => to_nano(time),
276
292
  'line' => result[:line]
277
293
  )
278
294
  end
metadata CHANGED
@@ -1,15 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-plugin-grafana-loki
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.3
4
+ version: 1.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - woodsaj
8
8
  - briangann
9
+ - cyriltovena
9
10
  autorequire:
10
11
  bindir: bin
11
12
  cert_chain: []
12
- date: 2019-11-29 00:00:00.000000000 Z
13
+ date: 2019-12-05 00:00:00.000000000 Z
13
14
  dependencies:
14
15
  - !ruby/object:Gem::Dependency
15
16
  name: bundler
@@ -77,6 +78,7 @@ description: Output plugin to ship logs to a Grafana Loki server
77
78
  email:
78
79
  - awoods@grafana.com
79
80
  - brian@grafana.com
81
+ - cyril.tovena@grafana.com
80
82
  executables: []
81
83
  extensions: []
82
84
  extra_rdoc_files: []