Class: Datadog::Contrib::Sinatra::TracerMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/ddtrace/contrib/sinatra/tracer_middleware.rb

Overview

Middleware used for automatically tagging configured headers and handle request span

Instance Method Summary collapse

Constructor Details

#initialize(app, app_instance: nil) ⇒ TracerMiddleware

Returns a new instance of TracerMiddleware.



11
12
13
14
# File 'lib/ddtrace/contrib/sinatra/tracer_middleware.rb', line 11

def initialize(app, app_instance: nil)
  @app = app
  @app_instance = app_instance
end

Instance Method Details

#call(env) ⇒ Object

rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/ddtrace/contrib/sinatra/tracer_middleware.rb', line 18

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

  tracer.trace(
    Ext::SPAN_REQUEST,
    service: configuration[:service_name],
    span_type: Datadog::Ext::HTTP::TYPE_INBOUND,
    resource: env['REQUEST_METHOD']
  ) do |span|
    begin
      Sinatra::Env.set_datadog_span(env, @app_instance, span)

      response = @app.call(env)
    ensure
      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

      request = ::Sinatra::Request.new(env)
      span.set_tag(Datadog::Ext::HTTP::URL, request.path)
      span.set_tag(Datadog::Ext::HTTP::METHOD, request.request_method)
      if request.script_name && !request.script_name.empty?
        span.set_tag(Ext::TAG_SCRIPT_NAME, request.script_name)
      end

      span.set_tag(Ext::TAG_APP_NAME, @app_instance.settings.name)

      # TODO: This backfills the non-matching Sinatra app with a "#{method} #{path}"
      # TODO: resource name. This shouldn't be the case, as that app has never handled
      # TODO: the response with that resource.
      # TODO: We should replace this backfill code with a clear `resource` that signals
      # TODO: that this Sinatra span was *not* responsible for processing the current request.
      rack_request_span = env[Datadog::Contrib::Rack::TraceMiddleware::RACK_REQUEST_SPAN]
      span.resource = rack_request_span.resource if rack_request_span && rack_request_span.resource

      if response
        if (status = response[0])
          sinatra_response = ::Sinatra::Response.new([], status) # Build object to use status code helpers

          span.set_tag(Datadog::Ext::HTTP::STATUS_CODE, sinatra_response.status)
          span.set_error(env['sinatra.error']) if sinatra_response.server_error?
        end

        if (headers = response[1])
          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
        end
      end

      # Set analytics sample rate
      Contrib::Analytics.set_sample_rate(span, analytics_sample_rate) if analytics_enabled?

      # Measure service stats
      Contrib::Analytics.set_measured(span)
    end
  end
end