Class: Datadog::DI::Component Private

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/di/component.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Component for dynamic instrumentation.

Only one instance of the Component should ever be active; if configuration is changed, the old distance should be shut down prior to the new instance being created.

The Component instance stores all state related to DI, for example which probes have been retrieved via remote config, intalled tracepoints and so on. Component will clean up all resources and installed tracepoints upon shutdown.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings, agent_settings, logger, code_tracker: nil, telemetry: nil) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initializes the DI component.

Parameters:



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/datadog/di/component.rb', line 87

def initialize(settings, agent_settings, logger, code_tracker: nil, telemetry: nil)
  @settings = settings
  @agent_settings = agent_settings
  logger = DI::Logger.new(settings, logger)
  @logger = logger
  @telemetry = telemetry
  @guardrails_telemetry = GuardrailsTelemetry.new(settings: settings, logger: logger, telemetry: telemetry)
  @code_tracker = code_tracker
  @redactor = Redactor.new(settings)
  @serializer = Serializer.new(settings, redactor, logger: logger,
    guardrails_telemetry: guardrails_telemetry, telemetry: telemetry)
  @capture_expression_evaluator = CaptureExpressionEvaluator.new(
    settings: settings, serializer: serializer, logger: logger,
    guardrails_telemetry: guardrails_telemetry, telemetry: telemetry,
  )
  @correlation_sampler = CorrelationSampler.new
  @instrumenter = Instrumenter.new(settings, serializer, logger, code_tracker: code_tracker,
    correlation_sampler: correlation_sampler, telemetry: telemetry,
    guardrails_telemetry: guardrails_telemetry,
    capture_expression_evaluator: capture_expression_evaluator)
  @probe_repository = ProbeRepository.new
  @probe_notification_builder = ProbeNotificationBuilder.new(settings, serializer, logger,
    capture_expression_evaluator: capture_expression_evaluator, guardrails_telemetry: guardrails_telemetry,
    telemetry: telemetry)
  @probe_notifier_worker = ProbeNotifierWorker.new(
    settings, logger,
    agent_settings: agent_settings,
    probe_repository: probe_repository,
    probe_notification_builder: probe_notification_builder,
    telemetry: telemetry,
    guardrails_telemetry: guardrails_telemetry,
  )
  @probe_manager = ProbeManager.new(
    settings, instrumenter, probe_notification_builder, probe_notifier_worker, logger, probe_repository,
    telemetry: telemetry,
  )
  # @started transitions are serialized by @lifecycle_mutex so that
  # concurrent RC callbacks (which run on the remote-config thread)
  # cannot race a foreground start! with a background stop!.
  @lifecycle_mutex = Mutex.new
  @started = false
  @rc_enablement_signal_paths = []
end

Instance Attribute Details

#agent_settings ⇒ Object (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



132
133
134
# File 'lib/datadog/di/component.rb', line 132

def agent_settings
  @agent_settings
end

#capture_expression_evaluator ⇒ CaptureExpressionEvaluator (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The capture expression evaluator.



140
141
142
# File 'lib/datadog/di/component.rb', line 140

def capture_expression_evaluator
  @capture_expression_evaluator
end

#code_tracker ⇒ Object (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



141
142
143
# File 'lib/datadog/di/component.rb', line 141

def code_tracker
  @code_tracker
end

#correlation_sampler ⇒ Datadog::DI::CorrelationSampler (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



143
144
145
# File 'lib/datadog/di/component.rb', line 143

def correlation_sampler
  @correlation_sampler
end

#guardrails_telemetry ⇒ GuardrailsTelemetry (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The guardrails metric emitter.

Returns:



137
138
139
# File 'lib/datadog/di/component.rb', line 137

def guardrails_telemetry
  @guardrails_telemetry
end

#instrumenter ⇒ Object (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



144
145
146
# File 'lib/datadog/di/component.rb', line 144

def instrumenter
  @instrumenter
end

#logger ⇒ Object (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



133
134
135
# File 'lib/datadog/di/component.rb', line 133

def logger
  @logger
end

#probe_manager ⇒ Object (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



148
149
150
# File 'lib/datadog/di/component.rb', line 148

def probe_manager
  @probe_manager
end

#probe_notification_builder ⇒ Object (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



147
148
149
# File 'lib/datadog/di/component.rb', line 147

def probe_notification_builder
  @probe_notification_builder
end

#probe_notifier_worker ⇒ Object (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



146
147
148
# File 'lib/datadog/di/component.rb', line 146

def probe_notifier_worker
  @probe_notifier_worker
end

#probe_repository ⇒ Object (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



145
146
147
# File 'lib/datadog/di/component.rb', line 145

def probe_repository
  @probe_repository
end

#rc_enablement_signal_paths ⇒ Array<Core::Remote::Configuration::Path> (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The repository paths of the APM_TRACING contents that supplied the dynamic_instrumentation_enabled value at the last merge where the field was present. The component holds the paths so that they outlive the RC client.



276
277
278
# File 'lib/datadog/di/component.rb', line 276

def rc_enablement_signal_paths
  @rc_enablement_signal_paths
end

#redactor ⇒ Object (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



149
150
151
# File 'lib/datadog/di/component.rb', line 149

def redactor
  @redactor
end

#serializer ⇒ Object (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



150
151
152
# File 'lib/datadog/di/component.rb', line 150

def serializer
  @serializer
end

#settings ⇒ Object (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



131
132
133
# File 'lib/datadog/di/component.rb', line 131

def settings
  @settings
end

#telemetry ⇒ Object (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



134
135
136
# File 'lib/datadog/di/component.rb', line 134

def telemetry
  @telemetry
end

Class Method Details

.build(settings, agent_settings, logger, telemetry: nil) ⇒ Component?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Builds and registers the DI component, or returns nil without building one when the settings have no dynamic_instrumentation section, DI is explicitly disabled, the runtime is unsupported, or Remote Configuration is disabled.

Parameters:

Returns:

  • (Component, nil) —

    the built component, or nil when no component was built



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/datadog/di/component.rb', line 30

def build(settings, agent_settings, logger, telemetry: nil)
  return unless settings.respond_to?(:dynamic_instrumentation)

  # Explicit DD_DYNAMIC_INSTRUMENTATION_ENABLED=false: do not build the
  # component at all. This is customer intent, not a failure — log at
  # debug and emit no telemetry error. Capabilities#register skips the
  # DI RC block under the same condition, so no enable signal arrives.
  if Remote.explicitly_disabled?(settings)
    logger.debug("di: dynamic instrumentation is explicitly disabled (DD_DYNAMIC_INSTRUMENTATION_ENABLED=false); not building component")
    return
  end

  reason = DI.unsupported_reason(settings)
  if reason
    # Log level mirrors customer intent: if the customer explicitly
    # opted in via DD_DYNAMIC_INSTRUMENTATION_ENABLED, warn. Otherwise
    # debug — with always-build, this path runs on every tracer boot
    # for every customer, including those who never wanted DI. Spamming
    # warnings to silent users (especially on JRuby or Ruby 2.5) would
    # be noise. Customers who later trigger implicit enablement via the
    # Datadog UI get a symmetric warn from Remote.handle_rc_enablement
    # when the RC enable signal finds no component to start.
    level = explicitly_enabled?(settings) ? :warn : :debug
    logger.public_send(level, "di: dynamic instrumentation is disabled: #{reason}")
    return
  end

  new(settings, agent_settings, logger, code_tracker: DI.code_tracker, telemetry: telemetry).tap do |component|
    DI.add_current_component(component)
  end
end

.explicitly_enabled?(settings) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

True when the customer explicitly set DD_DYNAMIC_INSTRUMENTATION_ENABLED=true (or its equivalent in code). Symmetric to Remote.explicitly_disabled?.

Uses Core::Configuration::Options::InstanceMethods#using_default? rather than options[:enabled].default_precedence? because the option hash is populated lazily on first access; reading the underlying option before build touches the value would NoMethodError on nil.

Parameters:

Returns:

  • (Boolean)


73
74
75
76
# File 'lib/datadog/di/component.rb', line 73

def explicitly_enabled?(settings)
  !settings.dynamic_instrumentation.using_default?(:enabled) &&
    settings.dynamic_instrumentation.enabled
end

Instance Method Details

#after_fork ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Rebuilds the process-scoped coordinated-sampling state after the process forks, so the child starts with fresh budgets.



214
215
216
# File 'lib/datadog/di/component.rb', line 214

def after_fork
  correlation_sampler.after_fork!
end

#implicitly_enabled? ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Whether the component is started while the customer left DD_DYNAMIC_INSTRUMENTATION_ENABLED at its default, the state produced by remote-configuration enablement.

An explicit enabled = false takes precedence over a prior RC enablement: once the customer sets the option, this returns false even while the component is still started.

Returns:

  • (Boolean)


227
228
229
# File 'lib/datadog/di/component.rb', line 227

def implicitly_enabled?
  started? && settings.dynamic_instrumentation.using_default?(:enabled)
end

#parse_probe_spec_and_notify(probe_spec) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/datadog/di/component.rb', line 313

def parse_probe_spec_and_notify(probe_spec)
  probe = ProbeBuilder.build_from_remote_config(probe_spec, logger: logger)
rescue Exception => exc # standard:disable Lint/RescueException
  Datadog::DI.reraise_if_fatal(exc)
  begin
    probe = Struct.new(:id).new(
      probe_spec["id"],
    )
    payload = probe_notification_builder.build_errored(probe, exc)
    probe_notifier_worker.add_status(payload)
  rescue Exception => nested_exc # standard:disable Lint/RescueException
    Datadog::DI.reraise_if_fatal(nested_exc)
    logger.debug { "di: failed to build error notification: #{nested_exc.class}: #{nested_exc.message}" }
    telemetry&.report(nested_exc, description: "Error building probe error notification")
    raise
  end

  raise
else
  payload = probe_notification_builder.build_received(probe)
  probe_notifier_worker.add_status(payload, probe: probe)
  probe
end

#rc_enablement_signal_withdrawn?(unparseable_contents) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Whether an APM_TRACING merge that omitted dynamic_instrumentation_enabled withdraws this component's RC enable signal, so the component must stop.

Returns true when the component is implicitly enabled and no unparseable content can be the signal carrier. A content at a recorded signal path can be the carrier. When no paths are recorded, every unparseable content can be the carrier.

Parameters:

Returns:

  • (Boolean)


241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/datadog/di/component.rb', line 241

def rc_enablement_signal_withdrawn?(unparseable_contents)
  return false unless implicitly_enabled?

  recorded_signal_paths = rc_enablement_signal_paths
  signal_source_unparseable =
    if recorded_signal_paths.empty?
      unparseable_contents.any?
    else
      unparseable_contents.any? { |content| recorded_signal_paths.include?(content.path) }
    end
  if signal_source_unparseable
    if recorded_signal_paths.empty?
      logger.trace do
        "APM_TRACING RC: dynamic_instrumentation_enabled absent while an APM_TRACING config " \
        "failed to parse; the enable signal carrier is unknown (no signal paths recorded), " \
        "keeping the DI state"
      end
    else
      logger.trace do
        "APM_TRACING RC: dynamic_instrumentation_enabled absent while the config that carried " \
        "the enable signal failed to parse; keeping the DI state"
      end
    end
    false
  else
    logger.trace { "APM_TRACING RC: dynamic_instrumentation_enabled withdrawn; treating as false" }
    true
  end
end

#record_rc_enablement_signal_paths(paths) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Replaces the recorded enable-signal carrier paths.

Parameters:



282
283
284
285
# File 'lib/datadog/di/component.rb', line 282

def record_rc_enablement_signal_paths(paths)
  @rc_enablement_signal_paths = paths.dup
  nil
end

#shutdown!(replacement = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Shuts down dynamic instrumentation permanently.

Removes all code hooks and stops background threads. Called by Components#shutdown! during component destruction. Unlike #stop!, this is not reversible.

Does not clear out the code tracker, because it's only populated by code when code is compiled and therefore, if the code tracker was replaced by a new instance, the new instance of it wouldn't have any of the already loaded code tracked.



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/datadog/di/component.rb', line 297

def shutdown!(replacement = nil)
  DI.remove_current_component(self)

  # Hold the lifecycle mutex so all transitions of @started are
  # serialized — start! / stop! / shutdown! cannot interleave with
  # one another. Without the mutex an in-flight stop! from an RC
  # callback could complete after shutdown!'s probe_manager.close,
  # producing an inconsistent state.
  @lifecycle_mutex.synchronize do
    @started = false
    probe_manager.clear_hooks
    probe_manager.close
    probe_notifier_worker.stop
  end
end

#start! ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Starts the DI component: begins accepting probes and processing snapshots.

Starts the probe notifier worker thread before enabling the definition trace point, so any future status emission from a trace-point-driven installation has a worker to drain it. Today ProbeManager#reopen re-hooks without emitting statuses, so the order is defensive rather than load-bearing. No-op if already started. Serialized by @lifecycle_mutex.



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/datadog/di/component.rb', line 163

def start!
  @lifecycle_mutex.synchronize do
    return if @started

    # DI.activate_tracking creates the global code tracker lazily. When DI
    # is enabled after boot via remote configuration (in-product
    # enablement), this component and its instrumenter were built with a
    # nil tracker; adopt the now-current global tracker so line probes
    # can be targeted.
    @code_tracker = DI.code_tracker
    instrumenter.code_tracker = @code_tracker

    probe_notifier_worker.start
    probe_manager.reopen
    @started = true
  end
end

#started? ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Whether the component is currently started.

Read by remote config dispatch to decide whether to apply probe changes (changes received while stopped are dropped, since the next start! will reconcile from the latest RC state).

Returns:

  • (Boolean) —

    true if start! has been called and stop! has not



206
207
208
# File 'lib/datadog/di/component.rb', line 206

def started?
  @started
end

#stop! ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Stops the DI component: removes all probes and stops background threads.

The component remains alive and can be restarted with #start!. Does not clear out the code tracker. No-op if already stopped. Serialized by @lifecycle_mutex.



189
190
191
192
193
194
195
196
197
# File 'lib/datadog/di/component.rb', line 189

def stop!
  @lifecycle_mutex.synchronize do
    return unless @started

    probe_manager.stop
    probe_notifier_worker.stop
    @started = false
  end
end