Skip to content

Commit 1719a91

Browse files
committed
squash: add url.template tests
1 parent 9c2d5b8 commit 1719a91

File tree

8 files changed

+162
-0
lines changed

8 files changed

+162
-0
lines changed

instrumentation/excon/test/opentelemetry/instrumentation/excon/dup/instrumentation_test.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
stub_request(:get, 'http://example.com/success?hello=there').to_return(status: 200)
2525
stub_request(:get, 'http://example.com/failure').to_return(status: 500)
2626
stub_request(:get, 'http://example.com/timeout').to_timeout
27+
stub_request(:get, 'http://example.com/users/123').to_return(status: 200)
2728

2829
# this is currently a noop but this will future proof the test
2930
@orig_propagation = OpenTelemetry.propagation
@@ -404,6 +405,24 @@
404405
_(exporter.finished_spans.size).must_equal(0)
405406
end
406407
end
408+
409+
it 'uses url.template in span name when present in client context' do
410+
client_context_attrs = { 'url.template' => '/users/{id}' }
411+
OpenTelemetry::Common::HTTP::ClientContext.with_attributes(client_context_attrs) do
412+
Excon.get('http://example.com/users/123')
413+
end
414+
415+
_(exporter.finished_spans.size).must_equal 1
416+
_(span.name).must_equal 'GET /users/{id}'
417+
_(span.attributes['http.method']).must_equal 'GET'
418+
_(span.attributes['http.request.method']).must_equal 'GET'
419+
_(span.attributes['url.template']).must_equal '/users/{id}'
420+
assert_requested(
421+
:get,
422+
'http://example.com/users/123',
423+
headers: { 'Traceparent' => "00-#{span.hex_trace_id}-#{span.hex_span_id}-01" }
424+
)
425+
end
407426
end
408427

409428
def assert_http_spans(scheme: 'http', host: 'localhost', port: nil, target: '/', exception: nil)

instrumentation/excon/test/opentelemetry/instrumentation/excon/stable/instrumentation_test.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
stub_request(:get, 'http://example.com/success?hello=there').to_return(status: 200)
2525
stub_request(:get, 'http://example.com/failure').to_return(status: 500)
2626
stub_request(:get, 'http://example.com/timeout').to_timeout
27+
stub_request(:get, 'http://example.com/users/123').to_return(status: 200)
2728

2829
# this is currently a noop but this will future proof the test
2930
@orig_propagation = OpenTelemetry.propagation
@@ -368,4 +369,23 @@ def assert_http_spans(scheme: 'http', host: 'localhost', port: nil, target: '/',
368369
end
369370
end
370371
end
372+
373+
describe 'url.template' do
374+
it 'uses url.template in span name when present in client context' do
375+
client_context_attrs = { 'url.template' => '/users/{id}' }
376+
OpenTelemetry::Common::HTTP::ClientContext.with_attributes(client_context_attrs) do
377+
Excon.get('http://example.com/users/123')
378+
end
379+
380+
_(exporter.finished_spans.size).must_equal 1
381+
_(span.name).must_equal 'GET /users/{id}'
382+
_(span.attributes['http.request.method']).must_equal 'GET'
383+
_(span.attributes['url.template']).must_equal '/users/{id}'
384+
assert_requested(
385+
:get,
386+
'http://example.com/users/123',
387+
headers: { 'Traceparent' => "00-#{span.hex_trace_id}-#{span.hex_span_id}-01" }
388+
)
389+
end
390+
end
371391
end

instrumentation/faraday/test/opentelemetry/instrumentation/faraday/middlewares/dup/tracer_middleware_test.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,5 +279,30 @@
279279
_(tracers).must_equal 1
280280
end
281281
end
282+
283+
describe 'url.template in span name' do
284+
let(:client) do
285+
Faraday.new('http://example.com') do |builder|
286+
builder.adapter(:test) do |stub|
287+
stub.get('/users/123') { |_| [200, {}, 'OK'] }
288+
end
289+
end
290+
end
291+
292+
it 'uses url.template in span name when present in client context' do
293+
client_context_attrs = { 'url.template' => '/users/{id}' }
294+
response = OpenTelemetry::Common::HTTP::ClientContext.with_attributes(client_context_attrs) do
295+
client.get('/users/123')
296+
end
297+
298+
_(span.name).must_equal 'GET /users/{id}'
299+
_(span.attributes['http.method']).must_equal 'GET'
300+
_(span.attributes['http.request.method']).must_equal 'GET'
301+
_(span.attributes['url.template']).must_equal '/users/{id}'
302+
_(response.env.request_headers['Traceparent']).must_equal(
303+
"00-#{span.hex_trace_id}-#{span.hex_span_id}-01"
304+
)
305+
end
306+
end
282307
end
283308
end

instrumentation/faraday/test/opentelemetry/instrumentation/faraday/middlewares/stable/tracer_middleware_test.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,5 +250,29 @@
250250
_(tracers).must_equal 1
251251
end
252252
end
253+
254+
describe 'url.template in span name' do
255+
let(:client) do
256+
Faraday.new('http://example.com') do |builder|
257+
builder.adapter(:test) do |stub|
258+
stub.get('/users/123') { |_| [200, {}, 'OK'] }
259+
end
260+
end
261+
end
262+
263+
it 'uses url.template in span name when present in client context' do
264+
client_context_attrs = { 'url.template' => '/users/{id}' }
265+
response = OpenTelemetry::Common::HTTP::ClientContext.with_attributes(client_context_attrs) do
266+
client.get('/users/123')
267+
end
268+
269+
_(span.name).must_equal 'GET /users/{id}'
270+
_(span.attributes['http.request.method']).must_equal 'GET'
271+
_(span.attributes['url.template']).must_equal '/users/{id}'
272+
_(response.env.request_headers['Traceparent']).must_equal(
273+
"00-#{span.hex_trace_id}-#{span.hex_span_id}-01"
274+
)
275+
end
276+
end
253277
end
254278
end

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
stub_request(:get, 'http://example.com/success?hello=there').to_return(status: 200)
3636
stub_request(:post, 'http://example.com/failure').to_return(status: 500)
3737
stub_request(:get, 'https://example.com/timeout').to_timeout
38+
stub_request(:get, 'http://example.com/users/123').to_return(status: 200)
3839
end
3940

4041
after do
@@ -272,5 +273,23 @@
272273
headers: { 'Traceparent' => "00-#{span.hex_trace_id}-#{span.hex_span_id}-01" }
273274
)
274275
end
276+
277+
it 'uses url.template in span name when present in client context' do
278+
client_context_attrs = { 'url.template' => '/users/{id}' }
279+
OpenTelemetry::Common::HTTP::ClientContext.with_attributes(client_context_attrs) do
280+
HTTP.get('http://example.com/users/123')
281+
end
282+
283+
_(exporter.finished_spans.size).must_equal 1
284+
_(span.name).must_equal 'GET /users/{id}'
285+
_(span.attributes['http.method']).must_equal 'GET'
286+
_(span.attributes['http.request.method']).must_equal 'GET'
287+
_(span.attributes['url.template']).must_equal '/users/{id}'
288+
assert_requested(
289+
:get,
290+
'http://example.com/users/123',
291+
headers: { 'Traceparent' => "00-#{span.hex_trace_id}-#{span.hex_span_id}-01" }
292+
)
293+
end
275294
end
276295
end

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
stub_request(:get, 'http://example.com/success?hello=there').to_return(status: 200)
3636
stub_request(:post, 'http://example.com/failure').to_return(status: 500)
3737
stub_request(:get, 'https://example.com/timeout').to_timeout
38+
stub_request(:get, 'http://example.com/users/123').to_return(status: 200)
3839
end
3940

4041
after do
@@ -214,5 +215,22 @@
214215
headers: { 'Traceparent' => "00-#{span.hex_trace_id}-#{span.hex_span_id}-01" }
215216
)
216217
end
218+
219+
it 'uses url.template in span name when present in client context' do
220+
client_context_attrs = { 'url.template' => '/users/{id}' }
221+
OpenTelemetry::Common::HTTP::ClientContext.with_attributes(client_context_attrs) do
222+
HTTP.get('http://example.com/users/123')
223+
end
224+
225+
_(exporter.finished_spans.size).must_equal 1
226+
_(span.name).must_equal 'GET /users/{id}'
227+
_(span.attributes['http.request.method']).must_equal 'GET'
228+
_(span.attributes['url.template']).must_equal '/users/{id}'
229+
assert_requested(
230+
:get,
231+
'http://example.com/users/123',
232+
headers: { 'Traceparent' => "00-#{span.hex_trace_id}-#{span.hex_span_id}-01" }
233+
)
234+
end
217235
end
218236
end

instrumentation/net_http/test/opentelemetry/instrumentation/net/http/dup/instrumentation_test.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
stub_request(:get, 'http://example.com/success?hello=there').to_return(status: 200)
2424
stub_request(:post, 'http://example.com/failure').to_return(status: 500)
2525
stub_request(:get, 'https://example.com/timeout').to_timeout
26+
stub_request(:get, 'http://example.com/users/123').to_return(status: 200)
2627

2728
# this is currently a noop but this will future proof the test
2829
@orig_propagation = OpenTelemetry.propagation
@@ -381,6 +382,24 @@ def fake_socket.close; end
381382
WebMock.disable_net_connect!
382383
end
383384

385+
it 'uses url.template in span name when present in client context' do
386+
client_context_attrs = { 'url.template' => '/users/{id}' }
387+
OpenTelemetry::Common::HTTP::ClientContext.with_attributes(client_context_attrs) do
388+
Net::HTTP.get('example.com', '/users/123')
389+
end
390+
391+
_(exporter.finished_spans.size).must_equal 1
392+
_(span.name).must_equal 'GET /users/{id}'
393+
_(span.attributes['http.method']).must_equal 'GET'
394+
_(span.attributes['http.request.method']).must_equal 'GET'
395+
_(span.attributes['url.template']).must_equal '/users/{id}'
396+
assert_requested(
397+
:get,
398+
'http://example.com/users/123',
399+
headers: { 'Traceparent' => "00-#{span.hex_trace_id}-#{span.hex_span_id}-01" }
400+
)
401+
end
402+
384403
it 'emits a "connect" span when connecting through an non-ssl proxy' do
385404
WebMock.allow_net_connect!
386405

instrumentation/net_http/test/opentelemetry/instrumentation/net/http/stable/instrumentation_test.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
stub_request(:get, 'http://example.com/success?hello=there').to_return(status: 200)
2424
stub_request(:post, 'http://example.com/failure').to_return(status: 500)
2525
stub_request(:get, 'https://example.com/timeout').to_timeout
26+
stub_request(:get, 'http://example.com/users/123').to_return(status: 200)
2627

2728
# this is currently a noop but this will future proof the test
2829
@orig_propagation = OpenTelemetry.propagation
@@ -355,5 +356,22 @@ def fake_socket.close; end
355356
ensure
356357
WebMock.disable_net_connect!
357358
end
359+
360+
it 'uses url.template in span name when present in client context' do
361+
client_context_attrs = { 'url.template' => '/users/{id}' }
362+
OpenTelemetry::Common::HTTP::ClientContext.with_attributes(client_context_attrs) do
363+
Net::HTTP.get('example.com', '/users/123')
364+
end
365+
366+
_(exporter.finished_spans.size).must_equal 1
367+
_(span.name).must_equal 'GET /users/{id}'
368+
_(span.attributes['http.request.method']).must_equal 'GET'
369+
_(span.attributes['url.template']).must_equal '/users/{id}'
370+
assert_requested(
371+
:get,
372+
'http://example.com/users/123',
373+
headers: { 'Traceparent' => "00-#{span.hex_trace_id}-#{span.hex_span_id}-01" }
374+
)
375+
end
358376
end
359377
end

0 commit comments

Comments
 (0)