Skip to content

Commit d5486c2

Browse files
authored
Replace statsd-client with statsd-instrument (#5278)
1 parent cc59500 commit d5486c2

17 files changed

Lines changed: 116 additions & 143 deletions

File tree

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ gem 'sequel', '~> 5.106'
2929
gem 'sequel_pg', require: 'sequel'
3030
gem 'sinatra', '~> 4.2'
3131
gem 'sinatra-contrib'
32-
gem 'statsd-ruby', '~> 1.5.0'
32+
gem 'statsd-instrument', '~> 3.11'
3333
gem 'talentbox-delayed_job_sequel', '~> 4.4.0'
3434
gem 'uri', '~> 1.1'
3535
gem 'vmstat', '~> 2.3'

Gemfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ GEM
431431
spring (4.7.0)
432432
spring-commands-rspec (1.0.4)
433433
spring (>= 0.9.1)
434-
statsd-ruby (1.5.0)
434+
statsd-instrument (3.11.1)
435435
stringio (3.2.0)
436436
talentbox-delayed_job_sequel (4.4.0)
437437
delayed_job (~> 4.1)
@@ -532,7 +532,7 @@ DEPENDENCIES
532532
solargraph
533533
spring
534534
spring-commands-rspec
535-
statsd-ruby (~> 1.5.0)
535+
statsd-instrument (~> 3.11)
536536
talentbox-delayed_job_sequel (~> 4.4.0)
537537
timecop
538538
uri (~> 1.1)

app/jobs/diego/sync.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'cloud_controller/diego/processes_sync'
22
require 'cloud_controller/diego/tasks_sync'
3-
require 'statsd'
3+
require 'statsd/instrument'
44

55
module VCAP::CloudController
66
module Jobs
@@ -25,7 +25,7 @@ def perform
2525
## so feed in the entire value!
2626
elapsed_ms = ((finish - start) * 1000).round
2727

28-
@statsd.timing('cc.diego_sync.duration', elapsed_ms)
28+
@statsd.measure('cc.diego_sync.duration', elapsed_ms)
2929
end
3030
end
3131

lib/cloud_controller/dependency_locator.rb

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
require 'cloud_controller/packager/local_bits_packer'
2525
require 'credhub/client'
2626
require 'cloud_controller/metrics/prometheus_updater'
27+
require 'statsd/instrument'
2728
require 'cloud_controller/execution_context'
2829

2930
module CloudController
@@ -367,8 +368,7 @@ def statsd_client
367368
if @dependencies[:statsd_client]
368369
@dependencies[:statsd_client]
369370
elsif config.get(:enable_statsd_metrics) == true || config.get(:enable_statsd_metrics).nil?
370-
Statsd.logger = Steno.logger('statsd.client')
371-
register(:statsd_client, Statsd.new(config.get(:statsd_host), config.get(:statsd_port)))
371+
register(:statsd_client, StatsD::Instrument::Client.new(sink: statsd_sink))
372372
else
373373
register(:statsd_client, NullStatsdClient.new)
374374
end
@@ -378,6 +378,14 @@ def statsd_client
378378

379379
private
380380

381+
# Memoized per address so one long-lived UDP socket serves the process; statsd-instrument
382+
# pins its socket in a thread-local store and never reclaims it, so a fresh sink per call would leak one.
383+
def statsd_sink
384+
addr = "#{config.get(:statsd_host)}:#{config.get(:statsd_port)}"
385+
@statsd_sinks ||= {}
386+
@statsd_sinks[addr] ||= StatsD::Instrument::Sink.for_addr(addr)
387+
end
388+
381389
def build_stager_client
382390
build_bbs_stager_client
383391
end
@@ -464,19 +472,15 @@ def create_paginated_collection_renderer(opts={})
464472
end
465473

466474
class NullStatsdClient
467-
def timing(_key, _value)
468-
# Null implementation
469-
end
470-
471-
def increment(_key)
475+
def gauge(_stat, _value, *)
472476
# Null implementation
473477
end
474478

475-
def gauge(_stat, _value, _sample_rate=1)
479+
def increment(_stat, _value=1, *)
476480
# Null implementation
477481
end
478482

479-
def batch
483+
def measure(_stat, _value=nil, *)
480484
# Null implementation
481485
end
482486
end

lib/cloud_controller/deployment_updater/scheduler.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def update(update_frequency:, statsd_client:)
5757
## milliseconds, then do know that the lack of precision here is not desired
5858
## so feed in the entire value!
5959
update_duration_ms = update_duration * 1000
60-
statsd_client.timing('cc.deployments.update.duration', update_duration_ms)
60+
statsd_client.measure('cc.deployments.update.duration', update_duration_ms)
6161

6262
logger.info("Update loop took #{update_duration}s")
6363

lib/cloud_controller/metrics/request_metrics.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require 'statsd'
1+
require 'statsd/instrument'
22

33
module VCAP::CloudController
44
module Metrics

lib/cloud_controller/metrics/statsd_updater.rb

Lines changed: 27 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require 'statsd'
1+
require 'statsd/instrument'
22

33
module VCAP::CloudController::Metrics
44
class StatsdUpdater
@@ -16,64 +16,50 @@ def update_user_count(user_count)
1616
end
1717

1818
def update_job_queue_length(pending_job_count_by_queue, total)
19-
@statsd.batch do |batch|
20-
pending_job_count_by_queue.each do |key, value|
21-
batch.gauge("cc.job_queue_length.#{key}", value)
22-
end
23-
batch.gauge('cc.job_queue_length.total', total)
19+
pending_job_count_by_queue.each do |key, value|
20+
@statsd.gauge("cc.job_queue_length.#{key}", value)
2421
end
22+
@statsd.gauge('cc.job_queue_length.total', total)
2523
end
2624

2725
def update_job_queue_load(pending_job_load_by_queue, total)
28-
@statsd.batch do |batch|
29-
pending_job_load_by_queue.each do |key, value|
30-
batch.gauge("cc.job_queue_load.#{key}", value)
31-
end
32-
batch.gauge('cc.job_queue_load.total', total)
26+
pending_job_load_by_queue.each do |key, value|
27+
@statsd.gauge("cc.job_queue_load.#{key}", value)
3328
end
29+
@statsd.gauge('cc.job_queue_load.total', total)
3430
end
3531

3632
def update_thread_info_thin(thread_info)
37-
@statsd.batch do |batch|
38-
batch.gauge('cc.thread_info.thread_count', thread_info[:thread_count])
39-
batch.gauge('cc.thread_info.event_machine.connection_count', thread_info[:event_machine][:connection_count])
40-
batch.gauge('cc.thread_info.event_machine.threadqueue.size', thread_info[:event_machine][:threadqueue][:size])
41-
batch.gauge('cc.thread_info.event_machine.threadqueue.num_waiting', thread_info[:event_machine][:threadqueue][:num_waiting])
42-
batch.gauge('cc.thread_info.event_machine.resultqueue.size', thread_info[:event_machine][:resultqueue][:size])
43-
batch.gauge('cc.thread_info.event_machine.resultqueue.num_waiting', thread_info[:event_machine][:resultqueue][:num_waiting])
44-
end
33+
@statsd.gauge('cc.thread_info.thread_count', thread_info[:thread_count])
34+
@statsd.gauge('cc.thread_info.event_machine.connection_count', thread_info[:event_machine][:connection_count])
35+
@statsd.gauge('cc.thread_info.event_machine.threadqueue.size', thread_info[:event_machine][:threadqueue][:size])
36+
@statsd.gauge('cc.thread_info.event_machine.threadqueue.num_waiting', thread_info[:event_machine][:threadqueue][:num_waiting])
37+
@statsd.gauge('cc.thread_info.event_machine.resultqueue.size', thread_info[:event_machine][:resultqueue][:size])
38+
@statsd.gauge('cc.thread_info.event_machine.resultqueue.num_waiting', thread_info[:event_machine][:resultqueue][:num_waiting])
4539
end
4640

4741
def update_failed_job_count(failed_jobs_by_queue, total)
48-
@statsd.batch do |batch|
49-
failed_jobs_by_queue.each do |key, value|
50-
batch.gauge("cc.failed_job_count.#{key}", value)
51-
end
52-
batch.gauge('cc.failed_job_count.total', total)
42+
failed_jobs_by_queue.each do |key, value|
43+
@statsd.gauge("cc.failed_job_count.#{key}", value)
5344
end
45+
@statsd.gauge('cc.failed_job_count.total', total)
5446
end
5547

5648
def update_vitals(vitals)
57-
@statsd.batch do |batch|
58-
vitals.each do |key, val|
59-
batch.gauge("cc.vitals.#{key}", val)
60-
end
49+
vitals.each do |key, val|
50+
@statsd.gauge("cc.vitals.#{key}", val)
6151
end
6252
end
6353

6454
def update_log_counts(counts)
65-
@statsd.batch do |batch|
66-
counts.each do |key, val|
67-
batch.gauge("cc.log_count.#{key}", val)
68-
end
55+
counts.each do |key, val|
56+
@statsd.gauge("cc.log_count.#{key}", val)
6957
end
7058
end
7159

7260
def update_task_stats(total_running_tasks, total_memory_in_mb)
73-
@statsd.batch do |batch|
74-
batch.gauge('cc.tasks_running.count', total_running_tasks)
75-
batch.gauge('cc.tasks_running.memory_in_mb', total_memory_in_mb)
76-
end
61+
@statsd.gauge('cc.tasks_running.count', total_running_tasks)
62+
@statsd.gauge('cc.tasks_running.memory_in_mb', total_memory_in_mb)
7763
end
7864

7965
def update_synced_invalid_lrps(lrp_count)
@@ -86,12 +72,12 @@ def start_staging_request_received
8672

8773
def report_staging_success_metrics(duration_ns)
8874
@statsd.increment('cc.staging.succeeded')
89-
@statsd.timing('cc.staging.succeeded_duration', nanoseconds_to_milliseconds(duration_ns))
75+
@statsd.measure('cc.staging.succeeded_duration', nanoseconds_to_milliseconds(duration_ns))
9076
end
9177

9278
def report_staging_failure_metrics(duration_ns)
9379
@statsd.increment('cc.staging.failed')
94-
@statsd.timing('cc.staging.failed_duration', nanoseconds_to_milliseconds(duration_ns))
80+
@statsd.measure('cc.staging.failed_duration', nanoseconds_to_milliseconds(duration_ns))
9581
end
9682

9783
def start_request
@@ -103,11 +89,9 @@ def complete_request(status)
10389
http_status_code = "#{status.to_s[0]}XX"
10490
http_status_metric = "cc.http_status.#{http_status_code}"
10591
@statsd.gauge('cc.requests.outstanding.gauge', store.decrement_request_outstanding_gauge)
106-
@statsd.batch do |batch|
107-
batch.decrement 'cc.requests.outstanding'
108-
batch.increment 'cc.requests.completed'
109-
batch.increment http_status_metric
110-
end
92+
@statsd.increment('cc.requests.outstanding', -1)
93+
@statsd.increment('cc.requests.completed')
94+
@statsd.increment(http_status_metric)
11195
end
11296

11397
private

spec/unit/controllers/base/base_controller_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module VCAP::CloudController
99
let(:config) { double(Config, get: nil) }
1010
let(:dependencies) do
1111
{
12-
statsd_client: double(Statsd)
12+
statsd_client: double(StatsD::Instrument::Client)
1313
}
1414
end
1515

spec/unit/controllers/base/model_controller_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module VCAP::CloudController
88
{
99
object_renderer: nil,
1010
collection_renderer: nil,
11-
statsd_client: double(Statsd)
11+
statsd_client: double(StatsD::Instrument::Client)
1212
}
1313
end
1414
let(:config) { double(Config, get: nil) }

spec/unit/controllers/internal/log_access_controller_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module VCAP::CloudController
2020
{},
2121
nil,
2222
{
23-
statsd_client: double(Statsd)
23+
statsd_client: double(StatsD::Instrument::Client)
2424
}
2525
)
2626
end

0 commit comments

Comments
 (0)