Class: Datadog::Core::Configuration::Components

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/core/configuration/components.rb

Overview

Global components for the trace library.

Defined Under Namespace

Classes: NullCloudwise

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings) ⇒ Components



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/datadog/core/configuration/components.rb', line 171

def initialize(settings)
  @settings = settings
  @logger = self.class.build_logger(settings)
  @environment_logger_extra = {}
  @datadog_components_initialized = false
  @pending_startup = nil
  StableConfig.log_result(@logger)
  Deprecations.log_deprecations_from_all_sources(@logger)

  # ============================================================
  # STEP 1: Initialize Cloudwise FIRST (Async - Non-blocking)
  # ============================================================
  # Cloudwise starts in background and doesn't block application startup.
  # Host ID generation runs async and retries until success.
  # Datadog components will NOT be initialized until Host ID is ready.
  # Application can still serve requests normally.
  # ============================================================

  @cloudwise = self.class.build_cloudwise(settings, @logger)

  if @cloudwise.enabled?
    # Pass a callback to initialize Datadog components
    # Note: If this is the first Components instance, Datadog components initialize immediately
    # If Cloudwise is already running, this callback executes immediately for this instance
    @cloudwise.initialize_async do
      initialize_datadog_components(settings)
    end
  else
    # If Cloudwise is disabled, initialize Datadog components immediately
    initialize_datadog_components(settings)
  end
end

Instance Attribute Details

#agent_infoObject (readonly)

Returns the value of attribute agent_info.



152
153
154
# File 'lib/datadog/core/configuration/components.rb', line 152

def agent_info
  @agent_info
end

#agent_settingsObject (readonly)

Returns the value of attribute agent_settings.



152
153
154
# File 'lib/datadog/core/configuration/components.rb', line 152

def agent_settings
  @agent_settings
end

#appsecObject (readonly)

Returns the value of attribute appsec.



152
153
154
# File 'lib/datadog/core/configuration/components.rb', line 152

def appsec
  @appsec
end

#cloudwiseObject (readonly)

Returns the value of attribute cloudwise.



152
153
154
# File 'lib/datadog/core/configuration/components.rb', line 152

def cloudwise
  @cloudwise
end

#crashtrackerObject (readonly)

Returns the value of attribute crashtracker.



152
153
154
# File 'lib/datadog/core/configuration/components.rb', line 152

def crashtracker
  @crashtracker
end

#data_streamsObject (readonly)

Returns the value of attribute data_streams.



152
153
154
# File 'lib/datadog/core/configuration/components.rb', line 152

def data_streams
  @data_streams
end

#dynamic_instrumentationObject (readonly)

Returns the value of attribute dynamic_instrumentation.



152
153
154
# File 'lib/datadog/core/configuration/components.rb', line 152

def dynamic_instrumentation
  @dynamic_instrumentation
end

#error_trackingObject (readonly)

Returns the value of attribute error_tracking.



152
153
154
# File 'lib/datadog/core/configuration/components.rb', line 152

def error_tracking
  @error_tracking
end

#health_metricsObject (readonly)

Returns the value of attribute health_metrics.



152
153
154
# File 'lib/datadog/core/configuration/components.rb', line 152

def health_metrics
  @health_metrics
end

#loggerObject (readonly)

Returns the value of attribute logger.



152
153
154
# File 'lib/datadog/core/configuration/components.rb', line 152

def logger
  @logger
end

#open_featureObject (readonly)

Returns the value of attribute open_feature.



152
153
154
# File 'lib/datadog/core/configuration/components.rb', line 152

def open_feature
  @open_feature
end

#profilerObject (readonly)

Returns the value of attribute profiler.



152
153
154
# File 'lib/datadog/core/configuration/components.rb', line 152

def profiler
  @profiler
end

#remoteObject (readonly)

Returns the value of attribute remote.



152
153
154
# File 'lib/datadog/core/configuration/components.rb', line 152

def remote
  @remote
end

#runtime_metricsObject (readonly)

Returns the value of attribute runtime_metrics.



152
153
154
# File 'lib/datadog/core/configuration/components.rb', line 152

def runtime_metrics
  @runtime_metrics
end

#settingsObject (readonly)

Returns the value of attribute settings.



152
153
154
# File 'lib/datadog/core/configuration/components.rb', line 152

def settings
  @settings
end

#telemetryObject (readonly)

Returns the value of attribute telemetry.



152
153
154
# File 'lib/datadog/core/configuration/components.rb', line 152

def telemetry
  @telemetry
end

#tracerObject (readonly)

Returns the value of attribute tracer.



152
153
154
# File 'lib/datadog/core/configuration/components.rb', line 152

def tracer
  @tracer
end

Class Method Details

.build_cloudwise(settings, logger) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/datadog/core/configuration/components.rb', line 95

def build_cloudwise(settings, logger)
  require_relative '../cloudwise/component'

  # Use singleton pattern - all Components instances share the same Cloudwise instance
  # This ensures ProbeState is consistent across all Tracer instances
  @cloudwise_singleton ||= Datadog::Core::Cloudwise::Component.new(
    settings: settings,
    logger: logger
  )
rescue => e
  Datadog.logger.warn("Failed to initialize Cloudwise: #{e.class}: #{e}")
  # Return a disabled component
  NullCloudwise.new
end

.build_crashtracker(settings, agent_settings, logger:) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'lib/datadog/core/configuration/components.rb', line 70

def build_crashtracker(settings, agent_settings, logger:)
  return unless settings.crashtracking.enabled

  if (libdatadog_api_failure = Datadog::Core::LIBDATADOG_API_FAILURE)
    logger.debug("Cannot enable crashtracking: #{libdatadog_api_failure}")
    return
  end

  Datadog::Core::Crashtracking::Component.build(settings, agent_settings, logger: logger)
end

.build_data_streams(settings, agent_settings, logger) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/datadog/core/configuration/components.rb', line 81

def build_data_streams(settings, agent_settings, logger)
  return unless settings.data_streams.enabled

  Datadog::DataStreams::Processor.new(
    interval: settings.data_streams.interval,
    logger: logger,
    settings: settings,
    agent_settings: agent_settings
  )
rescue => e
  logger.warn("Failed to initialize Data Streams Monitoring: #{e.class}: #{e}")
  nil
end

.build_health_metrics(settings, logger, telemetry) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/datadog/core/configuration/components.rb', line 31

def build_health_metrics(settings, logger, telemetry)
  settings = settings.health_metrics
  options = {enabled: settings.enabled}
  options[:statsd] = settings.statsd unless settings.statsd.nil?

  Core::Diagnostics::Health::Metrics.new(telemetry: telemetry, logger: logger, **options)
end

.build_logger(settings) ⇒ Object



39
40
41
42
43
44
# File 'lib/datadog/core/configuration/components.rb', line 39

def build_logger(settings)
  logger = settings.logger.instance || Core::Logger.new($stdout)
  logger.level = settings.diagnostics.debug ? ::Logger::DEBUG : settings.logger.level

  logger
end

.build_runtime_metrics(settings, logger, telemetry) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/datadog/core/configuration/components.rb', line 46

def build_runtime_metrics(settings, logger, telemetry)
  options = {enabled: settings.runtime_metrics.enabled}
  options[:statsd] = settings.runtime_metrics.statsd unless settings.runtime_metrics.statsd.nil?
  options[:services] = [settings.service] unless settings.service.nil?
  options[:experimental_runtime_id_enabled] = settings.runtime_metrics.experimental_runtime_id_enabled

  Core::Runtime::Metrics.new(logger: logger, telemetry: telemetry, **options)
end

.build_runtime_metrics_worker(settings, logger, telemetry) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/datadog/core/configuration/components.rb', line 55

def build_runtime_metrics_worker(settings, logger, telemetry)
  # NOTE: Should we just ignore building the worker if its not enabled?
  options = settings.runtime_metrics.opts.merge(
    enabled: settings.runtime_metrics.enabled,
    metrics: build_runtime_metrics(settings, logger, telemetry),
    logger: logger,
  )

  Core::Workers::RuntimeMetrics.new(telemetry: telemetry, **options)
end

.build_telemetry(settings, agent_settings, logger) ⇒ Object



66
67
68
# File 'lib/datadog/core/configuration/components.rb', line 66

def build_telemetry(settings, agent_settings, logger)
  Telemetry::Component.build(settings, agent_settings, logger)
end

Instance Method Details

#datadog_components_initialized?Boolean

Check if Datadog components have been initialized



260
261
262
# File 'lib/datadog/core/configuration/components.rb', line 260

def datadog_components_initialized?
  @datadog_components_initialized == true
end

#initialize_datadog_components(settings) ⇒ Object

Initialize Datadog components This is called either:

  1. Immediately if Cloudwise is disabled
  2. After Host ID is ready if Cloudwise is enabled


208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/datadog/core/configuration/components.rb', line 208

def initialize_datadog_components(settings)
  # This agent_settings is intended for use within Core. If you require
  # agent_settings within a product outside of core you should extend
  # the Core resolver from within your product/component's namespace.
  @agent_settings = AgentSettingsResolver.call(settings, logger: @logger)

  # Exposes agent capability information for detection by any components
  @agent_info = Core::Environment::AgentInfo.new(agent_settings, logger: @logger)

  @telemetry = self.class.build_telemetry(settings, agent_settings, @logger)

  @remote = Remote::Component.build(settings, agent_settings, logger: @logger, telemetry: telemetry)
  @tracer = Datadog::Tracing::Component.build_tracer(settings, agent_settings, logger: @logger)

  # Set the components reference on the tracer so it can check Cloudwise state
  @tracer&.instance_variable_set(:@components, self)

  @crashtracker = self.class.build_crashtracker(settings, agent_settings, logger: @logger)

  @profiler, profiler_logger_extra = Datadog::Profiling::Component.build_profiler_component(
    settings: settings,
    agent_settings: agent_settings,
    optional_tracer: @tracer,
    logger: @logger,
  )
  @environment_logger_extra.merge!(profiler_logger_extra) if profiler_logger_extra

  @runtime_metrics = self.class.build_runtime_metrics_worker(settings, @logger, telemetry)
  @health_metrics = self.class.build_health_metrics(settings, @logger, telemetry)
  @appsec = Datadog::AppSec::Component.build_appsec_component(settings, telemetry: telemetry)
  @open_feature = OpenFeature::Component.build(settings, agent_settings, logger: @logger, telemetry: telemetry)
  @dynamic_instrumentation = Datadog::DI::Component.build(settings, agent_settings, @logger, telemetry: telemetry)
  @error_tracking = Datadog::ErrorTracking::Component.build(settings, @tracer, @logger)
  @data_streams = self.class.build_data_streams(settings, agent_settings, @logger)
  @environment_logger_extra[:dynamic_instrumentation_enabled] = !!@dynamic_instrumentation

  # Configure non-privileged components.
  Datadog::Tracing::Contrib::Component.configure(settings)

  # Mark that Datadog components are ready
  @datadog_components_initialized = true

  # If startup was deferred, call it now
  if @pending_startup
    startup!(@pending_startup[:settings], old_state: @pending_startup[:old_state])
    @pending_startup = nil
  end
rescue => e
  Datadog.logger.error { "Failed to initialize Datadog components: #{e.class.name} #{e.message}" }
end

#reconfigure_sampler(settings = Datadog.configuration) ⇒ Object

Hot-swaps with a new sampler. This operation acquires the Components lock to ensure there is no concurrent modification of the sampler.



267
268
269
270
# File 'lib/datadog/core/configuration/components.rb', line 267

def reconfigure_sampler(settings = Datadog.configuration)
  sampler = Datadog::Tracing::Component.build_sampler(settings)
  Datadog.send(:safely_synchronize) { tracer.sampler.sampler = sampler }
end

#shutdown!(replacement = nil) ⇒ Object

Shuts down all the components in use. If it has another instance to compare to, it will compare and avoid tearing down parts still in use.



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/datadog/core/configuration/components.rb', line 307

def shutdown!(replacement = nil)
  # Don't shutdown Cloudwise if it's a singleton shared across all Components
  # Only shutdown if there's no replacement (final shutdown) or replacement uses different Cloudwise
  if !replacement || (cloudwise && !cloudwise.equal?(replacement.cloudwise))
    cloudwise&.stop
  end

  # Shutdown remote configuration
  remote&.shutdown!

  # Shutdown DI after remote, since remote config triggers DI operations.
  dynamic_instrumentation&.shutdown!

  # Shutdown OpenFeature component
  open_feature&.shutdown!

  # Decommission AppSec
  appsec&.shutdown!

  # Shutdown the old tracer, unless it's still being used.
  # (e.g. a custom tracer instance passed in.)
  tracer&.shutdown! unless replacement && tracer&.equal?(replacement.tracer)

  # Shutdown old profiler
  profiler&.shutdown!

  # Shutdown workers
  runtime_metrics&.stop(true, close_metrics: false)

  # Shutdown Data Streams Monitoring processor
  data_streams&.stop(true)

  # Shutdown the old metrics, unless they are still being used.
  # (e.g. custom Statsd instances.)
  #
  # TODO: This violates the encapsulation created by Runtime::Metrics and
  # Health::Metrics, by directly manipulating `statsd` and changing
  # it's lifecycle management.
  # If we need to directly have ownership of `statsd` lifecycle, we should
  # have direct ownership of it.

  # Only process statsd if components are initialized
  if datadog_components_initialized?
    old_statsd = [
      runtime_metrics&.metrics&.statsd,
      health_metrics&.statsd
    ].compact.uniq

    new_statsd = if replacement
      [
        replacement.runtime_metrics&.metrics&.statsd,
        replacement.health_metrics&.statsd
      ].compact.uniq
    else
      []
    end

    unused_statsd = (old_statsd - (old_statsd & new_statsd))
    unused_statsd.each(&:close)

    # enqueue closing event before stopping telemetry so it will be sent out on shutdown
    telemetry&.emit_closing! unless replacement&.telemetry&.enabled
    telemetry&.shutdown!
  end

  Core::ProcessDiscovery.shutdown!
end

#startup!(settings, old_state: nil) ⇒ Object

Starts up components



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/datadog/core/configuration/components.rb', line 273

def startup!(settings, old_state: nil)
  telemetry.start(old_state&.telemetry_enabled?, components: self)

  if settings.profiling.enabled
    if profiler
      profiler.start
    else
      # Display a warning for users who expected profiling to be enabled
      unsupported_reason = Profiling.unsupported_reason
      logger.warn("Profiling was requested but is not supported, profiling disabled: #{unsupported_reason}")
    end
  end

  if settings.remote.enabled && old_state&.remote_started?
    # The library was reconfigured and previously it already started
    # the remote component (i.e., it received at least one request
    # through the installed Rack middleware which started the remote).
    # If the new configuration also has remote enabled, start the
    # new remote right away.
    # remote should always be not nil here but steep doesn't know this.
    remote&.start
  end

  # This should stay here, not in initialize. During reconfiguration, the order of the calls is:
  # initialize new components, shutdown old components, startup new components.
  # Because this is a singleton, if we call it in initialize, it will be shutdown right away.
  Core::ProcessDiscovery.publish(settings)

  Core::Diagnostics::EnvironmentLogger.collect_and_log!(@environment_logger_extra)
end

#stateObject

Returns the current state of various components.



376
377
378
379
380
381
# File 'lib/datadog/core/configuration/components.rb', line 376

def state
  ComponentsState.new(
    telemetry_enabled: telemetry&.enabled || false,
    remote_started: remote&.started?,
  )
end