Skip to content

Commit

Permalink
Adopt latest semantic conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
tt committed Jun 16, 2024
1 parent ea385d7 commit 6a6d46d
Show file tree
Hide file tree
Showing 19 changed files with 294 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +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.response.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
Expand Down Expand Up @@ -85,12 +86,17 @@ def otel_span_started?
def span_creation_attributes(method)
instrumentation_attrs = {
OpenTelemetry::SemanticConventions::Trace::HTTP_METHOD => method
'http.request.method' => method
}

uri = _otel_cleanse_uri(url)
if uri
instrumentation_attrs[OpenTelemetry::SemanticConventions::Trace::HTTP_URL] = uri.to_s
instrumentation_attrs[OpenTelemetry::SemanticConventions::Trace::NET_PEER_NAME] = uri.host if uri.host
instrumentation_attrs['server.address'] = uri.host if uri.host
instrumentation_attrs['server.port'] = uri.port if uri.port
instrumentation_attrs['url.full'] = uri.to_s
instrumentation_attrs['url.scheme'] = uri.scheme if uri.scheme
end

config = Ethon::Instrumentation.instance.config
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,15 @@

_(span.name).must_equal 'HTTP N/A'
_(span.attributes['http.method']).must_equal 'N/A'
_(span.attributes['http.request.method']).must_equal 'N/A'
_(span.attributes['http.response.status_code']).must_be_nil
_(span.attributes['http.status_code']).must_be_nil
_(span.attributes['http.url']).must_equal 'http://example.com/test'
_(span.attributes['net.peer.name']).must_equal 'example.com'
_(span.attributes['server.address']).must_equal 'example.com'
_(span.attributes['server.port']).must_equal 80
_(span.attributes['url.full']).must_equal 'http://example.com/test'
_(span.attributes['url.scheme']).must_equal 'http'
end
end
end
Expand All @@ -92,8 +98,11 @@ def stub_response(options)
stub_response(response_code: 200) do
_(span.name).must_equal 'HTTP N/A'
_(span.attributes['http.method']).must_equal 'N/A'
_(span.attributes['http.request.method']).must_equal 'N/A'
_(span.attributes['http.response.status_code']).must_equal 200
_(span.attributes['http.status_code']).must_equal 200
_(span.attributes['http.url']).must_equal 'http://example.com/test'
_(span.attributes['url.full']).must_equal 'http://example.com/test'
_(easy.instance_eval { @otel_span }).must_be_nil
_(
easy.instance_eval { @otel_original_headers['traceparent'] }
Expand All @@ -105,8 +114,11 @@ def stub_response(options)
stub_response(response_code: 500) do
_(span.name).must_equal 'HTTP N/A'
_(span.attributes['http.method']).must_equal 'N/A'
_(span.attributes['http.request.method']).must_equal 'N/A'
_(span.attributes['http.response.status_code']).must_equal 500
_(span.attributes['http.status_code']).must_equal 500
_(span.attributes['http.url']).must_equal 'http://example.com/test'
_(span.attributes['url.full']).must_equal 'http://example.com/test'
_(easy.instance_eval { @otel_span }).must_be_nil
_(
easy.instance_eval { @otel_original_headers['traceparent'] }
Expand All @@ -118,8 +130,11 @@ def stub_response(options)
stub_response(response_code: 0, return_code: :operation_timedout) do
_(span.name).must_equal 'HTTP N/A'
_(span.attributes['http.method']).must_equal 'N/A'
_(span.attributes['http.request.method']).must_equal 'N/A'
_(span.attributes['http.response.status_code']).must_be_nil
_(span.attributes['http.status_code']).must_be_nil
_(span.attributes['http.url']).must_equal 'http://example.com/test'
_(span.attributes['url.full']).must_equal 'http://example.com/test'
_(span.status.code).must_equal(
OpenTelemetry::Trace::Status::ERROR
)
Expand All @@ -140,8 +155,10 @@ def stub_response(options)
OpenTelemetry::Common::HTTP::ClientContext.with_attributes(client_context_attrs) do
stub_response(response_code: 200) do
_(span.attributes['http.method']).must_equal 'OVERRIDE'
_(span.attributes['http.request.method']).must_equal 'N/A'
_(span.attributes['http.url']).must_equal 'http://example.com/test'
_(span.attributes['test.attribute']).must_equal 'test.value'
_(span.attributes['url.full']).must_equal 'http://example.com/test'
end
end
end
Expand Down Expand Up @@ -278,8 +295,12 @@ def stub_response(options)
_(exporter.finished_spans.size).must_equal 2
_(exporter.finished_spans[0].attributes['http.url']).must_be_nil
_(exporter.finished_spans[0].attributes['net.peer.name']).must_be_nil
_(exporter.finished_spans[0].attributes['server.address']).must_be_nil
_(exporter.finished_spans[0].attributes['url.full']).must_be_nil
_(exporter.finished_spans[1].attributes['http.url']).must_equal 'test'
_(exporter.finished_spans[1].attributes['net.peer.name']).must_be_nil
_(exporter.finished_spans[1].attributes['server.address']).must_be_nil
_(exporter.finished_spans[1].attributes['url.full']).must_equal 'test'
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,15 @@ def request_call(datum)
attributes = {
OpenTelemetry::SemanticConventions::Trace::HTTP_HOST => datum[:host],
OpenTelemetry::SemanticConventions::Trace::HTTP_METHOD => http_method,
'http.request.method' => http_method,
OpenTelemetry::SemanticConventions::Trace::HTTP_SCHEME => datum[:scheme],
OpenTelemetry::SemanticConventions::Trace::HTTP_TARGET => datum[:path],
OpenTelemetry::SemanticConventions::Trace::NET_PEER_NAME => datum[:hostname],
OpenTelemetry::SemanticConventions::Trace::NET_PEER_PORT => datum[:port]
OpenTelemetry::SemanticConventions::Trace::NET_PEER_PORT => datum[:port],
'server.address' => datum[:hostname],
'server.port' => datum[:port],
'url.full' => OpenTelemetry::Common::Utilities.cleanse_url(::Excon::Utils.request_uri(datum)),
'url.scheme' => datum[:scheme]
}

peer_service = Excon::Instrumentation.instance.config[:peer_service]
Expand Down Expand Up @@ -82,6 +87,7 @@ def handle_response(datum)

if datum.key?(:response)
response = datum[:response]
span.set_attribute('http.response.status_code', response[:status])
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ def connect

attributes = {
OpenTelemetry::SemanticConventions::Trace::NET_PEER_NAME => conn_address,
OpenTelemetry::SemanticConventions::Trace::NET_PEER_PORT => conn_port
OpenTelemetry::SemanticConventions::Trace::NET_PEER_PORT => conn_port,
'server.address' => conn_address,
'server.port' => conn_port
}.merge!(OpenTelemetry::Common::HTTP::ClientContext.attributes)

if is_a?(::Excon::SSLSocket) && @data[:proxy]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,15 @@
_(span.name).must_equal 'HTTP GET'
_(span.attributes['http.host']).must_equal 'example.com'
_(span.attributes['http.method']).must_equal 'GET'
_(span.attributes['http.request.method']).must_equal 'GET'
_(span.attributes['http.response.status_code']).must_equal 200
_(span.attributes['http.scheme']).must_equal 'http'
_(span.attributes['http.status_code']).must_equal 200
_(span.attributes['http.target']).must_equal '/success'
_(span.attributes['server.address']).must_equal 'example.com'
_(span.attributes['server.port']).must_equal 80
_(span.attributes['url.full']).must_equal 'http://example.com/success'
_(span.attributes['url.scheme']).must_equal 'http'
assert_requested(
:get,
'http://example.com/success',
Expand All @@ -63,6 +69,7 @@
specify 'after request with capital-letters HTTP method' do
Excon.new('http://example.com/success').request(method: 'GET')

_(span.attributes['http.request.method']).must_equal 'GET'
_(span.attributes['http.method']).must_equal 'GET'
end

Expand All @@ -73,9 +80,15 @@
_(span.name).must_equal 'HTTP GET'
_(span.attributes['http.host']).must_equal 'example.com'
_(span.attributes['http.method']).must_equal 'GET'
_(span.attributes['http.request.method']).must_equal 'GET'
_(span.attributes['http.response.status_code']).must_equal 500
_(span.attributes['http.scheme']).must_equal 'http'
_(span.attributes['http.status_code']).must_equal 500
_(span.attributes['http.target']).must_equal '/failure'
_(span.attributes['server.address']).must_equal 'example.com'
_(span.attributes['server.port']).must_equal 80
_(span.attributes['url.full']).must_equal 'http://example.com/failure'
_(span.attributes['url.scheme']).must_equal 'http'
assert_requested(
:get,
'http://example.com/failure',
Expand All @@ -92,8 +105,13 @@
_(span.name).must_equal 'HTTP GET'
_(span.attributes['http.host']).must_equal 'example.com'
_(span.attributes['http.method']).must_equal 'GET'
_(span.attributes['http.request.method']).must_equal 'GET'
_(span.attributes['http.scheme']).must_equal 'http'
_(span.attributes['http.target']).must_equal '/timeout'
_(span.attributes['server.address']).must_equal 'example.com'
_(span.attributes['server.port']).must_equal 80
_(span.attributes['url.full']).must_equal 'http://example.com/timeout'
_(span.attributes['url.scheme']).must_equal 'http'
_(span.status.code).must_equal(
OpenTelemetry::Trace::Status::ERROR
)
Expand Down Expand Up @@ -121,10 +139,16 @@
_(span.name).must_equal 'HTTP GET'
_(span.attributes['http.host']).must_equal 'example.com'
_(span.attributes['http.method']).must_equal 'OVERRIDE'
_(span.attributes['http.request.method']).must_equal 'GET'
_(span.attributes['http.response.status_code']).must_equal 200
_(span.attributes['http.scheme']).must_equal 'http'
_(span.attributes['http.status_code']).must_equal 200
_(span.attributes['http.target']).must_equal '/success'
_(span.attributes['server.address']).must_equal 'example.com'
_(span.attributes['server.port']).must_equal 80
_(span.attributes['test.attribute']).must_equal 'test.value'
_(span.attributes['url.full']).must_equal 'http://example.com/success'
_(span.attributes['url.scheme']).must_equal 'http'
assert_requested(
:get,
'http://example.com/success',
Expand Down Expand Up @@ -188,6 +212,9 @@
_(span.name).must_equal 'HTTP GET'
_(span.attributes['http.host']).must_equal 'example.com'
_(span.attributes['http.method']).must_equal 'GET'
_(span.attributes['http.request.method']).must_equal 'GET'
_(span.attributes['server.address']).must_equal 'example.com'
_(span.attributes['server.port']).must_equal 80
end

it 'creates a span on connect for a non-ignored request' do
Expand All @@ -200,6 +227,8 @@
_(span.kind).must_equal(:internal)
_(span.attributes['net.peer.name']).must_equal('example.com')
_(span.attributes['net.peer.port']).must_equal(80)
_(span.attributes['server.address']).must_equal('example.com')
_(span.attributes['server.port']).must_equal(80)
end
end

Expand Down Expand Up @@ -234,8 +263,11 @@
_(span.attributes['net.peer.name']).must_equal('localhost')
_(span.attributes['net.peer.port']).wont_be_nil
_(span.attributes['net.peer.port']).must_equal(port)
_(span.attributes['server.address']).must_equal('localhost')
_(span.attributes['server.port']).wont_be_nil
_(span.attributes['server.port']).must_equal(port)

assert_http_spans(target: '/example', exception: 'Excon::Error::Timeout')
assert_http_spans(port: port, target: '/example', exception: 'Excon::Error::Timeout')
end

it 'captures errors' do
Expand All @@ -245,13 +277,15 @@
_(span.name).must_equal 'connect'
_(span.attributes['net.peer.name']).must_equal('invalid.com')
_(span.attributes['net.peer.port']).must_equal(99_999)
_(span.attributes['server.address']).must_equal('invalid.com')
_(span.attributes['server.port']).must_equal(99_999)

span_event = span.events.first
_(span_event.name).must_equal 'exception'
# Depending on the Ruby and Excon Version this will be a SocketError, Socket::ResolutionError or Resolv::ResolvError
_(span_event.attributes['exception.type']).must_match(/(Socket|Resolv)/)

assert_http_spans(host: 'invalid.com', target: '/example')
assert_http_spans(host: 'invalid.com', port: 99_999, target: '/example')
end

it '[BUG] fails to emit an HTTP CONNECT span when connecting through an SSL proxy for an HTTP service' do
Expand All @@ -262,6 +296,8 @@
_(span.kind).must_equal(:internal)
_(span.attributes['net.peer.name']).must_equal('localhost')
_(span.attributes['net.peer.port']).must_equal(443)
_(span.attributes['server.address']).must_equal('localhost')
_(span.attributes['server.port']).must_equal(443)

assert_http_spans
end
Expand All @@ -274,6 +310,8 @@
_(span.kind).must_equal(:client)
_(span.attributes['net.peer.name']).must_equal('localhost')
_(span.attributes['net.peer.port']).must_equal(443)
_(span.attributes['server.address']).must_equal('localhost')
_(span.attributes['server.port']).must_equal(443)

assert_http_spans(scheme: 'https')
end
Expand All @@ -286,6 +324,8 @@
_(span.kind).must_equal(:internal)
_(span.attributes['net.peer.name']).must_equal('localhost')
_(span.attributes['net.peer.port']).must_equal(443)
_(span.attributes['server.address']).must_equal('localhost')
_(span.attributes['server.port']).must_equal(443)

assert_http_spans(exception: 'Excon::Error::Socket')
end
Expand All @@ -299,13 +339,17 @@
end
end

def assert_http_spans(scheme: 'http', host: 'localhost', target: '/', exception: nil)
def assert_http_spans(scheme: 'http', host: 'localhost', port: nil, target: '/', exception: nil)
exporter.finished_spans[1..].each do |http_span|
_(http_span.name).must_equal 'HTTP GET'
_(http_span.attributes['http.host']).must_equal host
_(http_span.attributes['http.method']).must_equal 'GET'
_(http_span.attributes['http.request.method']).must_equal 'GET'
_(http_span.attributes['http.scheme']).must_equal scheme
_(http_span.attributes['http.target']).must_equal target
_(http_span.attributes['server.address']).must_equal host
_(http_span.attributes['url.full']).must_equal "#{scheme}://#{host}#{port&.to_s&.prepend(':')}#{target}"
_(http_span.attributes['url.scheme']).must_equal scheme
_(http_span.status.code).must_equal(
OpenTelemetry::Trace::Status::ERROR
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,16 @@ def call(env)
def span_creation_attributes(http_method:, url:, config:)
instrumentation_attrs = {
OpenTelemetry::SemanticConventions::Trace::HTTP_METHOD => http_method,
'http.request.method' => http_method,
OpenTelemetry::SemanticConventions::Trace::HTTP_URL => OpenTelemetry::Common::Utilities.cleanse_url(url.to_s),
'url.full' => OpenTelemetry::Common::Utilities.cleanse_url(url.to_s),
'url.scheme' => url.scheme,
'faraday.adapter.name' => app.class.name
}
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['server.address'] = url.host if url.host
instrumentation_attrs['server.port'] = url.port if url.port

instrumentation_attrs.merge!(
OpenTelemetry::Common::HTTP::ClientContext.attributes
Expand All @@ -67,6 +72,7 @@ def tracer
end

def trace_response(span, status)
span.set_attribute('http.response.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
Expand Down
Loading

0 comments on commit 6a6d46d

Please sign in to comment.