lib/opentelemetry/instrumentation/http/patches/client.rb in opentelemetry-instrumentation-http-0.22.0 vs lib/opentelemetry/instrumentation/http/patches/client.rb in opentelemetry-instrumentation-http-0.23.0
- old
+ new
@@ -11,35 +11,51 @@
# Module to prepend to HTTP::Client for instrumentation
module Client
def perform(req, options)
uri = req.uri
request_method = req.verb.to_s.upcase
+ span_name = create_request_span_name(request_method, uri.path)
attributes = {
'http.method' => request_method,
'http.scheme' => uri.scheme,
'http.target' => uri.path,
'http.url' => "#{uri.scheme}://#{uri.host}",
'net.peer.name' => uri.host,
'net.peer.port' => uri.port
}.merge!(OpenTelemetry::Common::HTTP::ClientContext.attributes)
- tracer.in_span("HTTP #{request_method}", attributes: attributes, kind: :client) do |span|
+ tracer.in_span(span_name, attributes: attributes, kind: :client) do |span|
OpenTelemetry.propagation.inject(req.headers)
super.tap do |response|
annotate_span_with_response!(span, response)
end
end
end
private
+ def config
+ OpenTelemetry::Instrumentation::HTTP::Instrumentation.instance.config
+ end
+
def annotate_span_with_response!(span, response)
return unless response&.status
status_code = response.status.to_i
span.set_attribute('http.status_code', status_code)
span.status = OpenTelemetry::Trace::Status.error unless (100..399).include?(status_code.to_i)
+ end
+
+ def create_request_span_name(request_method, request_path)
+ if (implementation = config[:span_name_formatter])
+ updated_span_name = implementation.call(request_method, request_path)
+ updated_span_name.is_a?(String) ? updated_span_name : "HTTP #{request_method}"
+ else
+ "HTTP #{request_method}"
+ end
+ rescue StandardError
+ "HTTP #{request_method}"
end
def tracer
HTTP::Instrumentation.instance.tracer
end