Class: Labkit::Tracing::RackMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/labkit/tracing/rack_middleware.rb

Overview

RackMiddleware is a rack middleware component for instrumenting incoming http requests into a Rails/Rack server

Constant Summary collapse

REQUEST_METHOD =
"REQUEST_METHOD"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ RackMiddleware

Returns a new instance of RackMiddleware.



14
15
16
# File 'lib/labkit/tracing/rack_middleware.rb', line 14

def initialize(app)
  @app = app
end

Class Method Details

.build_sanitized_url_from_env(env) ⇒ Object

Generate a sanitized (safe) request URL from the rack environment



30
31
32
33
34
35
36
37
38
# File 'lib/labkit/tracing/rack_middleware.rb', line 30

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

Instance Method Details

#call(env) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/labkit/tracing/rack_middleware.rb', line 18

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