Class: OpenTelemetry::Instrumentation::Rack::Middlewares::Stable::TracerMiddleware
- Inherits:
-
Object
- Object
- OpenTelemetry::Instrumentation::Rack::Middlewares::Stable::TracerMiddleware
- Defined in:
- lib/opentelemetry/instrumentation/rack/middlewares/stable/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.
55 56 57 58 |
# File 'lib/opentelemetry/instrumentation/rack/middlewares/stable/tracer_middleware.rb', line 55 def initialize(app) @app = app @untraced_endpoints = config[:untraced_endpoints] end |
Class Method Details
.allowed_rack_request_headers ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/opentelemetry/instrumentation/rack/middlewares/stable/tracer_middleware.rb', line 18 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
30 31 32 33 34 35 |
# File 'lib/opentelemetry/instrumentation/rack/middlewares/stable/tracer_middleware.rb', line 30 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
37 38 39 |
# File 'lib/opentelemetry/instrumentation/rack/middlewares/stable/tracer_middleware.rb', line 37 def build_attribute_name(prefix, suffix) prefix + suffix.to_s.downcase.gsub(/[-\s]/, '_') end |
.config ⇒ Object
41 42 43 |
# File 'lib/opentelemetry/instrumentation/rack/middlewares/stable/tracer_middleware.rb', line 41 def config Rack::Instrumentation.instance.config end |
Instance Method Details
#call(env) ⇒ Object
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 91 92 93 |
# File 'lib/opentelemetry/instrumentation/rack/middlewares/stable/tracer_middleware.rb', line 60 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| request_start_time = OpenTelemetry::Instrumentation::Rack::Util::QueueTime.get_request_start(env) request_span.add_event('http.proxy.request.started', timestamp: request_start_time) unless request_start_time.nil? 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 |