class Datadog::Runtime::Metrics

For generating runtime metrics

Attributes

service_tags[R]
services[R]

Public Class Methods

new(**options) click to toggle source
Calls superclass method Datadog::Metrics::new
# File lib/ddtrace/runtime/metrics.rb, line 15
def initialize(**options)
  super

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

Public Instance Methods

associate_with_span(span) click to toggle source
# File lib/ddtrace/runtime/metrics.rb, line 24
def associate_with_span(span)
  return if !enabled? || span.nil?

  # Register service as associated with metrics
  register_service(span.service) unless span.service.nil?

  # Tag span with language and runtime ID for association with metrics.
  # We only tag spans that performed internal application work.
  unless span.get_tag(Datadog::Ext::Integration::TAG_PEER_SERVICE)
    span.set_tag(Ext::Runtime::TAG_LANG, Core::Environment::Identity.lang)
  end
end
default_metric_options() click to toggle source
# File lib/ddtrace/runtime/metrics.rb, line 81
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?
  end
end
flush() click to toggle source

Flush all runtime metrics to Statsd client

# File lib/ddtrace/runtime/metrics.rb, line 53
def flush
  return unless enabled?

  try_flush do
    if Core::Environment::ClassCount.available?
      gauge(Ext::Runtime::Metrics::METRIC_CLASS_COUNT, Core::Environment::ClassCount.value)
    end
  end
  try_flush do
    if Core::Environment::ThreadCount.available?
      gauge(Ext::Runtime::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? }
end
gc_metrics() click to toggle source
# File lib/ddtrace/runtime/metrics.rb, line 69
def gc_metrics
  Core::Environment::GC.stat.flat_map do |k, v|
    nested_gc_metric(Ext::Runtime::Metrics::METRIC_GC_PREFIX, k, v)
  end.to_h
end
register_service(service) click to toggle source

Associate service with runtime metrics

# File lib/ddtrace/runtime/metrics.rb, line 38
def register_service(service)
  return if !enabled? || service.nil?

  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_flush() { || ... } click to toggle source
# File lib/ddtrace/runtime/metrics.rb, line 75
def try_flush
  yield
rescue StandardError => e
  Datadog.logger.error("Error while sending runtime metric. Cause: #{e.message}")
end

Private Instance Methods

compile_service_tags!() click to toggle source
# File lib/ddtrace/runtime/metrics.rb, line 98
def compile_service_tags!
  @service_tags = services.to_a.collect do |service|
    "#{Ext::Runtime::Metrics::TAG_SERVICE}:#{service}".freeze
  end
end
nested_gc_metric(prefix, k, v) click to toggle source
# File lib/ddtrace/runtime/metrics.rb, line 104
def nested_gc_metric(prefix, k, v)
  path = "#{prefix}.#{k}"

  if v.is_a?(Hash)
    v.flat_map do |key, value|
      nested_gc_metric(path, key, value)
    end
  else
    [[to_metric_name(path), v]]
  end
end
to_metric_name(str) click to toggle source
# File lib/ddtrace/runtime/metrics.rb, line 116
def to_metric_name(str)
  str.downcase.gsub(/[-\s]/, '_')
end