Skip to content

Commit

Permalink
Merge pull request #280 from DataDog/anmarchenko/add_telemetry_logs
Browse files Browse the repository at this point in the history
[SDTEST-741] Add telemetry logs
  • Loading branch information
anmarchenko authored Jan 20, 2025
2 parents 153414f + 364b716 commit f7e0830
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 10 deletions.
4 changes: 4 additions & 0 deletions lib/datadog/ci/ext/environment.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require "datadog/core/telemetry/logging"

require_relative "git"
require_relative "environment/extractor"

Expand Down Expand Up @@ -73,6 +75,7 @@ def validate_repository_url(repo_url)
return if !repo_url.nil? && !repo_url.empty?

Datadog.logger.error("DD_GIT_REPOSITORY_URL is not set or empty; no repo URL was automatically extracted")
Core::Telemetry::Logger.error("DD_GIT_REPOSITORY_URL is not set or empty; no repo URL was automatically extracted")
end

def validate_git_sha(git_sha)
Expand All @@ -89,6 +92,7 @@ def validate_git_sha(git_sha)
end

Datadog.logger.error(message)
Core::Telemetry::Logger.error(message)
end
end
end
Expand Down
3 changes: 3 additions & 0 deletions lib/datadog/ci/ext/environment/providers/github_actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require "json"

require "datadog/core/telemetry/logging"
require "datadog/core/utils/url"

require_relative "base"
Expand Down Expand Up @@ -96,6 +97,8 @@ def additional_tags
result
rescue => e
Datadog.logger.error("Failed to extract additional tags from GitHub Actions: #{e}")
Core::Telemetry::Logger.report(e, description: "Failed to extract additional tags from GitHub Actions")

{}
end

Expand Down
2 changes: 2 additions & 0 deletions lib/datadog/ci/test_optimisation/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require "pp"

require "datadog/core/telemetry/logging"
require "datadog/core/utils/forking"

require_relative "../ext/test"
Expand Down Expand Up @@ -224,6 +225,7 @@ def load_datadog_cov!
Datadog.logger.debug("Loaded Datadog code coverage collector, using coverage mode: #{code_coverage_mode}")
rescue LoadError => e
Datadog.logger.error("Failed to load coverage collector: #{e}. Code coverage will not be collected.")
Core::Telemetry::Logger.report(e, description: "Failed to load coverage collector")

@code_coverage_enabled = false
end
Expand Down
15 changes: 14 additions & 1 deletion lib/datadog/ci/test_visibility/transport.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

require "datadog/core/environment/identity"
require "datadog/core/telemetry/logging"
require "datadog/core/utils/only_once"

require_relative "serializers/factories/test_level"
require_relative "../ext/app_types"
Expand All @@ -15,6 +17,10 @@ module TestVisibility
class Transport < Datadog::CI::Transport::EventPlatformTransport
attr_reader :serializers_factory, :dd_env

def self.log_once
@log_once ||= Datadog::Core::Utils::OnlyOnce.new
end

def initialize(
api:,
dd_env:,
Expand Down Expand Up @@ -64,8 +70,15 @@ def encode_span(trace, span)

encoded
else
Datadog.logger.warn("Invalid event skipped: #{serializer} Errors: #{serializer.validation_errors}")
message = "Invalid event skipped: #{serializer} Errors: #{serializer.validation_errors}"
Datadog.logger.warn(message)
CI::Transport::Telemetry.endpoint_payload_dropped(1, endpoint: telemetry_endpoint_tag)

# log invalid message once as error to internal telemetry
self.class.log_once.run do
Core::Telemetry::Logger.error(message)
end

nil
end
end
Expand Down
3 changes: 3 additions & 0 deletions sig/datadog/ci/test_visibility/transport.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ module Datadog
attr_reader serializers_factory: singleton(Datadog::CI::TestVisibility::Serializers::Factories::TestLevel) | singleton(Datadog::CI::TestVisibility::Serializers::Factories::TestSuiteLevel)
attr_reader dd_env: String?

self.@log_once: Datadog::Core::Utils::OnlyOnce
def self.log_once: () -> Datadog::Core::Utils::OnlyOnce

@dd_env: String?
@serializers_factory: singleton(Datadog::CI::TestVisibility::Serializers::Factories::TestLevel) | singleton(Datadog::CI::TestVisibility::Serializers::Factories::TestSuiteLevel)

Expand Down
33 changes: 24 additions & 9 deletions spec/datadog/ci/test_visibility/transport_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,13 @@
end

context "when some spans are broken" do
let(:expected_events_count) { 3 }
let(:http_span) { spans.find { |span| span.type == "http" } }
let(:expected_events_count) { 2 }
let(:http_spans) { spans.filter { |span| span.type == "http" } }

before do
http_span.start_time = Time.at(0)
http_spans.each do |http_span|
http_span.start_time = Time.at(0)
end
end

it "filters out invalid events" do
Expand All @@ -187,20 +189,25 @@

events = payload["events"]
expect(events.count).to eq(expected_events_count)

span_events = events.filter { |e| e["type"] == "span" }
expect(span_events.count).to eq(1)
end
end

it "logs warning that events were filtered out" do
subject

expect(Datadog.logger).to have_received(:warn).with(/must be greater than or equal to 946684800000000000/)
expect(Datadog.logger).to have_received(:warn).with(/must be greater than or equal to 946684800000000000/).twice
end

it "sends telemetry log once" do
Datadog::CI::TestVisibility::Transport.log_once.send(:reset_ran_once_state_for_tests)

expect(Datadog::Core::Telemetry::Logger).to receive(:error).once

subject
end

it_behaves_like "emits telemetry metric", :inc, "events_enqueued_for_serialization", 3
it_behaves_like "emits telemetry metric", :distribution, "endpoint_payload.events_count", 3
it_behaves_like "emits telemetry metric", :inc, "events_enqueued_for_serialization", 2
it_behaves_like "emits telemetry metric", :distribution, "endpoint_payload.events_count", 2
it_behaves_like "emits telemetry metric", :distribution, "endpoint_payload.events_serialization_ms"
it_behaves_like "emits telemetry metric", :inc, "endpoint_payload.dropped"
it_behaves_like "emits telemetry metric", :inc, "endpoint_payload.requests"
Expand Down Expand Up @@ -257,6 +264,14 @@
expect(api).not_to have_received(:citestcycle_request)
end

it "sends telemetry log once" do
Datadog::CI::TestVisibility::Transport.log_once.send(:reset_ran_once_state_for_tests)

expect(Datadog::Core::Telemetry::Logger).to receive(:error).once

subject
end

it_behaves_like "emits telemetry metric", :inc, "endpoint_payload.dropped"
end
end
Expand Down

0 comments on commit f7e0830

Please sign in to comment.