Sha256: 67c65c7a735cfe24c50e96dbf87425c83d49193c89b850c0aa6d3a12b65e1df6
Contents?: true
Size: 1.65 KB
Versions: 4
Compression:
Stored size: 1.65 KB
Contents
# frozen_string_literal: true # Copyright The OpenTelemetry Authors # # SPDX-License-Identifier: Apache-2.0 module OpenTelemetry module Instrumentation module Sinatra module Middlewares # Middleware to trace Sinatra requests class TracerMiddleware def initialize(app) @app = app end def call(env) tracer.in_span( env['PATH_INFO'], attributes: { 'http.method' => env['REQUEST_METHOD'], 'http.url' => env['PATH_INFO'] }, kind: :server, with_parent: parent_context(env) ) do |span| app.call(env).tap { |resp| trace_response(span, env, resp) } end end private attr_reader :app def parent_context(env) OpenTelemetry.propagation.extract( env, getter: OpenTelemetry::Context::Propagation.rack_env_getter ) end def tracer OpenTelemetry::Instrumentation::Sinatra::Instrumentation.instance.tracer end def trace_response(span, env, resp) status, _headers, _response_body = resp span.set_attribute('http.status_code', status) span.set_attribute('http.status_text', ::Rack::Utils::HTTP_STATUS_CODES[status]) span.set_attribute('http.route', env['sinatra.route'].split.last) if env['sinatra.route'] span.name = env['sinatra.route'] if env['sinatra.route'] span.status = OpenTelemetry::Trace::Status.http_to_status(status) end end end end end end
Version data entries
4 entries across 4 versions & 1 rubygems