Skip to content

Commit 03eefec

Browse files
authored
DEBUG-3700 Replace telemetry transport with a core-based transport that supports UDS (#4630)
1 parent f85f2c0 commit 03eefec

File tree

27 files changed

+627
-551
lines changed

27 files changed

+627
-551
lines changed

.rubocop.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ AllCops:
2424
- 'spec/**/**/interesting_backtrace_helper.rb' # This file needs quite a few bizarre code patterns by design
2525
- 'vendor/bundle/**/*'
2626
- 'spec/datadog/tracing/contrib/grpc/support/gen/**/*.rb' # Skip protoc autogenerated code
27+
# Telemetry transport code is copied from DI and is formatted to
28+
# standard rules. We are migrating to standard therefore avoid
29+
# reformatting all of this code to rubocop's demands and then have it
30+
# be different from telemetry.
31+
- lib/datadog/core/telemetry/transport/**/*
2732
- lib/datadog/di/**/*
2833
- lib/datadog/di.rb
2934
- spec/datadog/di/**/*

lib/datadog/core/telemetry/component.rb

Lines changed: 47 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
require_relative 'emitter'
44
require_relative 'event'
5-
require_relative 'http/transport'
65
require_relative 'metrics_manager'
76
require_relative 'worker'
87
require_relative 'logging'
8+
require_relative 'transport/http'
99

1010
require_relative '../configuration/ext'
11+
require_relative '../configuration/agentless_settings_resolver'
1112
require_relative '../utils/forking'
1213

1314
module Datadog
@@ -16,7 +17,7 @@ module Telemetry
1617
# Telemetry entrypoint, coordinates sending telemetry events at various points in app lifecycle.
1718
# Note: Telemetry does not spawn its worker thread in fork processes, thus no telemetry is sent in forked processes.
1819
class Component
19-
attr_reader :enabled, :logger
20+
attr_reader :enabled, :logger, :transport, :worker
2021

2122
include Core::Utils::Forking
2223
include Telemetry::Logging
@@ -25,89 +26,83 @@ def self.build(settings, agent_settings, logger)
2526
enabled = settings.telemetry.enabled
2627
agentless_enabled = settings.telemetry.agentless_enabled
2728

28-
if !agentless_enabled && agent_settings.adapter != Datadog::Core::Configuration::Ext::Agent::HTTP::ADAPTER
29-
enabled = false
30-
logger.debug { "Telemetry disabled. Agent network adapter not supported: #{agent_settings.adapter}" }
31-
end
32-
3329
if agentless_enabled && settings.api_key.nil?
3430
enabled = false
35-
logger.debug { 'Telemetry disabled. Agentless telemetry requires an DD_API_KEY variable to be set.' }
31+
logger.debug { 'Telemetry disabled. Agentless telemetry requires a DD_API_KEY variable to be set.' }
3632
end
3733

38-
transport = if agentless_enabled
39-
Datadog::Core::Telemetry::Http::Transport.build_agentless_transport(
40-
api_key: settings.api_key,
41-
dd_site: settings.site,
42-
url_override: settings.telemetry.agentless_url_override
43-
)
44-
else
45-
Datadog::Core::Telemetry::Http::Transport.build_agent_transport(agent_settings)
46-
end
47-
4834
Telemetry::Component.new(
49-
http_transport: transport,
35+
settings: settings,
36+
agent_settings: agent_settings,
5037
enabled: enabled,
51-
metrics_enabled: enabled && settings.telemetry.metrics_enabled,
52-
heartbeat_interval_seconds: settings.telemetry.heartbeat_interval_seconds,
53-
metrics_aggregation_interval_seconds: settings.telemetry.metrics_aggregation_interval_seconds,
54-
dependency_collection: settings.telemetry.dependency_collection,
5538
logger: logger,
56-
shutdown_timeout_seconds: settings.telemetry.shutdown_timeout_seconds,
57-
log_collection_enabled: settings.telemetry.log_collection_enabled
5839
)
5940
end
6041

6142
# @param enabled [Boolean] Determines whether telemetry events should be sent to the API
62-
# @param metrics_enabled [Boolean] Determines whether telemetry metrics should be sent to the API
63-
# @param heartbeat_interval_seconds [Float] How frequently heartbeats will be reported, in seconds.
64-
# @param metrics_aggregation_interval_seconds [Float] How frequently metrics will be aggregated, in seconds.
65-
# @param [Boolean] dependency_collection Whether to send the `app-dependencies-loaded` event
66-
def initialize(
67-
heartbeat_interval_seconds:,
68-
metrics_aggregation_interval_seconds:,
69-
dependency_collection:,
43+
def initialize( # rubocop: disable Metrics/MethodLength
44+
settings:,
45+
agent_settings:,
7046
logger:,
71-
http_transport:,
72-
shutdown_timeout_seconds:,
73-
enabled: true,
74-
metrics_enabled: true,
75-
log_collection_enabled: true
47+
enabled:
7648
)
7749
@enabled = enabled
78-
@log_collection_enabled = log_collection_enabled
50+
@log_collection_enabled = settings.telemetry.log_collection_enabled
7951
@logger = logger
8052

8153
@metrics_manager = MetricsManager.new(
82-
enabled: enabled && metrics_enabled,
83-
aggregation_interval: metrics_aggregation_interval_seconds
54+
enabled: @enabled && settings.telemetry.metrics_enabled,
55+
aggregation_interval: settings.telemetry.metrics_aggregation_interval_seconds,
8456
)
8557

58+
@stopped = false
59+
60+
return unless @enabled
61+
62+
@transport = if settings.telemetry.agentless_enabled
63+
agent_settings = Core::Configuration::AgentlessSettingsResolver.call(
64+
settings,
65+
host_prefix: 'instrumentation-telemetry-intake',
66+
url_override: settings.telemetry.agentless_url_override,
67+
url_override_source: 'c.telemetry.agentless_url_override',
68+
logger: logger,
69+
)
70+
Telemetry::Transport::HTTP.agentless_telemetry(
71+
agent_settings: agent_settings,
72+
logger: logger,
73+
# api_key should have already validated to be
74+
# not nil by +build+ method above.
75+
api_key: settings.api_key,
76+
)
77+
else
78+
Telemetry::Transport::HTTP.agent_telemetry(
79+
agent_settings: agent_settings, logger: logger,
80+
)
81+
end
82+
8683
@worker = Telemetry::Worker.new(
8784
enabled: @enabled,
88-
heartbeat_interval_seconds: heartbeat_interval_seconds,
89-
metrics_aggregation_interval_seconds: metrics_aggregation_interval_seconds,
90-
emitter: Emitter.new(http_transport: http_transport),
85+
heartbeat_interval_seconds: settings.telemetry.heartbeat_interval_seconds,
86+
metrics_aggregation_interval_seconds: settings.telemetry.metrics_aggregation_interval_seconds,
87+
emitter: Emitter.new(@transport, logger: @logger),
9188
metrics_manager: @metrics_manager,
92-
dependency_collection: dependency_collection,
89+
dependency_collection: settings.telemetry.dependency_collection,
9390
logger: logger,
94-
shutdown_timeout: shutdown_timeout_seconds
91+
shutdown_timeout: settings.telemetry.shutdown_timeout_seconds,
9592
)
9693

97-
@stopped = false
98-
9994
@worker.start
10095
end
10196

10297
def disable!
10398
@enabled = false
104-
@worker.enabled = false
99+
@worker&.enabled = false
105100
end
106101

107102
def stop!
108103
return if @stopped
109104

110-
@worker.stop(true)
105+
@worker&.stop(true)
111106
@stopped = true
112107
end
113108

@@ -135,6 +130,8 @@ def log!(event)
135130
#
136131
# @api private
137132
def flush
133+
return if !@enabled || forked?
134+
138135
@worker.flush
139136
end
140137

lib/datadog/core/telemetry/emitter.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# frozen_string_literal: true
22

33
require_relative 'request'
4-
require_relative 'http/transport'
54
require_relative '../transport/response'
65
require_relative '../utils/sequence'
76
require_relative '../utils/forking'
@@ -11,25 +10,26 @@ module Core
1110
module Telemetry
1211
# Class that emits telemetry events
1312
class Emitter
14-
attr_reader :http_transport
13+
attr_reader :transport, :logger
1514

1615
extend Core::Utils::Forking
1716

18-
# @param http_transport [Datadog::Core::Telemetry::Http::Transport] Transport object that can be used to send
19-
# telemetry requests via the agent
20-
def initialize(http_transport:)
21-
@http_transport = http_transport
17+
# @param transport [Datadog::Core::Telemetry::Transport::Telemetry::Transport]
18+
# Transport object that can be used to send telemetry requests
19+
def initialize(transport, logger: Datadog.logger)
20+
@transport = transport
21+
@logger = logger
2222
end
2323

2424
# Retrieves and emits a TelemetryRequest object based on the request type specified
2525
def request(event)
2626
seq_id = self.class.sequence.next
2727
payload = Request.build_payload(event, seq_id)
28-
res = @http_transport.request(request_type: event.type, payload: payload.to_json)
29-
Datadog.logger.debug { "Telemetry sent for event `#{event.type}` (response: #{res})" }
28+
res = @transport.send_telemetry(request_type: event.type, payload: payload)
29+
logger.debug { "Telemetry sent for event `#{event.type}` (response code: #{res.code})" }
3030
res
3131
rescue => e
32-
Datadog.logger.debug("Unable to send telemetry request for event `#{event.type rescue 'unknown'}`: #{e}")
32+
logger.debug { "Unable to send telemetry request for event `#{event.type rescue 'unknown'}`: #{e}" }
3333
Core::Transport::InternalErrorResponse.new(e)
3434
end
3535

lib/datadog/core/telemetry/http/ext.rb

Lines changed: 0 additions & 28 deletions
This file was deleted.

lib/datadog/core/telemetry/http/transport.rb

Lines changed: 0 additions & 98 deletions
This file was deleted.

lib/datadog/core/telemetry/request.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ module Request
1111
class << self
1212
using Core::Utils::Hash::Refinement
1313

14-
def build_payload(event, seq_id)
14+
def build_payload(event, seq_id, api_version: 'v2')
1515
hash = {
16-
api_version: Http::Ext::API_VERSION,
16+
api_version: api_version,
1717
application: application,
1818
debug: false,
1919
host: host,

0 commit comments

Comments
 (0)