Sha256: 568d1bf0f8a953a27783642975f925395e9ae4316f343be520f809a0e5b52b27

Contents?: true

Size: 1.7 KB

Versions: 4

Compression:

Stored size: 1.7 KB

Contents

# frozen_string_literal: true

module Puma
  class Plugin
    module Telemetry
      module Targets
        # Target wrapping Datadog Statsd client. You can configure
        # all details like _metrics prefix_ and _tags_ in the client
        # itself.
        #
        # ## Example
        #
        #     require "datadog/statsd"
        #
        #     client = Datadog::Statsd.new(namespace: "ruby.puma",
        #                                  tags: {
        #                                    service: "my-webapp",
        #                                    env: ENV["RAILS_ENV"],
        #                                    version: ENV["CODE_VERSION"]
        #                                  })
        #
        #     DatadogStatsdTarget.new(client: client)
        #
        class DatadogStatsdTarget
          def initialize(client:)
            @client = client
          end

          # We are using `gauge` metric type, which means that only the last
          # value will get send to datadog. DD Statsd client is using extra
          # thread since v5 for aggregating metrics before it sends them.
          #
          # This means that we could publish metrics from here several times
          # before they get flushed from the aggregation thread, and when they
          # do, only the last values will get sent.
          #
          # That's why we are explicitly calling flush here, in order to persist
          # all metrics, and not only the most recent ones.
          #
          def call(telemetry)
            telemetry.each do |metric, value|
              @client.gauge(metric, value)
            end

            @client.flush(sync: true)
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
puma-plugin-telemetry-1.1.4 lib/puma/plugin/telemetry/targets/datadog_statsd_target.rb
puma-plugin-telemetry-1.1.2 lib/puma/plugin/telemetry/targets/datadog_statsd_target.rb
puma-plugin-telemetry-1.1.1 lib/puma/plugin/telemetry/targets/datadog_statsd_target.rb
puma-plugin-telemetry-1.1.0 lib/puma/plugin/telemetry/targets/datadog_statsd_target.rb