Module: Datadog::Contrib::Sinatra::Tracer

Defined in:
lib/ddtrace/contrib/sinatra/tracer.rb

Overview

Datadog::Contrib::Sinatra::Tracer is a Sinatra extension which traces requests.

Defined Under Namespace

Modules: Base

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.registered(app) ⇒ Object


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
# File 'lib/ddtrace/contrib/sinatra/tracer.rb', line 38

def self.registered(app)
  app.use TracerMiddleware, app_instance: app

  app.after do
    configuration = Datadog.configuration[:sinatra]
    next unless configuration[:tracer].enabled

    span = Sinatra::Env.datadog_span(env, app)

    # TODO: `route` should *only* be populated if @datadog_route is defined.
    # TODO: If @datadog_route is not defined, then this Sinatra app is not responsible
    # TODO: for handling this request.
    # TODO:
    # TODO: This change would be BREAKING for any Sinatra app (classic or modular),
    # TODO: as it affects the `resource` value for requests not handled by the Sinatra app.
    # TODO: Currently we use "#{method} #{path}" in such aces, but `path` is the raw,
    # TODO: high-cardinality HTTP path, and can contain PII.
    # TODO:
    # TODO: The value we should use as the `resource` when the Sinatra app is not
    # TODO: responsible for the request is a tricky subject.
    # TODO: The best option is a value that clearly communicates that this app did not
    # TODO: handle this request. It's important to keep in mind that an unhandled request
    # TODO: by this Sinatra app might still be handled by another Rack middleware (which can
    # TODO: be a Sinatra app itself) or it might just 404 if not handled at all.
    # TODO:
    # TODO: A possible value for `resource` could set a high level description, e.g.
    # TODO: `request.request_method`, given we don't have the response object available yet.
    route = if defined?(@datadog_route)
              @datadog_route
            else
              # Fallback in case no routes have matched
              request.path
            end

    span.resource = "#{request.request_method} #{route}"
    span.set_tag(Ext::TAG_ROUTE_PATH, route)
  end
end

Instance Method Details

#route(verb, action) ⇒ Object


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ddtrace/contrib/sinatra/tracer.rb', line 18

def route(verb, action, *)
  # Keep track of the route name when the app is instantiated for an
  # incoming request.
  condition do
    # If the option to prepend script names is enabled, then
    # prepend the script name from the request onto the action.
    #
    # DEV: env['sinatra.route'] already exists with very similar information,
    # DEV: but doesn't account for our `resource_script_names` logic.
    #
    @datadog_route = if Datadog.configuration[:sinatra][:resource_script_names]
                       "#{request.script_name}#{action}"
                     else
                       action
                     end
  end

  super
end