Class: Puma::Plugin::Telemetry::Targets::DatadogStatsdTarget

Inherits:
Object
  • Object
show all
Defined in:
lib/puma/plugin/telemetry/targets/datadog_statsd_target.rb

Overview

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)

Instance Method Summary collapse

Constructor Details

#initialize(client:) ⇒ DatadogStatsdTarget

Returns a new instance of DatadogStatsdTarget.



25
26
27
# File 'lib/puma/plugin/telemetry/targets/datadog_statsd_target.rb', line 25

def initialize(client:)
  @client = client
end

Instance Method Details

#call(telemetry) ⇒ Object

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.



40
41
42
43
44
45
46
# File 'lib/puma/plugin/telemetry/targets/datadog_statsd_target.rb', line 40

def call(telemetry)
  telemetry.each do |metric, value|
    @client.gauge(metric, value)
  end

  @client.flush(sync: true)
end