Sha256: 0259cb9f39e1fb35b3500cc706932c605a1dfb49adf053036b0c8d3b9adde0da

Contents?: true

Size: 1.19 KB

Versions: 9

Compression:

Stored size: 1.19 KB

Contents

# 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
        # Middleware to trace Sinatra requests
        class TracerMiddleware
          def initialize(app)
            @app = app
          end

          def call(env)
            response = @app.call(env)
          ensure
            trace_response(env, response)
          end

          def trace_response(env, response)
            span = OpenTelemetry::Instrumentation::Rack.current_span
            return unless span.recording?

            span.set_attribute('http.route', env['sinatra.route'].split.last) if env['sinatra.route']
            span.name = env['sinatra.route'] if env['sinatra.route']

            return if response.nil?

            sinatra_response = ::Sinatra::Response.new([], response.first)
            return unless sinatra_response.server_error?

            span.record_exception(env['sinatra.error']) if env['sinatra.error']
            span.status = OpenTelemetry::Trace::Status.error
          end
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
opentelemetry-instrumentation-sinatra-0.24.1 lib/opentelemetry/instrumentation/sinatra/middlewares/tracer_middleware.rb
opentelemetry-instrumentation-sinatra-0.24.0 lib/opentelemetry/instrumentation/sinatra/middlewares/tracer_middleware.rb
opentelemetry-instrumentation-sinatra-0.23.5 lib/opentelemetry/instrumentation/sinatra/middlewares/tracer_middleware.rb
opentelemetry-instrumentation-sinatra-0.23.3 lib/opentelemetry/instrumentation/sinatra/middlewares/tracer_middleware.rb
opentelemetry-instrumentation-sinatra-0.23.2 lib/opentelemetry/instrumentation/sinatra/middlewares/tracer_middleware.rb
opentelemetry-instrumentation-sinatra-0.23.1 lib/opentelemetry/instrumentation/sinatra/middlewares/tracer_middleware.rb
opentelemetry-instrumentation-sinatra-0.23.0 lib/opentelemetry/instrumentation/sinatra/middlewares/tracer_middleware.rb
opentelemetry-instrumentation-sinatra-0.22.0 lib/opentelemetry/instrumentation/sinatra/middlewares/tracer_middleware.rb
opentelemetry-instrumentation-sinatra-0.21.5 lib/opentelemetry/instrumentation/sinatra/middlewares/tracer_middleware.rb