22
33require_relative 'emitter'
44require_relative 'event'
5- require_relative 'http/transport'
65require_relative 'metrics_manager'
76require_relative 'worker'
87require_relative 'logging'
8+ require_relative 'transport/http'
99
1010require_relative '../configuration/ext'
11+ require_relative '../configuration/agentless_settings_resolver'
1112require_relative '../utils/forking'
1213
1314module 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
0 commit comments