opentelemetry-instrumentation-faraday 0.27.0 → 0.28.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9277b3989aec5b86a371febc29f4e311be7f9b8f875766b87cb95a36f34e3ac1
4
- data.tar.gz: 8214e43a5609615156bd124b19132e62d8432ae46545266e10160eb50026810e
3
+ metadata.gz: b79cd4f94a4533971dc6e87e0b7b385be4905ba2fd29ff28483184929c680c03
4
+ data.tar.gz: 5cf513bb53039de6901d33d7327c97b4b539cea350e557da09bafa74a5684af3
5
5
  SHA512:
6
- metadata.gz: 338344d347896039ca70ca71930758193e90eef6ba21c32892c7089e2b046d67239fc251e23bfb696d239b30bdea3d8f81a40d28e75c5c608f4f6756f36dd1b3
7
- data.tar.gz: 69689f9129b606d4a387aa94c5632c043bfe91717625fbada5455e4a1353ae8a7eb077415c5232044bc260bde17753df548d1f15a3ab5fcdd8c6b11ec96504b6
6
+ metadata.gz: 5598c9c8d064e90594e9ad8e4a299bfafe1acb51fb3b5ab23366bfbe6f9761d136b740019c6ad60d25ace28cc9021daac72f1be079f84f3bd4d7777701f87af2
7
+ data.tar.gz: 82180c8e8cf09fd3bcb3c37df9569fbf55cf951a39b560a74ca882d265e14946d2edde143893815fb1c71f8815a205cfed5bfd786fb1306846cac2c478582c41
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Release History: opentelemetry-instrumentation-faraday
2
2
 
3
+ ### v0.28.0 / 2025-08-12
4
+
5
+ * ADDED: Add Faraday `OTEL_SEMCONV_STABILITY_OPT_IN` environment variable [#1592](https://github.com/open-telemetry/opentelemetry-ruby-contrib/pull/1592)
6
+
3
7
  ### v0.27.0 / 2025-06-03
4
8
 
5
9
  * ADDED: Suppress internal spans with Faraday instrumentation
data/README.md CHANGED
@@ -59,3 +59,19 @@ Apache 2.0 license. See [LICENSE][license-github] for more information.
59
59
  [community-meetings]: https://github.com/open-telemetry/community#community-meetings
60
60
  [slack-channel]: https://cloud-native.slack.com/archives/C01NWKKMKMY
61
61
  [discussions-url]: https://github.com/open-telemetry/opentelemetry-ruby/discussions
62
+
63
+ ## HTTP semantic convention stability
64
+
65
+ In the OpenTelemetry ecosystem, HTTP semantic conventions have now reached a stable state. However, the initial Faraday instrumentation was introduced before this stability was achieved, which resulted in HTTP attributes being based on an older version of the semantic conventions.
66
+
67
+ To facilitate the migration to stable semantic conventions, you can use the `OTEL_SEMCONV_STABILITY_OPT_IN` environment variable. This variable allows you to opt-in to the new stable conventions, ensuring compatibility and future-proofing your instrumentation.
68
+
69
+ When setting the value for `OTEL_SEMCONV_STABILITY_OPT_IN`, you can specify which conventions you wish to adopt:
70
+
71
+ - `http` - Emits the stable HTTP and networking conventions and ceases emitting the old conventions previously emitted by the instrumentation.
72
+ - `http/dup` - Emits both the old and stable HTTP and networking conventions, enabling a phased rollout of the stable semantic conventions.
73
+ - Default behavior (in the absence of either value) is to continue emitting the old HTTP and networking conventions the instrumentation previously emitted.
74
+
75
+ During the transition from old to stable conventions, Faraday instrumentation code comes in three patch versions: `dup`, `old`, and `stable`. These versions are identical except for the attributes they send. Any changes to Faraday instrumentation should consider all three patches.
76
+
77
+ For additional information on migration, please refer to our [documentation](https://opentelemetry.io/docs/specs/semconv/non-normative/http-migration/).
@@ -13,9 +13,10 @@ module OpenTelemetry
13
13
  MINIMUM_VERSION = Gem::Version.new('1.0')
14
14
 
15
15
  install do |_config|
16
- require_dependencies
17
- register_tracer_middleware
18
- use_middleware_by_default
16
+ patch_type = determine_semconv
17
+ send(:"require_dependencies_#{patch_type}")
18
+ send(:"register_tracer_middleware_#{patch_type}")
19
+ send(:"use_middleware_by_default_#{patch_type}")
19
20
  end
20
21
 
21
22
  compatible do
@@ -36,19 +37,62 @@ module OpenTelemetry
36
37
  Gem::Version.new(::Faraday::VERSION)
37
38
  end
38
39
 
39
- def require_dependencies
40
- require_relative 'middlewares/tracer_middleware'
41
- require_relative 'patches/connection'
40
+ def determine_semconv
41
+ stability_opt_in = ENV.fetch('OTEL_SEMCONV_STABILITY_OPT_IN', '')
42
+ values = stability_opt_in.split(',').map(&:strip)
43
+
44
+ if values.include?('http/dup')
45
+ 'dup'
46
+ elsif values.include?('http')
47
+ 'stable'
48
+ else
49
+ 'old'
50
+ end
51
+ end
52
+
53
+ def require_dependencies_dup
54
+ require_relative 'middlewares/dup/tracer_middleware'
55
+ require_relative 'patches/dup/connection'
56
+ end
57
+
58
+ def require_dependencies_old
59
+ require_relative 'middlewares/old/tracer_middleware'
60
+ require_relative 'patches/old/connection'
42
61
  end
43
62
 
44
- def register_tracer_middleware
63
+ def require_dependencies_stable
64
+ require_relative 'middlewares/stable/tracer_middleware'
65
+ require_relative 'patches/stable/connection'
66
+ end
67
+
68
+ def register_tracer_middleware_dup
45
69
  ::Faraday::Middleware.register_middleware(
46
- open_telemetry: Middlewares::TracerMiddleware
70
+ open_telemetry: Middlewares::Dup::TracerMiddleware
47
71
  )
48
72
  end
49
73
 
50
- def use_middleware_by_default
51
- ::Faraday::Connection.prepend(Patches::Connection)
74
+ def register_tracer_middleware_old
75
+ ::Faraday::Middleware.register_middleware(
76
+ open_telemetry: Middlewares::Old::TracerMiddleware
77
+ )
78
+ end
79
+
80
+ def register_tracer_middleware_stable
81
+ ::Faraday::Middleware.register_middleware(
82
+ open_telemetry: Middlewares::Stable::TracerMiddleware
83
+ )
84
+ end
85
+
86
+ def use_middleware_by_default_dup
87
+ ::Faraday::Connection.prepend(Patches::Dup::Connection)
88
+ end
89
+
90
+ def use_middleware_by_default_old
91
+ ::Faraday::Connection.prepend(Patches::Old::Connection)
92
+ end
93
+
94
+ def use_middleware_by_default_stable
95
+ ::Faraday::Connection.prepend(Patches::Stable::Connection)
52
96
  end
53
97
  end
54
98
  end
@@ -0,0 +1,98 @@
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 Faraday
10
+ module Middlewares
11
+ module Dup
12
+ # TracerMiddleware propagates context and instruments Faraday requests
13
+ # by way of its middleware system
14
+ class TracerMiddleware < ::Faraday::Middleware
15
+ HTTP_METHODS_SYMBOL_TO_STRING = {
16
+ connect: 'CONNECT',
17
+ delete: 'DELETE',
18
+ get: 'GET',
19
+ head: 'HEAD',
20
+ options: 'OPTIONS',
21
+ patch: 'PATCH',
22
+ post: 'POST',
23
+ put: 'PUT',
24
+ trace: 'TRACE'
25
+ }.freeze
26
+
27
+ # Constant for the HTTP status range
28
+ HTTP_STATUS_SUCCESS_RANGE = (100..399)
29
+
30
+ def call(env)
31
+ http_method = HTTP_METHODS_SYMBOL_TO_STRING[env.method]
32
+ config = Faraday::Instrumentation.instance.config
33
+
34
+ attributes = span_creation_attributes(
35
+ http_method: http_method, url: env.url, config: config
36
+ )
37
+
38
+ OpenTelemetry::Common::HTTP::ClientContext.with_attributes(attributes) do |attrs, _|
39
+ tracer.in_span(
40
+ http_method, attributes: attrs, kind: config.fetch(:span_kind)
41
+ ) do |span|
42
+ OpenTelemetry.propagation.inject(env.request_headers)
43
+
44
+ if config[:enable_internal_instrumentation] == false
45
+ OpenTelemetry::Common::Utilities.untraced do
46
+ app.call(env).on_complete { |resp| trace_response(span, resp.status) }
47
+ end
48
+ else
49
+ app.call(env).on_complete { |resp| trace_response(span, resp.status) }
50
+ end
51
+ rescue ::Faraday::Error => e
52
+ trace_response(span, e.response[:status]) if e.response
53
+
54
+ raise
55
+ end
56
+ end
57
+ end
58
+
59
+ private
60
+
61
+ def span_creation_attributes(http_method:, url:, config:)
62
+ cleansed_url = OpenTelemetry::Common::Utilities.cleanse_url(url.to_s)
63
+ attrs = {
64
+ 'http.method' => http_method,
65
+ 'http.request.method' => http_method,
66
+ 'http.url' => cleansed_url,
67
+ 'url.full' => cleansed_url,
68
+ 'faraday.adapter.name' => app.class.name
69
+ }
70
+ if url.host
71
+ attrs['net.peer.name'] = url.host
72
+ attrs['server.address'] = url.host
73
+ end
74
+ attrs['peer.service'] = config[:peer_service] if config[:peer_service]
75
+
76
+ attrs.merge!(
77
+ OpenTelemetry::Common::HTTP::ClientContext.attributes
78
+ )
79
+ end
80
+
81
+ # Versions prior to 1.0 do not define an accessor for app
82
+ attr_reader :app if Gem::Version.new(Faraday::VERSION) < Gem::Version.new('1.0.0')
83
+
84
+ def tracer
85
+ Faraday::Instrumentation.instance.tracer
86
+ end
87
+
88
+ def trace_response(span, status)
89
+ span.set_attribute('http.status_code', status)
90
+ span.set_attribute('http.response.status_code', status)
91
+ span.status = OpenTelemetry::Trace::Status.error unless HTTP_STATUS_SUCCESS_RANGE.cover?(status.to_i)
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,91 @@
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 Faraday
10
+ module Middlewares
11
+ module Old
12
+ # TracerMiddleware propagates context and instruments Faraday requests
13
+ # by way of its middleware system
14
+ class TracerMiddleware < ::Faraday::Middleware
15
+ HTTP_METHODS_SYMBOL_TO_STRING = {
16
+ connect: 'CONNECT',
17
+ delete: 'DELETE',
18
+ get: 'GET',
19
+ head: 'HEAD',
20
+ options: 'OPTIONS',
21
+ patch: 'PATCH',
22
+ post: 'POST',
23
+ put: 'PUT',
24
+ trace: 'TRACE'
25
+ }.freeze
26
+
27
+ # Constant for the HTTP status range
28
+ HTTP_STATUS_SUCCESS_RANGE = (100..399)
29
+
30
+ def call(env)
31
+ http_method = HTTP_METHODS_SYMBOL_TO_STRING[env.method]
32
+ config = Faraday::Instrumentation.instance.config
33
+
34
+ attributes = span_creation_attributes(
35
+ http_method: http_method, url: env.url, config: config
36
+ )
37
+
38
+ OpenTelemetry::Common::HTTP::ClientContext.with_attributes(attributes) do |attrs, _|
39
+ tracer.in_span(
40
+ "HTTP #{http_method}", attributes: attrs, kind: config.fetch(:span_kind)
41
+ ) do |span|
42
+ OpenTelemetry.propagation.inject(env.request_headers)
43
+
44
+ if config[:enable_internal_instrumentation] == false
45
+ OpenTelemetry::Common::Utilities.untraced do
46
+ app.call(env).on_complete { |resp| trace_response(span, resp.status) }
47
+ end
48
+ else
49
+ app.call(env).on_complete { |resp| trace_response(span, resp.status) }
50
+ end
51
+ rescue ::Faraday::Error => e
52
+ trace_response(span, e.response[:status]) if e.response
53
+
54
+ raise
55
+ end
56
+ end
57
+ end
58
+
59
+ private
60
+
61
+ def span_creation_attributes(http_method:, url:, config:)
62
+ attrs = {
63
+ 'http.method' => http_method,
64
+ 'http.url' => OpenTelemetry::Common::Utilities.cleanse_url(url.to_s),
65
+ 'faraday.adapter.name' => app.class.name
66
+ }
67
+ attrs['net.peer.name'] = url.host if url.host
68
+ attrs['peer.service'] = config[:peer_service] if config[:peer_service]
69
+
70
+ attrs.merge!(
71
+ OpenTelemetry::Common::HTTP::ClientContext.attributes
72
+ )
73
+ end
74
+
75
+ # Versions prior to 1.0 do not define an accessor for app
76
+ attr_reader :app if Gem::Version.new(Faraday::VERSION) < Gem::Version.new('1.0.0')
77
+
78
+ def tracer
79
+ Faraday::Instrumentation.instance.tracer
80
+ end
81
+
82
+ def trace_response(span, status)
83
+ span.set_attribute('http.status_code', status)
84
+ span.status = OpenTelemetry::Trace::Status.error unless HTTP_STATUS_SUCCESS_RANGE.cover?(status.to_i)
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,91 @@
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 Faraday
10
+ module Middlewares
11
+ module Stable
12
+ # TracerMiddleware propagates context and instruments Faraday requests
13
+ # by way of its middleware system
14
+ class TracerMiddleware < ::Faraday::Middleware
15
+ HTTP_METHODS_SYMBOL_TO_STRING = {
16
+ connect: 'CONNECT',
17
+ delete: 'DELETE',
18
+ get: 'GET',
19
+ head: 'HEAD',
20
+ options: 'OPTIONS',
21
+ patch: 'PATCH',
22
+ post: 'POST',
23
+ put: 'PUT',
24
+ trace: 'TRACE'
25
+ }.freeze
26
+
27
+ # Constant for the HTTP status range
28
+ HTTP_STATUS_SUCCESS_RANGE = (100..399)
29
+
30
+ def call(env)
31
+ http_method = HTTP_METHODS_SYMBOL_TO_STRING[env.method]
32
+ config = Faraday::Instrumentation.instance.config
33
+
34
+ attributes = span_creation_attributes(
35
+ http_method: http_method, url: env.url, config: config
36
+ )
37
+
38
+ OpenTelemetry::Common::HTTP::ClientContext.with_attributes(attributes) do |attrs, _|
39
+ tracer.in_span(
40
+ http_method, attributes: attrs, kind: config.fetch(:span_kind)
41
+ ) do |span|
42
+ OpenTelemetry.propagation.inject(env.request_headers)
43
+
44
+ if config[:enable_internal_instrumentation] == false
45
+ OpenTelemetry::Common::Utilities.untraced do
46
+ app.call(env).on_complete { |resp| trace_response(span, resp.status) }
47
+ end
48
+ else
49
+ app.call(env).on_complete { |resp| trace_response(span, resp.status) }
50
+ end
51
+ rescue ::Faraday::Error => e
52
+ trace_response(span, e.response[:status]) if e.response
53
+
54
+ raise
55
+ end
56
+ end
57
+ end
58
+
59
+ private
60
+
61
+ def span_creation_attributes(http_method:, url:, config:)
62
+ attrs = {
63
+ 'http.request.method' => http_method,
64
+ 'url.full' => OpenTelemetry::Common::Utilities.cleanse_url(url.to_s),
65
+ 'faraday.adapter.name' => app.class.name
66
+ }
67
+ attrs['server.address'] = url.host if url.host
68
+ attrs['peer.service'] = config[:peer_service] if config[:peer_service]
69
+
70
+ attrs.merge!(
71
+ OpenTelemetry::Common::HTTP::ClientContext.attributes
72
+ )
73
+ end
74
+
75
+ # Versions prior to 1.0 do not define an accessor for app
76
+ attr_reader :app if Gem::Version.new(Faraday::VERSION) < Gem::Version.new('1.0.0')
77
+
78
+ def tracer
79
+ Faraday::Instrumentation.instance.tracer
80
+ end
81
+
82
+ def trace_response(span, status)
83
+ span.set_attribute('http.response.status_code', status)
84
+ span.status = OpenTelemetry::Trace::Status.error unless HTTP_STATUS_SUCCESS_RANGE.cover?(status.to_i)
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,29 @@
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 Faraday
10
+ module Patches
11
+ module Dup
12
+ # Module to be prepended to force Faraday to use the middleware by
13
+ # default so the user doesn't have to call `use` for every connection.
14
+ module Connection
15
+ # Wraps Faraday::Connection#initialize:
16
+ # https://github.com/lostisland/faraday/blob/ff9dc1d1219a1bbdba95a9a4cf5d135b97247ee2/lib/faraday/connection.rb#L62-L92
17
+ def initialize(...)
18
+ super.tap do
19
+ use(:open_telemetry) unless builder.handlers.any? do |handler|
20
+ handler.klass == Middlewares::Dup::TracerMiddleware
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,29 @@
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 Faraday
10
+ module Patches
11
+ module Old
12
+ # Module to be prepended to force Faraday to use the middleware by
13
+ # default so the user doesn't have to call `use` for every connection.
14
+ module Connection
15
+ # Wraps Faraday::Connection#initialize:
16
+ # https://github.com/lostisland/faraday/blob/ff9dc1d1219a1bbdba95a9a4cf5d135b97247ee2/lib/faraday/connection.rb#L62-L92
17
+ def initialize(...)
18
+ super.tap do
19
+ use(:open_telemetry) unless builder.handlers.any? do |handler|
20
+ handler.klass == Middlewares::Old::TracerMiddleware
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,29 @@
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 Faraday
10
+ module Patches
11
+ module Stable
12
+ # Module to be prepended to force Faraday to use the middleware by
13
+ # default so the user doesn't have to call `use` for every connection.
14
+ module Connection
15
+ # Wraps Faraday::Connection#initialize:
16
+ # https://github.com/lostisland/faraday/blob/ff9dc1d1219a1bbdba95a9a4cf5d135b97247ee2/lib/faraday/connection.rb#L62-L92
17
+ def initialize(...)
18
+ super.tap do
19
+ use(:open_telemetry) unless builder.handlers.any? do |handler|
20
+ handler.klass == Middlewares::Stable::TracerMiddleware
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -7,7 +7,7 @@
7
7
  module OpenTelemetry
8
8
  module Instrumentation
9
9
  module Faraday
10
- VERSION = '0.27.0'
10
+ VERSION = '0.28.0'
11
11
  end
12
12
  end
13
13
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opentelemetry-instrumentation-faraday
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.27.0
4
+ version: 0.28.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - OpenTelemetry Authors
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-06-03 00:00:00.000000000 Z
11
+ date: 2025-08-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: opentelemetry-api
@@ -53,17 +53,21 @@ files:
53
53
  - lib/opentelemetry/instrumentation.rb
54
54
  - lib/opentelemetry/instrumentation/faraday.rb
55
55
  - lib/opentelemetry/instrumentation/faraday/instrumentation.rb
56
- - lib/opentelemetry/instrumentation/faraday/middlewares/tracer_middleware.rb
57
- - lib/opentelemetry/instrumentation/faraday/patches/connection.rb
56
+ - lib/opentelemetry/instrumentation/faraday/middlewares/dup/tracer_middleware.rb
57
+ - lib/opentelemetry/instrumentation/faraday/middlewares/old/tracer_middleware.rb
58
+ - lib/opentelemetry/instrumentation/faraday/middlewares/stable/tracer_middleware.rb
59
+ - lib/opentelemetry/instrumentation/faraday/patches/dup/connection.rb
60
+ - lib/opentelemetry/instrumentation/faraday/patches/old/connection.rb
61
+ - lib/opentelemetry/instrumentation/faraday/patches/stable/connection.rb
58
62
  - lib/opentelemetry/instrumentation/faraday/version.rb
59
63
  homepage: https://github.com/open-telemetry/opentelemetry-ruby-contrib
60
64
  licenses:
61
65
  - Apache-2.0
62
66
  metadata:
63
- changelog_uri: https://rubydoc.info/gems/opentelemetry-instrumentation-faraday/0.27.0/file/CHANGELOG.md
67
+ changelog_uri: https://rubydoc.info/gems/opentelemetry-instrumentation-faraday/0.28.0/file/CHANGELOG.md
64
68
  source_code_uri: https://github.com/open-telemetry/opentelemetry-ruby-contrib/tree/main/instrumentation/faraday
65
69
  bug_tracker_uri: https://github.com/open-telemetry/opentelemetry-ruby-contrib/issues
66
- documentation_uri: https://rubydoc.info/gems/opentelemetry-instrumentation-faraday/0.27.0
70
+ documentation_uri: https://rubydoc.info/gems/opentelemetry-instrumentation-faraday/0.28.0
67
71
  post_install_message:
68
72
  rdoc_options: []
69
73
  require_paths:
@@ -1,89 +0,0 @@
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 Faraday
10
- module Middlewares
11
- # TracerMiddleware propagates context and instruments Faraday requests
12
- # by way of its middleware system
13
- class TracerMiddleware < ::Faraday::Middleware
14
- HTTP_METHODS_SYMBOL_TO_STRING = {
15
- connect: 'CONNECT',
16
- delete: 'DELETE',
17
- get: 'GET',
18
- head: 'HEAD',
19
- options: 'OPTIONS',
20
- patch: 'PATCH',
21
- post: 'POST',
22
- put: 'PUT',
23
- trace: 'TRACE'
24
- }.freeze
25
-
26
- # Constant for the HTTP status range
27
- HTTP_STATUS_SUCCESS_RANGE = (100..399)
28
-
29
- def call(env)
30
- http_method = HTTP_METHODS_SYMBOL_TO_STRING[env.method]
31
- config = Faraday::Instrumentation.instance.config
32
-
33
- attributes = span_creation_attributes(
34
- http_method: http_method, url: env.url, config: config
35
- )
36
-
37
- OpenTelemetry::Common::HTTP::ClientContext.with_attributes(attributes) do |attrs, _|
38
- tracer.in_span(
39
- "HTTP #{http_method}", attributes: attrs, kind: config.fetch(:span_kind)
40
- ) do |span|
41
- OpenTelemetry.propagation.inject(env.request_headers)
42
-
43
- if config[:enable_internal_instrumentation] == false
44
- OpenTelemetry::Common::Utilities.untraced do
45
- app.call(env).on_complete { |resp| trace_response(span, resp.status) }
46
- end
47
- else
48
- app.call(env).on_complete { |resp| trace_response(span, resp.status) }
49
- end
50
- rescue ::Faraday::Error => e
51
- trace_response(span, e.response[:status]) if e.response
52
-
53
- raise
54
- end
55
- end
56
- end
57
-
58
- private
59
-
60
- def span_creation_attributes(http_method:, url:, config:)
61
- attrs = {
62
- 'http.method' => http_method,
63
- 'http.url' => OpenTelemetry::Common::Utilities.cleanse_url(url.to_s),
64
- 'faraday.adapter.name' => app.class.name
65
- }
66
- attrs['net.peer.name'] = url.host if url.host
67
- attrs['peer.service'] = config[:peer_service] if config[:peer_service]
68
-
69
- attrs.merge!(
70
- OpenTelemetry::Common::HTTP::ClientContext.attributes
71
- )
72
- end
73
-
74
- # Versions prior to 1.0 do not define an accessor for app
75
- attr_reader :app if Gem::Version.new(Faraday::VERSION) < Gem::Version.new('1.0.0')
76
-
77
- def tracer
78
- Faraday::Instrumentation.instance.tracer
79
- end
80
-
81
- def trace_response(span, status)
82
- span.set_attribute('http.status_code', status)
83
- span.status = OpenTelemetry::Trace::Status.error unless HTTP_STATUS_SUCCESS_RANGE.cover?(status.to_i)
84
- end
85
- end
86
- end
87
- end
88
- end
89
- end
@@ -1,27 +0,0 @@
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 Faraday
10
- module Patches
11
- # Module to be prepended to force Faraday to use the middleware by
12
- # default so the user doesn't have to call `use` for every connection.
13
- module Connection
14
- # Wraps Faraday::Connection#initialize:
15
- # https://github.com/lostisland/faraday/blob/ff9dc1d1219a1bbdba95a9a4cf5d135b97247ee2/lib/faraday/connection.rb#L62-L92
16
- def initialize(...)
17
- super.tap do
18
- use(:open_telemetry) unless builder.handlers.any? do |handler|
19
- handler.klass == Middlewares::TracerMiddleware
20
- end
21
- end
22
- end
23
- end
24
- end
25
- end
26
- end
27
- end