Skip to content

Commit

Permalink
Merge pull request #260 from DataDog/anmarchenko/telemetry_auto_injec…
Browse files Browse the repository at this point in the history
…ted_tag

[SDTEST-228] Send auto_injected: true to internal telemetry when auto instrumentation was used
  • Loading branch information
anmarchenko authored Nov 26, 2024
2 parents 7e3bb16 + 20357b8 commit a2377e4
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 2 deletions.
5 changes: 5 additions & 0 deletions lib/datadog/ci/auto_instrument.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions lib/datadog/ci/contrib/instrumentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module Instrumentation
class InvalidIntegrationError < StandardError; end

@registry = {}
@auto_instrumented = false

def self.registry
@registry
Expand All @@ -18,13 +19,18 @@ 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.
# On every file load it checks if any of the integrations are patchable now.
# 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?
Expand Down
3 changes: 2 additions & 1 deletion lib/datadog/ci/test_visibility/telemetry.rb
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
}
)
Expand Down
4 changes: 4 additions & 0 deletions sig/datadog/ci/contrib/instrumentation.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion spec/datadog/ci/contrib/minitest/instrumentation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,8 @@ def test_foo
end

context "run minitest suite" do
include_context "Telemetry spy"

before do
Minitest.run([])
end
Expand Down Expand Up @@ -452,6 +454,10 @@ 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["auto_injected"]).to eq("false")
end

it "creates a test module span" do
Expand Down Expand Up @@ -486,7 +492,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\"]"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -34,5 +35,9 @@ 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["auto_injected"]).to eq("true")
end
end

0 comments on commit a2377e4

Please sign in to comment.