Skip to content

Commit

Permalink
Merge pull request #35 from sj26/fix-tag-dimension
Browse files Browse the repository at this point in the history
Fix tag dimension
  • Loading branch information
sj26 authored Mar 6, 2023
2 parents 49704bd + 355ecc2 commit 756e3af
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 23 deletions.
14 changes: 6 additions & 8 deletions lib/sidekiq/cloudwatchmetrics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,17 +159,15 @@ def publish
]

processes.each do |process|
metrics << {
metric_name: "Utilization",
dimensions: [{name: "Hostname", value: process["hostname"]}],
timestamp: now,
value: process["busy"] / process["concurrency"].to_f * 100.0,
unit: "Percent",
}
process_dimensions = [{name: "Hostname", value: process["hostname"]}]

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

metrics << {
metric_name: "Utilization",
dimensions: [{name: "Tag", value: process["tag"]}],
dimensions: process_dimensions,
timestamp: now,
value: process["busy"] / process["concurrency"].to_f * 100.0,
unit: "Percent",
Expand Down
57 changes: 42 additions & 15 deletions spec/sidekiq/cloudwatchmetrics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,6 @@
value: 30,
unit: "Count",
},
{
metric_name: "Utilization",
timestamp: now,
value: 30.0,
unit: "Percent",
},
{
metric_name: "DefaultQueueLatency",
timestamp: now,
Expand All @@ -163,17 +157,9 @@
},
{
metric_name: "Utilization",
dimensions: [{name: "Tag", value: nil}],
timestamp: now,
unit: "Percent",
value: 50.0,
},
{
metric_name: "Utilization",
dimensions: [{name: "Tag", value: nil}],
timestamp: now,
value: 30.0,
unit: "Percent",
value: 10.0,
},
{
metric_name: "Utilization",
Expand Down Expand Up @@ -236,6 +222,47 @@
end
end

context "with process tags" do
let(:processes) do
[
Sidekiq::Process.new("busy" => 5, "concurrency" => 10, "hostname" => "foo", "tag" => "default"),
Sidekiq::Process.new("busy" => 2, "concurrency" => 20, "hostname" => "bar", "tag" => "shard-one"),
]
end

it "publishes metrics including tag as a dimension" do
Timecop.freeze(now = Time.now) do
publisher.publish

expect(client).to have_received(:put_metric_data).with(
namespace: "Sidekiq",
metric_data: include(
{
metric_name: "Utilization",
timestamp: now,
value: 30.0,
unit: "Percent",
},
{
metric_name: "Utilization",
dimensions: [{name: "Hostname", value: "foo"}, {name: "Tag", value: "default"}],
timestamp: now,
unit: "Percent",
value: 50.0,
},
{
metric_name: "Utilization",
dimensions: [{name: "Hostname", value: "bar"}, {name: "Tag", value: "shard-one"}],
timestamp: now,
unit: "Percent",
value: 10.0,
},
),
)
end
end
end

context "with custom dimensions" do
subject(:publisher) { Sidekiq::CloudWatchMetrics::Publisher.new(client: client, additional_dimensions: {appCluster: 1, type: "foo"}) }

Expand Down

0 comments on commit 756e3af

Please sign in to comment.