twingly-metrics 0.1.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 +4 -4
- data/README.md +26 -0
- data/lib/twingly/metrics/grafana_cloud_reporter.rb +57 -8
- data/lib/twingly/metrics/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 645a000c596946e7ec3ac3ace454267fc2d55784eb757f4357a26beaaf165fef
|
4
|
+
data.tar.gz: edf608261027d9dee342a10cd473c2e04d621b1d189db93c24c27bac1159ae9f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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:
|
@@ -70,3 +80,19 @@ The reporter currently supports the following metric types from the `metriks` ge
|
|
70
80
|
[grafana-cloud]: https://grafana.com/products/cloud/
|
71
81
|
[grafana-cloud-graphite-api]: https://grafana.com/docs/grafana-cloud/send-data/metrics/metrics-graphite/http-api/#adding-new-data-posting-to-metrics
|
72
82
|
[metriks-librato_metrics]: https://github.com/eric/metriks-librato_metrics
|
83
|
+
|
84
|
+
## Release workflow
|
85
|
+
|
86
|
+
* Bump the version in `lib/twingly/metrics/version.rb` in a commit, no need to push (the release task does that).
|
87
|
+
|
88
|
+
* Build and [publish](http://guides.rubygems.org/publishing/) the gem. This will create the proper tag in git, push the commit and tag and upload to RubyGems.
|
89
|
+
|
90
|
+
bundle exec rake release
|
91
|
+
|
92
|
+
* If you are not logged in as [twingly][twingly-rubygems] with ruby gems, the rake task will fail. Login using `gem signin` and run the `release` task again. It will be okay.
|
93
|
+
|
94
|
+
* Update the changelog with [GitHub Changelog Generator](https://github.com/skywinder/github-changelog-generator/) (`gem install github_changelog_generator` if you don't have it, set `CHANGELOG_GITHUB_TOKEN` to a personal access token to avoid rate limiting by GitHub). This command will update `CHANGELOG.md`, commit and push manually.
|
95
|
+
|
96
|
+
github_changelog_generator -u twingly -p twingly-metrics
|
97
|
+
|
98
|
+
[twingly-rubygems]: https://rubygems.org/profiles/twingly
|
@@ -3,6 +3,7 @@
|
|
3
3
|
require "net/https"
|
4
4
|
require "json"
|
5
5
|
require "metriks"
|
6
|
+
require "socket"
|
6
7
|
|
7
8
|
module Twingly
|
8
9
|
module Metrics
|
@@ -43,15 +44,17 @@ module Twingly
|
|
43
44
|
attr_reader :interval
|
44
45
|
attr_reader :timeout
|
45
46
|
|
46
|
-
def initialize(endpoint
|
47
|
-
@endpoint = endpoint
|
48
|
-
@user = user
|
49
|
-
@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 }
|
50
51
|
|
51
52
|
@prefix = options[:prefix]
|
52
|
-
@source = options[:source]
|
53
|
+
@source = options[:source] || default_source
|
53
54
|
@logger = options[:logger]
|
54
55
|
|
56
|
+
log_missing_credentials unless credentials_provided?
|
57
|
+
|
55
58
|
@registry = options[:registry] || Metriks::Registry.default
|
56
59
|
@interval = options[:interval] || 60
|
57
60
|
@on_error = options[:on_error] || proc { |ex| @logger&.error(ex) }
|
@@ -68,7 +71,8 @@ module Twingly
|
|
68
71
|
@reporter_thread = nil
|
69
72
|
end
|
70
73
|
|
71
|
-
def start
|
74
|
+
def start # rubocop:disable Metrics/MethodLength
|
75
|
+
return unless credentials_provided?
|
72
76
|
return if @reporter_thread&.alive?
|
73
77
|
|
74
78
|
@running = true
|
@@ -84,6 +88,8 @@ module Twingly
|
|
84
88
|
end
|
85
89
|
|
86
90
|
def stop
|
91
|
+
return unless credentials_provided?
|
92
|
+
|
87
93
|
@running = false
|
88
94
|
|
89
95
|
return unless @reporter_thread
|
@@ -98,10 +104,14 @@ module Twingly
|
|
98
104
|
end
|
99
105
|
|
100
106
|
def flush
|
107
|
+
return unless credentials_provided?
|
108
|
+
|
101
109
|
@flush_metrics_mutex.synchronize do
|
102
110
|
request_data = build_metrics_data_for_request
|
103
111
|
|
104
|
-
|
112
|
+
retry_failing_request do
|
113
|
+
make_request(request_data)
|
114
|
+
end
|
105
115
|
end
|
106
116
|
rescue StandardError => e
|
107
117
|
@on_error.call(e)
|
@@ -109,6 +119,17 @@ module Twingly
|
|
109
119
|
|
110
120
|
private
|
111
121
|
|
122
|
+
def default_source
|
123
|
+
process_id = Process.pid # In case there are multiple processes running on the same host
|
124
|
+
hostname = if ENV["NOMAD_ALLOC_ID"].to_s.strip.empty?
|
125
|
+
Socket.gethostname.gsub(/[^a-zA-Z0-9_-]/, "_")
|
126
|
+
else
|
127
|
+
ENV.fetch("NOMAD_ALLOC_ID")
|
128
|
+
end
|
129
|
+
|
130
|
+
"#{hostname}_pid-#{process_id}"
|
131
|
+
end
|
132
|
+
|
112
133
|
def make_request(request_data) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
113
134
|
return if request_data.empty?
|
114
135
|
|
@@ -132,6 +153,24 @@ module Twingly
|
|
132
153
|
end
|
133
154
|
end
|
134
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
|
+
|
135
174
|
def build_metrics_data_for_request
|
136
175
|
time = now_floored
|
137
176
|
|
@@ -212,7 +251,7 @@ module Twingly
|
|
212
251
|
time: time.to_i,
|
213
252
|
}
|
214
253
|
|
215
|
-
graphite_data[:tags] = ["source=#{
|
254
|
+
graphite_data[:tags] = ["source=#{source}"]
|
216
255
|
|
217
256
|
graphite_data
|
218
257
|
end
|
@@ -235,6 +274,16 @@ module Twingly
|
|
235
274
|
@next_time = now if @next_time <= now
|
236
275
|
@next_time += @interval - (@next_time % @interval)
|
237
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
|
238
287
|
end
|
239
288
|
end
|
240
289
|
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.
|
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-
|
10
|
+
date: 2025-07-28 00:00:00.000000000 Z
|
11
11
|
dependencies:
|
12
12
|
- !ruby/object:Gem::Dependency
|
13
13
|
name: metriks
|