Skip to content

Commit

Permalink
Merge pull request #365 from DataDog/deprecation/rack_compliant_keys
Browse files Browse the repository at this point in the history
Use Rack compliant keys with deprecation
  • Loading branch information
delner authored Mar 28, 2018
2 parents 6e73481 + 7edd458 commit 62377c2
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/ddtrace/contrib/grape/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def self.endpoint_run(name, start, finish, id, payload)
span.resource = resource

# set the request span resource if it's a `rack.request` span
request_span = payload[:env][:datadog_rack_request_span]
request_span = payload[:env][Datadog::Contrib::Rack::TraceMiddleware::RACK_REQUEST_SPAN]
if !request_span.nil? && request_span.name == 'rack.request'
request_span.resource = resource
end
Expand Down
39 changes: 38 additions & 1 deletion lib/ddtrace/contrib/rack/middlewares.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ module Rack
# application. If request tags are not set by the app, they will be set using
# information available at the Rack level.
class TraceMiddleware
RACK_REQUEST_SPAN = 'datadog.rack_request_span'.freeze

def initialize(app)
@app = app
end
Expand All @@ -35,7 +37,14 @@ def call(env)
# start a new request span and attach it to the current Rack environment;
# we must ensure that the span `resource` is set later
request_span = tracer.trace('rack.request', trace_options)
env[:datadog_rack_request_span] = request_span
env[RACK_REQUEST_SPAN] = request_span

# TODO: For backwards compatibility; this attribute is deprecated.
# Will be removed in version 0.13.0.
env[:datadog_rack_request_span] = env[RACK_REQUEST_SPAN]

# Add deprecation warnings
add_deprecation_warnings(env)

# Copy the original env, before the rest of the stack executes.
# Values may change; we want values before that happens.
Expand Down Expand Up @@ -140,6 +149,34 @@ def get_request_id(headers, env)
headers ||= {}
headers['X-Request-Id'] || headers['X-Request-ID'] || env['HTTP_X_REQUEST_ID']
end

private

REQUEST_SPAN_DEPRECATION_WARNING = %(
:datadog_rack_request_span is considered an internal symbol in the Rack env,
and has been been DEPRECATED. Public support for its usage is discontinued.
If you need the Rack request span, try using `Datadog.tracer.active_span`.
This key will be removed in version 0.13.0).freeze

def add_deprecation_warnings(env)
env.instance_eval do
def [](key)
if key == :datadog_rack_request_span && !@request_span_warning_issued
Datadog::Tracer.log.warn(REQUEST_SPAN_DEPRECATION_WARNING)
@request_span_warning_issued = true
end
super
end

def []=(key, value)
if key == :datadog_rack_request_span && !@request_span_warning_issued
Datadog::Tracer.log.warn(REQUEST_SPAN_DEPRECATION_WARNING)
@request_span_warning_issued = true
end
super
end
end
end
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions test/contrib/rack/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def app
run(proc do |env|
# this should be considered a web framework that can alter
# the request span after routing / controller processing
request_span = env[:datadog_rack_request_span]
request_span = env[Datadog::Contrib::Rack::TraceMiddleware::RACK_REQUEST_SPAN]
request_span.resource = 'GET /app/'
request_span.set_tag('http.method', 'GET_V2')
request_span.set_tag('http.status_code', 201)
Expand All @@ -54,7 +54,7 @@ def app
run(proc do |env|
# this should be considered a web framework that can alter
# the request span after routing / controller processing
request_span = env[:datadog_rack_request_span]
request_span = env[Datadog::Contrib::Rack::TraceMiddleware::RACK_REQUEST_SPAN]
request_span.status = 1
request_span.set_tag('error.stack', 'Handled exception')

Expand All @@ -66,7 +66,7 @@ def app
run(proc do |env|
# this should be considered a web framework that can alter
# the request span after routing / controller processing
request_span = env[:datadog_rack_request_span]
request_span = env[Datadog::Contrib::Rack::TraceMiddleware::RACK_REQUEST_SPAN]
request_span.set_tag('error.stack', 'Handled exception')

[500, { 'Content-Type' => 'text/html' }, 'OK']
Expand Down

0 comments on commit 62377c2

Please sign in to comment.