Sha256: c9d8fc075383998357eaaaeff8d7044e51614e2997aa9ee45ebce924448a5030

Contents?: true

Size: 1.75 KB

Versions: 16

Compression:

Stored size: 1.75 KB

Contents

require 'ddtrace/contrib/sinatra/env'
require 'ddtrace/contrib/sinatra/headers'

module Datadog
  module Contrib
    module Sinatra
      # Middleware used for automatically tagging configured headers and handle request span
      class TracerMiddleware
        REQUEST_TRACE_NAME = 'sinatra.request'.freeze

        def initialize(app)
          @app = app
        end

        def call(env)
          # Set the trace context (e.g. distributed tracing)
          if configuration[:distributed_tracing] && tracer.provider.context.trace_id.nil?
            context = HTTPPropagator.extract(env)
            tracer.provider.context = context if context.trace_id
          end

          # Begin the trace
          tracer.trace(
            REQUEST_TRACE_NAME,
            service: configuration[:service_name],
            span_type: Datadog::Ext::HTTP::TYPE
          ) do |span|
            Sinatra::Env.set_datadog_span(env, span)

            Sinatra::Env.request_header_tags(env, configuration[:headers][:request]).each do |name, value|
              span.set_tag(name, value) if span.get_tag(name).nil?
            end

            # Run application stack
            status, headers, response_body = @app.call(env)

            Sinatra::Headers.response_header_tags(headers, configuration[:headers][:response]).each do |name, value|
              span.set_tag(name, value) if span.get_tag(name).nil?
            end

            [status, headers, response_body]
          end
        end

        private

        def tracer
          configuration[:tracer]
        end

        def configuration
          Datadog.configuration[:sinatra]
        end

        def header_to_rack_header(name)
          "HTTP_#{name.to_s.upcase.gsub(/[-\s]/, '_')}"
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
ddtrace-0.16.1 lib/ddtrace/contrib/sinatra/tracer_middleware.rb
ddtrace-0.15.0.internaltracinfeature1 lib/ddtrace/contrib/sinatra/tracer_middleware.rb
ddtrace-0.16.0 lib/ddtrace/contrib/sinatra/tracer_middleware.rb
ddtrace-0.14.2.disableprotocolversion4 lib/ddtrace/contrib/sinatra/tracer_middleware.rb
ddtrace-0.15.0 lib/ddtrace/contrib/sinatra/tracer_middleware.rb
ddtrace-0.14.2.withoutpriorityparsing1 lib/ddtrace/contrib/sinatra/tracer_middleware.rb
ddtrace-0.14.2 lib/ddtrace/contrib/sinatra/tracer_middleware.rb
ddtrace-0.14.1 lib/ddtrace/contrib/sinatra/tracer_middleware.rb
ddtrace-0.15.0.beta1 lib/ddtrace/contrib/sinatra/tracer_middleware.rb
ddtrace-0.14.0 lib/ddtrace/contrib/sinatra/tracer_middleware.rb
ddtrace-0.14.0.rc1 lib/ddtrace/contrib/sinatra/tracer_middleware.rb
ddtrace-0.13.2 lib/ddtrace/contrib/sinatra/tracer_middleware.rb
ddtrace-0.14.0.beta2 lib/ddtrace/contrib/sinatra/tracer_middleware.rb
ddtrace-0.14.0.beta1 lib/ddtrace/contrib/sinatra/tracer_middleware.rb
ddtrace-0.13.1 lib/ddtrace/contrib/sinatra/tracer_middleware.rb
ddtrace-0.13.0 lib/ddtrace/contrib/sinatra/tracer_middleware.rb