influx_reporter 1.1.0 → 1.2.0

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: 3ddddb2aec52aed72cd27b0068fd4c293b9f3300e4ad01955068aa335a2bca73
4
- data.tar.gz: ae5677b7955e961a06f2e95927fb112474e02457ac506b8f84f16009518afad7
3
+ metadata.gz: cd77289b6d02bb08bdc655882de791a0e45f3964b4b89aeeac780213b41fd0d3
4
+ data.tar.gz: a52df2325d7c556d944417befe98266e4cbbb8fa4e1d1741654c79a85a5d3d06
5
5
  SHA512:
6
- metadata.gz: 73ab7f2528040eee3c65135adb3e5e2192705378741b3c3863c3d5d9ee1e34e4e9a617f577448eca2396919433e45db21488c5b39c893f1d774af071b5941578
7
- data.tar.gz: 6f6013c5f538cd50c46a07ccb3b5facef447af48dc1891e924cd9cdaab47f8e2085d6b19dd2987e339cf3bc1ea94f516e3a920e3447c9909546543ab2986f93a
6
+ metadata.gz: 42f7c94c2992dbef8de3e6cc191d89c1238e08d8fe568ccc28e1403423c41e59e336585bac53bcebb4e44c01fdb597d388fa5b704dabf127a3fc3d7ac1efc120
7
+ data.tar.gz: b97402fbe910ee75aef5f72573ac0fdd0ddaa7b363eaa23402a109175a60178e4c9935dc555d1167de2dc9855b12c47763fec4d899f0948096e8aa326e18a379
data/README.md CHANGED
@@ -156,6 +156,11 @@ Adding tags & values is also possible:
156
156
  InfluxReporter.report_event 'event_name', extra: { tags: { key: 'tag' }, values: { key: 'value' } }
157
157
  ```
158
158
 
159
+ Finally, events might generate lots of keys and you may want to use a specific database just for this purpose.
160
+ ```ruby
161
+ InfluxReporter.report_event 'event_name', database: 'events_database'
162
+ ```
163
+
159
164
  ## Manual profiling
160
165
 
161
166
  It's easy to add performance tracking wherever you want using the `InfluxReporter` module.
@@ -173,7 +173,7 @@ module InfluxReporter
173
173
  return if @pending_transactions.empty?
174
174
 
175
175
  data = @data_builders.transactions.build(@pending_transactions)
176
- enqueue Worker::PostRequest.new('/transactions/', data)
176
+ enqueue Worker::PostRequest.new(resource_from_path('transactions', {}), data)
177
177
 
178
178
  @last_sent_transactions = Time.now.utc
179
179
  @pending_transactions = []
@@ -214,7 +214,7 @@ module InfluxReporter
214
214
  if error_message = ErrorMessage.from_exception(config, exception, opts)
215
215
  error_message.add_extra(@context) if @context
216
216
  data = @data_builders.error_message.build error_message
217
- enqueue Worker::PostRequest.new('/errors/', data)
217
+ enqueue Worker::PostRequest.new(resource_from_path('errors', opts), data)
218
218
  end
219
219
  end
220
220
 
@@ -226,7 +226,7 @@ module InfluxReporter
226
226
  error_message = ErrorMessage.new(config, message, opts)
227
227
  error_message.add_extra(@context) if @context
228
228
  data = @data_builders.error_message.build error_message
229
- enqueue Worker::PostRequest.new('/errors/', data)
229
+ enqueue Worker::PostRequest.new(resource_from_path('errors', opts), data)
230
230
  end
231
231
 
232
232
  def report_event(message, opts = {})
@@ -235,7 +235,7 @@ module InfluxReporter
235
235
  event = EventMessage.new(config, message, opts)
236
236
  event.add_extra(@context) if @context
237
237
  data = @data_builders.event.build event
238
- enqueue Worker::PostRequest.new('/events/', data)
238
+ enqueue Worker::PostRequest.new(resource_from_path('events', opts), data)
239
239
  end
240
240
 
241
241
  def capture
@@ -264,6 +264,13 @@ module InfluxReporter
264
264
  @queue << request
265
265
  end
266
266
 
267
+ def resource_from_path(path, opts)
268
+ obj = { url: "/#{path}/" }
269
+ obj[:database] = opts[:database] if opts[:database]
270
+ obj
271
+ end
272
+
273
+
267
274
  def start_worker
268
275
  return if worker_running?
269
276
 
@@ -45,6 +45,7 @@ module InfluxReporter
45
45
  attr_accessor :stacktrace
46
46
  attr_accessor :http
47
47
  attr_accessor :user
48
+ attr_accessor :database
48
49
 
49
50
  def self.from_exception(config, exception, opts = {})
50
51
  message = "#{exception.class}: #{exception.message}"
@@ -24,6 +24,7 @@ module InfluxReporter
24
24
  attr_accessor :message
25
25
  attr_reader :timestamp
26
26
  attr_accessor :extra
27
+ attr_accessor :database
27
28
 
28
29
  def add_extra(info)
29
30
  @extra ||= {}
@@ -21,7 +21,7 @@ module InfluxReporter
21
21
  attr_reader :config
22
22
 
23
23
  def post(resource, data)
24
- debug "POST #{resource}"
24
+ debug "POST #{resource[:url]}"
25
25
 
26
26
  unless state.should_try?
27
27
  info 'Temporarily skipping sending to InfluxReporter due to previous failure.'
@@ -30,7 +30,7 @@ module InfluxReporter
30
30
 
31
31
  begin
32
32
  data = [data] unless data.is_a?(Array)
33
- client.write_points data
33
+ client.write_points data, nil, nil, resource.fetch(:database, nil)
34
34
  rescue StandardError => e
35
35
  debug { e.message }
36
36
  @state.fail!
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module InfluxReporter
4
- VERSION = '1.1.0'
4
+ VERSION = '1.2.0'
5
5
  end
@@ -5,10 +5,10 @@ module InfluxReporter
5
5
  class Worker
6
6
  include Logging
7
7
 
8
- class PostRequest < Struct.new(:path, :data)
8
+ class PostRequest < Struct.new(:resource, :data)
9
9
  # require all parameters
10
- def initialize(path, data)
11
- super(path, data)
10
+ def initialize(resource, data)
11
+ super(resource, data)
12
12
  end
13
13
  end
14
14
 
@@ -46,7 +46,7 @@ module InfluxReporter
46
46
  end
47
47
 
48
48
  begin
49
- @influx_client.post(req.path, req.data)
49
+ @influx_client.post(req.resource, req.data)
50
50
  rescue => e
51
51
  fatal "Failed POST: #{e.inspect}"
52
52
  debug e.backtrace.join("\n")
@@ -25,13 +25,13 @@ module InfluxReporter
25
25
  end
26
26
 
27
27
  it 'pops the queue' do
28
- @queue << Worker::PostRequest.new('/errors/', { series: 'test', values: { id: 1 }, timestamp: 0})
29
- @queue << Worker::PostRequest.new('/errors/', { series: 'test', values: { id: 1 }, timestamp: 1})
28
+ @queue << Worker::PostRequest.new({ url: '/errors/' }, { series: 'test', values: { id: 1 }, timestamp: 0})
29
+ @queue << Worker::PostRequest.new({ url: '/errors/', database: 'db2' }, { series: 'test', values: { id: 1 }, timestamp: 1})
30
30
 
31
31
  subject
32
32
 
33
33
  expect(WebMock).to have_requested(:post, %r{/write}).with(body: 'test id=1i 0')
34
- expect(WebMock).to have_requested(:post, %r{/write}).with(body: 'test id=1i 1')
34
+ expect(WebMock).to have_requested(:post, %r{/write\?db=db2}).with(body: 'test id=1i 1')
35
35
  end
36
36
  end
37
37
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: influx_reporter
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kacper Kawecki
@@ -94,7 +94,6 @@ files:
94
94
  - lib/influx_reporter/error_message/user.rb
95
95
  - lib/influx_reporter/event_message.rb
96
96
  - lib/influx_reporter/filter.rb
97
- - lib/influx_reporter/http_client.rb
98
97
  - lib/influx_reporter/influx_db_client.rb
99
98
  - lib/influx_reporter/injections.rb
100
99
  - lib/influx_reporter/injections/json.rb
@@ -1,140 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'uri'
4
- require 'net/http'
5
- require 'json'
6
-
7
- module InfluxReporter
8
- # @api private
9
- class HttpClient
10
- include Logging
11
-
12
- USER_AGENT = "influx_reporter-ruby/#{InfluxReporter::VERSION}"
13
-
14
- attr_reader :state
15
- attr_reader :adapter
16
-
17
- def initialize(config)
18
- @config = config
19
- @adapter = HTTPAdapter.new(config)
20
- @state = ClientState.new config
21
- end
22
-
23
- attr_reader :config
24
-
25
- def post(resource, body)
26
- path = abs_path(resource)
27
- debug "POST #{resource}"
28
-
29
- unless state.should_try?
30
- info 'Temporarily skipping sending to InfluxReporter due to previous failure.'
31
- return
32
- end
33
-
34
- body = JSON.dump(body) if body.is_a?(Hash) || body.is_a?(Array)
35
-
36
- request = adapter.post path do |req|
37
- req['Authorization'] = auth_header
38
- req['Content-Type'] = 'application/json'
39
- req['Content-Length'] = body.bytesize.to_s
40
- req['User-Agent'] = USER_AGENT
41
- req.body = body
42
- end
43
-
44
- begin
45
- response = adapter.perform_request request
46
- unless response.code.to_i.between?(200, 299)
47
- raise Error, "Error from InfluxReporter server (#{response.code}): #{response.body}"
48
- end
49
- rescue
50
- debug { JSON.parse(body).inspect }
51
- @state.fail!
52
- raise
53
- end
54
-
55
- @state.success!
56
-
57
- response
58
- end
59
-
60
- private
61
-
62
- def auth_header
63
- "Bearer #{@config.secret_token}"
64
- end
65
-
66
- def abs_path(path)
67
- "/api/v1/organizations/#{@config.organization_id}" \
68
- "/apps/#{@config.app_id}#{path}"
69
- end
70
-
71
- def encode(event)
72
- event_hash = @filter.process_event_hash(event.to_hash)
73
- event_hash.to_json
74
- end
75
-
76
- class HTTPAdapter
77
- def initialize(conf)
78
- @config = conf
79
- end
80
-
81
- def post(path)
82
- req = Net::HTTP::Post.new path
83
- yield req if block_given?
84
- req
85
- end
86
-
87
- def perform_request(req)
88
- http.start do |http|
89
- http.request req
90
- end
91
- end
92
-
93
- private
94
-
95
- def http
96
- return @http if @http
97
-
98
- http = Net::HTTP.new server_uri.host, server_uri.port
99
- http.use_ssl = @config.use_ssl
100
- http.read_timeout = @config.timeout
101
- http.open_timeout = @config.open_timeout
102
-
103
- @http = http
104
- end
105
-
106
- def server_uri
107
- @uri ||= URI(@config.server)
108
- end
109
- end
110
-
111
- class ClientState
112
- def initialize(config)
113
- @config = config
114
- @retry_number = 0
115
- @last_check = Time.now.utc
116
- end
117
-
118
- def should_try?
119
- return true if @status == :online
120
-
121
- interval = ([@retry_number, 6].min**2) * @config.backoff_multiplier
122
- return true if Time.now.utc - @last_check > interval
123
-
124
- false
125
- end
126
-
127
- def fail!
128
- @status = :error
129
- @retry_number += 1
130
- @last_check = Time.now.utc
131
- end
132
-
133
- def success!
134
- @status = :online
135
- @retry_number = 0
136
- @last_check = nil
137
- end
138
- end
139
- end
140
- end