Sha256: ed76726d85eddb696a23b849ccca491cea2611e7c80198feeb4a4e2d7d3e6ed6

Contents?: true

Size: 1.66 KB

Versions: 8

Compression:

Stored size: 1.66 KB

Contents

module Routemaster
  module Middleware
    class Metrics
      INTERACTION_KEY = 'api_client'.freeze

      def initialize(app, options = {})
        @app    = app
        @client = options[:client]
        @source_peer = options[:source_peer]
      end

      def call(request_env)
        return @app.call(request_env) unless can_log?

        increment_req_count(request_tags(request_env))

        record_latency(request_tags(request_env)) do
          begin
            @app.call(request_env).on_complete do |response_env|
              increment_response_count(response_tags(response_env))
            end
          rescue Routemaster::Errors::BaseError => e
            increment_response_count(response_tags(e.env))
            raise e
          end
        end
      end

      private

      attr_reader :client, :source_peer

      def increment_req_count(tags)
        client.increment("#{INTERACTION_KEY}.request.count", tags: tags)
      end

      def increment_response_count(tags)
        client.increment("#{INTERACTION_KEY}.response.count", tags: tags)
      end

      def record_latency(tags, &block)
        client.time("#{INTERACTION_KEY}.latency", tags: tags) do
          block.call
        end
      end

      def can_log?
        client && source_peer
      end

      def destination_peer(env)
        env.url.host
      end

      def peers_tags(env)
        [
          "source:#{source_peer}",
          "destination:#{destination_peer(env)}"
        ]
      end

      def request_tags(env)
        peers_tags(env).concat(["verb:#{env.method}"])
      end

      def response_tags(env)
        peers_tags(env).concat(["status:#{env.status}"])
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
routemaster-drain-3.7.1 lib/routemaster/middleware/metrics.rb
routemaster-drain-3.7.0 lib/routemaster/middleware/metrics.rb
routemaster-drain-3.6.8 lib/routemaster/middleware/metrics.rb
routemaster-drain-3.6.7 lib/routemaster/middleware/metrics.rb
routemaster-drain-3.6.6 lib/routemaster/middleware/metrics.rb
routemaster-drain-3.6.5 lib/routemaster/middleware/metrics.rb
routemaster-drain-3.6.4 lib/routemaster/middleware/metrics.rb
routemaster-drain-3.6.3 lib/routemaster/middleware/metrics.rb