Skip to content

Commit c5bca7a

Browse files
Create the configuration for a dedicated metrics publishing service
- This allows for publishing service-independent metrics - This is valuable for higher level stats that should not be produced per running container
1 parent 6075796 commit c5bca7a

4 files changed

Lines changed: 41 additions & 10 deletions

File tree

bin/docker-start

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,15 @@ elif [ "$SERVER_TYPE" == "sidekiq" ]; then
1212
"$BIN_DIR"/prometheus_exporter &
1313
sleep 5
1414
exec "$BIN_DIR"/sidekiq
15+
elif [ "$SERVER_TYPE" == "metric-publisher" ]; then
16+
echo "Starting metrics publisher..."
17+
"$BIN_DIR"/prometheus_exporter skip-server-labels &
18+
sleep 5
19+
exec "$BIN_DIR"/metrics-publisher
1520
elif [ "$SERVER_TYPE" == "none" ]; then
1621
echo "No server started"
1722
exec tail -f /dev/null # Keep container running
1823
else
19-
echo "SERVER_TYPE variable: '$SERVER_TYPE' unknown. Allowed values: web, sidekiq, none"
24+
echo "SERVER_TYPE variable: '$SERVER_TYPE' unknown. Allowed values: web, sidekiq, metric-publisher, none"
2025
exit 1
2126
fi

bin/metrics-publisher

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
require_relative "../config/environment"
5+
require "prometheus_exporter/instrumentation"
6+
7+
PrometheusExporter::Instrumentation::SidekiqStats.start
8+
PrometheusExporter::Instrumentation::SidekiqQueue.start(all_queues: true)
9+
10+
at_exit { PrometheusExporter::Client.default.stop(wait_timeout_seconds: 10) }
11+
12+
Kernel.sleep

bin/prometheus_exporter

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,31 @@ def fetch_ecs_task_id
2727
end
2828
end
2929

30-
if ARGV.include?("local")
31-
task_id = "N/A"
32-
service_name = "N/A"
33-
else
34-
task_id = fetch_ecs_task_id
35-
service_name = ENV["SERVICE_NAME"]
30+
# An explicitly-set env var (including empty string) overrides the default.
31+
# An empty value omits the label entirely — useful for the metrics task,
32+
# whose series identity must stay stable across container replacements.
33+
def resolve_label(env_key, &default)
34+
if ENV.key?(env_key)
35+
value = ENV[env_key]
36+
return value.empty? ? nil : value
37+
end
38+
39+
default.call
3640
end
3741

42+
labels = {
43+
"TaskId" => resolve_label("PROMETHEUS_TASK_ID_LABEL") do
44+
ARGV.include?("skip-server-labels") ? nil : fetch_ecs_task_id
45+
end,
46+
"ServiceName" => resolve_label("PROMETHEUS_SERVICE_NAME_LABEL") do
47+
ARGV.include?("skip-server-labels") ? nil : ENV["SERVICE_NAME"]
48+
end
49+
}.compact
50+
3851
runner = PrometheusExporter::Server::Runner.new(
3952
port: 9394,
4053
bind: "0.0.0.0",
41-
label: { "TaskId" => task_id, "ServiceName" => service_name },
54+
label: labels,
4255
type_collectors: [PrometheusExporter::CustomActiveRecordCollector],
4356
)
4457

config/initializers/sidekiq.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ def call(_worker, job, _queue)
4949
PrometheusExporter::Instrumentation::Process.start type: "sidekiq"
5050
PrometheusExporter::Instrumentation::ActiveRecord.start
5151
PrometheusExporter::Instrumentation::SidekiqProcess.start
52-
PrometheusExporter::Instrumentation::SidekiqQueue.start
53-
PrometheusExporter::Instrumentation::SidekiqStats.start
52+
# SidekiqStats and SidekiqQueue are global Redis-backed metrics published
53+
# by the metrics task (bin/metrics-publisher) to avoid duplicate
54+
# series across Sidekiq containers.
5455
end
5556

5657
at_exit do

0 commit comments

Comments
 (0)