Skip to content

Commit 83a8979

Browse files
SamSaffronlis2
andauthored
Version bump (#299)
Also cleans up some linting and gets the test suite to pass Co-authored-by: Krzysztof Kotlarek <[email protected]>
1 parent e763dc6 commit 83a8979

File tree

11 files changed

+159
-101
lines changed

11 files changed

+159
-101
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ jobs:
2727
strategy:
2828
fail-fast: false
2929
matrix:
30-
ruby: ['2.6', '2.7', '3.0', '3.1', '3.2']
31-
activerecord: [60, 61]
30+
ruby: ['3.0', '3.1', '3.2', '3.3']
31+
activerecord: [60, 61, 70]
3232

3333
steps:
3434
- uses: actions/checkout@v2

Appraisals

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
# frozen_string_literal: true
22

33
appraise "ar-60" do
4-
# we are using this version as default in gemspec
5-
# gem "activerecord", "~> 6.0.0"
4+
gem "activerecord", "~> 6.0.0"
65
end
76

87
appraise "ar-61" do
98
gem "activerecord", "~> 6.1.1"
109
end
10+
11+
appraise "ar-70" do
12+
# latest version
13+
gem "activerecord", "~> 7.1.2"
14+
end

CHANGELOG

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
2.1.0 - 2024-08-01
2+
3+
- FEATURE: good_job instrumentation
4+
- PERF: improve performance of histogram
5+
- DEV: use new metric collector pattern so we reuse code between collectors
6+
17
2.0.8 - 2023-01-20
28

39
- FEATURE: attempting to make our first docker release

gemfiles/ar_70.gemfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# This file was generated by Appraisal
2+
3+
source "https://rubygems.org"
4+
5+
gemspec path: "../"

lib/prometheus_exporter.rb

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22

33
require_relative "prometheus_exporter/version"
44
require "json"
5-
require "thread"
65

76
module PrometheusExporter
87
# per: https://github.com/prometheus/prometheus/wiki/Default-port-allocations
98
DEFAULT_PORT = 9394
10-
DEFAULT_BIND_ADDRESS = 'localhost'
11-
DEFAULT_PREFIX = 'ruby_'
9+
DEFAULT_BIND_ADDRESS = "localhost"
10+
DEFAULT_PREFIX = "ruby_"
1211
DEFAULT_LABEL = {}
1312
DEFAULT_TIMEOUT = 2
14-
DEFAULT_REALM = 'Prometheus Exporter'
13+
DEFAULT_REALM = "Prometheus Exporter"
1514

1615
class OjCompat
1716
def self.parse(obj)
1817
Oj.compat_load(obj)
1918
end
19+
2020
def self.dump(obj)
2121
Oj.dump(obj, mode: :compat)
2222
end
@@ -25,7 +25,7 @@ def self.dump(obj)
2525
def self.hostname
2626
@hostname ||=
2727
begin
28-
require 'socket'
28+
require "socket"
2929
Socket.gethostname
3030
rescue => e
3131
STDERR.puts "Unable to lookup hostname #{e}"
@@ -45,13 +45,12 @@ def self.detect_json_serializer(preferred)
4545
def self.has_oj?
4646
(
4747
@@has_oj ||=
48-
begin
49-
require 'oj'
50-
:true
51-
rescue LoadError
52-
:false
53-
end
54-
) == :true
48+
begin
49+
require "oj"
50+
:T
51+
rescue LoadError
52+
:F
53+
end
54+
) == :T
5555
end
56-
5756
end

lib/prometheus_exporter/client.rb

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
# frozen_string_literal: true
22

3-
require 'socket'
4-
require 'thread'
5-
require 'logger'
3+
require "socket"
4+
require "logger"
65

76
module PrometheusExporter
87
class Client
@@ -25,7 +24,9 @@ def standard_values(value, keys, prometheus_exporter_action = nil)
2524
keys: keys,
2625
value: value
2726
}
28-
values[:prometheus_exporter_action] = prometheus_exporter_action if prometheus_exporter_action
27+
values[
28+
:prometheus_exporter_action
29+
] = prometheus_exporter_action if prometheus_exporter_action
2930
values[:opts] = @opts if @opts
3031
values
3132
end
@@ -57,8 +58,11 @@ def self.default=(client)
5758
attr_reader :logger
5859

5960
def initialize(
60-
host: ENV.fetch('PROMETHEUS_EXPORTER_HOST', 'localhost'),
61-
port: ENV.fetch('PROMETHEUS_EXPORTER_PORT', PrometheusExporter::DEFAULT_PORT),
61+
host: ENV.fetch("PROMETHEUS_EXPORTER_HOST", "localhost"),
62+
port: ENV.fetch(
63+
"PROMETHEUS_EXPORTER_PORT",
64+
PrometheusExporter::DEFAULT_PORT
65+
),
6266
max_queue_size: nil,
6367
thread_sleep: 0.5,
6468
json_serializer: nil,
@@ -90,7 +94,8 @@ def initialize(
9094
@mutex = Mutex.new
9195
@thread_sleep = thread_sleep
9296

93-
@json_serializer = json_serializer == :oj ? PrometheusExporter::OjCompat : JSON
97+
@json_serializer =
98+
json_serializer == :oj ? PrometheusExporter::OjCompat : JSON
9499

95100
@custom_labels = custom_labels
96101
end
@@ -100,7 +105,14 @@ def custom_labels=(custom_labels)
100105
end
101106

102107
def register(type, name, help, opts = nil)
103-
metric = RemoteMetric.new(type: type, name: name, help: help, client: self, opts: opts)
108+
metric =
109+
RemoteMetric.new(
110+
type: type,
111+
name: name,
112+
help: help,
113+
client: self,
114+
opts: opts
115+
)
104116
@metrics << metric
105117
metric
106118
end
@@ -161,9 +173,7 @@ def stop(wait_timeout_seconds: 0)
161173
@mutex.synchronize do
162174
wait_for_empty_queue_with_timeout(wait_timeout_seconds)
163175
@worker_thread&.kill
164-
while @worker_thread&.alive?
165-
sleep 0.001
166-
end
176+
sleep 0.001 while @worker_thread&.alive?
167177
@worker_thread = nil
168178
close_socket!
169179
end
@@ -183,12 +193,13 @@ def ensure_worker_thread!
183193
@mutex.synchronize do
184194
return if @worker_thread&.alive?
185195

186-
@worker_thread = Thread.new do
187-
while true
188-
worker_loop
189-
sleep @thread_sleep
196+
@worker_thread =
197+
Thread.new do
198+
while true
199+
worker_loop
200+
sleep @thread_sleep
201+
end
190202
end
191-
end
192203
end
193204
end
194205
rescue ThreadError => e
@@ -212,7 +223,8 @@ def close_socket!
212223
end
213224

214225
def close_socket_if_old!
215-
if @socket_pid == Process.pid && @socket && @socket_started && ((@socket_started + MAX_SOCKET_AGE) < Time.now.to_f)
226+
if @socket_pid == Process.pid && @socket && @socket_started &&
227+
((@socket_started + MAX_SOCKET_AGE) < Time.now.to_f)
216228
close_socket!
217229
end
218230
end
@@ -240,7 +252,7 @@ def ensure_socket!
240252
end
241253

242254
nil
243-
rescue
255+
rescue StandardError
244256
@socket = nil
245257
@socket_started = nil
246258
@socket_pid = nil
@@ -250,7 +262,10 @@ def ensure_socket!
250262
def wait_for_empty_queue_with_timeout(timeout_seconds)
251263
start_time = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
252264
while @queue.length > 0
253-
break if start_time + timeout_seconds < ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
265+
if start_time + timeout_seconds <
266+
::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
267+
break
268+
end
254269
sleep(0.05)
255270
end
256271
end

lib/prometheus_exporter/instrumentation/sidekiq.rb

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
# frozen_string_literal: true
22

3-
require 'yaml'
3+
require "yaml"
44

55
module PrometheusExporter::Instrumentation
6-
JOB_WRAPPER_CLASS_NAME = 'ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper'
7-
DELAYED_CLASS_NAMES = [
8-
'Sidekiq::Extensions::DelayedClass',
9-
'Sidekiq::Extensions::DelayedModel',
10-
'Sidekiq::Extensions::DelayedMailer',
6+
JOB_WRAPPER_CLASS_NAME =
7+
"ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper"
8+
DELAYED_CLASS_NAMES = %w[
9+
Sidekiq::Extensions::DelayedClass
10+
Sidekiq::Extensions::DelayedModel
11+
Sidekiq::Extensions::DelayedMailer
1112
]
1213

1314
class Sidekiq
1415
def self.death_handler
15-
-> (job, ex) do
16+
->(job, ex) do
1617
job_is_fire_and_forget = job["retry"] == false
1718

1819
worker_class = Object.const_get(job["class"])
@@ -43,7 +44,8 @@ def self.get_worker_custom_labels(worker_class, msg)
4344
end
4445

4546
def initialize(options = { client: nil })
46-
@client = options.fetch(:client, nil) || PrometheusExporter::Client.default
47+
@client =
48+
options.fetch(:client, nil) || PrometheusExporter::Client.default
4749
end
4850

4951
def call(worker, msg, queue)
@@ -82,33 +84,33 @@ def self.get_name(class_name, msg)
8284
end
8385

8486
def self.get_job_wrapper_name(msg)
85-
msg['wrapped']
87+
msg["wrapped"]
8688
end
8789

8890
def self.get_delayed_name(msg, class_name)
8991
begin
9092
# fallback to class_name since we're relying on the internal implementation
9193
# of the delayed extensions
9294
# https://github.com/mperham/sidekiq/blob/master/lib/sidekiq/extensions/class_methods.rb
93-
(target, method_name, _args) = YAML.load(msg['args'].first) # rubocop:disable Security/YAMLLoad
95+
target, method_name, _args = YAML.load(msg["args"].first)
9496
if target.class == Class
9597
"#{target.name}##{method_name}"
9698
else
9799
"#{target.class.name}##{method_name}"
98100
end
99101
rescue Psych::DisallowedClass, ArgumentError
100-
parsed = Psych.parse(msg['args'].first)
102+
parsed = Psych.parse(msg["args"].first)
101103
children = parsed.root.children
102-
target = (children[0].value || children[0].tag).sub('!', '')
103-
method_name = (children[1].value || children[1].tag).sub(':', '')
104+
target = (children[0].value || children[0].tag).sub("!", "")
105+
method_name = (children[1].value || children[1].tag).sub(":", "")
104106

105107
if target && method_name
106108
"#{target}##{method_name}"
107109
else
108110
class_name
109111
end
110112
end
111-
rescue
113+
rescue StandardError
112114
class_name
113115
end
114116
end

0 commit comments

Comments
 (0)