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
-
#initialize(app) ⇒ TracerMiddleware
constructor
A new instance of TracerMiddleware.
Constructor Details
#initialize(app) ⇒ TracerMiddleware
Returns a new instance of TracerMiddleware.
54 55 56 57 |
# File 'lib/opentelemetry/instrumentation/rack/middlewares/tracer_middleware.rb', line 54 def initialize(app) @app = app @untraced_endpoints = config[:untraced_endpoints] end |
Class Method Details
.allowed_rack_request_headers ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/opentelemetry/instrumentation/rack/middlewares/tracer_middleware.rb', line 17 def allowed_rack_request_headers @allowed_rack_request_headers ||= Array(config[:allowed_request_headers]).each_with_object({}) do |header, memo| key = header.to_s.upcase.gsub(/[-\s]/, '_') case key when 'CONTENT_TYPE', 'CONTENT_LENGTH' memo[key] = build_attribute_name('http.request.header.', header) else memo["HTTP_#{key}"] = build_attribute_name('http.request.header.', header) end end end |
.allowed_response_headers ⇒ Object
29 30 31 32 33 34 |
# File 'lib/opentelemetry/instrumentation/rack/middlewares/tracer_middleware.rb', line 29 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.header.', header) memo[header.to_s.upcase] = build_attribute_name('http.response.header.', header) end end |
.build_attribute_name(prefix, suffix) ⇒ Object
36 37 38 |
# File 'lib/opentelemetry/instrumentation/rack/middlewares/tracer_middleware.rb', line 36 def build_attribute_name(prefix, suffix) prefix + suffix.to_s.downcase.gsub(/[-\s]/, '_') end |
.config ⇒ Object
40 41 42 |
# File 'lib/opentelemetry/instrumentation/rack/middlewares/tracer_middleware.rb', line 40 def config Rack::Instrumentation.instance.config end |
Instance Method Details
#call(env) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/opentelemetry/instrumentation/rack/middlewares/tracer_middleware.rb', line 59 def call(env) if untraced_request?(env) OpenTelemetry::Common::Utilities.untraced do return @app.call(env) end end original_env = env.dup extracted_context = OpenTelemetry.propagation.extract( env, getter: OpenTelemetry::Common::Propagation.rack_env_getter ) 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'], env) 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) config[:response_propagators].each { |propagator| propagator.inject(headers) } end end end end ensure finish_span(frontend_context) end |