Class: Datadog::Core::Telemetry::Component

Inherits:
Object
  • Object
show all
Includes:
Logging, Utils::Forking
Defined in:
lib/datadog/core/telemetry/component.rb

Overview

Telemetry entrypoint, coordinates sending telemetry events at various points in app lifecycle. Note: Telemetry does not spawn its worker thread in fork processes, thus no telemetry is sent in forked processes.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

#error, #report

Methods included from Utils::Forking

#after_fork!, extended, #fork_pid, #forked?, included, #update_fork_pid!

Constructor Details

#initialize(heartbeat_interval_seconds:, metrics_aggregation_interval_seconds:, dependency_collection:, http_transport:, shutdown_timeout_seconds:, enabled: true, metrics_enabled: true, log_collection_enabled: true) ⇒ Component

Returns a new instance of Component.

Parameters:

  • enabled (Boolean) (defaults to: true)

    Determines whether telemetry events should be sent to the API

  • metrics_enabled (Boolean) (defaults to: true)

    Determines whether telemetry metrics should be sent to the API

  • heartbeat_interval_seconds (Float)

    How frequently heartbeats will be reported, in seconds.

  • metrics_aggregation_interval_seconds (Float)

    How frequently metrics will be aggregated, in seconds.

  • dependency_collection (Boolean)

    Whether to send the ‘app-dependencies-loaded` event



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/datadog/core/telemetry/component.rb', line 65

def initialize(
  heartbeat_interval_seconds:,
  metrics_aggregation_interval_seconds:,
  dependency_collection:,
  http_transport:,
  shutdown_timeout_seconds:,
  enabled: true,
  metrics_enabled: true,
  log_collection_enabled: true
)
  @enabled = enabled
  @log_collection_enabled = log_collection_enabled

  @metrics_manager = MetricsManager.new(
    enabled: enabled && metrics_enabled,
    aggregation_interval: metrics_aggregation_interval_seconds
  )

  @worker = Telemetry::Worker.new(
    enabled: @enabled,
    heartbeat_interval_seconds: heartbeat_interval_seconds,
    metrics_aggregation_interval_seconds: metrics_aggregation_interval_seconds,
    emitter: Emitter.new(http_transport: http_transport),
    metrics_manager: @metrics_manager,
    dependency_collection: dependency_collection,
    shutdown_timeout: shutdown_timeout_seconds
  )

  @stopped = false

  @worker.start
end

Instance Attribute Details

#enabledObject (readonly)

Returns the value of attribute enabled.



19
20
21
# File 'lib/datadog/core/telemetry/component.rb', line 19

def enabled
  @enabled
end

Class Method Details

.build(settings, agent_settings, logger) ⇒ Object



24
25
26
27
28
29
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
# File 'lib/datadog/core/telemetry/component.rb', line 24

def self.build(settings, agent_settings, logger)
  enabled = settings.telemetry.enabled
  agentless_enabled = settings.telemetry.agentless_enabled

  if !agentless_enabled && agent_settings.adapter != Datadog::Core::Configuration::Ext::Agent::HTTP::ADAPTER
    enabled = false
    logger.debug { "Telemetry disabled. Agent network adapter not supported: #{agent_settings.adapter}" }
  end

  if agentless_enabled && settings.api_key.nil?
    enabled = false
    logger.debug { 'Telemetry disabled. Agentless telemetry requires an DD_API_KEY variable to be set.' }
  end

  transport = if agentless_enabled
                Datadog::Core::Telemetry::Http::Transport.build_agentless_transport(
                  api_key: settings.api_key,
                  dd_site: settings.site,
                  url_override: settings.telemetry.agentless_url_override
                )
              else
                Datadog::Core::Telemetry::Http::Transport.build_agent_transport(agent_settings)
              end

  Telemetry::Component.new(
    http_transport: transport,
    enabled: enabled,
    metrics_enabled: enabled && settings.telemetry.metrics_enabled,
    heartbeat_interval_seconds: settings.telemetry.heartbeat_interval_seconds,
    metrics_aggregation_interval_seconds: settings.telemetry.metrics_aggregation_interval_seconds,
    dependency_collection: settings.telemetry.dependency_collection,
    shutdown_timeout_seconds: settings.telemetry.shutdown_timeout_seconds,
    log_collection_enabled: settings.telemetry.log_collection_enabled
  )
end

Instance Method Details

#client_configuration_change!(changes) ⇒ Object

Report configuration changes caused by Remote Configuration.



129
130
131
132
133
# File 'lib/datadog/core/telemetry/component.rb', line 129

def client_configuration_change!(changes)
  return if !@enabled || forked?

  @worker.enqueue(Event::AppClientConfigurationChange.new(changes, 'remote_config'))
end

#dec(namespace, metric_name, value, tags: {}, common: true) ⇒ Object

Decremenets a count metric.



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

def dec(namespace, metric_name, value, tags: {}, common: true)
  @metrics_manager.dec(namespace, metric_name, value, tags: tags, common: common)
end

#disable!Object



98
99
100
101
# File 'lib/datadog/core/telemetry/component.rb', line 98

def disable!
  @enabled = false
  @worker.enabled = false
end

#distribution(namespace, metric_name, value, tags: {}, common: true) ⇒ Object

Tracks distribution metric.



156
157
158
# File 'lib/datadog/core/telemetry/component.rb', line 156

def distribution(namespace, metric_name, value, tags: {}, common: true)
  @metrics_manager.distribution(namespace, metric_name, value, tags: tags, common: common)
end

#emit_closing!Object



110
111
112
113
114
# File 'lib/datadog/core/telemetry/component.rb', line 110

def emit_closing!
  return if !@enabled || forked?

  @worker.enqueue(Event::AppClosing.new)
end

#gauge(namespace, metric_name, value, tags: {}, common: true) ⇒ Object

Tracks gauge metric.



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

def gauge(namespace, metric_name, value, tags: {}, common: true)
  @metrics_manager.gauge(namespace, metric_name, value, tags: tags, common: common)
end

#inc(namespace, metric_name, value, tags: {}, common: true) ⇒ Object

Increments a count metric.



136
137
138
# File 'lib/datadog/core/telemetry/component.rb', line 136

def inc(namespace, metric_name, value, tags: {}, common: true)
  @metrics_manager.inc(namespace, metric_name, value, tags: tags, common: common)
end

#integrations_change!Object



116
117
118
119
120
# File 'lib/datadog/core/telemetry/component.rb', line 116

def integrations_change!
  return if !@enabled || forked?

  @worker.enqueue(Event::AppIntegrationsChange.new)
end

#log!(event) ⇒ Object



122
123
124
125
126
# File 'lib/datadog/core/telemetry/component.rb', line 122

def log!(event)
  return if !@enabled || forked? || !@log_collection_enabled

  @worker.enqueue(event)
end

#rate(namespace, metric_name, value, tags: {}, common: true) ⇒ Object

Tracks rate metric.



151
152
153
# File 'lib/datadog/core/telemetry/component.rb', line 151

def rate(namespace, metric_name, value, tags: {}, common: true)
  @metrics_manager.rate(namespace, metric_name, value, tags: tags, common: common)
end

#stop!Object



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

def stop!
  return if @stopped

  @worker.stop(true)
  @stopped = true
end