Class: OpenTelemetry::Instrumentation::Rack::Middlewares::TracerMiddleware
- Inherits:
-
Object
- Object
- OpenTelemetry::Instrumentation::Rack::Middlewares::TracerMiddleware
- Defined in:
- lib/opentelemetry/instrumentation/rack/middlewares/tracer_middleware.rb
Overview
TracerMiddleware propagates context and instruments Rack requests by way of its middleware system
Constant Summary collapse
- EMPTY_HASH =
{}.freeze
Class Method Summary collapse
- .allowed_rack_request_headers ⇒ Object
- .allowed_response_headers ⇒ Object
- .build_attribute_name(prefix, suffix) ⇒ Object
- .config ⇒ Object
Instance Method Summary collapse
-
#call(env) ⇒ Object
rubocop:disable Metrics/AbcSize.
-
#initialize(app) ⇒ TracerMiddleware
constructor
A new instance of TracerMiddleware.
Constructor Details
#initialize(app) ⇒ TracerMiddleware
Returns a new instance of TracerMiddleware.
50 51 52 |
# File 'lib/opentelemetry/instrumentation/rack/middlewares/tracer_middleware.rb', line 50 def initialize(app) @app = app end |
Class Method Details
.allowed_rack_request_headers ⇒ Object
19 20 21 22 23 |
# File 'lib/opentelemetry/instrumentation/rack/middlewares/tracer_middleware.rb', line 19 def allowed_rack_request_headers @allowed_rack_request_headers ||= Array(config[:allowed_request_headers]).each_with_object({}) do |header, memo| memo["HTTP_#{header.to_s.upcase.gsub(/[-\s]/, '_')}"] = build_attribute_name('http.request.headers.', header) end end |
.allowed_response_headers ⇒ Object
25 26 27 28 29 30 |
# File 'lib/opentelemetry/instrumentation/rack/middlewares/tracer_middleware.rb', line 25 def allowed_response_headers @allowed_response_headers ||= Array(config[:allowed_response_headers]).each_with_object({}) do |header, memo| memo[header] = build_attribute_name('http.response.headers.', header) memo[header.to_s.upcase] = build_attribute_name('http.response.headers.', header) end end |
.build_attribute_name(prefix, suffix) ⇒ Object
32 33 34 |
# File 'lib/opentelemetry/instrumentation/rack/middlewares/tracer_middleware.rb', line 32 def build_attribute_name(prefix, suffix) prefix + suffix.to_s.downcase.gsub(/[-\s]/, '_') end |
.config ⇒ Object
36 37 38 |
# File 'lib/opentelemetry/instrumentation/rack/middlewares/tracer_middleware.rb', line 36 def config Rack::Instrumentation.instance.config end |
Instance Method Details
#call(env) ⇒ Object
rubocop:disable Metrics/AbcSize
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/opentelemetry/instrumentation/rack/middlewares/tracer_middleware.rb', line 54 def call(env) # rubocop:disable Metrics/AbcSize original_env = env.dup extracted_context = OpenTelemetry.propagation.http.extract(env) frontend_context = create_frontend_span(env, extracted_context) # restore extracted context in this process: OpenTelemetry::Context.with_current(frontend_context || extracted_context) do request_span_name = create_request_span_name(env['REQUEST_URI'] || original_env['PATH_INFO']) request_span_kind = frontend_context.nil? ? :server : :internal tracer.in_span(request_span_name, attributes: request_span_attributes(env: env), kind: request_span_kind) do |request_span| OpenTelemetry::Instrumentation::Rack.with_span(request_span) do @app.call(env).tap do |status, headers, response| set_attributes_after_request(request_span, status, headers, response) end end end end ensure finish_span(frontend_context) end |