Skip to content

Commit 62ce61e

Browse files
committed
Add tests for environment variable
1 parent f1c9701 commit 62ce61e

File tree

7 files changed

+83
-6
lines changed

7 files changed

+83
-6
lines changed

instrumentation/http/lib/opentelemetry/instrumentation/http/instrumentation.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base
2222
option :span_name_formatter, default: nil, validate: :callable
2323

2424
def determine_semconv
25-
stability_opt_in = ENV.fetch('OTEL_SEMCONV_STABILITY_OPT_IN', nil)
26-
values = stability_opt_in&.split(',')
25+
stability_opt_in = ENV.fetch('OTEL_SEMCONV_STABILITY_OPT_IN', '')
26+
values = stability_opt_in.split(',').map(&:strip)
2727

28-
if values&.include?('http/dup')
28+
if values.include?('http/dup')
2929
'dup'
30-
elsif values&.include?('http')
30+
elsif values.include?('http')
3131
'stable'
3232
else
3333
'old'

instrumentation/http/test/instrumentation/http/patches/dup/client_test.rb

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
before do
2424
skip unless ENV['BUNDLE_GEMFILE'].include?('dup')
2525

26-
ENV['OTEL_SEMCONV_STABILITY_OPT_IN'] = 'http/dup, database'
26+
ENV['OTEL_SEMCONV_STABILITY_OPT_IN'] = 'http/dup'
2727
exporter.reset
2828
@orig_propagation = OpenTelemetry.propagation
2929
propagator = OpenTelemetry::Trace::Propagation::TraceContext.text_map_propagator
@@ -44,6 +44,29 @@
4444
OpenTelemetry.propagation = @orig_propagation
4545
end
4646

47+
describe 'installation' do
48+
it 'applies the correct patch when stability options include only http/dup and database' do
49+
ENV['OTEL_SEMCONV_STABILITY_OPT_IN'] = 'http/dup, database'
50+
51+
# simulate a fresh install:
52+
instrumentation.instance_variable_set(:@installed, false)
53+
instrumentation.install(config)
54+
55+
_(HTTP::Client.ancestors).must_include OpenTelemetry::Instrumentation::HTTP::Patches::Dup::Client
56+
end
57+
58+
it 'applies the http/dup patch and excludes the Stable patch when both http and http/dup are present' do
59+
ENV['OTEL_SEMCONV_STABILITY_OPT_IN'] = 'http, http/dup'
60+
61+
# simulate a fresh install:
62+
instrumentation.instance_variable_set(:@installed, false)
63+
instrumentation.install(config)
64+
65+
_(HTTP::Client.ancestors).must_include OpenTelemetry::Instrumentation::HTTP::Patches::Dup::Client
66+
_(HTTP::Client.ancestors).wont_include OpenTelemetry::Instrumentation::HTTP::Patches::Stable::Client
67+
end
68+
end
69+
4770
describe '#perform' do
4871
it 'traces a simple request' do
4972
HTTP.get('http://example.com/success')

instrumentation/http/test/instrumentation/http/patches/dup/connection_test.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
before do
1818
skip unless ENV['BUNDLE_GEMFILE'].include?('dup')
1919

20-
ENV['OTEL_SEMCONV_STABILITY_OPT_IN'] = 'http/dup, database'
20+
ENV['OTEL_SEMCONV_STABILITY_OPT_IN'] = 'http/dup'
2121
exporter.reset
2222
instrumentation.install({})
2323
end
@@ -28,6 +28,15 @@
2828
instrumentation.instance_variable_set(:@installed, false)
2929
end
3030

31+
describe 'installation' do
32+
it 'installs the patch when env var has multiple configs' do
33+
ENV['OTEL_SEMCONV_STABILITY_OPT_IN'] = 'http/dup, database'
34+
instrumentation.install({}) # simulate a fresh install:
35+
36+
_(HTTP::Connection.ancestors).must_include OpenTelemetry::Instrumentation::HTTP::Patches::Dup::Connection
37+
end
38+
end
39+
3140
describe '#connect' do
3241
it 'emits span on connect' do
3342
WebMock.allow_net_connect!

instrumentation/http/test/instrumentation/http/patches/old/client_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@
4242
OpenTelemetry.propagation = @orig_propagation
4343
end
4444

45+
describe 'installation' do
46+
it 'applies the correct patch when stability options do not include HTTP stability modes' do
47+
ENV['OTEL_SEMCONV_STABILITY_OPT_IN'] = 'database'
48+
49+
# simulate a fresh install:
50+
instrumentation.instance_variable_set(:@installed, false)
51+
instrumentation.install(config)
52+
53+
_(HTTP::Client.ancestors).must_include OpenTelemetry::Instrumentation::HTTP::Patches::Old::Client
54+
end
55+
end
56+
4557
describe '#perform' do
4658
it 'traces a simple request' do
4759
HTTP.get('http://example.com/success')

instrumentation/http/test/instrumentation/http/patches/old/connection.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@
2424
# Force re-install of instrumentation
2525
after { instrumentation.instance_variable_set(:@installed, false) }
2626

27+
describe 'installation' do
28+
it 'applies the correct patch when stability options do not include HTTP stability modes' do
29+
ENV['OTEL_SEMCONV_STABILITY_OPT_IN'] = 'database'
30+
31+
# simulate a fresh install:
32+
instrumentation.instance_variable_set(:@installed, false)
33+
instrumentation.install(config)
34+
35+
_(HTTP::Connection.ancestors).must_include OpenTelemetry::Instrumentation::HTTP::Patches::Old::Connection
36+
end
37+
end
38+
2739
describe '#connect' do
2840
it 'emits span on connect' do
2941
WebMock.allow_net_connect!

instrumentation/http/test/instrumentation/http/patches/stable/client_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@
4545
OpenTelemetry.propagation = @orig_propagation
4646
end
4747

48+
describe 'installation' do
49+
it 'applies the correct patch when stability options include only http/dup and database' do
50+
ENV['OTEL_SEMCONV_STABILITY_OPT_IN'] = 'http, database'
51+
52+
# simulate a fresh install:
53+
instrumentation.instance_variable_set(:@installed, false)
54+
instrumentation.install(config)
55+
56+
_(HTTP::Client.ancestors).must_include OpenTelemetry::Instrumentation::HTTP::Patches::Stable::Client
57+
end
58+
end
59+
4860
describe '#perform' do
4961
it 'traces a simple request' do
5062
HTTP.get('http://example.com/success')

instrumentation/http/test/instrumentation/http/patches/stable/connection.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@
2828
instrumentation.instance_variable_set(:@installed, false)
2929
end
3030

31+
describe 'installation' do
32+
it 'installs the patch when env var has multiple configs' do
33+
ENV['OTEL_SEMCONV_STABILITY_OPT_IN'] = 'http/dup, database'
34+
instrumentation.install({}) # simulate a fresh install:
35+
36+
_(HTTP::Connection.ancestors).must_include OpenTelemetry::Instrumentation::HTTP::Patches::Dup::Connection
37+
end
38+
end
39+
3140
describe '#connect' do
3241
it 'emits span on connect' do
3342
WebMock.allow_net_connect!

0 commit comments

Comments
 (0)