module Datadog::Contrib::Sinatra::Tracer

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

Public Class Methods

registered(app) click to toggle source
# 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

Public Instance Methods

route(verb, action, *) click to toggle source
Calls superclass method
# 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