Sha256: 77470ce5a241dacdda2b04b535a43cdd594350593597805174919fed7fae2d1d

Contents?: true

Size: 1.1 KB

Versions: 2

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: true

require 'rack'

module OpenTracing
  module Instrumentation
    module Rack
      # UrlCommandNameBuilder build command name with request url
      class UrlCommandNameBuilder
        DEFAULT_COMMAND_PATTERN = \
          'rack(%<method>s %<schema>s://%<host>s:%<port>d%<path>s)'
        DEFAULT_PORT = 80

        def initialize(
          command_pattern: DEFAULT_COMMAND_PATTERN,
          path_sanitazer: RegexpPathSanitazer.new
        )
          @command_pattern = command_pattern
          @path_sanitazer = path_sanitazer
        end

        def build_command_name(env)
          format(@command_pattern, pattern_args(env))
        end

        private

        def pattern_args(env)
          path = env[::Rack::REQUEST_PATH]
          sanitazed_path = @path_sanitazer.sanitaze_path(path)
          {
            schema: env[::Rack::RACK_URL_SCHEME],
            method: env[::Rack::REQUEST_METHOD],
            host: env[::Rack::HTTP_HOST],
            port: env[::Rack::HTTP_PORT] || DEFAULT_PORT,
            path: sanitazed_path,
          }
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
opentracing-instrumentation-0.1.3 lib/opentracing/instrumentation/rack/url_command_name_builder.rb
opentracing-instrumentation-0.1.2 lib/opentracing/instrumentation/rack/url_command_name_builder.rb