Class: Datadog::Core::Runtime::Metrics

Inherits:
Metrics::Client show all
Defined in:
lib/datadog/core/runtime/metrics.rb

Overview

For generating runtime metrics

Constant Summary

Constants included from Metrics::Options

Metrics::Options::DEFAULT

Instance Attribute Summary

Attributes inherited from Metrics::Client

#logger, #statsd, #telemetry

Instance Method Summary collapse

Methods inherited from Metrics::Client

#close, #configure, #count, #default_hostname, #default_port, #default_statsd_client, #distribution, #enabled=, #enabled?, #gauge, #increment, #send_metrics, #send_stats?, #supported?, #time

Methods included from Metrics::Options

#metric_options

Constructor Details

#initialize(telemetry:, **options) ⇒ Metrics

Returns a new instance of Metrics.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/datadog/core/runtime/metrics.rb', line 18

def initialize(telemetry:, **options)
  super

  # Initialize service list
  @services = Set.new(options.fetch(:services, []))
  @service_tags = nil
  compile_service_tags!

  # Initialize the collection of runtime-id
  @runtime_id_enabled = options.fetch(:experimental_runtime_id_enabled, false)

  # Initialized process tags support
  @process_tags_enabled = options.fetch(:experimental_propagate_process_tags_enabled, false)
end

Instance Method Details

#default_metric_optionsObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/datadog/core/runtime/metrics.rb', line 107

def default_metric_options
  # Return dupes, so that the constant isn't modified,
  # and defaults are unfrozen for mutation in Statsd.
  super.tap do |options|
    options[:tags] = options[:tags].dup

    # Add services dynamically because they might change during runtime.
    options[:tags].concat(service_tags) unless service_tags.nil?

    # Add runtime-id dynamically because it might change during runtime.
    options[:tags].concat(["runtime-id:#{Core::Environment::Identity.id}"]) if @runtime_id_enabled

    # Add process tags when enabled
    if @process_tags_enabled
      options[:tags].concat(Core::Environment::Process.tags)
    end
  end
end

#flushObject

Flush all runtime metrics to Statsd client



49
50
51
52
53
54
55
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
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/datadog/core/runtime/metrics.rb', line 49

def flush
  return unless enabled?

  try_flush do
    if Core::Environment::ClassCount.available?
      gauge(Core::Runtime::Ext::Metrics::METRIC_CLASS_COUNT, Core::Environment::ClassCount.value)
    end
  end

  try_flush do
    if Core::Environment::ThreadCount.available?
      gauge(Core::Runtime::Ext::Metrics::METRIC_THREAD_COUNT, Core::Environment::ThreadCount.value)
    end
  end

  try_flush { gc_metrics.each { |metric, value| gauge(metric, value) } if Core::Environment::GC.available? }

  try_flush do
    if Core::Environment::VMCache.available?
      # Only on Ruby < 3.2
      gauge_if_not_nil(
        Core::Runtime::Ext::Metrics::METRIC_GLOBAL_CONSTANT_STATE,
        Core::Environment::VMCache.global_constant_state
      )

      # Only on Ruby 2.x
      gauge_if_not_nil(
        Core::Runtime::Ext::Metrics::METRIC_GLOBAL_METHOD_STATE,
        Core::Environment::VMCache.global_method_state
      )

      # Only on Ruby >= 3.2
      gauge_if_not_nil(
        Core::Runtime::Ext::Metrics::METRIC_CONSTANT_CACHE_INVALIDATIONS,
        Core::Environment::VMCache.constant_cache_invalidations
      )
      gauge_if_not_nil(
        Core::Runtime::Ext::Metrics::METRIC_CONSTANT_CACHE_MISSES,
        Core::Environment::VMCache.constant_cache_misses
      )
    end
  end

  flush_yjit_stats
end

#gc_metricsObject



95
96
97
98
99
# File 'lib/datadog/core/runtime/metrics.rb', line 95

def gc_metrics
  Core::Environment::GC.stat.flat_map do |k, v|
    nested_gc_metric(Core::Runtime::Ext::Metrics::METRIC_GC_PREFIX, k, v)
  end.to_h
end

#register_service(service) ⇒ Object

Associate service with runtime metrics



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/datadog/core/runtime/metrics.rb', line 34

def register_service(service)
  return unless enabled? && service

  service = service.to_s

  unless @services.include?(service)
    # Add service to list and update services tag
    services << service

    # Recompile the service tags
    compile_service_tags!
  end
end

#try_flushObject



101
102
103
104
105
# File 'lib/datadog/core/runtime/metrics.rb', line 101

def try_flush
  yield
rescue => e
  Datadog.logger.warn("Error while sending runtime metric. Cause: #{e.class.name} #{e.message}")
end