Skip to content

Commit 30d8f7c

Browse files
author
Willem Homan
committed
fix(platform): PAYMENTS-11567 Require time stdlib, clamp negative queue latency, fix changelog
1 parent 12b01d8 commit 30d8f7c

4 files changed

Lines changed: 21 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Changelog for the bc-prometheus-ruby gem.
22

33
### Pending Release
44

5-
- Add opt-in per-Resque-job histograms `resque_job_queue_latency_seconds` and `resque_job_perform_duration_seconds`, labelled by `job_class`. Recorded from the parent worker process (via `Resque.before_fork` and a `Module#prepend` around `Resque::Worker#perform_with_fork`), so no synchronous flush is needed in the forked child. Gated by `PROMETHEUS_RESQUE_PER_JOB_METRICS_ENABLED` (default off).
5+
- Add opt-in per-Resque-job histograms `resque_job_queue_latency_seconds` and `resque_job_perform_duration_seconds`, labelled by `job_class`. Recorded from the parent worker process via a `Module#prepend` around `Resque::Worker#perform_with_fork` (queue latency before the fork, perform duration after), so no synchronous flush is needed in the forked child. Gated by `PROMETHEUS_RESQUE_PER_JOB_METRICS_ENABLED` (default off).
66

77
## 0.8.1
88

lib/bigcommerce/prometheus/integrations/resque/job_metrics.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,15 @@ def record_queue_latency(payload)
8383
anchor = payload.anchor_time
8484
return unless anchor
8585

86+
# Clock skew between the enqueuer/scheduler and the worker can put
87+
# the anchor in the future; clamp so the histogram never records a
88+
# negative latency.
89+
latency = (Time.now - anchor).to_f.clamp(0.0..)
90+
8691
@client.send_json(
8792
type: 'resque_job',
8893
metric: 'queue_latency',
89-
value: (Time.now - anchor).to_f,
94+
value: latency,
9095
custom_labels: { job_class: payload.job_class }
9196
)
9297
rescue StandardError => e

lib/bigcommerce/prometheus/integrations/resque/job_payload.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
1616
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1717
#
18+
require 'time'
19+
1820
module Bigcommerce
1921
module Prometheus
2022
module Integrations

spec/bigcommerce/prometheus/integrations/resque/job_metrics_spec.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,18 @@ def active_job_payload(job_class: 'MyJob', enqueued_at: nil, scheduled_at: nil)
9191
described_class.record_queue_latency(payload_for(active_job_payload(enqueued_at: enqueued)))
9292
end
9393

94+
it 'clamps the value to zero when the anchor is in the future (clock skew)' do
95+
future = (Time.now + 5).iso8601(6)
96+
97+
expect(client).to receive(:send_json).with(
98+
hash_including(metric: 'queue_latency', value: 0.0)
99+
)
100+
101+
described_class.record_queue_latency(payload_for(active_job_payload(enqueued_at: future)))
102+
end
103+
94104
it 'is a no-op for a vanilla Resque payload (no anchor available, no exception)' do
95-
payload = { 'class' => 'RawResqueJob', 'args' => [12345, 'some_string'] }
105+
payload = { 'class' => 'RawResqueJob', 'args' => [12_345, 'some_string'] }
96106

97107
expect(client).not_to receive(:send_json)
98108

@@ -128,7 +138,7 @@ def active_job_payload(job_class: 'MyJob', enqueued_at: nil, scheduled_at: nil)
128138
end
129139

130140
it 'labels with the raw Resque payload class for vanilla Resque jobs' do
131-
payload = { 'class' => 'RawResqueJob', 'args' => [12345, 'some_string'] }
141+
payload = { 'class' => 'RawResqueJob', 'args' => [12_345, 'some_string'] }
132142

133143
expect(client).to receive(:send_json).with(
134144
hash_including(custom_labels: { job_class: 'RawResqueJob' })

0 commit comments

Comments
 (0)