Skip to content

Commit

Permalink
Use constants when available
Browse files Browse the repository at this point in the history
  • Loading branch information
tt committed Jun 16, 2024
1 parent 2230403 commit 39221d1
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 39221d1

Please sign in to comment.