Skip to content

Commit

Permalink
Allow disabling per-process metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
sj26 committed Mar 6, 2023
1 parent 4fc1127 commit 28dc515
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 15 deletions.
33 changes: 18 additions & 15 deletions lib/sidekiq/cloudwatchmetrics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,13 @@ class Publisher

INTERVAL = 60 # seconds

def initialize(config: Sidekiq, client: Aws::CloudWatch::Client.new, namespace: "Sidekiq", additional_dimensions: {})
def initialize(config: Sidekiq, client: Aws::CloudWatch::Client.new, namespace: "Sidekiq", process_metrics: true, additional_dimensions: {})
# Sidekiq 6.5+ requires @config, which defaults to the top-level
# `Sidekiq` module, but can be overridden when running multiple Sidekiqs.
@config = config
@client = client
@namespace = namespace
@process_metrics = process_metrics
@additional_dimensions = additional_dimensions.map { |k, v| {name: k.to_s, value: v.to_s} }
end

Expand Down Expand Up @@ -163,23 +164,25 @@ def publish
}
end

processes.each do |process|
process_utilization = process["busy"] / process["concurrency"].to_f * 100.0
if @process_metrics
processes.each do |process|
process_utilization = process["busy"] / process["concurrency"].to_f * 100.0

unless process_utilization.nan?
process_dimensions = [{name: "Hostname", value: process["hostname"]}]
unless process_utilization.nan?
process_dimensions = [{name: "Hostname", value: process["hostname"]}]

if process["tag"]
process_dimensions << {name: "Tag", value: process["tag"]}
end
if process["tag"]
process_dimensions << {name: "Tag", value: process["tag"]}
end

metrics << {
metric_name: "Utilization",
dimensions: process_dimensions,
timestamp: now,
value: process_utilization,
unit: "Percent",
}
metrics << {
metric_name: "Utilization",
dimensions: process_dimensions,
timestamp: now,
value: process_utilization,
unit: "Percent",
}
end
end
end

Expand Down
23 changes: 23 additions & 0 deletions spec/sidekiq/cloudwatchmetrics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,29 @@
end
end
end

context "when per process metrics are disabled" do
subject(:publisher) { Sidekiq::CloudWatchMetrics::Publisher.new(client: client, process_metrics: false) }

it "only publishes a single Utilization metric" do
Timecop.freeze(now = Time.now) do
publisher.publish

expect(client).to have_received(:put_metric_data) { |metrics|
utilization_data = metrics[:metric_data].select { |data| data[:metric_name] == "Utilization" }

expect(utilization_data).to contain_exactly(
{
metric_name: "Utilization",
timestamp: now,
value: 30.0,
unit: "Percent",
},
)
}
end
end
end
end

describe "#stop" do
Expand Down

0 comments on commit 28dc515

Please sign in to comment.