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.

API:

  • private

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings, agent_settings, logger, code_tracker: nil, 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.

Returns a new instance of Component.

API:

  • private



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/datadog/di/component.rb', line 56

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
  @code_tracker = code_tracker
  @redactor = Redactor.new(settings)
  @serializer = Serializer.new(settings, redactor, telemetry: telemetry)
  @instrumenter = Instrumenter.new(settings, serializer, logger, code_tracker: code_tracker, telemetry: telemetry)
  @probe_repository = ProbeRepository.new
  @probe_notification_builder = ProbeNotificationBuilder.new(settings, serializer)
  @probe_notifier_worker = ProbeNotifierWorker.new(
    settings, logger,
    agent_settings: agent_settings,
    probe_repository: probe_repository,
    probe_notification_builder: probe_notification_builder,
    telemetry: telemetry,
  )
  @probe_manager = ProbeManager.new(
    settings, instrumenter, probe_notification_builder, probe_notifier_worker, logger, probe_repository,
    telemetry: telemetry,
  )
  probe_notifier_worker.start
end

Instance Attribute Details

#agent_settingsObject (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.

API:

  • private



83
84
85
# File 'lib/datadog/di/component.rb', line 83

def agent_settings
  @agent_settings
end

#code_trackerObject (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.

API:

  • private



86
87
88
# File 'lib/datadog/di/component.rb', line 86

def code_tracker
  @code_tracker
end

#instrumenterObject (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.

API:

  • private



87
88
89
# File 'lib/datadog/di/component.rb', line 87

def instrumenter
  @instrumenter
end

#loggerObject (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.

API:

  • private



84
85
86
# File 'lib/datadog/di/component.rb', line 84

def logger
  @logger
end

#probe_managerObject (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.

API:

  • private



91
92
93
# File 'lib/datadog/di/component.rb', line 91

def probe_manager
  @probe_manager
end

#probe_notification_builderObject (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.

API:

  • private



90
91
92
# File 'lib/datadog/di/component.rb', line 90

def probe_notification_builder
  @probe_notification_builder
end

#probe_notifier_workerObject (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.

API:

  • private



89
90
91
# File 'lib/datadog/di/component.rb', line 89

def probe_notifier_worker
  @probe_notifier_worker
end

#probe_repositoryObject (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.

API:

  • private



88
89
90
# File 'lib/datadog/di/component.rb', line 88

def probe_repository
  @probe_repository
end

#redactorObject (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.

API:

  • private



92
93
94
# File 'lib/datadog/di/component.rb', line 92

def redactor
  @redactor
end

#serializerObject (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.

API:

  • private



93
94
95
# File 'lib/datadog/di/component.rb', line 93

def serializer
  @serializer
end

#settingsObject (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.

API:

  • private



82
83
84
# File 'lib/datadog/di/component.rb', line 82

def settings
  @settings
end

#telemetryObject (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.

API:

  • private



85
86
87
# File 'lib/datadog/di/component.rb', line 85

def telemetry
  @telemetry
end

Class Method Details

.build(settings, agent_settings, logger, telemetry: 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.

API:

  • private



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/datadog/di/component.rb', line 17

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

  unless settings.respond_to?(:remote) && settings.remote.enabled
    logger.warn("di: dynamic instrumentation could not be enabled because Remote Configuration Management is not available. To enable Remote Configuration, see https://docs.datadoghq.com/agent/remote_config")
    return
  end

  return unless environment_supported?(settings, logger)

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

.environment_supported?(settings, logger) ⇒ 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.

Checks whether the runtime environment is supported by dynamic instrumentation. Currently we only require that, if Rails is used, that Rails environment is not development because DI does not currently support code unloading and reloading.

Returns:

API:

  • private



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/datadog/di/component.rb', line 36

def environment_supported?(settings, logger)
  # TODO add tests?
  unless settings.dynamic_instrumentation.internal.development
    if Datadog::Core::Environment::Execution.development?
      logger.warn("di: development environment detected; not enabling dynamic instrumentation")
      return false
    end
  end
  if RUBY_ENGINE != 'ruby'
    logger.warn("di: cannot enable dynamic instrumentation: MRI is required, but running on #{RUBY_ENGINE}")
    return false
  end
  if RUBY_VERSION < '2.6'
    logger.warn("di: cannot enable dynamic instrumentation: Ruby 2.6+ is required, but running on #{RUBY_VERSION}")
    return false
  end
  true
end

Instance Method Details

#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.

API:

  • private



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/datadog/di/component.rb', line 111

def parse_probe_spec_and_notify(probe_spec)
  probe = ProbeBuilder.build_from_remote_config(probe_spec)
rescue => 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 => nested_exc
    logger.debug { "di: failed to build error notification: #{nested_exc.class}: #{nested_exc}" }
    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

#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.

Removes all code hooks and stops background threads.

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.

API:

  • private



103
104
105
106
107
108
109
# File 'lib/datadog/di/component.rb', line 103

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

  probe_manager.clear_hooks
  probe_manager.close
  probe_notifier_worker.stop
end