Sha256: 4f22a8828ed9e48210e1a6d3c2c431816d2dc14ed80081c31b2a65204fe6db0a

Contents?: true

Size: 1.15 KB

Versions: 27

Compression:

Stored size: 1.15 KB

Contents

# frozen_string_literal: true

module Salestation
  class Web < Module
    class StatsdMiddleware
      EXTRA_TAGS_ENV_KEY = 'salestation.statsd.tags'

      DURATION_MILLISECOND_PRECISION = 3

      def initialize(app, statsd, metric:)
        @app = app
        @statsd = statsd
        @metric = metric
      end

      def call(env)
        start = system_monotonic_time

        status, header, body = @app.call env

        method = env['REQUEST_METHOD']
        path =
          if route = env['sinatra.route']
            route.split(' ').last
          else
            'unknown-route'
          end

        tags = [
          "path:#{path}",
          "method:#{method}",
          "status:#{status}",
          "status_class:#{status / 100}xx"
        ] + env.fetch(EXTRA_TAGS_ENV_KEY, [])

        @statsd.timing(@metric, duration_ms(from: start), tags: tags)

        [status, header, body]
      end

      private

      def duration_ms(from:)
        ((system_monotonic_time - from) * 1000).round(DURATION_MILLISECOND_PRECISION)
      end

      def system_monotonic_time
        Process.clock_gettime(Process::CLOCK_MONOTONIC)
      end
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
salestation-5.3.3 lib/salestation/web/statsd_middleware.rb
salestation-5.3.2 lib/salestation/web/statsd_middleware.rb
salestation-5.3.1 lib/salestation/web/statsd_middleware.rb
salestation-5.3.0 lib/salestation/web/statsd_middleware.rb
salestation-5.2.0 lib/salestation/web/statsd_middleware.rb
salestation-5.1.0 lib/salestation/web/statsd_middleware.rb
salestation-5.0.2 lib/salestation/web/statsd_middleware.rb
salestation-5.0.1 lib/salestation/web/statsd_middleware.rb
salestation-5.0.0 lib/salestation/web/statsd_middleware.rb
salestation-4.6.1 lib/salestation/web/statsd_middleware.rb
salestation-4.6.0 lib/salestation/web/statsd_middleware.rb
salestation-4.5.0 lib/salestation/web/statsd_middleware.rb
salestation-4.4.2 lib/salestation/web/statsd_middleware.rb
salestation-4.4.1 lib/salestation/web/statsd_middleware.rb
salestation-4.4.0 lib/salestation/web/statsd_middleware.rb
salestation-4.3.1 lib/salestation/web/statsd_middleware.rb
salestation-4.3.0 lib/salestation/web/statsd_middleware.rb
salestation-4.2.0 lib/salestation/web/statsd_middleware.rb
salestation-4.1.0 lib/salestation/web/statsd_middleware.rb
salestation-4.0.3 lib/salestation/web/statsd_middleware.rb