-
Notifications
You must be signed in to change notification settings - Fork 375
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add http.base_url tag to Rack #327
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,16 +20,14 @@ def initialize(app) | |
def call(env) | ||
# retrieve integration settings | ||
tracer = Datadog.configuration[:rack][:tracer] | ||
service = Datadog.configuration[:rack][:service_name] | ||
distributed_tracing = Datadog.configuration[:rack][:distributed_tracing] | ||
|
||
trace_options = { | ||
service: service, | ||
service: Datadog.configuration[:rack][:service_name], | ||
resource: nil, | ||
span_type: Datadog::Ext::HTTP::TYPE | ||
} | ||
|
||
if distributed_tracing | ||
if Datadog.configuration[:rack][:distributed_tracing] | ||
context = HTTPPropagator.extract(env) | ||
tracer.provider.context = context if context.trace_id | ||
end | ||
|
@@ -56,35 +54,13 @@ def call(env) | |
request_span.set_error(e) unless request_span.nil? | ||
raise e | ||
ensure | ||
# the source of truth in Rack is the PATH_INFO key that holds the | ||
# URL for the current request; some framework may override that | ||
# value, especially during exception handling and because of that | ||
# we prefer using the `REQUEST_URI` if this is available. | ||
# NOTE: `REQUEST_URI` is Rails specific and may not apply for other frameworks | ||
url = env['REQUEST_URI'] || env['PATH_INFO'] | ||
|
||
# Rack is a really low level interface and it doesn't provide any | ||
# advanced functionality like routers. Because of that, we assume that | ||
# the underlying framework or application has more knowledge about | ||
# the result for this request; `resource` and `tags` are expected to | ||
# be set in another level but if they're missing, reasonable defaults | ||
# are used. | ||
request_span.resource ||= resource_name_for(env, status) | ||
if request_span.get_tag(Datadog::Ext::HTTP::METHOD).nil? | ||
request_span.set_tag(Datadog::Ext::HTTP::METHOD, env['REQUEST_METHOD']) | ||
end | ||
if request_span.get_tag(Datadog::Ext::HTTP::URL).nil? | ||
request_span.set_tag(Datadog::Ext::HTTP::URL, url) | ||
end | ||
if request_span.get_tag(Datadog::Ext::HTTP::STATUS_CODE).nil? && status | ||
request_span.set_tag(Datadog::Ext::HTTP::STATUS_CODE, status) | ||
end | ||
|
||
# detect if the status code is a 5xx and flag the request span as an error | ||
# unless it has been already set by the underlying framework | ||
if status.to_s.start_with?('5') && request_span.status.zero? | ||
request_span.status = 1 | ||
end | ||
set_request_tags!(request_span, env, status, headers, response) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to reduce the method Abc complexity, I've moved these actions in a separated method that updates in place |
||
|
||
# ensure the request_span is finished and the context reset; | ||
# this assumes that the Rack middleware creates a root span | ||
|
@@ -103,6 +79,44 @@ def resource_name_for(env, status) | |
"#{env['REQUEST_METHOD']} #{status}".strip | ||
end | ||
end | ||
|
||
def set_request_tags!(request_span, env, status, headers, response) | ||
# the source of truth in Rack is the PATH_INFO key that holds the | ||
# URL for the current request; some framework may override that | ||
# value, especially during exception handling and because of that | ||
# we prefer using the `REQUEST_URI` if this is available. | ||
# NOTE: `REQUEST_URI` is Rails specific and may not apply for other frameworks | ||
url = env['REQUEST_URI'] || env['PATH_INFO'] | ||
|
||
request_span.resource ||= resource_name_for(env, status) | ||
if request_span.get_tag(Datadog::Ext::HTTP::METHOD).nil? | ||
request_span.set_tag(Datadog::Ext::HTTP::METHOD, env['REQUEST_METHOD']) | ||
end | ||
if request_span.get_tag(Datadog::Ext::HTTP::URL).nil? | ||
request_span.set_tag(Datadog::Ext::HTTP::URL, url) | ||
end | ||
if request_span.get_tag(Datadog::Ext::HTTP::BASE_URL).nil? | ||
request_obj = ::Rack::Request.new(env) | ||
|
||
base_url = if request_obj.respond_to?(:base_url) | ||
request_obj.base_url | ||
else | ||
# Compatibility for older Rack versions | ||
request_obj.url.chomp(request_obj.fullpath) | ||
end | ||
|
||
request_span.set_tag(Datadog::Ext::HTTP::BASE_URL, base_url) | ||
end | ||
if request_span.get_tag(Datadog::Ext::HTTP::STATUS_CODE).nil? && status | ||
request_span.set_tag(Datadog::Ext::HTTP::STATUS_CODE, status) | ||
end | ||
|
||
# detect if the status code is a 5xx and flag the request span as an error | ||
# unless it has been already set by the underlying framework | ||
if status.to_s.start_with?('5') && request_span.status.zero? | ||
request_span.status = 1 | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed this variables since they're used once (reducing the method's length)