Skip to content

Commit

Permalink
Merge pull request #236 from DataDog/anmarchenko/cpu_count_metric
Browse files Browse the repository at this point in the history
[SDTEST-865] Send internal vCPU count metric
  • Loading branch information
anmarchenko authored Sep 20, 2024
2 parents b8f49d9 + 576529e commit 3db15fe
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lib/datadog/ci/ext/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ module Test
# common tags that are serialized directly in msgpack header in metadata field
METADATA_TAG_TEST_SESSION_NAME = "test_session.name"

# internal metric with the number of virtual CPUs
METRIC_CPU_COUNT = "_dd.host.vcpu_count"

# tags that are common for the whole session and can be inherited from the test session
INHERITABLE_TAGS = [TAG_FRAMEWORK, TAG_FRAMEWORK_VERSION].freeze

Expand Down
7 changes: 7 additions & 0 deletions lib/datadog/ci/span.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ def clear_tag(key)
tracer_span.clear_tag(key)
end

# Gets metric value by key.
# @param [String] key the key of the metric.
# @return [Numeric] value the value of the metric.
def get_metric(key)
tracer_span.get_metric(key)
end

# Sets metric value by key.
# @param [String] key the key of the metric.
# @param [Numeric] value the value of the metric.
Expand Down
4 changes: 4 additions & 0 deletions lib/datadog/ci/test_visibility/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
require_relative "../ext/environment"
require_relative "../ext/test"

require_relative "../utils/test_run"

require_relative "../span"
require_relative "../test"
require_relative "../test_session"
Expand Down Expand Up @@ -203,6 +205,8 @@ def set_initial_tags(ci_span, tags)

ci_span.set_tags(tags)
ci_span.set_tags(@environment_tags)

ci_span.set_metric(Ext::Test::METRIC_CPU_COUNT, Utils::TestRun.virtual_cpu_count)
end

# PROPAGATING CONTEXT FROM TOP-LEVEL TO THE LOWER LEVELS
Expand Down
8 changes: 8 additions & 0 deletions lib/datadog/ci/utils/test_run.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require "etc"

module Datadog
module CI
module Utils
Expand Down Expand Up @@ -34,6 +36,12 @@ def self.custom_configuration(env_tags)
end
res
end

def self.virtual_cpu_count
return @virtual_cpu_count if defined?(@virtual_cpu_count)

@virtual_cpu_count = ::Etc.nprocessors
end
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions sig/datadog/ci/ext/test.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ module Datadog

METADATA_TAG_TEST_SESSION_NAME: "test_session.name"

METRIC_CPU_COUNT: "_dd.host.vcpu_count"

module Status
PASS: "pass"

Expand Down
2 changes: 2 additions & 0 deletions sig/datadog/ci/span.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ module Datadog

def clear_tag: (String key) -> void

def get_metric: (String key) -> Numeric?

def set_metric: (String key, untyped value) -> void

def set_tags: (Hash[untyped, untyped] tags) -> void
Expand Down
7 changes: 7 additions & 0 deletions sig/datadog/ci/utils/test_run.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Datadog
module Utils
module TestRun
self.@command: String
self.@virtual_cpu_count: Integer

def self.command: () -> String

Expand All @@ -11,7 +12,13 @@ module Datadog
def self.test_parameters: (?arguments: Hash[untyped, untyped], ?metadata: Hash[untyped, untyped]) -> String

def self.custom_configuration: (Hash[String, String]? env_tags) -> Hash[String, String]

def self.virtual_cpu_count: () -> Integer
end
end
end
end

module Etc
def self.nprocessors: () -> Integer
end
1 change: 1 addition & 0 deletions spec/datadog/ci/test_visibility/component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
expect(span_under_test).to have_test_tag(tag)
end
expect(span_under_test).to have_test_tag(:command, test_command)
expect(span_under_test.get_metric(Datadog::CI::Ext::Test::METRIC_CPU_COUNT)).to eq(Etc.nprocessors)
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
}
)
expect(metrics).to eq(
{"_dd.top_level" => 1.0, "memory_allocations" => 16}
{"_dd.top_level" => 1.0, "memory_allocations" => 16, "_dd.host.vcpu_count" => Etc.nprocessors}
)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
expect(meta["_test.session_id"]).to be_nil
expect(meta["_test.module_id"]).to be_nil

expect(metrics).to eq({"_dd.top_level" => 1, "memory_allocations" => 16})
expect(metrics).to eq({"_dd.top_level" => 1, "memory_allocations" => 16, "_dd.host.vcpu_count" => Etc.nprocessors})
end
end

Expand Down
6 changes: 6 additions & 0 deletions spec/datadog/ci/utils/test_run_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,10 @@
it { is_expected.to eq({"tag1" => "value1", "tag2" => "value2"}) }
end
end

describe ".virtual_cpu_count" do
subject { described_class.virtual_cpu_count }

it { is_expected.to eq(::Etc.nprocessors) }
end
end

0 comments on commit 3db15fe

Please sign in to comment.