Sha256: 8db2b21033aca1d6aa9eda7a0aa7a39566cc474fa32462bada582711ecfbd9e1

Contents?: true

Size: 1.73 KB

Versions: 11

Compression:

Stored size: 1.73 KB

Contents

require 'ddtrace/contrib/sinatra/ext'
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
        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(
            Ext::SPAN_REQUEST,
            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

11 entries across 11 versions & 1 rubygems

Version Path
ddtrace-0.20.0 lib/ddtrace/contrib/sinatra/tracer_middleware.rb
ddtrace-0.19.1 lib/ddtrace/contrib/sinatra/tracer_middleware.rb
ddtrace-0.19.0 lib/ddtrace/contrib/sinatra/tracer_middleware.rb
ddtrace-0.18.3 lib/ddtrace/contrib/sinatra/tracer_middleware.rb
ddtrace-0.18.2 lib/ddtrace/contrib/sinatra/tracer_middleware.rb
ddtrace-0.18.1 lib/ddtrace/contrib/sinatra/tracer_middleware.rb
ddtrace-0.18.0 lib/ddtrace/contrib/sinatra/tracer_middleware.rb
ddtrace-0.17.3 lib/ddtrace/contrib/sinatra/tracer_middleware.rb
ddtrace-0.17.2 lib/ddtrace/contrib/sinatra/tracer_middleware.rb
ddtrace-0.17.1 lib/ddtrace/contrib/sinatra/tracer_middleware.rb
ddtrace-0.17.0 lib/ddtrace/contrib/sinatra/tracer_middleware.rb