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

Inherits:
Object
  • Object
show all
Extended by:
Profiling::Component, Tracing::Component
Defined in:
lib/datadog/core/configuration/components.rb

Overview

Global components for the trace library.

Constant Summary

Constants included from Tracing::Component

Tracing::Component::WRITER_RECORD_ENVIRONMENT_INFORMATION_CALLBACK

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Profiling::Component

build_profiler

Methods included from Tracing::Component

build_sampler, build_span_sampler, build_trace_flush, build_tracer, build_writer, ensure_priority_sampling, subscribe_to_writer_events!, writer_update_priority_sampler_rates_callback

Constructor Details

#initialize(settings) ⇒ Components

Returns a new instance of Components.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/datadog/core/configuration/components.rb', line 69

def initialize(settings)
  # Logger
  @logger = self.class.build_logger(settings)

  agent_settings = AgentSettingsResolver.call(settings, logger: @logger)

  # Tracer
  @tracer = self.class.build_tracer(settings, agent_settings)

  # Profiler
  @profiler = self.class.build_profiler(settings, agent_settings, @tracer)

  # Runtime metrics
  @runtime_metrics = self.class.build_runtime_metrics_worker(settings)

  # Health metrics
  @health_metrics = self.class.build_health_metrics(settings)

  # Telemetry
  @telemetry = self.class.build_telemetry(settings)

  # AppSec
  @appsec = Datadog::AppSec::Component.build_appsec_component(settings)
end

Instance Attribute Details

#appsecObject (readonly)

Returns the value of attribute appsec.



60
61
62
# File 'lib/datadog/core/configuration/components.rb', line 60

def appsec
  @appsec
end

#health_metricsObject (readonly)

Returns the value of attribute health_metrics.



60
61
62
# File 'lib/datadog/core/configuration/components.rb', line 60

def health_metrics
  @health_metrics
end

#loggerObject (readonly)

Returns the value of attribute logger.



60
61
62
# File 'lib/datadog/core/configuration/components.rb', line 60

def logger
  @logger
end

#profilerObject (readonly)

Returns the value of attribute profiler.



60
61
62
# File 'lib/datadog/core/configuration/components.rb', line 60

def profiler
  @profiler
end

#runtime_metricsObject (readonly)

Returns the value of attribute runtime_metrics.



60
61
62
# File 'lib/datadog/core/configuration/components.rb', line 60

def runtime_metrics
  @runtime_metrics
end

#telemetryObject (readonly)

Returns the value of attribute telemetry.



60
61
62
# File 'lib/datadog/core/configuration/components.rb', line 60

def telemetry
  @telemetry
end

#tracerObject (readonly)

Returns the value of attribute tracer.



60
61
62
# File 'lib/datadog/core/configuration/components.rb', line 60

def tracer
  @tracer
end

Class Method Details

.build_health_metrics(settings) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/datadog/core/configuration/components.rb', line 22

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

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

.build_logger(settings) ⇒ Object



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

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) ⇒ Object



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

def build_runtime_metrics(settings)
  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?

  Core::Runtime::Metrics.new(**options)
end

.build_runtime_metrics_worker(settings) ⇒ Object



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

def build_runtime_metrics_worker(settings)
  # 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)
  )

  Core::Workers::RuntimeMetrics.new(options)
end

.build_telemetry(settings) ⇒ Object



55
56
57
# File 'lib/datadog/core/configuration/components.rb', line 55

def build_telemetry(settings)
  Telemetry::Client.new(enabled: settings.telemetry.enabled)
end

Instance Method Details

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



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/datadog/core/configuration/components.rb', line 113

def shutdown!(replacement = nil)
  # Decommission AppSec
  appsec.shutdown! if appsec

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

  # Shutdown old profiler
  profiler.shutdown! unless profiler.nil?

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

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

  telemetry.stop!
  telemetry.emit_closing! unless replacement
end

#startup!(settings) ⇒ Object

Starts up components



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

def startup!(settings)
  if settings.profiling.enabled
    if profiler
      @logger.debug('Profiling started')
      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
  else
    @logger.debug('Profiling is disabled')
  end
end