Skip to content

Commit 4ccf10a

Browse files
committed
feat: HTTP Span Name Updates
1 parent 44337aa commit 4ccf10a

File tree

11 files changed

+49
-50
lines changed

11 files changed

+49
-50
lines changed

.rubocop.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ Style/MultilineIfModifier:
5353
- "**/test/**/*"
5454
Style/RedundantRegexpEscape:
5555
Enabled: false
56+
Style/RedundantReturn:
57+
Enabled: false
5658
Style/StringLiterals:
5759
Exclude:
5860
- "**/gemfiles/*"

instrumentation/excon/lib/opentelemetry/instrumentation/excon.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ module OpenTelemetry
1111
module Instrumentation
1212
# Contains the OpenTelemetry instrumentation for the Excon gem
1313
module Excon
14+
def self.span_name(attrs)
15+
http_method = attrs['http.request.method'] || attrs['http.method']
16+
url_template = attrs['url.template']
17+
return "#{http_method} #{url_template}" if url_template && http_method
18+
return url_template if url_template
19+
return http_method if http_method
20+
21+
return 'HTTP' # Fallback span name for cases where the HTTP method is _OTHER or unknown
22+
end
1423
end
1524
end
1625
end

instrumentation/excon/lib/opentelemetry/instrumentation/excon/middlewares/dup/tracer_middleware.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ class TracerMiddleware < ::Excon::Middleware::Base
1818
hash[uppercase_method] = uppercase_method
1919
end.freeze
2020

21-
HTTP_METHODS_TO_SPAN_NAMES = HTTP_METHODS_TO_UPPERCASE.values.each_with_object({}) do |uppercase_method, hash|
22-
hash[uppercase_method] ||= uppercase_method
23-
end.freeze
24-
2521
# Constant for the HTTP status range
2622
HTTP_STATUS_SUCCESS_RANGE = (100..399)
2723

@@ -49,7 +45,7 @@ def request_call(datum)
4945
peer_service = Excon::Instrumentation.instance.config[:peer_service]
5046
attributes[OpenTelemetry::SemanticConventions::Trace::PEER_SERVICE] = peer_service if peer_service
5147
attributes.merge!(OpenTelemetry::Common::HTTP::ClientContext.attributes)
52-
span = tracer.start_span(HTTP_METHODS_TO_SPAN_NAMES[http_method], attributes: attributes, kind: :client)
48+
span = tracer.start_span(OpenTelemetry::Instrumentation::Excon.span_name(attributes), attributes: attributes, kind: :client)
5349
ctx = OpenTelemetry::Trace.context_with_span(span)
5450
datum[:otel_span] = span
5551
datum[:otel_token] = OpenTelemetry::Context.attach(ctx)

instrumentation/excon/lib/opentelemetry/instrumentation/excon/middlewares/old/tracer_middleware.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ class TracerMiddleware < ::Excon::Middleware::Base
1818
hash[uppercase_method] = uppercase_method
1919
end.freeze
2020

21-
HTTP_METHODS_TO_SPAN_NAMES = HTTP_METHODS_TO_UPPERCASE.values.each_with_object({}) do |uppercase_method, hash|
22-
hash[uppercase_method] ||= "HTTP #{uppercase_method}"
23-
end.freeze
24-
2521
# Constant for the HTTP status range
2622
HTTP_STATUS_SUCCESS_RANGE = (100..399)
2723

@@ -41,7 +37,7 @@ def request_call(datum)
4137
peer_service = Excon::Instrumentation.instance.config[:peer_service]
4238
attributes[OpenTelemetry::SemanticConventions::Trace::PEER_SERVICE] = peer_service if peer_service
4339
attributes.merge!(OpenTelemetry::Common::HTTP::ClientContext.attributes)
44-
span = tracer.start_span(HTTP_METHODS_TO_SPAN_NAMES[http_method], attributes: attributes, kind: :client)
40+
span = tracer.start_span(OpenTelemetry::Instrumentation::Excon.span_name(attributes), attributes: attributes, kind: :client)
4541
ctx = OpenTelemetry::Trace.context_with_span(span)
4642
datum[:otel_span] = span
4743
datum[:otel_token] = OpenTelemetry::Context.attach(ctx)

instrumentation/excon/lib/opentelemetry/instrumentation/excon/middlewares/stable/tracer_middleware.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ class TracerMiddleware < ::Excon::Middleware::Base
1818
hash[uppercase_method] = uppercase_method
1919
end.freeze
2020

21-
HTTP_METHODS_TO_SPAN_NAMES = HTTP_METHODS_TO_UPPERCASE.values.each_with_object({}) do |uppercase_method, hash|
22-
hash[uppercase_method] ||= uppercase_method
23-
end.freeze
24-
2521
# Constant for the HTTP status range
2622
HTTP_STATUS_SUCCESS_RANGE = (100..399)
2723

@@ -41,7 +37,7 @@ def request_call(datum)
4137
peer_service = Excon::Instrumentation.instance.config[:peer_service]
4238
attributes[OpenTelemetry::SemanticConventions::Trace::PEER_SERVICE] = peer_service if peer_service
4339
attributes.merge!(OpenTelemetry::Common::HTTP::ClientContext.attributes)
44-
span = tracer.start_span(HTTP_METHODS_TO_SPAN_NAMES[http_method], attributes: attributes, kind: :client)
40+
span = tracer.start_span(OpenTelemetry::Instrumentation::Excon.span_name(attributes), attributes: attributes, kind: :client)
4541
ctx = OpenTelemetry::Trace.context_with_span(span)
4642
datum[:otel_span] = span
4743
datum[:otel_token] = OpenTelemetry::Context.attach(ctx)

instrumentation/excon/lib/opentelemetry/instrumentation/excon/patches/dup/socket.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def connect
3535
span_name = 'CONNECT'
3636
span_kind = :client
3737
else
38-
span_name = 'connect'
38+
span_name = 'tcp connect'
3939
span_kind = :internal
4040
end
4141

instrumentation/excon/lib/opentelemetry/instrumentation/excon/patches/old/socket.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ def connect
2727
attributes = { OpenTelemetry::SemanticConventions::Trace::NET_PEER_NAME => conn_address, OpenTelemetry::SemanticConventions::Trace::NET_PEER_PORT => conn_port }.merge!(OpenTelemetry::Common::HTTP::ClientContext.attributes)
2828

2929
if is_a?(::Excon::SSLSocket) && @data[:proxy]
30-
span_name = 'HTTP CONNECT'
30+
span_name = 'CONNECT'
3131
span_kind = :client
3232
else
33-
span_name = 'connect'
33+
span_name = 'tcp connect'
3434
span_kind = :internal
3535
end
3636

instrumentation/excon/lib/opentelemetry/instrumentation/excon/patches/stable/socket.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def connect
3030
span_name = 'CONNECT'
3131
span_kind = :client
3232
else
33-
span_name = 'connect'
33+
span_name = 'tcp connect'
3434
span_kind = :internal
3535
end
3636

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -138,23 +138,23 @@
138138

139139
it 'merges HTTP client context' do
140140
client_context_attrs = {
141-
'test.attribute' => 'test.value', 'http.method' => 'OVERRIDE', 'http.request.method' => 'OVERRIDE'
141+
'test.attribute' => 'test.value', 'http.method' => 'PATCH', 'http.request.method' => 'PATCH'
142142
}
143143
OpenTelemetry::Common::HTTP::ClientContext.with_attributes(client_context_attrs) do
144144
Excon.get('http://example.com/success')
145145
end
146146

147147
_(exporter.finished_spans.size).must_equal 1
148-
_(span.name).must_equal 'GET'
148+
_(span.name).must_equal 'PATCH'
149149
_(span.attributes['http.host']).must_equal 'example.com'
150-
_(span.attributes['http.method']).must_equal 'OVERRIDE'
150+
_(span.attributes['http.method']).must_equal 'PATCH'
151151
_(span.attributes['http.scheme']).must_equal 'http'
152152
_(span.attributes['http.status_code']).must_equal 200
153153
_(span.attributes['http.target']).must_equal '/success'
154154
_(span.attributes['http.url']).must_equal 'http://example.com/success'
155155
_(span.attributes['test.attribute']).must_equal 'test.value'
156156
# stable semconv
157-
_(span.attributes['http.request.method']).must_equal 'OVERRIDE'
157+
_(span.attributes['http.request.method']).must_equal 'PATCH'
158158
_(span.attributes['url.scheme']).must_equal 'http'
159159
_(span.attributes['http.response.status_code']).must_equal 200
160160
_(span.attributes['url.path']).must_equal '/success'
@@ -257,7 +257,7 @@
257257
Excon::Socket.new(hostname: uri.host, port: uri.port)
258258

259259
_(exporter.finished_spans.size).must_equal 1
260-
_(span.name).must_equal('connect')
260+
_(span.name).must_equal('tcp connect')
261261
_(span.kind).must_equal(:internal)
262262
_(span.attributes['net.peer.name']).must_equal('example.com')
263263
_(span.attributes['net.peer.port']).must_equal(80)
@@ -293,7 +293,7 @@
293293
end
294294

295295
_(exporter.finished_spans.size).must_equal(3)
296-
_(span.name).must_equal 'connect'
296+
_(span.name).must_equal 'tcp connect'
297297
_(span.attributes['net.peer.name']).must_equal('localhost')
298298
_(span.attributes['net.peer.port']).wont_be_nil
299299
_(span.attributes['net.peer.port']).must_equal(port)
@@ -309,7 +309,7 @@
309309
_(-> { Excon.get('http://invalid.com:99999/example') }).must_raise
310310

311311
_(exporter.finished_spans.size).must_equal(3)
312-
_(span.name).must_equal 'connect'
312+
_(span.name).must_equal 'tcp connect'
313313
_(span.attributes['net.peer.name']).must_equal('invalid.com')
314314
_(span.attributes['net.peer.port']).must_equal(99_999)
315315
# stable semconv
@@ -328,7 +328,7 @@
328328
_(-> { Excon.get('http://localhost/', proxy: 'https://proxy_user:proxy_pass@localhost') }).must_raise(Excon::Error::Socket)
329329

330330
_(exporter.finished_spans.size).must_equal(3)
331-
_(span.name).must_equal 'connect'
331+
_(span.name).must_equal 'tcp connect'
332332
_(span.kind).must_equal(:internal)
333333
_(span.attributes['net.peer.name']).must_equal('localhost')
334334
_(span.attributes['net.peer.port']).must_equal(443)
@@ -358,7 +358,7 @@
358358
_(-> { Excon.get('http://localhost', proxy: 'https://proxy_user:proxy_pass@localhost') }).must_raise(Excon::Error::Socket)
359359

360360
_(exporter.finished_spans.size).must_equal(3)
361-
_(span.name).must_equal 'connect'
361+
_(span.name).must_equal 'tcp connect'
362362
_(span.kind).must_equal(:internal)
363363
_(span.attributes['net.peer.name']).must_equal('localhost')
364364
_(span.attributes['net.peer.port']).must_equal(443)

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
Excon.get('http://example.com/success')
5050

5151
_(exporter.finished_spans.size).must_equal 1
52-
_(span.name).must_equal 'HTTP GET'
52+
_(span.name).must_equal 'GET'
5353
_(span.attributes['http.host']).must_equal 'example.com'
5454
_(span.attributes['http.method']).must_equal 'GET'
5555
_(span.attributes['http.scheme']).must_equal 'http'
@@ -73,7 +73,7 @@
7373
Excon.get('http://example.com/failure')
7474

7575
_(exporter.finished_spans.size).must_equal 1
76-
_(span.name).must_equal 'HTTP GET'
76+
_(span.name).must_equal 'GET'
7777
_(span.attributes['http.host']).must_equal 'example.com'
7878
_(span.attributes['http.method']).must_equal 'GET'
7979
_(span.attributes['http.scheme']).must_equal 'http'
@@ -93,7 +93,7 @@
9393
end.must_raise Excon::Error::Timeout
9494

9595
_(exporter.finished_spans.size).must_equal 1
96-
_(span.name).must_equal 'HTTP GET'
96+
_(span.name).must_equal 'GET'
9797
_(span.attributes['http.host']).must_equal 'example.com'
9898
_(span.attributes['http.method']).must_equal 'GET'
9999
_(span.attributes['http.scheme']).must_equal 'http'
@@ -116,16 +116,16 @@
116116

117117
it 'merges HTTP client context' do
118118
client_context_attrs = {
119-
'test.attribute' => 'test.value', 'http.method' => 'OVERRIDE'
119+
'test.attribute' => 'test.value', 'http.method' => 'PATCH'
120120
}
121121
OpenTelemetry::Common::HTTP::ClientContext.with_attributes(client_context_attrs) do
122122
Excon.get('http://example.com/success')
123123
end
124124

125125
_(exporter.finished_spans.size).must_equal 1
126-
_(span.name).must_equal 'HTTP GET'
126+
_(span.name).must_equal 'PATCH'
127127
_(span.attributes['http.host']).must_equal 'example.com'
128-
_(span.attributes['http.method']).must_equal 'OVERRIDE'
128+
_(span.attributes['http.method']).must_equal 'PATCH'
129129
_(span.attributes['http.scheme']).must_equal 'http'
130130
_(span.attributes['http.status_code']).must_equal 200
131131
_(span.attributes['http.target']).must_equal '/success'
@@ -204,7 +204,7 @@
204204
Excon.get('http://example.com/body')
205205

206206
_(exporter.finished_spans.size).must_equal 1
207-
_(span.name).must_equal 'HTTP GET'
207+
_(span.name).must_equal 'GET'
208208
_(span.attributes['http.host']).must_equal 'example.com'
209209
_(span.attributes['http.method']).must_equal 'GET'
210210
end
@@ -215,7 +215,7 @@
215215
Excon::Socket.new(hostname: uri.host, port: uri.port)
216216

217217
_(exporter.finished_spans.size).must_equal 1
218-
_(span.name).must_equal('connect')
218+
_(span.name).must_equal('tcp connect')
219219
_(span.kind).must_equal(:internal)
220220
_(span.attributes['net.peer.name']).must_equal('example.com')
221221
_(span.attributes['net.peer.port']).must_equal(80)
@@ -249,7 +249,7 @@
249249
end
250250

251251
_(exporter.finished_spans.size).must_equal(3)
252-
_(span.name).must_equal 'connect'
252+
_(span.name).must_equal 'tcp connect'
253253
_(span.attributes['net.peer.name']).must_equal('localhost')
254254
_(span.attributes['net.peer.port']).wont_be_nil
255255
_(span.attributes['net.peer.port']).must_equal(port)
@@ -261,7 +261,7 @@
261261
_(-> { Excon.get('http://invalid.com:99999/example') }).must_raise
262262

263263
_(exporter.finished_spans.size).must_equal(3)
264-
_(span.name).must_equal 'connect'
264+
_(span.name).must_equal 'tcp connect'
265265
_(span.attributes['net.peer.name']).must_equal('invalid.com')
266266
_(span.attributes['net.peer.port']).must_equal(99_999)
267267

@@ -277,7 +277,7 @@
277277
_(-> { Excon.get('http://localhost/', proxy: 'https://proxy_user:proxy_pass@localhost') }).must_raise(Excon::Error::Socket)
278278

279279
_(exporter.finished_spans.size).must_equal(3)
280-
_(span.name).must_equal 'connect'
280+
_(span.name).must_equal 'tcp connect'
281281
_(span.kind).must_equal(:internal)
282282
_(span.attributes['net.peer.name']).must_equal('localhost')
283283
_(span.attributes['net.peer.port']).must_equal(443)
@@ -289,7 +289,7 @@
289289
_(-> { Excon.get('https://localhost/', proxy: 'https://proxy_user:proxy_pass@localhost') }).must_raise(Excon::Error::Socket)
290290

291291
_(exporter.finished_spans.size).must_equal(3)
292-
_(span.name).must_equal 'HTTP CONNECT'
292+
_(span.name).must_equal 'CONNECT'
293293
_(span.kind).must_equal(:client)
294294
_(span.attributes['net.peer.name']).must_equal('localhost')
295295
_(span.attributes['net.peer.port']).must_equal(443)
@@ -301,7 +301,7 @@
301301
_(-> { Excon.get('http://localhost', proxy: 'https://proxy_user:proxy_pass@localhost') }).must_raise(Excon::Error::Socket)
302302

303303
_(exporter.finished_spans.size).must_equal(3)
304-
_(span.name).must_equal 'connect'
304+
_(span.name).must_equal 'tcp connect'
305305
_(span.kind).must_equal(:internal)
306306
_(span.attributes['net.peer.name']).must_equal('localhost')
307307
_(span.attributes['net.peer.port']).must_equal(443)
@@ -320,7 +320,7 @@
320320

321321
def assert_http_spans(scheme: 'http', host: 'localhost', port: nil, target: '/', exception: nil)
322322
exporter.finished_spans[1..].each do |http_span|
323-
_(http_span.name).must_equal 'HTTP GET'
323+
_(http_span.name).must_equal 'GET'
324324
_(http_span.attributes['http.host']).must_equal host
325325
_(http_span.attributes['http.method']).must_equal 'GET'
326326
_(http_span.attributes['http.scheme']).must_equal scheme

0 commit comments

Comments
 (0)