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.

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



63
64
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
# File 'lib/datadog/core/telemetry/component.rb', line 63

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

  @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
  )
  @worker.start
end

Instance Attribute Details

#enabledObject (readonly)

Returns the value of attribute enabled.



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

def enabled
  @enabled
end

Class Method Details

.build(settings, agent_settings, logger) ⇒ Object



23
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
# File 'lib/datadog/core/telemetry/component.rb', line 23

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

Instance Method Details

#client_configuration_change!(changes) ⇒ Object

Report configuration changes caused by Remote Configuration.



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

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.



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

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

#disable!Object



92
93
94
95
# File 'lib/datadog/core/telemetry/component.rb', line 92

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

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

Tracks distribution metric.



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

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

#emit_closing!Object



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

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.



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

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.



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

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

#integrations_change!Object



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

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

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

#log!(event) ⇒ Object



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

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

  @worker.enqueue(event)
end

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

Tracks rate metric.



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

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

#stop!Object



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

def stop!
  return if @stopped

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