Description
This is in response to #3367:
If you require this hook, please open a "Feature request" stating your use case so we can asses how to best support it.
Describe the goal of the feature
We were looking for a good way to consistently (i.e. without manual repetition) set the resource name of HTTP requests. This is what we did through the after_request
hook. Snippet:
Datadog::Tracing::Contrib::HTTP::Instrumentation.after_request do |span, _http, _req, _res|
break if HTTPFestival::TraceCalls.current_caller.nil?
break if HTTPFestival::TraceCalls.current_caller.empty?
span.resource = HTTPFestival::TraceCalls.current_caller
rescue StandardError
nil # If we mess up, we should not break the original request
end
It's worth noting that HTTPFestival::TraceCalls.current_caller
goes back to a thread-local variable, so we ensure that for each HTTP request that might concurrently occur, we set a proper request name. We had a mechanism in place to automatically set a reasonable default for resource names just before we issued the actual HTTP request.
Describe alternatives you've considered
I am not aware of any alternative that could be used to overwrite the resource name of HTTP calls. This leaves us with unhelpful defaults such as GET 200
or POST 404
, when our implementation allowed us to construct names that were specific to the action being executed, without being derailed by details like identifiers in the request path.
Judging from the implementation that we had in place before, something that would alternatively work for us is an API like this:
Datadog::Tracing::Contrib::HTTP.with_resource_name('GET /foos/:id') do
# do the actual HTTP request here (its resource name in Datadog will be "GET /foos/:id")
end