Sha256: c1b33ad5327d8e128108283a9d40c9807bb59b3c76b610960f723c055f42c966

Contents?: true

Size: 1.82 KB

Versions: 21

Compression:

Stored size: 1.82 KB

Contents

require 'ddtrace/diagnostics/health'

module Datadog
  module Transport
    # Tracks statistics for transports
    module Statistics
      def stats
        @stats ||= Counts.new
      end

      def update_stats_from_response!(response)
        if response.ok?
          stats.success += 1
          stats.consecutive_errors = 0
        else
          stats.client_error += 1 if response.client_error?
          stats.server_error += 1 if response.server_error?
          stats.internal_error += 1 if response.internal_error?
          stats.consecutive_errors += 1
        end

        # Send health metrics
        Datadog.health_metrics.send_metrics(
          metrics_for_response(response).values
        )
      end

      def metrics_for_response(response)
        {}.tap do |metrics|
          metrics[:api_errors] = Metrics::Metric.new(:api_errors, nil, 1) if response.internal_error?
          metrics[:api_responses] = Metrics::Metric.new(:api_responses, nil, 1) unless response.internal_error?
        end
      end

      def update_stats_from_exception!(exception)
        stats.internal_error += 1
        stats.consecutive_errors += 1

        # Send health metrics
        Datadog.health_metrics.send_metrics(
          metrics_for_exception(exception).values
        )
      end

      def metrics_for_exception(_exception)
        { api_errors: Metrics::Metric.new(:api_errors, nil, 1) }
      end

      # Stat counts
      class Counts
        attr_accessor \
          :success,
          :client_error,
          :server_error,
          :internal_error,
          :consecutive_errors

        def initialize
          reset!
        end

        def reset!
          @success = 0
          @client_error = 0
          @server_error = 0
          @internal_error = 0
          @consecutive_errors = 0
        end
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 2 rubygems

Version Path
ddtrace-0.51.1 lib/ddtrace/transport/statistics.rb
ddtrace-0.51.0 lib/ddtrace/transport/statistics.rb
ddtrace-0.50.0 lib/ddtrace/transport/statistics.rb
ddtrace-0.49.0 lib/ddtrace/transport/statistics.rb
ddtrace-0.48.0 lib/ddtrace/transport/statistics.rb
ddtrace-0.47.0 lib/ddtrace/transport/statistics.rb
ddtrace-0.46.0 lib/ddtrace/transport/statistics.rb
ddtrace-0.45.0 lib/ddtrace/transport/statistics.rb
ddtrace-0.44.0 lib/ddtrace/transport/statistics.rb
ddtrace-0.43.0 lib/ddtrace/transport/statistics.rb
ddtrace-0.42.0 lib/ddtrace/transport/statistics.rb
ddtrace-0.41.0 lib/ddtrace/transport/statistics.rb
ls-trace-0.2.0 lib/ddtrace/transport/statistics.rb
ddtrace-0.40.0 lib/ddtrace/transport/statistics.rb
ddtrace-0.39.0 lib/ddtrace/transport/statistics.rb
ddtrace-0.38.0 lib/ddtrace/transport/statistics.rb
ddtrace-0.37.0 lib/ddtrace/transport/statistics.rb
ddtrace-0.36.0 lib/ddtrace/transport/statistics.rb
ddtrace-0.35.2 lib/ddtrace/transport/statistics.rb
ddtrace-0.35.1 lib/ddtrace/transport/statistics.rb