Sha256: 208e978316b70b460d170c92c83ae1409fbac37f88eee12048c531df2077e02d

Contents?: true

Size: 1.25 KB

Versions: 27

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

require "opentracing"
require "action_dispatch"

module Labkit
  module Tracing
    # RackMiddleware is a rack middleware component for
    # instrumenting incoming http requests into a Rails/Rack
    # server
    class RackMiddleware
      REQUEST_METHOD = "REQUEST_METHOD"

      def initialize(app)
        @app = app
      end

      def call(env)
        method = env[REQUEST_METHOD]

        context = TracingUtils.tracer.extract(OpenTracing::FORMAT_RACK, env)
        tags = { "component" => "rack", "span.kind" => "server", "http.method" => method, "http.url" => self.class.build_sanitized_url_from_env(env) }

        TracingUtils.with_tracing(operation_name: "http:#{method}", child_of: context, tags: tags) do |span|
          @app.call(env).tap { |status_code, _headers, _body| span.set_tag("http.status_code", status_code) }
        end
      end

      # Generate a sanitized (safe) request URL from the rack environment
      def self.build_sanitized_url_from_env(env)
        request = ::ActionDispatch::Request.new(env)

        original_url = request.original_url
        uri = URI.parse(original_url)
        uri.query = request.filtered_parameters.to_query if uri.query.present?

        uri.to_s
      end
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
gitlab-labkit-0.36.1 lib/labkit/tracing/rack_middleware.rb
gitlab-labkit-0.36.0 lib/labkit/tracing/rack_middleware.rb
gitlab-labkit-0.35.1 lib/labkit/tracing/rack_middleware.rb
gitlab-labkit-0.35.0 lib/labkit/tracing/rack_middleware.rb
gitlab-labkit-0.34.0 lib/labkit/tracing/rack_middleware.rb
gitlab-labkit-0.33.0 lib/labkit/tracing/rack_middleware.rb
gitlab-labkit-0.32.0 lib/labkit/tracing/rack_middleware.rb
gitlab-labkit-0.31.1 lib/labkit/tracing/rack_middleware.rb
gitlab-labkit-0.31.0 lib/labkit/tracing/rack_middleware.rb
gitlab-labkit-0.30.1 lib/labkit/tracing/rack_middleware.rb
gitlab-labkit-0.30.0 lib/labkit/tracing/rack_middleware.rb
gitlab-labkit-0.29.0 lib/labkit/tracing/rack_middleware.rb
gitlab-labkit-0.28.0 lib/labkit/tracing/rack_middleware.rb
gitlab-labkit-0.27.0 lib/labkit/tracing/rack_middleware.rb
gitlab-labkit-0.26.0 lib/labkit/tracing/rack_middleware.rb
gitlab-labkit-0.25.0 lib/labkit/tracing/rack_middleware.rb
gitlab-labkit-0.24.0 lib/labkit/tracing/rack_middleware.rb
gitlab-labkit-0.23.0 lib/labkit/tracing/rack_middleware.rb
gitlab-labkit-0.22.0 lib/labkit/tracing/rack_middleware.rb
gitlab-labkit-0.21.3 lib/labkit/tracing/rack_middleware.rb