From 39221d1b816d30b7261705519424cb88e605f8ba Mon Sep 17 00:00:00 2001 From: Troels Thomsen Date: Sun, 16 Jun 2024 13:42:58 +0200 Subject: [PATCH] Use constants when available --- .../instrumentation/ethon/patches/easy.rb | 10 +++++----- .../faraday/middlewares/tracer_middleware.rb | 10 +++++----- .../instrumentation/http/patches/client.rb | 14 +++++++------- .../instrumentation/http/patches/connection.rb | 4 ++-- .../instrumentation/http_client/patches/client.rb | 14 +++++++------- .../instrumentation/http_client/patches/session.rb | 2 +- 6 files changed, 27 insertions(+), 27 deletions(-) diff --git a/instrumentation/ethon/lib/opentelemetry/instrumentation/ethon/patches/easy.rb b/instrumentation/ethon/lib/opentelemetry/instrumentation/ethon/patches/easy.rb index 74791818e..14f6b429d 100644 --- a/instrumentation/ethon/lib/opentelemetry/instrumentation/ethon/patches/easy.rb +++ b/instrumentation/ethon/lib/opentelemetry/instrumentation/ethon/patches/easy.rb @@ -41,7 +41,7 @@ def complete message = return_code ? ::Ethon::Curl.easy_strerror(return_code) : 'unknown reason' @otel_span.status = OpenTelemetry::Trace::Status.error("Request has failed: #{message}") else - @otel_span.set_attribute('http.status_code', response_code) + @otel_span.set_attribute(OpenTelemetry::SemanticConventions::Trace::HTTP_STATUS_CODE, response_code) @otel_span.status = OpenTelemetry::Trace::Status.error unless (100..399).cover?(response_code.to_i) end ensure @@ -84,17 +84,17 @@ def otel_span_started? def span_creation_attributes(method) instrumentation_attrs = { - 'http.method' => method + OpenTelemetry::SemanticConventions::Trace::HTTP_METHOD => method } uri = _otel_cleanse_uri(url) if uri - instrumentation_attrs['http.url'] = uri.to_s - instrumentation_attrs['net.peer.name'] = uri.host if uri.host + instrumentation_attrs[OpenTelemetry::SemanticConventions::Trace::HTTP_URL] = uri.to_s + instrumentation_attrs[OpenTelemetry::SemanticConventions::Trace::NET_PEER_NAME] = uri.host if uri.host end config = Ethon::Instrumentation.instance.config - instrumentation_attrs['peer.service'] = config[:peer_service] if config[:peer_service] + instrumentation_attrs[OpenTelemetry::SemanticConventions::Trace::PEER_SERVICE] = config[:peer_service] if config[:peer_service] instrumentation_attrs.merge!( OpenTelemetry::Common::HTTP::ClientContext.attributes ) diff --git a/instrumentation/faraday/lib/opentelemetry/instrumentation/faraday/middlewares/tracer_middleware.rb b/instrumentation/faraday/lib/opentelemetry/instrumentation/faraday/middlewares/tracer_middleware.rb index 1b4452ac3..f7bf04abb 100644 --- a/instrumentation/faraday/lib/opentelemetry/instrumentation/faraday/middlewares/tracer_middleware.rb +++ b/instrumentation/faraday/lib/opentelemetry/instrumentation/faraday/middlewares/tracer_middleware.rb @@ -47,12 +47,12 @@ def call(env) def span_creation_attributes(http_method:, url:, config:) instrumentation_attrs = { - 'http.method' => http_method, - 'http.url' => OpenTelemetry::Common::Utilities.cleanse_url(url.to_s), + OpenTelemetry::SemanticConventions::Trace::HTTP_METHOD => http_method, + OpenTelemetry::SemanticConventions::Trace::HTTP_URL => OpenTelemetry::Common::Utilities.cleanse_url(url.to_s), 'faraday.adapter.name' => app.class.name } - instrumentation_attrs['net.peer.name'] = url.host if url.host - instrumentation_attrs['peer.service'] = config[:peer_service] if config[:peer_service] + instrumentation_attrs[OpenTelemetry::SemanticConventions::Trace::NET_PEER_NAME] = url.host if url.host + instrumentation_attrs[OpenTelemetry::SemanticConventions::Trace::PEER_SERVICE] = config[:peer_service] if config[:peer_service] instrumentation_attrs.merge!( OpenTelemetry::Common::HTTP::ClientContext.attributes @@ -67,7 +67,7 @@ def tracer end def trace_response(span, status) - span.set_attribute('http.status_code', status) + span.set_attribute(OpenTelemetry::SemanticConventions::Trace::HTTP_STATUS_CODE, status) span.status = OpenTelemetry::Trace::Status.error unless (100..399).cover?(status.to_i) end end diff --git a/instrumentation/http/lib/opentelemetry/instrumentation/http/patches/client.rb b/instrumentation/http/lib/opentelemetry/instrumentation/http/patches/client.rb index f814e1a65..3ceafcdba 100644 --- a/instrumentation/http/lib/opentelemetry/instrumentation/http/patches/client.rb +++ b/instrumentation/http/lib/opentelemetry/instrumentation/http/patches/client.rb @@ -16,12 +16,12 @@ def perform(req, options) 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 + OpenTelemetry::SemanticConventions::Trace::HTTP_METHOD => request_method, + OpenTelemetry::SemanticConventions::Trace::HTTP_SCHEME => uri.scheme, + OpenTelemetry::SemanticConventions::Trace::HTTP_TARGET => uri.path, + OpenTelemetry::SemanticConventions::Trace::HTTP_URL => "#{uri.scheme}://#{uri.host}", + OpenTelemetry::SemanticConventions::Trace::NET_PEER_NAME => uri.host, + OpenTelemetry::SemanticConventions::Trace::NET_PEER_PORT => uri.port }.merge!(OpenTelemetry::Common::HTTP::ClientContext.attributes) tracer.in_span(span_name, attributes: attributes, kind: :client) do |span| @@ -42,7 +42,7 @@ 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.set_attribute(OpenTelemetry::SemanticConventions::Trace::HTTP_STATUS_CODE, status_code) span.status = OpenTelemetry::Trace::Status.error unless (100..399).cover?(status_code.to_i) end diff --git a/instrumentation/http/lib/opentelemetry/instrumentation/http/patches/connection.rb b/instrumentation/http/lib/opentelemetry/instrumentation/http/patches/connection.rb index 6e162886f..9efba7b45 100644 --- a/instrumentation/http/lib/opentelemetry/instrumentation/http/patches/connection.rb +++ b/instrumentation/http/lib/opentelemetry/instrumentation/http/patches/connection.rb @@ -12,8 +12,8 @@ module Patches module Connection def initialize(req, options) attributes = { - 'net.peer.name' => req.uri.host, - 'net.peer.port' => req.uri.port + OpenTelemetry::SemanticConventions::Trace::NET_PEER_NAME => req.uri.host, + OpenTelemetry::SemanticConventions::Trace::NET_PEER_PORT => req.uri.port }.merge!(OpenTelemetry::Common::HTTP::ClientContext.attributes) tracer.in_span('HTTP CONNECT', attributes: attributes) do diff --git a/instrumentation/http_client/lib/opentelemetry/instrumentation/http_client/patches/client.rb b/instrumentation/http_client/lib/opentelemetry/instrumentation/http_client/patches/client.rb index a71c8ad7b..cd743c596 100644 --- a/instrumentation/http_client/lib/opentelemetry/instrumentation/http_client/patches/client.rb +++ b/instrumentation/http_client/lib/opentelemetry/instrumentation/http_client/patches/client.rb @@ -18,12 +18,12 @@ def do_get_block(req, proxy, conn, &block) request_method = req.header.request_method attributes = { - 'http.method' => request_method, - 'http.scheme' => uri.scheme, - 'http.target' => uri.path, - 'http.url' => url, - 'net.peer.name' => uri.host, - 'net.peer.port' => uri.port + OpenTelemetry::SemanticConventions::Trace::HTTP_METHOD => request_method, + OpenTelemetry::SemanticConventions::Trace::HTTP_SCHEME => uri.scheme, + OpenTelemetry::SemanticConventions::Trace::HTTP_TARGET => uri.path, + OpenTelemetry::SemanticConventions::Trace::HTTP_URL => url, + OpenTelemetry::SemanticConventions::Trace::NET_PEER_NAME => uri.host, + OpenTelemetry::SemanticConventions::Trace::NET_PEER_PORT => uri.port }.merge!(OpenTelemetry::Common::HTTP::ClientContext.attributes) tracer.in_span("HTTP #{request_method}", attributes: attributes, kind: :client) do |span| @@ -41,7 +41,7 @@ def annotate_span_with_response!(span, response) status_code = response.status_code.to_i - span.set_attribute('http.status_code', status_code) + span.set_attribute(OpenTelemetry::SemanticConventions::Trace::HTTP_STATUS_CODE, status_code) span.status = OpenTelemetry::Trace::Status.error unless (100..399).cover?(status_code.to_i) end diff --git a/instrumentation/http_client/lib/opentelemetry/instrumentation/http_client/patches/session.rb b/instrumentation/http_client/lib/opentelemetry/instrumentation/http_client/patches/session.rb index 7604dff4c..ac3ef9e34 100644 --- a/instrumentation/http_client/lib/opentelemetry/instrumentation/http_client/patches/session.rb +++ b/instrumentation/http_client/lib/opentelemetry/instrumentation/http_client/patches/session.rb @@ -14,7 +14,7 @@ def connect site = @proxy || @dest url = site.addr - attributes = { 'http.url' => url }.merge!(OpenTelemetry::Common::HTTP::ClientContext.attributes) + attributes = { OpenTelemetry::SemanticConventions::Trace::HTTP_URL => url }.merge!(OpenTelemetry::Common::HTTP::ClientContext.attributes) tracer.in_span('HTTP CONNECT', attributes: attributes) do super end