Class: Datadog::Configuration::Components

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

Overview

Global components for the trace library. rubocop:disable Metrics/LineLength

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings) ⇒ Components

Returns a new instance of Components.



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/ddtrace/configuration/components.rb', line 101

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

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

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

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

Instance Attribute Details

#health_metricsObject (readonly)

Returns the value of attribute health_metrics.



95
96
97
# File 'lib/ddtrace/configuration/components.rb', line 95

def health_metrics
  @health_metrics
end

#loggerObject (readonly)

Returns the value of attribute logger.



95
96
97
# File 'lib/ddtrace/configuration/components.rb', line 95

def logger
  @logger
end

#runtime_metricsObject (readonly)

Returns the value of attribute runtime_metrics.



95
96
97
# File 'lib/ddtrace/configuration/components.rb', line 95

def runtime_metrics
  @runtime_metrics
end

#tracerObject (readonly)

Returns the value of attribute tracer.



95
96
97
# File 'lib/ddtrace/configuration/components.rb', line 95

def tracer
  @tracer
end

Class Method Details

.build_health_metrics(settings) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/ddtrace/configuration/components.rb', line 13

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

  Datadog::Diagnostics::Health::Metrics.new(options)
end

.build_logger(settings) ⇒ Object



21
22
23
24
25
26
# File 'lib/ddtrace/configuration/components.rb', line 21

def build_logger(settings)
  logger = settings.logger.instance || Datadog::Logger.new(STDOUT)
  logger.level = settings.diagnostics.debug ? ::Logger::DEBUG : settings.logger.level

  logger
end

.build_runtime_metrics(settings) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/ddtrace/configuration/components.rb', line 28

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?

  Datadog::Runtime::Metrics.new(options)
end

.build_runtime_metrics_worker(settings) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/ddtrace/configuration/components.rb', line 36

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

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

.build_tracer(settings) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ddtrace/configuration/components.rb', line 46

def build_tracer(settings)
  # If a custom tracer has been provided, use it instead.
  # Ignore all other options (they should already be configured.)
  tracer = settings.tracer.instance
  return tracer unless tracer.nil?

  tracer = Tracer.new(
    default_service: settings.service,
    enabled: settings.tracer.enabled,
    partial_flush: settings.tracer.partial_flush.enabled,
    tags: build_tracer_tags(settings)
  )

  # TODO: We reconfigure the tracer here because it has way too many
  #       options it allows to mutate, and it's overwhelming to rewrite
  #       tracer initialization for now. Just reconfigure using the
  #       existing mutable #configure function. Remove when these components
  #       are extracted.
  tracer.configure(build_tracer_options(settings))

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



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
# File 'lib/ddtrace/configuration/components.rb', line 121

def shutdown!(replacement = nil)
  # 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 workers
  runtime_metrics.enabled = false
  runtime_metrics.stop(true)

  # Shutdown the old metrics, unless they are still being used.
  # (e.g. custom Statsd instances.)
  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)
end

#startup!(settings) ⇒ Object

Starts up components



116
# File 'lib/ddtrace/configuration/components.rb', line 116

def startup!(settings); end