twingly-metrics 0.2.0 → 0.3.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: be11bd1a0f91edf2ac37637732b5ed9452d9ac959354c7cd4cb2dac448e00939
4
- data.tar.gz: 4f4aa1606534e1a67ddacdc5058a737733ca0be788af51fa91fb807d7cac8e3c
3
+ metadata.gz: 645a000c596946e7ec3ac3ace454267fc2d55784eb757f4357a26beaaf165fef
4
+ data.tar.gz: edf608261027d9dee342a10cd473c2e04d621b1d189db93c24c27bac1159ae9f
5
5
  SHA512:
6
- metadata.gz: cb94deb9dc0274ec9400662f84b93a857fc43d15d39d6c6bcd2ff57c7b2a0a11da6fb1923283537c44fc33ce135464b9788a615a4c6a97a9c670832004471e1d
7
- data.tar.gz: df1823f5e97aa1c08d1fdca1ab4e8d30d9620881aa0facbafbe700218f5d1d4bac6e79ee2ba50c436abbce5fa22e1c3499ef00bb30c660db80daf4720f132463
6
+ metadata.gz: 979af04631cb1cc9e2697db8471119b4dcef5bb3d769aa96c4ce6aa9e35f08e7a9bcda6913b41aca8841751c0cb650df0e985aa2931ea136f0309c077d58006c
7
+ data.tar.gz: da7fd09eff4fee2554bbeb2d78016f8bec47ac01833d834f6a2c6f97cf2a72d89668b23d776f1d44535ab96e1b666a2961f2257377beb022f078d2e01f1669d6
data/README.md CHANGED
@@ -57,6 +57,16 @@ at_exit do
57
57
  end
58
58
  ```
59
59
 
60
+ The `endpoint`, `user`, and `token` options can also be configured by setting the following environment variables:
61
+
62
+ ```
63
+ GRAFANA_METRICS_ENDPOINT=
64
+ GRAFANA_METRICS_USER=
65
+ GRAFANA_METRICS_TOKEN=
66
+ ```
67
+
68
+ *Note: If the environment variables are not set, or the corresponding parameters are not provided, the reporter will not try to report any metrics to Grafana.*
69
+
60
70
  ### Supported metric types
61
71
 
62
72
  The reporter currently supports the following metric types from the `metriks` gem:
@@ -44,15 +44,17 @@ module Twingly
44
44
  attr_reader :interval
45
45
  attr_reader :timeout
46
46
 
47
- def initialize(endpoint:, user:, token:, **options) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity
48
- @endpoint = endpoint
49
- @user = user
50
- @token = token
47
+ def initialize(endpoint: nil, user: nil, token: nil, **options) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
48
+ @endpoint = endpoint || ENV.fetch("GRAFANA_METRICS_ENDPOINT") { nil }
49
+ @user = user || ENV.fetch("GRAFANA_METRICS_USER") { nil }
50
+ @token = token || ENV.fetch("GRAFANA_METRICS_TOKEN") { nil }
51
51
 
52
52
  @prefix = options[:prefix]
53
53
  @source = options[:source] || default_source
54
54
  @logger = options[:logger]
55
55
 
56
+ log_missing_credentials unless credentials_provided?
57
+
56
58
  @registry = options[:registry] || Metriks::Registry.default
57
59
  @interval = options[:interval] || 60
58
60
  @on_error = options[:on_error] || proc { |ex| @logger&.error(ex) }
@@ -69,7 +71,8 @@ module Twingly
69
71
  @reporter_thread = nil
70
72
  end
71
73
 
72
- def start
74
+ def start # rubocop:disable Metrics/MethodLength
75
+ return unless credentials_provided?
73
76
  return if @reporter_thread&.alive?
74
77
 
75
78
  @running = true
@@ -85,6 +88,8 @@ module Twingly
85
88
  end
86
89
 
87
90
  def stop
91
+ return unless credentials_provided?
92
+
88
93
  @running = false
89
94
 
90
95
  return unless @reporter_thread
@@ -99,10 +104,14 @@ module Twingly
99
104
  end
100
105
 
101
106
  def flush
107
+ return unless credentials_provided?
108
+
102
109
  @flush_metrics_mutex.synchronize do
103
110
  request_data = build_metrics_data_for_request
104
111
 
105
- make_request(request_data)
112
+ retry_failing_request do
113
+ make_request(request_data)
114
+ end
106
115
  end
107
116
  rescue StandardError => e
108
117
  @on_error.call(e)
@@ -144,6 +153,24 @@ module Twingly
144
153
  end
145
154
  end
146
155
 
156
+ def retry_failing_request(max_retries: 3)
157
+ retries = 0
158
+
159
+ begin
160
+ yield
161
+ rescue RequestFailedError => e
162
+ retries += 1
163
+
164
+ raise if retries > max_retries
165
+
166
+ @logger&.debug("GrafanaCloudReporter: Request failed, retrying (#{retries}/#{max_retries}) - #{e.message}")
167
+
168
+ sleep 1
169
+
170
+ retry
171
+ end
172
+ end
173
+
147
174
  def build_metrics_data_for_request
148
175
  time = now_floored
149
176
 
@@ -247,6 +274,16 @@ module Twingly
247
274
  @next_time = now if @next_time <= now
248
275
  @next_time += @interval - (@next_time % @interval)
249
276
  end
277
+
278
+ def credentials_provided?
279
+ @endpoint && @user && @token
280
+ end
281
+
282
+ def log_missing_credentials
283
+ return unless @logger
284
+
285
+ @logger.warn("#{self.class.name}: Metrics won't be reported, as endpoint, user, or token is missing.")
286
+ end
250
287
  end
251
288
  end
252
289
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Twingly
4
4
  module Metrics
5
- VERSION = "0.2.0"
5
+ VERSION = "0.3.0"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: twingly-metrics
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Twingly AB
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-07-21 00:00:00.000000000 Z
10
+ date: 2025-07-28 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: metriks