-
Notifications
You must be signed in to change notification settings - Fork 207
feat: HTTPX semantic convention stability opt in #1589
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kaylareopelle
merged 6 commits into
open-telemetry:main
from
hannahramadan:httpx_semconv_stability_opt_in
Aug 7, 2025
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c149be0
feat: semantic convention stability opt in
hannahramadan 247cc2f
fix: appease rubocop whitespace error
hannahramadan 232a8bf
Merge branch 'main' into httpx_semconv_stability_opt_in
hannahramadan f9cbcc8
Fix: remove redundant .to_s and deprecated http.host
hannahramadan 01ec0c1
Merge branch 'httpx_semconv_stability_opt_in' of https://github.com/h…
hannahramadan 5c6dac3
Merge branch 'main' into httpx_semconv_stability_opt_in
kaylareopelle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
instrumentation/httpx/lib/opentelemetry/instrumentation/httpx/dup/plugin.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
# frozen_string_literal: true | ||
|
||
# Copyright The OpenTelemetry Authors | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
module OpenTelemetry | ||
module Instrumentation | ||
module HTTPX | ||
module Dup | ||
module Plugin | ||
# Instruments around HTTPX's request/response lifecycle in order to generate | ||
# an OTEL trace. | ||
module RequestTracer | ||
module_function | ||
|
||
# initializes tracing on the +request+. | ||
def call(request) | ||
span = nil | ||
|
||
# request objects are reused, when already buffered requests get rerouted to a different | ||
# connection due to connection issues, or when they already got a response, but need to | ||
# be retried. In such situations, the original span needs to be extended for the former, | ||
# while a new is required for the latter. | ||
request.on(:idle) do | ||
span = nil | ||
end | ||
# the span is initialized when the request is buffered in the parser, which is the closest | ||
# one gets to actually sending the request. | ||
request.on(:headers) do | ||
next if span | ||
|
||
span = initialize_span(request) | ||
end | ||
|
||
request.on(:response) do |response| | ||
unless span | ||
next unless response.is_a?(::HTTPX::ErrorResponse) && response.error.respond_to?(:connection) | ||
|
||
# handles the case when the +error+ happened during name resolution, which means | ||
# that the tracing start point hasn't been triggered yet; in such cases, the approximate | ||
# initial resolving time is collected from the connection, and used as span start time, | ||
# and the tracing object in inserted before the on response callback is called. | ||
span = initialize_span(request, response.error.connection.init_time) | ||
|
||
end | ||
|
||
finish(response, span) | ||
end | ||
end | ||
|
||
def finish(response, span) | ||
if response.is_a?(::HTTPX::ErrorResponse) | ||
span.record_exception(response.error) | ||
span.status = Trace::Status.error(response.error.to_s) | ||
else | ||
span.set_attribute(OpenTelemetry::SemanticConventions::Trace::HTTP_STATUS_CODE, response.status) | ||
span.set_attribute('http.response.status_code', response.status) | ||
|
||
if response.status.between?(400, 599) | ||
err = ::HTTPX::HTTPError.new(response) | ||
span.record_exception(err) | ||
span.status = Trace::Status.error(err.to_s) | ||
end | ||
end | ||
|
||
span.finish | ||
end | ||
|
||
# return a span initialized with the +@request+ state. | ||
def initialize_span(request, start_time = ::Time.now) | ||
verb = request.verb | ||
uri = request.uri | ||
|
||
config = HTTPX::Instrumentation.instance.config | ||
|
||
attributes = { | ||
OpenTelemetry::SemanticConventions::Trace::HTTP_HOST => uri.host, | ||
OpenTelemetry::SemanticConventions::Trace::HTTP_METHOD => verb, | ||
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, | ||
'http.request.method' => verb, | ||
'url.scheme' => uri.scheme, | ||
'url.path' => uri.path, | ||
'url.full' => "#{uri.scheme}://#{uri.host}", | ||
'server.address' => uri.host, | ||
'server.port' => uri.port | ||
} | ||
|
||
attributes['url.query'] = uri.query unless uri.query.nil? | ||
attributes[OpenTelemetry::SemanticConventions::Trace::PEER_SERVICE] = config[:peer_service] if config[:peer_service] | ||
attributes.merge!(OpenTelemetry::Common::HTTP::ClientContext.attributes) | ||
|
||
span = tracer.start_span(verb.to_s, attributes: attributes, kind: :client, start_timestamp: start_time) | ||
|
||
OpenTelemetry::Trace.with_span(span) do | ||
OpenTelemetry.propagation.inject(request.headers) | ||
end | ||
|
||
span | ||
rescue StandardError => e | ||
OpenTelemetry.handle_error(exception: e) | ||
end | ||
|
||
def tracer | ||
HTTPX::Instrumentation.instance.tracer | ||
end | ||
end | ||
|
||
# Request patch to initiate the trace on initialization. | ||
module RequestMethods | ||
def initialize(*) | ||
super | ||
|
||
RequestTracer.call(self) | ||
end | ||
end | ||
|
||
# Connection patch to start monitoring on initialization. | ||
module ConnectionMethods | ||
attr_reader :init_time | ||
|
||
def initialize(*) | ||
super | ||
|
||
@init_time = ::Time.now | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
instrumentation/httpx/lib/opentelemetry/instrumentation/httpx/old/plugin.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
# frozen_string_literal: true | ||
|
||
# Copyright The OpenTelemetry Authors | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
module OpenTelemetry | ||
module Instrumentation | ||
module HTTPX | ||
module Old | ||
module Plugin | ||
# Instruments around HTTPX's request/response lifecycle in order to generate | ||
# an OTEL trace. | ||
module RequestTracer | ||
module_function | ||
|
||
# initializes tracing on the +request+. | ||
def call(request) | ||
span = nil | ||
|
||
# request objects are reused, when already buffered requests get rerouted to a different | ||
# connection due to connection issues, or when they already got a response, but need to | ||
# be retried. In such situations, the original span needs to be extended for the former, | ||
# while a new is required for the latter. | ||
request.on(:idle) do | ||
span = nil | ||
end | ||
# the span is initialized when the request is buffered in the parser, which is the closest | ||
# one gets to actually sending the request. | ||
request.on(:headers) do | ||
next if span | ||
|
||
span = initialize_span(request) | ||
end | ||
|
||
request.on(:response) do |response| | ||
unless span | ||
next unless response.is_a?(::HTTPX::ErrorResponse) && response.error.respond_to?(:connection) | ||
|
||
# handles the case when the +error+ happened during name resolution, which means | ||
# that the tracing start point hasn't been triggered yet; in such cases, the approximate | ||
# initial resolving time is collected from the connection, and used as span start time, | ||
# and the tracing object in inserted before the on response callback is called. | ||
span = initialize_span(request, response.error.connection.init_time) | ||
|
||
end | ||
|
||
finish(response, span) | ||
end | ||
end | ||
|
||
def finish(response, span) | ||
if response.is_a?(::HTTPX::ErrorResponse) | ||
span.record_exception(response.error) | ||
span.status = Trace::Status.error(response.error.to_s) | ||
else | ||
span.set_attribute(OpenTelemetry::SemanticConventions::Trace::HTTP_STATUS_CODE, response.status) | ||
|
||
if response.status.between?(400, 599) | ||
err = ::HTTPX::HTTPError.new(response) | ||
span.record_exception(err) | ||
span.status = Trace::Status.error(err.to_s) | ||
end | ||
end | ||
|
||
span.finish | ||
end | ||
|
||
# return a span initialized with the +@request+ state. | ||
def initialize_span(request, start_time = ::Time.now) | ||
verb = request.verb | ||
uri = request.uri | ||
|
||
config = HTTPX::Instrumentation.instance.config | ||
|
||
attributes = { | ||
OpenTelemetry::SemanticConventions::Trace::HTTP_HOST => uri.host, | ||
OpenTelemetry::SemanticConventions::Trace::HTTP_METHOD => verb, | ||
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 | ||
} | ||
|
||
attributes[OpenTelemetry::SemanticConventions::Trace::PEER_SERVICE] = config[:peer_service] if config[:peer_service] | ||
attributes.merge!(OpenTelemetry::Common::HTTP::ClientContext.attributes) | ||
|
||
span = tracer.start_span("HTTP #{verb}", attributes: attributes, kind: :client, start_timestamp: start_time) | ||
|
||
OpenTelemetry::Trace.with_span(span) do | ||
OpenTelemetry.propagation.inject(request.headers) | ||
end | ||
|
||
span | ||
rescue StandardError => e | ||
OpenTelemetry.handle_error(exception: e) | ||
end | ||
|
||
def tracer | ||
HTTPX::Instrumentation.instance.tracer | ||
end | ||
end | ||
|
||
# Request patch to initiate the trace on initialization. | ||
module RequestMethods | ||
def initialize(*) | ||
super | ||
|
||
RequestTracer.call(self) | ||
end | ||
end | ||
|
||
# Connection patch to start monitoring on initialization. | ||
module ConnectionMethods | ||
attr_reader :init_time | ||
|
||
def initialize(*) | ||
super | ||
|
||
@init_time = ::Time.now | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.