Sha256: 7a33ce50c44d3099d5243f5897ddad1e5bcfcacc089b48fb57f1ff7a3ccd736e

Contents?: true

Size: 1.27 KB

Versions: 30

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

require "opentracing"
require "active_support/all"
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

30 entries across 30 versions & 1 rubygems

Version Path
gitlab-labkit-0.16.2 lib/labkit/tracing/rack_middleware.rb
gitlab-labkit-0.16.1 lib/labkit/tracing/rack_middleware.rb
gitlab-labkit-0.16.0 lib/labkit/tracing/rack_middleware.rb
gitlab-labkit-0.15.0 lib/labkit/tracing/rack_middleware.rb
gitlab-labkit-0.14.0 lib/labkit/tracing/rack_middleware.rb
gitlab-labkit-0.13.5 lib/labkit/tracing/rack_middleware.rb
gitlab-labkit-0.13.4 lib/labkit/tracing/rack_middleware.rb
gitlab-labkit-0.13.3 lib/labkit/tracing/rack_middleware.rb
gitlab-labkit-0.13.2 lib/labkit/tracing/rack_middleware.rb
gitlab-labkit-0.13.1 lib/labkit/tracing/rack_middleware.rb
gitlab-labkit-0.13.0 lib/labkit/tracing/rack_middleware.rb
gitlab-labkit-0.12.2 lib/labkit/tracing/rack_middleware.rb
gitlab-labkit-0.12.1 lib/labkit/tracing/rack_middleware.rb
gitlab-labkit-0.12.0 lib/labkit/tracing/rack_middleware.rb
gitlab-labkit-0.11.0 lib/labkit/tracing/rack_middleware.rb
gitlab-labkit-0.10.1 lib/labkit/tracing/rack_middleware.rb
gitlab-labkit-0.10.0 lib/labkit/tracing/rack_middleware.rb
gitlab-labkit-0.9.1 lib/labkit/tracing/rack_middleware.rb
gitlab-labkit-0.9.0 lib/labkit/tracing/rack_middleware.rb
gitlab-labkit-0.8.0 lib/labkit/tracing/rack_middleware.rb