From 11cd389df93905d63db20cc2cd49f332ddd98798 Mon Sep 17 00:00:00 2001 From: Andrey Date: Tue, 26 Nov 2024 08:44:52 +0100 Subject: [PATCH 1/3] for test_session telemetry metric set auto_injected tag to true when test session is auto instrumented --- lib/datadog/ci/contrib/instrumentation.rb | 6 ++++++ lib/datadog/ci/test_visibility/telemetry.rb | 3 ++- sig/datadog/ci/contrib/instrumentation.rbs | 4 ++++ .../ci/contrib/minitest/instrumentation_spec.rb | 11 ++++++++++- .../minitest_auto_instrument/instrumentation_spec.rb | 8 ++++++++ 5 files changed, 30 insertions(+), 2 deletions(-) diff --git a/lib/datadog/ci/contrib/instrumentation.rb b/lib/datadog/ci/contrib/instrumentation.rb index b921f881..dd50d302 100644 --- a/lib/datadog/ci/contrib/instrumentation.rb +++ b/lib/datadog/ci/contrib/instrumentation.rb @@ -9,6 +9,7 @@ module Instrumentation class InvalidIntegrationError < StandardError; end @registry = {} + @auto_instrumented = false def self.registry @registry @@ -18,6 +19,10 @@ def self.register_integration(integration_class) @registry[integration_name(integration_class)] = integration_class.new end + def self.auto_instrumented? + @auto_instrumented + end + # Auto instrumentation of all integrations. # # Registers a :script_compiled tracepoint to watch for new Ruby files being loaded. @@ -25,6 +30,7 @@ def self.register_integration(integration_class) # Only the integrations that are available in the environment are checked. def self.auto_instrument Datadog.logger.debug("Auto instrumenting all integrations...") + @auto_instrumented = true auto_instrumented_integrations = fetch_auto_instrumented_integrations if auto_instrumented_integrations.empty? diff --git a/lib/datadog/ci/test_visibility/telemetry.rb b/lib/datadog/ci/test_visibility/telemetry.rb index b65637dd..6ba25631 100644 --- a/lib/datadog/ci/test_visibility/telemetry.rb +++ b/lib/datadog/ci/test_visibility/telemetry.rb @@ -1,5 +1,6 @@ # frozen_string_literal: true +require_relative "../contrib/instrumentation" require_relative "../ext/app_types" require_relative "../ext/environment" require_relative "../ext/telemetry" @@ -33,7 +34,7 @@ def self.test_session_started(test_session) Ext::Telemetry::METRIC_TEST_SESSION, 1, { - Ext::Telemetry::TAG_AUTO_INJECTED => "false", # ruby doesn't support auto injection yet + Ext::Telemetry::TAG_AUTO_INJECTED => Contrib::Instrumentation.auto_instrumented?.to_s, Ext::Telemetry::TAG_PROVIDER => test_session.ci_provider || Ext::Telemetry::Provider::UNSUPPORTED } ) diff --git a/sig/datadog/ci/contrib/instrumentation.rbs b/sig/datadog/ci/contrib/instrumentation.rbs index 8d820d3e..16b743b1 100644 --- a/sig/datadog/ci/contrib/instrumentation.rbs +++ b/sig/datadog/ci/contrib/instrumentation.rbs @@ -7,12 +7,16 @@ module Datadog self.@registry: Hash[Symbol, untyped] + self.@auto_instrumented: bool + self.@configure_once: Datadog::Core::Utils::OnlyOnce def self.registry: () -> Hash[Symbol, untyped] def self.auto_instrument: () -> void + def self.auto_instrumented?: () -> bool + def self.instrument: (Symbol integration_name, ?::Hash[untyped, untyped] options) { (?) -> untyped } -> void def self.instrument_on_session_start: () -> void diff --git a/spec/datadog/ci/contrib/minitest/instrumentation_spec.rb b/spec/datadog/ci/contrib/minitest/instrumentation_spec.rb index 89b45d81..bd8d91f9 100644 --- a/spec/datadog/ci/contrib/minitest/instrumentation_spec.rb +++ b/spec/datadog/ci/contrib/minitest/instrumentation_spec.rb @@ -404,6 +404,8 @@ def test_foo end context "run minitest suite" do + include_context "Telemetry spy" + before do Minitest.run([]) end @@ -452,6 +454,13 @@ def test_pass_other expect(test_session_span).to have_test_tag(:code_coverage_lines_pct) expect(test_session_span).to have_pass_status + + # test_session metric has auto_injected false + test_session_started_metric = telemetry_metric(:inc, "test_session") + expect(test_session_started_metric.tags).to eq( + "provider" => "unsupported", + "auto_injected" => "false" + ) end it "creates a test module span" do @@ -486,7 +495,7 @@ def test_pass_other :source_file, "spec/datadog/ci/contrib/minitest/instrumentation_spec.rb" ) - expect(first_test_suite_span).to have_test_tag(:source_start, "417") + expect(first_test_suite_span).to have_test_tag(:source_start, "419") expect(first_test_suite_span).to have_test_tag( :codeowners, "[\"@DataDog/ruby-guild\", \"@DataDog/ci-app-libraries\"]" diff --git a/spec/datadog/ci/contrib/minitest_auto_instrument/instrumentation_spec.rb b/spec/datadog/ci/contrib/minitest_auto_instrument/instrumentation_spec.rb index 2e2cfd8e..73393a52 100644 --- a/spec/datadog/ci/contrib/minitest_auto_instrument/instrumentation_spec.rb +++ b/spec/datadog/ci/contrib/minitest_auto_instrument/instrumentation_spec.rb @@ -2,6 +2,7 @@ include_context "CI mode activated" do let(:integration_name) { :no_instrument } end + include_context "Telemetry spy" before do require_relative "../../../../../lib/datadog/ci/auto_instrument" @@ -34,5 +35,12 @@ def test_pass_other expect(test_spans).to have_unique_tag_values_count(:test_session_id, 1) expect(test_spans).to have_unique_tag_values_count(:test_module_id, 1) expect(test_spans).to have_unique_tag_values_count(:test_suite_id, 1) + + # test_session metric has auto_injected tag + test_session_started_metric = telemetry_metric(:inc, "test_session") + expect(test_session_started_metric.tags).to eq( + "provider" => "unsupported", + "auto_injected" => "true" + ) end end From 4d1619cbba04844e80e385f9d35067d9a7a7eac1 Mon Sep 17 00:00:00 2001 From: Andrey Date: Tue, 26 Nov 2024 10:50:42 +0100 Subject: [PATCH 2/3] if auto instrumentation is used in JRuby, print error and bail out --- lib/datadog/ci/auto_instrument.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/datadog/ci/auto_instrument.rb b/lib/datadog/ci/auto_instrument.rb index 9a241843..b08d0c1d 100644 --- a/lib/datadog/ci/auto_instrument.rb +++ b/lib/datadog/ci/auto_instrument.rb @@ -1,3 +1,8 @@ require "datadog/ci" +if RUBY_ENGINE == "jruby" + Datadog.logger.error("Auto instrumentation is not supported on JRuby. Please use manual instrumentation instead.") + return +end + Datadog::CI::Contrib::Instrumentation.auto_instrument From 20357b82216cc17721109caaff3788c53882c45f Mon Sep 17 00:00:00 2001 From: Andrey Date: Tue, 26 Nov 2024 10:52:42 +0100 Subject: [PATCH 3/3] fix test assertions --- spec/datadog/ci/contrib/minitest/instrumentation_spec.rb | 5 +---- .../contrib/minitest_auto_instrument/instrumentation_spec.rb | 5 +---- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/spec/datadog/ci/contrib/minitest/instrumentation_spec.rb b/spec/datadog/ci/contrib/minitest/instrumentation_spec.rb index bd8d91f9..bf65d01a 100644 --- a/spec/datadog/ci/contrib/minitest/instrumentation_spec.rb +++ b/spec/datadog/ci/contrib/minitest/instrumentation_spec.rb @@ -457,10 +457,7 @@ def test_pass_other # test_session metric has auto_injected false test_session_started_metric = telemetry_metric(:inc, "test_session") - expect(test_session_started_metric.tags).to eq( - "provider" => "unsupported", - "auto_injected" => "false" - ) + expect(test_session_started_metric.tags["auto_injected"]).to eq("false") end it "creates a test module span" do diff --git a/spec/datadog/ci/contrib/minitest_auto_instrument/instrumentation_spec.rb b/spec/datadog/ci/contrib/minitest_auto_instrument/instrumentation_spec.rb index 73393a52..50fe625b 100644 --- a/spec/datadog/ci/contrib/minitest_auto_instrument/instrumentation_spec.rb +++ b/spec/datadog/ci/contrib/minitest_auto_instrument/instrumentation_spec.rb @@ -38,9 +38,6 @@ def test_pass_other # test_session metric has auto_injected tag test_session_started_metric = telemetry_metric(:inc, "test_session") - expect(test_session_started_metric.tags).to eq( - "provider" => "unsupported", - "auto_injected" => "true" - ) + expect(test_session_started_metric.tags["auto_injected"]).to eq("true") end end