Skip to content

feat: Rack semantic stability opt in #1594

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

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion instrumentation/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -458,8 +458,11 @@ end
# Set up fake Rack application
builder = Rack::Builder.app do
# Integration is automatic in web frameworks but plain Rack applications require this line.
# - middleware_args_old to emit old HTTP semantic conventions
# - middleware_args_stable to emit stable HTTP semantic conventions
# - middleware_args_dup to emit both old and stable HTTP semantic conventions
# Enable it in your config.ru.
use *OpenTelemetry::Instrumentation::Rack::Instrumentation.instance.middleware_args
use *OpenTelemetry::Instrumentation::Rack::Instrumentation.instance.middleware_args_old
run ExampleAPI
end
app = Rack::MockRequest.new(builder)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,20 @@ class Railtie < ::Rails::Railtie
config.before_initialize do |app|
OpenTelemetry::Instrumentation::Rack::Instrumentation.instance.install({})

stability_opt_in = ENV.fetch('OTEL_SEMCONV_STABILITY_OPT_IN', '')
values = stability_opt_in.split(',').map(&:strip)

middleware_args = if values.include?('http/dup')
OpenTelemetry::Instrumentation::Rack::Instrumentation.instance.middleware_args_dup
elsif values.include?('http')
OpenTelemetry::Instrumentation::Rack::Instrumentation.instance.middleware_args_stable
else
OpenTelemetry::Instrumentation::Rack::Instrumentation.instance.middleware_args_old
end

app.middleware.insert_before(
0,
*OpenTelemetry::Instrumentation::Rack::Instrumentation.instance.middleware_args
*middleware_args
)
end
end
Expand Down
37 changes: 23 additions & 14 deletions instrumentation/rack/Appraisals
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,31 @@
#
# SPDX-License-Identifier: Apache-2.0

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

appraise 'rack-3.0' do
gem 'rack', '~> 3.0.0'
end
semconv_stability = %w[stable old dup]

appraise 'rack-2.2.x' do
gem 'rack', '~> 2.2.0'
end
semconv_stability.each do |mode|
appraise "rack-latest-#{mode}" do
gem 'rack'
end

appraise 'rack-2.1' do
gem 'rack', '~> 2.1.2'
end
appraise "rack-3.0-#{mode}" do
gem 'rack', '~> 3.0.0'
end

appraise "rack-2.2.x-#{mode}" do
gem 'rack', '~> 2.2.0'
end

appraise "rack-2.1-#{mode}" do
gem 'rack', '~> 2.1.2'
end

appraise 'rack-2.0' do
gem 'rack', '~> 2.0.8'
appraise "rack-2.0-#{mode}" do
gem 'rack', '~> 2.0.8'
end
end
16 changes: 16 additions & 0 deletions instrumentation/rack/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,19 @@ The `opentelemetry-instrumentation-rack` gem is distributed under the Apache 2.0
[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 Rack 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, Rack instrumentation code comes in three patch versions: `dup`, `old`, and `stable`. These versions are identical except for the attributes they send. Any changes to Rack 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 @@ -13,7 +13,8 @@ module Rack
# instrumentation
class Instrumentation < OpenTelemetry::Instrumentation::Base
install do |_config|
require_dependencies
patch_type = determine_semconv
send(:"require_dependencies_#{patch_type}")
end

present do
Expand All @@ -35,23 +36,64 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base
#
# @example Default usage
# Rack::Builder.new do
# use *OpenTelemetry::Instrumentation::Rack::Instrumenation.instance.middleware_args
# use *OpenTelemetry::Instrumentation::Rack::Instrumenation.instance.middleware_args_old
# run lambda { |_arg| [200, { 'Content-Type' => 'text/plain' }, body] }
# end
# @return [Array] consisting of a middleware and arguments used in rack builders
def middleware_args
if config.fetch(:use_rack_events, false) == true && defined?(OpenTelemetry::Instrumentation::Rack::Middlewares::EventHandler)
[::Rack::Events, [OpenTelemetry::Instrumentation::Rack::Middlewares::EventHandler.new]]
def middleware_args_old
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this break existing monkey patches of the rack instrumentation, as this is not private? Is that a reasonable concern? I understand we are not 1.x here yet so it's not technically a semver question, but just trying to be mindful around touching rack instrumentation as it's probably got a rather large install base at this point.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fantastic call out - it probably would break some stuff! I've aliased the new and old method definitions, as well as added a test 7290822

if config.fetch(:use_rack_events, false) == true && defined?(OpenTelemetry::Instrumentation::Rack::Middlewares::Old::EventHandler)
[::Rack::Events, [OpenTelemetry::Instrumentation::Rack::Middlewares::Old::EventHandler.new]]
else
[OpenTelemetry::Instrumentation::Rack::Middlewares::TracerMiddleware]
[OpenTelemetry::Instrumentation::Rack::Middlewares::Old::TracerMiddleware]
end
end

alias middleware_args middleware_args_old

def middleware_args_dup
if config.fetch(:use_rack_events, false) == true && defined?(OpenTelemetry::Instrumentation::Rack::Middlewares::Dup::EventHandler)
[::Rack::Events, [OpenTelemetry::Instrumentation::Rack::Middlewares::Dup::EventHandler.new]]
else
[OpenTelemetry::Instrumentation::Rack::Middlewares::Dup::TracerMiddleware]
end
end

def middleware_args_stable
if config.fetch(:use_rack_events, false) == true && defined?(OpenTelemetry::Instrumentation::Rack::Middlewares::Stable::EventHandler)
[::Rack::Events, [OpenTelemetry::Instrumentation::Rack::Middlewares::Stable::EventHandler.new]]
else
[OpenTelemetry::Instrumentation::Rack::Middlewares::Stable::TracerMiddleware]
end
end

private

def require_dependencies
require_relative 'middlewares/event_handler' if defined?(::Rack::Events)
require_relative 'middlewares/tracer_middleware'
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_old
require_relative 'middlewares/old/event_handler' if defined?(::Rack::Events)
require_relative 'middlewares/old/tracer_middleware'
end

def require_dependencies_stable
require_relative 'middlewares/stable/event_handler' if defined?(::Rack::Events)
require_relative 'middlewares/stable/tracer_middleware'
end

def require_dependencies_dup
require_relative 'middlewares/dup/event_handler' if defined?(::Rack::Events)
require_relative 'middlewares/dup/tracer_middleware'
end

def config_options(user_config)
Expand Down
Loading
Loading