Sha256: 42e07451d77048ccfa43e32d14cd62aaefa3a801ceede2e5bbd3863eac3b128b

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

require_relative '../../idempotency'

class Idempotency
  module Instrumentation
    class StatsdListener
      EVENT_NAME_TO_METRIC_MAPPINGS = {
        Events::CACHE_HIT => 'idempotency_cache_hit_count',
        Events::CACHE_MISS => 'idempotency_cache_miss_count',
        Events::LOCK_CONFLICT => 'idempotency_lock_conflict_count'
      }.freeze

      def initialize(statsd_client, namespace = nil)
        @statsd_client = statsd_client
        @namespace = namespace
      end

      def setup_subscriptions
        EVENT_NAME_TO_METRIC_MAPPINGS.each do |event_name, metric|
          Idempotency.notifier.subscribe(event_name) do |event|
            send_metric(metric, event.payload)
          end
        end
      end

      private

      attr_reader :namespace, :statsd_client

      def send_metric(metric_name, event_data)
        action = event_data[:action] || "#{event_data[:request].request_method}:#{event_data[:request].path}"
        tags = ["action:#{action}"]
        tags << "namespace:#{@namespace}" if @namespace

        @statsd_client.increment(metric_name, tags:)
        @statsd_client.histogram(
          'idempotency_cache_duration_seconds', event_data[:duration], tags: tags + ["metric:#{metric_name}"]
        )
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
idempotency-0.1.4 lib/idempotency/instrumentation/statsd_listener.rb