Skip to content

Commit c2d1eab

Browse files
committed
Services should carry the same namespace
To ensure that there are no conflicts when loading classes, lets namespace them the same across the gem.
1 parent a14cde3 commit c2d1eab

File tree

5 files changed

+104
-98
lines changed

5 files changed

+104
-98
lines changed
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
require_relative "auto_scaling_metrics/reporter"
22
require_relative "auto_scaling_metrics/sidekiq_reporter"
33

4-
module AutoScalingMetrics
4+
module RequestQueueTime
5+
module AutoScalingMetrics
6+
end
57
end

lib/services/auto_scaling_metrics/reporter.rb

Lines changed: 78 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -6,105 +6,107 @@
66
require "aws-sdk-cloudwatch"
77
require "singleton"
88

9-
module AutoScalingMetrics
10-
class Reporter
11-
include Singleton
9+
module RequestQueueTime
10+
module AutoScalingMetrics
11+
class Reporter
12+
include Singleton
1213

13-
DIMENSIONS = [
14-
{name: "service", value: ENV["APP_NAME"]},
15-
{name: "environment", value: ENV["SERVER_ENVIRONMENT"]}
16-
]
14+
DIMENSIONS = [
15+
{name: "service", value: ENV["APP_NAME"]},
16+
{name: "environment", value: ENV["SERVER_ENVIRONMENT"]}
17+
]
1718

18-
attr_accessor :collector
19+
attr_accessor :collector
1920

20-
def self.start(&)
21-
return false if instance.started?
21+
def self.start(&)
22+
return false if instance.started?
2223

23-
instance.start!(&)
24-
end
25-
26-
def self.stop
27-
return unless instance.started?
28-
instance.stop!
29-
end
30-
31-
def start!(&block)
32-
Rails.logger.info "Starting AutoScalingMetrics::Reporter"
33-
@interval = 30
34-
@buffer = Queue.new
35-
@buffer_size_limit = 1000
36-
@cloudwatch = Aws::CloudWatch::Client.new
37-
@last_flush_time = Time.now
38-
@pid = Process.pid
24+
instance.start!(&)
25+
end
3926

40-
yield self if block
27+
def self.stop
28+
return unless instance.started?
29+
instance.stop!
30+
end
4131

42-
start_flush_thread
43-
end
32+
def start!(&block)
33+
Rails.logger.info "Starting AutoScalingMetrics::Reporter"
34+
@interval = 30
35+
@buffer = Queue.new
36+
@buffer_size_limit = 1000
37+
@cloudwatch = Aws::CloudWatch::Client.new
38+
@last_flush_time = Time.now
39+
@pid = Process.pid
4440

45-
def started?
46-
@pid == Process.pid
47-
end
41+
yield self if block
4842

49-
def stop!
50-
@_thread&.terminate
51-
@_thread = nil
52-
@pid = nil
53-
end
43+
start_flush_thread
44+
end
5445

55-
def track_request_queue_time(time)
56-
add_metric(metric_name: "request_queue_time", value: time, unit: "Milliseconds", timestamp: Time.now)
57-
end
46+
def started?
47+
@pid == Process.pid
48+
end
5849

59-
def self.add_metric(metric)
60-
instance.add_metric(metric)
61-
end
50+
def stop!
51+
@_thread&.terminate
52+
@_thread = nil
53+
@pid = nil
54+
end
6255

63-
def add_metric(metric)
64-
metric[:dimensions] = (metric[:dimensions] || []) + DIMENSIONS
65-
@buffer << metric
66-
end
56+
def track_request_queue_time(time)
57+
add_metric(metric_name: "request_queue_time", value: time, unit: "Milliseconds", timestamp: Time.now)
58+
end
6759

68-
private
60+
def self.add_metric(metric)
61+
instance.add_metric(metric)
62+
end
6963

70-
def start_flush_thread
71-
@_thread = Thread.new do
72-
Thread.current.name = "auto_scaling_metrics.#{@pid}"
64+
def add_metric(metric)
65+
metric[:dimensions] = (metric[:dimensions] || []) + DIMENSIONS
66+
@buffer << metric
67+
end
7368

74-
loop do
75-
if should_flush?
76-
collector&.call
77-
flush_metrics
69+
private
70+
71+
def start_flush_thread
72+
@_thread = Thread.new do
73+
Thread.current.name = "auto_scaling_metrics.#{@pid}"
74+
75+
loop do
76+
if should_flush?
77+
collector&.call
78+
flush_metrics
79+
end
80+
rescue => ex
81+
Sentry.capture_exception(ex)
82+
ensure
83+
sleep(1)
7884
end
79-
rescue => ex
80-
Sentry.capture_exception(ex)
81-
ensure
82-
sleep(1)
8385
end
8486
end
85-
end
8687

87-
def should_flush?
88-
@buffer.size >= @buffer_size_limit || (Time.now - @last_flush_time) >= @interval
89-
end
88+
def should_flush?
89+
@buffer.size >= @buffer_size_limit || (Time.now - @last_flush_time) >= @interval
90+
end
9091

91-
def flush_metrics
92-
metrics_to_flush = []
93-
metrics_to_flush << @buffer.pop until @buffer.empty?
92+
def flush_metrics
93+
metrics_to_flush = []
94+
metrics_to_flush << @buffer.pop until @buffer.empty?
9495

95-
unless metrics_to_flush.empty?
96-
put_metrics_to_cloudwatch(metrics_to_flush)
97-
@last_flush_time = Time.now
96+
unless metrics_to_flush.empty?
97+
put_metrics_to_cloudwatch(metrics_to_flush)
98+
@last_flush_time = Time.now
99+
end
98100
end
99-
end
100101

101-
def put_metrics_to_cloudwatch(metrics)
102-
return if Rails.env.development?
102+
def put_metrics_to_cloudwatch(metrics)
103+
return if Rails.env.development?
103104

104-
@cloudwatch.put_metric_data(
105-
namespace: "Teamtailor/queue_times",
106-
metric_data: metrics.take(1000)
107-
)
105+
@cloudwatch.put_metric_data(
106+
namespace: "Teamtailor/queue_times",
107+
metric_data: metrics.take(1000)
108+
)
109+
end
108110
end
109111
end
110112
end

lib/services/auto_scaling_metrics/sidekiq_reporter.rb

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
1-
module AutoScalingMetrics
2-
class SidekiqReporter
3-
def self.enable
4-
Sidekiq.configure_server do |config|
5-
config.on(:leader) do
6-
AutoScalingMetrics::Reporter.start do |reporter|
7-
reporter.collector = method(:collect_metrics)
1+
module RequestQueueTime
2+
module AutoScalingMetrics
3+
class SidekiqReporter
4+
def self.enable
5+
Sidekiq.configure_server do |config|
6+
config.on(:leader) do
7+
AutoScalingMetrics::Reporter.start do |reporter|
8+
reporter.collector = method(:collect_metrics)
9+
end
810
end
911
end
1012
end
11-
end
1213

13-
def self.collect_metrics
14-
Sidekiq::Queue.all.each do |queue|
15-
AutoScalingMetrics::Reporter.add_metric(
16-
metric_name: "sidekiq_queue_latency",
17-
value: queue.latency,
18-
unit: "Seconds",
19-
dimensions: [{name: "queue_name", value: queue.name}]
20-
)
14+
def self.collect_metrics
15+
Sidekiq::Queue.all.each do |queue|
16+
AutoScalingMetrics::Reporter.add_metric(
17+
metric_name: "sidekiq_queue_latency",
18+
value: queue.latency,
19+
unit: "Seconds",
20+
dimensions: [{name: "queue_name", value: queue.name}]
21+
)
22+
end
2123
end
2224
end
2325
end

spec/services/reporter_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
require "spec_helper"
44

5-
RSpec.describe AutoScalingMetrics::Reporter do
5+
RSpec.describe RequestQueueTime::AutoScalingMetrics::Reporter do
66
let(:subject) { described_class.instance }
77

88
before do

spec/services/sidekiq_reporter_spec.rb

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

33
require "spec_helper"
44

5-
RSpec.describe AutoScalingMetrics::SidekiqReporter do
5+
RSpec.describe RequestQueueTime::AutoScalingMetrics::SidekiqReporter do
66
describe ".enable" do
77
it "starts the reporter and collects metrics" do
88
expect(Sidekiq).to receive(:configure_server).and_yield(config = double)
99
expect(config).to receive(:on).with(:leader).and_yield
10-
expect(AutoScalingMetrics::Reporter).to receive(:start).and_yield(reporter = double)
10+
expect(RequestQueueTime::AutoScalingMetrics::Reporter).to receive(:start).and_yield(reporter = double)
1111
expect(reporter).to receive(:collector=).with(described_class.method(:collect_metrics))
1212

1313
described_class.enable
@@ -20,13 +20,13 @@
2020
queue2 = double(name: "queue2", latency: 20)
2121
allow(Sidekiq::Queue).to receive(:all).and_return([queue1, queue2])
2222

23-
expect(AutoScalingMetrics::Reporter).to receive(:add_metric).with(
23+
expect(RequestQueueTime::AutoScalingMetrics::Reporter).to receive(:add_metric).with(
2424
metric_name: "sidekiq_queue_latency",
2525
value: 10,
2626
unit: "Seconds",
2727
dimensions: [{name: "queue_name", value: "queue1"}]
2828
)
29-
expect(AutoScalingMetrics::Reporter).to receive(:add_metric).with(
29+
expect(RequestQueueTime::AutoScalingMetrics::Reporter).to receive(:add_metric).with(
3030
metric_name: "sidekiq_queue_latency",
3131
value: 20,
3232
unit: "Seconds",

0 commit comments

Comments
 (0)