Sha256: f4b5dc9539d88b39db5921e22df0307e53a6aca2007994e83676b7e8494d78e6

Contents?: true

Size: 1.53 KB

Versions: 21

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true

require 'sinatra/base'

module OpenTracing
  module Instrumentation
    module Sinatra
      # TraceMiddleware for sinatra
      #
      # Usage
      #   class MyApp < Sinatra::Base
      #     use OpenTracing::Instrumentation::Sinatra::TraceMiddleware
      #     set :tracer, MyTracer.instance # optionaly
      #   end
      module TraceMiddleware
        def self.registered(app)
          app.helpers self

          app.set :tracer, OpenTracing.global_tracer
          app.set :command_name, 'sinatra_request'

          app.before do
            start_span
          end

          app.after do
            close_span
          end
        end

        private

        def start_span
          @scope = settings.tracer.start_active_span(
            settings.command_name,
            tags: build_base_tags,
          )
        end

        def close_span
          set_response_span_tags(@scope.span, env)
          @scope.close
        end

        def build_base_tags
          {
            'sinatra.app' => self.class.to_s,
            'sinatra.environment' => settings.environment,
            'sinatra.threaded' => settings.threaded,
            'sinatra.show_exceptions' => settings.show_exceptions,
          }
        end

        def set_response_span_tags(span, env)
          sinatra_route = env['sinatra.route']
          span.set_tag 'sinatra.route', sinatra_route

          sinatra_error = env['sinatra.error']
          span.set_tag('error', true) if sinatra_error
        end
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
opentracing-instrumentation-0.2.1 lib/opentracing/instrumentation/sinatra/trace_middleware.rb
opentracing-instrumentation-0.2.0 lib/opentracing/instrumentation/sinatra/trace_middleware.rb
opentracing-instrumentation-0.1.18 lib/opentracing/instrumentation/sinatra/trace_middleware.rb
opentracing-instrumentation-0.1.17 lib/opentracing/instrumentation/sinatra/trace_middleware.rb
opentracing-instrumentation-0.1.16 lib/opentracing/instrumentation/sinatra/trace_middleware.rb
opentracing-instrumentation-0.1.15 lib/opentracing/instrumentation/sinatra/trace_middleware.rb
opentracing-instrumentation-0.1.14 lib/opentracing/instrumentation/sinatra/trace_middleware.rb
opentracing-instrumentation-0.1.13 lib/opentracing/instrumentation/sinatra/trace_middleware.rb
opentracing-instrumentation-0.1.12 lib/opentracing/instrumentation/sinatra/trace_middleware.rb
opentracing-instrumentation-0.1.11 lib/opentracing/instrumentation/sinatra/trace_middleware.rb
opentracing-instrumentation-0.1.10 lib/opentracing/instrumentation/sinatra/trace_middleware.rb
opentracing-instrumentation-0.1.9 lib/opentracing/instrumentation/sinatra/trace_middleware.rb
opentracing-instrumentation-0.1.8 lib/opentracing/instrumentation/sinatra/trace_middleware.rb
opentracing-instrumentation-0.1.7 lib/opentracing/instrumentation/sinatra/trace_middleware.rb
opentracing-instrumentation-0.1.6 lib/opentracing/instrumentation/sinatra/trace_middleware.rb
opentracing-instrumentation-0.1.5 lib/opentracing/instrumentation/sinatra/trace_middleware.rb
opentracing-instrumentation-0.1.4 lib/opentracing/instrumentation/sinatra/trace_middleware.rb
opentracing-instrumentation-0.1.3 lib/opentracing/instrumentation/sinatra/trace_middleware.rb
opentracing-instrumentation-0.1.2 lib/opentracing/instrumentation/sinatra/trace_middleware.rb
opentracing-instrumentation-0.1.1 lib/opentracing/instrumentation/sinatra/trace_middleware.rb