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, Metrics/MethodLength.
-
#initialize(app) ⇒ TracerMiddleware
constructor
A new instance of TracerMiddleware.
Constructor Details
#initialize(app) ⇒ TracerMiddleware
Returns a new instance of TracerMiddleware.
56 57 58 59 |
# File 'lib/opentelemetry/instrumentation/rack/middlewares/tracer_middleware.rb', line 56 def initialize(app) @app = app @untraced_endpoints = config[:untraced_endpoints] end |
Class Method Details
.allowed_rack_request_headers ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 |
# 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| key = header.to_s.upcase.gsub(/[-\s]/, '_') case key when 'CONTENT_TYPE', 'CONTENT_LENGTH' memo[key] = build_attribute_name('http.request.headers.', header) else memo["HTTP_#{key}"] = build_attribute_name('http.request.headers.', header) end end end |
.allowed_response_headers ⇒ Object
31 32 33 34 35 36 |
# File 'lib/opentelemetry/instrumentation/rack/middlewares/tracer_middleware.rb', line 31 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
38 39 40 |
# File 'lib/opentelemetry/instrumentation/rack/middlewares/tracer_middleware.rb', line 38 def build_attribute_name(prefix, suffix) prefix + suffix.to_s.downcase.gsub(/[-\s]/, '_') end |
.config ⇒ Object
42 43 44 |
# File 'lib/opentelemetry/instrumentation/rack/middlewares/tracer_middleware.rb', line 42 def config Rack::Instrumentation.instance.config end |
Instance Method Details
#call(env) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength
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 |
# File 'lib/opentelemetry/instrumentation/rack/middlewares/tracer_middleware.rb', line 61 def call(env) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength 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) end end end end ensure finish_span(frontend_context) end |