Skip to content

Commit ed9b71c

Browse files
committed
Add stable and dupe patch files
1 parent c5395f9 commit ed9b71c

File tree

9 files changed

+361
-107
lines changed

9 files changed

+361
-107
lines changed

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

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,38 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base
2121
option :span_name_formatter, default: nil, validate: :callable
2222

2323
def patch
24-
::HTTP::Client.prepend(Patches::Client)
25-
::HTTP::Connection.prepend(Patches::Connection)
24+
case ENV['OTEL_SEMCONV_STABILITY_OPT_IN']
25+
when 'http'
26+
patch_stable_semconv
27+
when 'http/dup'
28+
patch_dupe_semconv
29+
else
30+
patch_old_semconv
31+
end
32+
end
33+
34+
def patch_old_semconv
35+
::HTTP::Client.prepend(Patches::Client::Old)
36+
::HTTP::Connection.prepend(Patches::Connection::Old)
37+
end
38+
39+
def patch_dupe_semconv
40+
::HTTP::Client.prepend(Patches::Client::Dupe)
41+
::HTTP::Connection.prepend(Patches::Connection::Dupe)
42+
end
43+
44+
def patch_stable_semconv
45+
::HTTP::Client.prepend(Patches::Client::Stable)
46+
::HTTP::Connection.prepend(Patches::Connection::Stable)
2647
end
2748

2849
def require_dependencies
29-
require_relative 'patches/client'
30-
require_relative 'patches/connection'
50+
require_relative 'patches/dupe_semconv_client'
51+
require_relative 'patches/dupe_semconv_connection'
52+
require_relative 'patches/old_semconv_client'
53+
require_relative 'patches/old_semconv_connection'
54+
require_relative 'patches/stable_semconv_client'
55+
require_relative 'patches/stable_semconv_connection'
3156
end
3257
end
3358
end

instrumentation/http/lib/opentelemetry/instrumentation/http/patches/client.rb

Lines changed: 0 additions & 70 deletions
This file was deleted.

instrumentation/http/lib/opentelemetry/instrumentation/http/patches/connection.rb

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# frozen_string_literal: true
2+
3+
# Copyright The OpenTelemetry Authors
4+
#
5+
# SPDX-License-Identifier: Apache-2.0
6+
7+
module OpenTelemetry
8+
module Instrumentation
9+
module HTTP
10+
module Patches
11+
# Module to prepend to HTTP::Client for instrumentation
12+
module Client
13+
module Dupe
14+
# Constant for the HTTP status range
15+
HTTP_STATUS_SUCCESS_RANGE = (100..399)
16+
17+
def perform(req, options)
18+
uri = req.uri
19+
request_method = req.verb.to_s.upcase
20+
span_name = create_request_span_name(request_method, uri.path)
21+
22+
attributes = {
23+
'http.method' => request_method,
24+
'http.request.method' => request_method,
25+
'http.scheme' => uri.scheme,
26+
'url.scheme' => uri.scheme,
27+
'http.target' => uri.path,
28+
'url.path' => uri.path,
29+
'url.query' => uri.query,
30+
'http.url' => "#{uri.scheme}://#{uri.host}",
31+
'url.full' => "#{uri.scheme}://#{uri.host}",
32+
'net.peer.name' => uri.host,
33+
'server.address' => uri.host,
34+
'net.peer.port' => uri.port,
35+
'server.port' => uri.port
36+
}.merge!(OpenTelemetry::Common::HTTP::ClientContext.attributes)
37+
38+
tracer.in_span(span_name, attributes: attributes, kind: :client) do |span|
39+
OpenTelemetry.propagation.inject(req.headers)
40+
super.tap do |response|
41+
annotate_span_with_response!(span, response)
42+
end
43+
end
44+
end
45+
46+
private
47+
48+
def config
49+
OpenTelemetry::Instrumentation::HTTP::Instrumentation.instance.config
50+
end
51+
52+
def annotate_span_with_response!(span, response)
53+
return unless response&.status
54+
55+
status_code = response.status.to_i
56+
span.set_attribute('http.status_code', status_code)
57+
span.set_attribute('http.response.status_code', status_code)
58+
span.status = OpenTelemetry::Trace::Status.error unless HTTP_STATUS_SUCCESS_RANGE.cover?(status_code)
59+
end
60+
61+
def create_request_span_name(request_method, request_path)
62+
if (implementation = config[:span_name_formatter])
63+
updated_span_name = implementation.call(request_method, request_path)
64+
updated_span_name.is_a?(String) ? updated_span_name : "HTTP #{request_method}"
65+
else
66+
"HTTP #{request_method}"
67+
end
68+
rescue StandardError
69+
"HTTP #{request_method}"
70+
end
71+
72+
def tracer
73+
HTTP::Instrumentation.instance.tracer
74+
end
75+
end
76+
end
77+
end
78+
end
79+
end
80+
end
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# frozen_string_literal: true
2+
3+
# Copyright The OpenTelemetry Authors
4+
#
5+
# SPDX-License-Identifier: Apache-2.0
6+
7+
module OpenTelemetry
8+
module Instrumentation
9+
module HTTP
10+
module Patches
11+
# Module to prepend to HTTP::Connection for instrumentation
12+
module Connection
13+
module Dupe
14+
def initialize(req, options)
15+
attributes = {
16+
'net.peer.name' => req.uri.host,
17+
'server.address' => req.uri.host,
18+
'net.peer.port' => req.uri.port,
19+
'server.port' => req.uri.port
20+
}.merge!(OpenTelemetry::Common::HTTP::ClientContext.attributes)
21+
22+
tracer.in_span('HTTP CONNECT', attributes: attributes) do
23+
super
24+
end
25+
end
26+
27+
private
28+
29+
def tracer
30+
HTTP::Instrumentation.instance.tracer
31+
end
32+
end
33+
end
34+
end
35+
end
36+
end
37+
end
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# frozen_string_literal: true
2+
3+
# Copyright The OpenTelemetry Authors
4+
#
5+
# SPDX-License-Identifier: Apache-2.0
6+
7+
module OpenTelemetry
8+
module Instrumentation
9+
module HTTP
10+
module Patches
11+
# Module to prepend to HTTP::Client for instrumentation
12+
module Client
13+
module Old
14+
# Constant for the HTTP status range
15+
HTTP_STATUS_SUCCESS_RANGE = (100..399)
16+
17+
def perform(req, options)
18+
uri = req.uri
19+
request_method = req.verb.to_s.upcase
20+
span_name = create_request_span_name(request_method, uri.path)
21+
22+
attributes = {
23+
'http.method' => request_method,
24+
'http.scheme' => uri.scheme,
25+
'http.target' => uri.path,
26+
'http.url' => "#{uri.scheme}://#{uri.host}",
27+
'net.peer.name' => uri.host,
28+
'net.peer.port' => uri.port
29+
}.merge!(OpenTelemetry::Common::HTTP::ClientContext.attributes)
30+
31+
tracer.in_span(span_name, attributes: attributes, kind: :client) do |span|
32+
OpenTelemetry.propagation.inject(req.headers)
33+
super.tap do |response|
34+
annotate_span_with_response!(span, response)
35+
end
36+
end
37+
end
38+
39+
private
40+
41+
def config
42+
OpenTelemetry::Instrumentation::HTTP::Instrumentation.instance.config
43+
end
44+
45+
def annotate_span_with_response!(span, response)
46+
return unless response&.status
47+
48+
status_code = response.status.to_i
49+
span.set_attribute('http.status_code', status_code)
50+
span.status = OpenTelemetry::Trace::Status.error unless HTTP_STATUS_SUCCESS_RANGE.cover?(status_code)
51+
end
52+
53+
def create_request_span_name(request_method, request_path)
54+
if (implementation = config[:span_name_formatter])
55+
updated_span_name = implementation.call(request_method, request_path)
56+
updated_span_name.is_a?(String) ? updated_span_name : "HTTP #{request_method}"
57+
else
58+
"HTTP #{request_method}"
59+
end
60+
rescue StandardError
61+
"HTTP #{request_method}"
62+
end
63+
64+
def tracer
65+
HTTP::Instrumentation.instance.tracer
66+
end
67+
end
68+
end
69+
end
70+
end
71+
end
72+
end
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# frozen_string_literal: true
2+
3+
# Copyright The OpenTelemetry Authors
4+
#
5+
# SPDX-License-Identifier: Apache-2.0
6+
7+
module OpenTelemetry
8+
module Instrumentation
9+
module HTTP
10+
module Patches
11+
# Module to prepend to HTTP::Connection for instrumentation
12+
module Connection
13+
module Old
14+
def initialize(req, options)
15+
attributes = {
16+
'net.peer.name' => req.uri.host,
17+
'net.peer.port' => req.uri.port
18+
}.merge!(OpenTelemetry::Common::HTTP::ClientContext.attributes)
19+
20+
tracer.in_span('HTTP CONNECT', attributes: attributes) do
21+
super
22+
end
23+
end
24+
25+
private
26+
27+
def tracer
28+
HTTP::Instrumentation.instance.tracer
29+
end
30+
end
31+
end
32+
end
33+
end
34+
end
35+
end

0 commit comments

Comments
 (0)