influx_reporter 1.1.0 → 1.2.2

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: 2b0730709cd958cf778fb9a098097e7e0cd5480de1d1eb5dae0741bd231d74f0
4
+ data.tar.gz: 63a762abff77d226038ece2240435626bf903efa95e7e1b1fb2c35a4f2e811a1
5
5
  SHA512:
6
- metadata.gz: 73ab7f2528040eee3c65135adb3e5e2192705378741b3c3863c3d5d9ee1e34e4e9a617f577448eca2396919433e45db21488c5b39c893f1d774af071b5941578
7
- data.tar.gz: 6f6013c5f538cd50c46a07ccb3b5facef447af48dc1891e924cd9cdaab47f8e2085d6b19dd2987e339cf3bc1ea94f516e3a920e3447c9909546543ab2986f93a
6
+ metadata.gz: 9b1b90c44b16e924f69be8ac91f558796612ea33576927d8f353d45cd5187205ac4a41da0dff6a6a6c862100d3124a007f6a2173f91aee4010acc9f5850a5c26
7
+ data.tar.gz: 6044d661cfb071d19119ed39b1b1fdff97378c9eab9e200b312c7de258d051aff87ebdf0453c8829cb09a238c3dd8d5dabb6d33c3d3fa9881297bff3cd0e7858
data/Gemfile CHANGED
@@ -2,6 +2,4 @@
2
2
 
3
3
  eval_gemfile 'gemfiles/Gemfile.rails-5.0.x'
4
4
 
5
- gem 'pry'
6
- gem 'ruby-debug-ide'
7
- gem 'debase'
5
+ gem 'pry'
data/README.md CHANGED
@@ -34,8 +34,8 @@ require 'influx_reporter'
34
34
 
35
35
  # set up an InfluxReporter configuration
36
36
  config = InfluxReporter::Configuration.new do |conf|
37
- conf.influx_reporter.database = 'endpoints'
38
- conf.influx_reporter.influx_db = {
37
+ conf.database = 'endpoints'
38
+ conf.influx_db = {
39
39
  host: 'influxdb.local',
40
40
  port: '8080'
41
41
  }
@@ -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.
@@ -25,5 +25,5 @@ gem 'resque', require: false
25
25
 
26
26
  # Freeze Sidekiq to < 5 in Ruby 2.2 Gemfiles
27
27
  if RUBY_VERSION >= '2'
28
- gem 'sidekiq', (RUBY_VERSION >= '2.2.2' ? '~> 5' : '~> 4'), require: false
28
+ gem 'sidekiq', (RUBY_VERSION >= '2.5.0' ? '~> 6' : '~> 4'), require: false
29
29
  end
@@ -0,0 +1,5 @@
1
+ eval_gemfile File.expand_path('../Gemfile.base', __FILE__)
2
+
3
+ gem 'rails', '~> 5.0.0'
4
+ gem 'tzinfo-data'
5
+ gem 'sinatra'
@@ -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 ||= {}
@@ -14,14 +14,14 @@ module InfluxReporter
14
14
  # @param config [InfluxReporter::Configuration]
15
15
  def initialize(config)
16
16
  @config = config
17
- @client = InfluxDB::Client.new config.database, config.influx_db.merge(time_precision: 'ns')
17
+ @client = InfluxDB::Client.new config.database, **config.influx_db.merge(time_precision: 'ns')
18
18
  @state = ClientState.new config
19
19
  end
20
20
 
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!
@@ -23,12 +23,14 @@ module InfluxReporter
23
23
  @start_time = Util.nanos
24
24
  @relative_start = start_time - relative_to
25
25
 
26
+ @transaction._trace_started self
26
27
  self
27
28
  end
28
29
 
29
30
  def done(ms = Util.nanos)
30
31
  @duration = ms - start_time
31
32
 
33
+ @transaction._trace_stopped self
32
34
  self
33
35
  end
34
36
 
@@ -19,6 +19,7 @@ module InfluxReporter
19
19
 
20
20
  @timestamp = Util.nanos
21
21
 
22
+ @running_traces = []
22
23
  @root_trace = Trace.new(self, ROOT_TRACE_NAME, ROOT_TRACE_NAME)
23
24
  @traces = [@root_trace]
24
25
  @notifications = []
@@ -77,6 +78,18 @@ module InfluxReporter
77
78
  result
78
79
  end
79
80
 
81
+ def _trace_started(trace)
82
+ @running_traces.push trace
83
+ end
84
+
85
+ def _trace_stopped(trace)
86
+ if @running_traces.last == trace
87
+ @running_traces.pop
88
+ else
89
+ @running_traces.delete trace
90
+ end
91
+ end
92
+
80
93
  def extra_tags
81
94
  @root_trace.extra[:tags] ||= {}
82
95
  yield @root_trace.extra[:tags]
@@ -88,11 +101,11 @@ module InfluxReporter
88
101
  end
89
102
 
90
103
  def running_traces
91
- traces.select(&:running?)
104
+ @running_traces.clone
92
105
  end
93
106
 
94
107
  def current_trace
95
- traces.reverse.find(&:running?)
108
+ @running_traces.last
96
109
  end
97
110
 
98
111
  def current_offset
@@ -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.2'
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,14 +1,14 @@
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.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kacper Kawecki
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-11-13 00:00:00.000000000 Z
11
+ date: 2022-05-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -52,7 +52,7 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
- description:
55
+ description:
56
56
  email: kacper@geniebelt.com
57
57
  executables: []
58
58
  extensions: []
@@ -77,6 +77,7 @@ files:
77
77
  - gemfiles/Gemfile.rails-4.1.x
78
78
  - gemfiles/Gemfile.rails-4.2.x
79
79
  - gemfiles/Gemfile.rails-5.0.x
80
+ - gemfiles/Gemfile.rails-6.0.x
80
81
  - gemfiles/Gemfile.rails-HEAD
81
82
  - influx_reporter.gemspec
82
83
  - lib/influx_reporter.rb
@@ -94,7 +95,6 @@ files:
94
95
  - lib/influx_reporter/error_message/user.rb
95
96
  - lib/influx_reporter/event_message.rb
96
97
  - lib/influx_reporter/filter.rb
97
- - lib/influx_reporter/http_client.rb
98
98
  - lib/influx_reporter/influx_db_client.rb
99
99
  - lib/influx_reporter/injections.rb
100
100
  - lib/influx_reporter/injections/json.rb
@@ -168,7 +168,7 @@ homepage: https://github.com/GenieBelt/influx-reporter
168
168
  licenses:
169
169
  - BSD-3-Clause
170
170
  metadata: {}
171
- post_install_message:
171
+ post_install_message:
172
172
  rdoc_options: []
173
173
  require_paths:
174
174
  - lib
@@ -183,9 +183,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
183
183
  - !ruby/object:Gem::Version
184
184
  version: '0'
185
185
  requirements: []
186
- rubyforge_project:
187
- rubygems_version: 2.7.7
188
- signing_key:
186
+ rubygems_version: 3.0.9
187
+ signing_key:
189
188
  specification_version: 4
190
189
  summary: Metrics collector for rails and InfluxDB based on Opbeat Ruby client library
191
190
  test_files: []
@@ -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