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, opt = {}) ⇒ TracerMiddleware

Returns a new instance of TracerMiddleware.



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

def initialize(app, opt = {})
  @app = app
  @app_instance = opt[:app_instance]
end

Instance Method Details

#call(env) ⇒ Object

rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity



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
80
81
82
83
84
85
86
# File 'lib/ddtrace/contrib/sinatra/tracer_middleware.rb', line 21

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,
    # this is kept nil until we set a correct one (either in the route or with a fallback in the ensure below)
    # the nil signals that there's no good one yet and is also seen by profiler, when sampling the resource
    resource: nil,
  ) 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)
      span.set_tag(Ext::TAG_SCRIPT_NAME, request.script_name) if request.script_name && !request.script_name.empty?

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

      # If this app handled the request, then Contrib::Sinatra::Tracer OR Contrib::Sinatra::Base set the
      # resource; if no resource was set, let's use a fallback
      span.resource = env['REQUEST_METHOD'] if span.resource.nil?

      # 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