Sha256: 15296c0db9742f61316dba2ae0c56b7bbff348b73afd734052af058608b58d01

Contents?: true

Size: 1.82 KB

Versions: 38

Compression:

Stored size: 1.82 KB

Contents

# frozen_string_literal: true

require_relative '../runtime/metrics'

require_relative '../worker'
require_relative 'async'
require_relative 'polling'

module Datadog
  module Core
    module Workers
      # Emits runtime metrics asynchronously on a timed loop
      class RuntimeMetrics < Worker
        include Workers::Polling

        # In seconds
        DEFAULT_FLUSH_INTERVAL = 10
        DEFAULT_BACK_OFF_MAX = 30

        attr_reader \
          :metrics

        def initialize(options = {})
          @metrics = options.fetch(:metrics) { Core::Runtime::Metrics.new }

          # Workers::Async::Thread settings
          self.fork_policy = options.fetch(:fork_policy, Workers::Async::Thread::FORK_POLICY_STOP)

          # Workers::IntervalLoop settings
          self.loop_base_interval = options.fetch(:interval, DEFAULT_FLUSH_INTERVAL)
          self.loop_back_off_ratio = options[:back_off_ratio] if options.key?(:back_off_ratio)
          self.loop_back_off_max = options.fetch(:back_off_max, DEFAULT_BACK_OFF_MAX)

          self.enabled = options.fetch(:enabled, false)
        end

        def perform
          metrics.flush
          true
        end

        def register_service(service)
          # Start the worker
          metrics.register_service(service).tap { perform }
        end

        # TODO: `close_metrics` is only needed because
        # Datadog::Core::Configuration::Components directly manipulates
        # the lifecycle of Runtime::Metrics.statsd instances.
        # This should be avoided, as it prevents this class from
        # ensuring correct resource decommission of its internal
        # dependencies.
        def stop(*args, close_metrics: true)
          self.enabled = false
          result = super(*args)
          @metrics.close if close_metrics
          result
        end
      end
    end
  end
end

Version data entries

38 entries across 38 versions & 2 rubygems

Version Path
datadog-2.12.0 lib/datadog/core/workers/runtime_metrics.rb
datadog-2.11.0 lib/datadog/core/workers/runtime_metrics.rb
datadog-2.10.0 lib/datadog/core/workers/runtime_metrics.rb
datadog-2.9.0 lib/datadog/core/workers/runtime_metrics.rb
datadog-2.8.0 lib/datadog/core/workers/runtime_metrics.rb
datadog-2.7.1 lib/datadog/core/workers/runtime_metrics.rb
datadog-2.7.0 lib/datadog/core/workers/runtime_metrics.rb
datadog-2.6.0 lib/datadog/core/workers/runtime_metrics.rb
datadog-2.5.0 lib/datadog/core/workers/runtime_metrics.rb
datadog-2.4.0 lib/datadog/core/workers/runtime_metrics.rb
datadog-2.3.0 lib/datadog/core/workers/runtime_metrics.rb
datadog-2.2.0 lib/datadog/core/workers/runtime_metrics.rb
ddtrace-1.23.3 lib/datadog/core/workers/runtime_metrics.rb
ddtrace-1.23.2 lib/datadog/core/workers/runtime_metrics.rb
datadog-2.1.0 lib/datadog/core/workers/runtime_metrics.rb
datadog-2.0.0 lib/datadog/core/workers/runtime_metrics.rb
ddtrace-1.23.1 lib/datadog/core/workers/runtime_metrics.rb
datadog-2.0.0.beta2 lib/datadog/core/workers/runtime_metrics.rb
ddtrace-1.22.0 lib/datadog/core/workers/runtime_metrics.rb
datadog-2.0.0.beta1 lib/datadog/core/workers/runtime_metrics.rb