lib/opentelemetry/instrumentation/sinatra/middlewares/tracer_middleware.rb in opentelemetry-instrumentation-sinatra-0.20.0 vs lib/opentelemetry/instrumentation/sinatra/middlewares/tracer_middleware.rb in opentelemetry-instrumentation-sinatra-0.21.0

- old
+ new

@@ -1,10 +1,11 @@ # frozen_string_literal: true # Copyright The OpenTelemetry Authors # # SPDX-License-Identifier: Apache-2.0 +require 'opentelemetry-instrumentation-rack' module OpenTelemetry module Instrumentation module Sinatra module Middlewares @@ -13,38 +14,26 @@ def initialize(app) @app = app end def call(env) - extracted_context = OpenTelemetry.propagation.extract( - env, - getter: OpenTelemetry::Common::Propagation.rack_env_getter - ) - OpenTelemetry::Context.with_current(extracted_context) do - tracer.in_span( - env['PATH_INFO'], - attributes: { 'http.method' => env['REQUEST_METHOD'], - 'http.url' => env['PATH_INFO'] }, - kind: :server - ) do |span| - @app.call(env).tap { |resp| trace_response(span, env, resp) } - end - end + response = @app.call(env) + ensure + trace_response(env, response) end - private + def trace_response(env, response) + span = OpenTelemetry::Instrumentation::Rack.current_span + return unless span.recording? - 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.route', env['sinatra.route'].split.last) if env['sinatra.route'] span.name = env['sinatra.route'] if env['sinatra.route'] - span.status = OpenTelemetry::Trace::Status.error unless (100..499).include?(status.to_i) + + sinatra_response = ::Sinatra::Response.new([], response.first) + return unless sinatra_response.server_error? + + span.record_exception(env['sinatra.error']) + span.status = OpenTelemetry::Trace::Status.error end end end end end