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
3 changes: 2 additions & 1 deletion instrumentation/rack/example/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ source 'https://rubygems.org'

gem 'opentelemetry-api'
gem 'opentelemetry-common'
gem 'opentelemetry-instrumentation-rack'
gem 'opentelemetry-instrumentation-rack', path: '../'
gem 'opentelemetry-sdk'
gem 'opentelemetry-sdk-experimental'
gem 'rack'
gem 'rack-test'
31 changes: 31 additions & 0 deletions instrumentation/rack/example/trace_demonstration3.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

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

# This uses the response_propagators option. ResponseTextMapPropagator is from the opentelemetry-sdk-experimental gem
require 'rubygems'
require 'bundler/setup'

Bundler.require

ENV['OTEL_TRACES_EXPORTER'] = 'console'
OpenTelemetry::SDK.configure do |c
c.use 'OpenTelemetry::Instrumentation::Rack', { response_propagators: [OpenTelemetry::Trace::Propagation::TraceContext::ResponseTextMapPropagator.new] }
end

# setup fake rack application:
builder = Rack::Builder.app do
# integration should be automatic in web frameworks (like rails),
# but for a plain Rack application, enable it in your config.ru, e.g.,
use OpenTelemetry::Instrumentation::Rack::Middlewares::TracerMiddleware

app = ->(_env) { [200, { 'Content-Type' => 'text/plain' }, ['All responses are OK']] }
run app
end

# demonstrate tracing (span output to console):
res = Rack::MockRequest.new(builder).get('/')
puts res
puts res.headers
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base
option :untraced_endpoints, default: [], validate: :array
option :url_quantization, default: nil, validate: :callable
option :untraced_requests, default: nil, validate: :callable
option :response_propagators, default: [], validate: :array

private

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def call(env) # rubocop:disable Metrics/MethodLength
OpenTelemetry::Instrumentation::Rack.with_span(request_span) do
@app.call(env).tap do |status, headers, response|
set_attributes_after_request(request_span, status, headers, response)
config[:response_propagators].each { |propagator| propagator.inject(headers) }
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'bundler', '>= 1.17'
spec.add_development_dependency 'minitest', '~> 5.0'
spec.add_development_dependency 'opentelemetry-sdk', '~> 1.1'
spec.add_development_dependency 'opentelemetry-sdk-experimental', '~> 0.1'
spec.add_development_dependency 'opentelemetry-test-helpers'
spec.add_development_dependency 'rack', '~> 2.0.8'
spec.add_development_dependency 'rack-test', '~> 1.1.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,40 @@
end
end

describe 'config[:response_propagators]' do
describe 'with default options' do
it 'does not inject the traceresponse header' do
res = Rack::MockRequest.new(rack_builder).get('/ping', env)
_(res.headers).wont_include('traceresponse')
end
end

describe 'with ResponseTextMapPropagator' do
let(:config) { default_config.merge(response_propagators: [OpenTelemetry::Trace::Propagation::TraceContext::ResponseTextMapPropagator.new]) }

it 'injects the traceresponse header' do
res = Rack::MockRequest.new(rack_builder).get('/ping', env)
_(res.headers).must_include('traceresponse')
end
end

describe 'propagator throws' do
class MockPropagator < OpenTelemetry::Trace::Propagation::TraceContext::ResponseTextMapPropagator
def inject(carrier)
raise 'Injection failed'
end
end

let(:config) { default_config.merge(response_propagators: [MockPropagator.new]) }

it 'leads to application errors when there are exceptions' do
assert_raises RuntimeError do
Rack::MockRequest.new(rack_builder).get('/ping', env)
end
end
end
end

describe '#call with error' do
SimulatedError = Class.new(StandardError)

Expand Down
1 change: 1 addition & 0 deletions instrumentation/rack/test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require 'rack'

require 'opentelemetry/sdk'
require 'opentelemetry-sdk-experimental'
require 'opentelemetry-test-helpers'

require 'pry'
Expand Down