Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions instrumentation/restclient/Appraisals
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
# frozen_string_literal: true

appraise 'rest-client-2.1' do
gem 'rest-client', '~> 2.1.0'
end
# To faclitate HTTP semantic convention stability migration, we are using
# appraisal to test the different semantic convention modes along with different
# gem versions. For more information on the semantic convention modes, see:
# https://opentelemetry.io/docs/specs/semconv/non-normative/http-migration/

semconv_stability = %w[dup stable old]

semconv_stability.each do |mode|
appraise "rest-client-2.1_#{mode}" do
gem 'rest-client', '~> 2.1.0'
end

appraise 'rest-client-2.0' do
gem 'rest-client', '~> 2.0.0'
appraise "rest-client-2.0_#{mode}" do
gem 'rest-client', '~> 2.0.0'
end
end
16 changes: 16 additions & 0 deletions instrumentation/restclient/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,19 @@ Apache 2.0 license. See [LICENSE][license-github] for more information.
[community-meetings]: https://github.com/open-telemetry/community#community-meetings
[slack-channel]: https://cloud-native.slack.com/archives/C01NWKKMKMY
[discussions-url]: https://github.com/open-telemetry/opentelemetry-ruby/discussions

## HTTP semantic convention stability

In the OpenTelemetry ecosystem, HTTP semantic conventions have now reached a stable state. However, the initial Faraday instrumentation was introduced before this stability was achieved, which resulted in HTTP attributes being based on an older version of the semantic conventions.

To facilitate the migration to stable semantic conventions, you can use the `OTEL_SEMCONV_STABILITY_OPT_IN` environment variable. This variable allows you to opt-in to the new stable conventions, ensuring compatibility and future-proofing your instrumentation.

When setting the value for `OTEL_SEMCONV_STABILITY_OPT_IN`, you can specify which conventions you wish to adopt:

- `http` - Emits the stable HTTP and networking conventions and ceases emitting the old conventions previously emitted by the instrumentation.
- `http/dup` - Emits both the old and stable HTTP and networking conventions, enabling a phased rollout of the stable semantic conventions.
- Default behavior (in the absence of either value) is to continue emitting the old HTTP and networking conventions the instrumentation previously emitted.

During the transition from old to stable conventions, RestClient instrumentation code comes in three patch versions: `dup`, `old`, and `stable`. These versions are identical except for the attributes they send. Any changes to RestClient instrumentation should consider all three patches.

For additional information on migration, please refer to our [documentation](https://opentelemetry.io/docs/specs/semconv/non-normative/http-migration/).
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ module RestClient
# instrumentation
class Instrumentation < OpenTelemetry::Instrumentation::Base
install do |_config|
require_dependencies
patch_request
patch_type = determine_semconv
send(:"require_dependencies_#{patch_type}")
send(:"patch_request_#{patch_type}")
end

present do
Expand All @@ -23,12 +24,41 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base

private

def require_dependencies
require_relative 'patches/request'
def determine_semconv
stability_opt_in = ENV.fetch('OTEL_SEMCONV_STABILITY_OPT_IN', '')
values = stability_opt_in.split(',').map(&:strip)

if values.include?('http/dup')
'dup'
elsif values.include?('http')
'stable'
else
'old'
end
end

def require_dependencies_dup
require_relative 'patches/dup/request'
end

def require_dependencies_stable
require_relative 'patches/stable/request'
end

def require_dependencies_old
require_relative 'patches/old/request'
end

def patch_request_dup
::RestClient::Request.prepend(Patches::Dup::Request)
end

def patch_request_stable
::RestClient::Request.prepend(Patches::Stable::Request)
end

def patch_request
::RestClient::Request.prepend(Patches::Request)
def patch_request_old
::RestClient::Request.prepend(Patches::Old::Request)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# frozen_string_literal: true

# Copyright The OpenTelemetry Authors
#
# SPDX-License-Identifier: Apache-2.0

module OpenTelemetry
module Instrumentation
module RestClient
module Patches
module Dup
# Module to prepend to RestClient::Request for instrumentation
module Request
# Constant for the HTTP status range
HTTP_STATUS_SUCCESS_RANGE = (100..399)

def execute(&)
trace_request do |_span|
super
end
end

private

def create_request_span
http_method = method.upcase
instrumentation_attrs = {
'http.method' => http_method.to_s,
'http.request.method' => http_method.to_s,
'http.url' => OpenTelemetry::Common::Utilities.cleanse_url(url),
'url.full' => OpenTelemetry::Common::Utilities.cleanse_url(url)
}
instrumentation_config = RestClient::Instrumentation.instance.config
instrumentation_attrs['peer.service'] = instrumentation_config[:peer_service] if instrumentation_config[:peer_service]
span = tracer.start_span(
http_method.to_s,
attributes: instrumentation_attrs.merge(
OpenTelemetry::Common::HTTP::ClientContext.attributes
),
kind: :client
)

OpenTelemetry::Trace.with_span(span) do
OpenTelemetry.propagation.inject(processed_headers)
end

span
end

def trace_request
span = create_request_span

yield(span).tap do |response|
# Verify return value is a response.
# If so, add additional attributes.
if response.is_a?(::RestClient::Response)
span.set_attribute('http.status_code', response.code)
span.set_attribute('http.response.status_code', response.code)
span.status = OpenTelemetry::Trace::Status.error unless HTTP_STATUS_SUCCESS_RANGE.cover?(response.code.to_i)
end
end
rescue ::RestClient::ExceptionWithResponse => e
span.set_attribute('http.status_code', e.http_code)
span.set_attribute('http.response.status_code', e.http_code)
span.status = OpenTelemetry::Trace::Status.error unless HTTP_STATUS_SUCCESS_RANGE.cover?(e.http_code.to_i)
raise e
ensure
span.finish
end

def tracer
RestClient::Instrumentation.instance.tracer
end
end
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# frozen_string_literal: true

# Copyright The OpenTelemetry Authors
#
# SPDX-License-Identifier: Apache-2.0

module OpenTelemetry
module Instrumentation
module RestClient
module Patches
module Old
# Module to prepend to RestClient::Request for instrumentation
module Request
# Constant for the HTTP status range
HTTP_STATUS_SUCCESS_RANGE = (100..399)

def execute(&)
trace_request do |_span|
super
end
end

private

def create_request_span
http_method = method.upcase
instrumentation_attrs = {
'http.method' => http_method.to_s,
'http.url' => OpenTelemetry::Common::Utilities.cleanse_url(url)
}
instrumentation_config = RestClient::Instrumentation.instance.config
instrumentation_attrs['peer.service'] = instrumentation_config[:peer_service] if instrumentation_config[:peer_service]
span = tracer.start_span(
"HTTP #{http_method}",
attributes: instrumentation_attrs.merge(
OpenTelemetry::Common::HTTP::ClientContext.attributes
),
kind: :client
)

OpenTelemetry::Trace.with_span(span) do
OpenTelemetry.propagation.inject(processed_headers)
end

span
end

def trace_request
span = create_request_span

yield(span).tap do |response|
# Verify return value is a response.
# If so, add additional attributes.
if response.is_a?(::RestClient::Response)
span.set_attribute('http.status_code', response.code)
span.status = OpenTelemetry::Trace::Status.error unless HTTP_STATUS_SUCCESS_RANGE.cover?(response.code.to_i)
end
end
rescue ::RestClient::ExceptionWithResponse => e
span.set_attribute('http.status_code', e.http_code)
span.status = OpenTelemetry::Trace::Status.error unless HTTP_STATUS_SUCCESS_RANGE.cover?(e.http_code.to_i)
raise e
ensure
span.finish
end

def tracer
RestClient::Instrumentation.instance.tracer
end
end
end
end
end
end
end

This file was deleted.

Loading