lib/opentelemetry/instrumentation/excon/middlewares/tracer_middleware.rb in opentelemetry-instrumentation-excon-0.21.3 vs lib/opentelemetry/instrumentation/excon/middlewares/tracer_middleware.rb in opentelemetry-instrumentation-excon-0.22.0

- old
+ new

@@ -20,29 +20,35 @@ HTTP_METHODS_TO_SPAN_NAMES = HTTP_METHODS_TO_UPPERCASE.values.each_with_object({}) do |uppercase_method, hash| hash[uppercase_method] ||= "HTTP #{uppercase_method}" end.freeze def request_call(datum) - begin - unless datum.key?(:otel_span) - http_method = HTTP_METHODS_TO_UPPERCASE[datum[:method]] - attributes = span_creation_attributes(datum, http_method) - tracer.start_span( - HTTP_METHODS_TO_SPAN_NAMES[http_method], - attributes: attributes, - kind: :client - ).tap do |span| - datum[:otel_span] = span - OpenTelemetry::Trace.with_span(span) do - OpenTelemetry.propagation.inject(datum[:headers]) - end - end - end - rescue StandardError => e - OpenTelemetry.logger.debug(e.message) - end + return @stack.request_call(datum) if untraced?(datum) + http_method = HTTP_METHODS_TO_UPPERCASE[datum[:method]] + + attributes = { + OpenTelemetry::SemanticConventions::Trace::HTTP_METHOD => http_method, + OpenTelemetry::SemanticConventions::Trace::HTTP_SCHEME => datum[:scheme], + OpenTelemetry::SemanticConventions::Trace::HTTP_TARGET => datum[:path], + OpenTelemetry::SemanticConventions::Trace::HTTP_HOST => datum[:host], + OpenTelemetry::SemanticConventions::Trace::NET_PEER_NAME => datum[:hostname], + OpenTelemetry::SemanticConventions::Trace::NET_PEER_PORT => datum[:port] + } + + peer_service = Excon::Instrumentation.instance.config[:peer_service] + attributes[OpenTelemetry::SemanticConventions::Trace::PEER_SERVICE] = peer_service if peer_service + attributes.merge!(OpenTelemetry::Common::HTTP::ClientContext.attributes) + + span = tracer.start_span(HTTP_METHODS_TO_SPAN_NAMES[http_method], attributes: attributes, kind: :client) + ctx = OpenTelemetry::Trace.context_with_span(span) + + datum[:otel_span] = span + datum[:otel_token] = OpenTelemetry::Context.attach(ctx) + + OpenTelemetry.propagation.inject(datum[:headers]) + @stack.request_call(datum) end def response_call(datum) @stack.response_call(datum).tap do |d| @@ -69,45 +75,37 @@ end private def handle_response(datum) - if datum.key?(:otel_span) - datum[:otel_span].tap do |span| - return span if span.end_timestamp + datum.delete(:otel_span)&.tap do |span| + return unless span.recording? - if datum.key?(:response) - response = datum[:response] - span.set_attribute('http.status_code', response[:status]) - span.status = OpenTelemetry::Trace::Status.error unless (100..399).cover?(response[:status].to_i) - end + if datum.key?(:response) + response = datum[:response] + span.set_attribute(OpenTelemetry::SemanticConventions::Trace::HTTP_STATUS_CODE, response[:status]) + span.status = OpenTelemetry::Trace::Status.error unless (100..399).cover?(response[:status].to_i) + end - span.status = OpenTelemetry::Trace::Status.error("Request has failed: #{datum[:error]}") if datum.key?(:error) - - span.finish - datum.delete(:otel_span) + if datum.key?(:error) + span.status = OpenTelemetry::Trace::Status.error('Request has failed') + span.record_exception(datum[:error]) end + + span.finish + + OpenTelemetry::Context.detach(datum.delete(:otel_token)) if datum.include?(:otel_token) end rescue StandardError => e - OpenTelemetry.logger.debug(e.message) + OpenTelemetry.handle_error(e) end - def span_creation_attributes(datum, http_method) - instrumentation_attrs = { - 'http.host' => datum[:host], - 'http.method' => http_method, - 'http.scheme' => datum[:scheme], - 'http.target' => datum[:path] - } - config = Excon::Instrumentation.instance.config - instrumentation_attrs['peer.service'] = config[:peer_service] if config[:peer_service] - instrumentation_attrs.merge!( - OpenTelemetry::Common::HTTP::ClientContext.attributes - ) - end - def tracer Excon::Instrumentation.instance.tracer + end + + def untraced?(datum) + datum.key?(:otel_span) || Excon::Instrumentation.instance.untraced?(datum[:host]) end end end end end