Sha256: e00aab100db7adcd517399a0cc0ccd94c415ecadb98ce16b04ffaf244743f8e4

Contents?: true

Size: 1.9 KB

Versions: 19

Compression:

Stored size: 1.9 KB

Contents

# frozen_string_literal: true

require 'rack'

module OpenTracing
  module Instrumentation
    module Rack
      # HttpTagger create addition tags on span
      class HttpTagger
        DEFAULT_TAG_REQUEST_HEADERS = {
          connection: 'Connection',
          content_type: 'Content-Type',
          user_agent: 'User-Agent',
        }.freeze

        DEFAULT_TAG_RESPONSE_HEADERS = {
          connection: 'Connection',
          content_type: 'Content-Type',
          keep_alive: 'Keep-Alive',
        }.freeze

        def initialize(
          tag_request_headers: DEFAULT_TAG_REQUEST_HEADERS,
          tag_response_headers: DEFAULT_TAG_RESPONSE_HEADERS
        )
          @tag_request_headers =
            prepare_request_mapping(tag_request_headers)
          @tag_response_headers =
            prepare_response_mapping(tag_response_headers)
        end

        def request_tags(env)
          @tag_request_headers
            .transform_values { |header_name| env[header_name] }
            .compact
        end

        def response_tags(headers)
          header_mapper = lambda do |header_regexp|
            headers.find do |(header, _value)|
              header_regexp.match?(header)
            end&.dig(1)
          end
          @tag_response_headers
            .transform_values(&header_mapper)
            .compact
        end

        private

        def prepare_request_mapping(source_mapping)
          source_mapping.map do |key, header|
            rack_header = 'HTTP_' + header.tr('-', '_').upcase
            tag_name = 'http.request.' + key.to_s
            [tag_name, rack_header]
          end.to_h
        end

        def prepare_response_mapping(source_mapping)
          source_mapping.map do |key, header|
            tag_name = 'http.response.' + key.to_s
            header_regexp = /^#{header}$/i
            [tag_name, header_regexp]
          end.to_h
        end
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
opentracing-instrumentation-0.1.18 lib/opentracing/instrumentation/rack/http_tagger.rb
opentracing-instrumentation-0.1.17 lib/opentracing/instrumentation/rack/http_tagger.rb
opentracing-instrumentation-0.1.16 lib/opentracing/instrumentation/rack/http_tagger.rb
opentracing-instrumentation-0.1.15 lib/opentracing/instrumentation/rack/http_tagger.rb
opentracing-instrumentation-0.1.14 lib/opentracing/instrumentation/rack/http_tagger.rb
opentracing-instrumentation-0.1.13 lib/opentracing/instrumentation/rack/http_tagger.rb
opentracing-instrumentation-0.1.12 lib/opentracing/instrumentation/rack/http_tagger.rb
opentracing-instrumentation-0.1.11 lib/opentracing/instrumentation/rack/http_tagger.rb
opentracing-instrumentation-0.1.10 lib/opentracing/instrumentation/rack/http_tagger.rb
opentracing-instrumentation-0.1.9 lib/opentracing/instrumentation/rack/http_tagger.rb
opentracing-instrumentation-0.1.8 lib/opentracing/instrumentation/rack/http_tagger.rb
opentracing-instrumentation-0.1.7 lib/opentracing/instrumentation/rack/http_tagger.rb
opentracing-instrumentation-0.1.6 lib/opentracing/instrumentation/rack/http_tagger.rb
opentracing-instrumentation-0.1.5 lib/opentracing/instrumentation/rack/http_tagger.rb
opentracing-instrumentation-0.1.4 lib/opentracing/instrumentation/rack/http_tagger.rb
opentracing-instrumentation-0.1.3 lib/opentracing/instrumentation/rack/http_tagger.rb
opentracing-instrumentation-0.1.2 lib/opentracing/instrumentation/rack/http_tagger.rb
opentracing-instrumentation-0.1.1 lib/opentracing/instrumentation/rack/http_tagger.rb
opentracing-instrumentation-0.1.0 lib/opentracing/instrumentation/rack/http_tagger.rb