opentelemetry-instrumentation-rage 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b4453a4c6f0fef9ceac987b101471e79282c722c2bb33d87a129309e0fb4ff32
4
+ data.tar.gz: baaa1946171c02158cec4fbcffae348ce1a1b09544f30171723db63abd6698cb
5
+ SHA512:
6
+ metadata.gz: 8f9d15d77f8022b17322226e9d4d390f445d8684d327a2836f5a0f34a0c319609403c43fdcf3e5fbeb2370d1169e1b7758dde462eac7b252abcd81082c6fd054
7
+ data.tar.gz: d04cadbfd94da5d07fc46a47137e28e8c7fb674e0d82ca57ab545ca9d0da8e949b6d7515b7184e62e9f58a4128ac1871c02128e7c08d772eb1f4f4c848138414
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.standard.yml ADDED
@@ -0,0 +1 @@
1
+ ruby_version: 3.3
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2026-01-07
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at 2270393+rsamoilov@users.noreply.github.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright The OpenTelemetry Authors
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # OpenTelemetry Rage Instrumentation
2
+
3
+ The OpenTelemetry Rage instrumentation provides automatic observability for [Rage](https://github.com/rage-rb/rage), a fiber-based framework with Rails-compatible syntax.
4
+
5
+ This instrumentation enables comprehensive tracing and logging for Rage applications:
6
+
7
+ * Creates spans for HTTP requests, WebSocket messages, event subscribers, and deferred tasks
8
+ * Propagates OpenTelemetry context across fibers created via `Fiber.schedule` and deferred tasks
9
+ * Enriches logs with trace and span IDs for correlated observability
10
+
11
+ ## How do I get started?
12
+
13
+ Install the gem using:
14
+
15
+ ```console
16
+ gem install opentelemetry-instrumentation-rage
17
+ ```
18
+
19
+ Or, if you use [bundler][bundler-home], include `opentelemetry-instrumentation-rage` in your `Gemfile`.
20
+
21
+ ## Usage
22
+
23
+ To use the instrumentation, call `use` with the name of the instrumentation:
24
+
25
+ ```ruby
26
+ OpenTelemetry::SDK.configure do |c|
27
+ c.use 'OpenTelemetry::Instrumentation::Rage'
28
+ end
29
+ ```
30
+
31
+ Alternatively, you can also call `use_all` to install all the available instrumentation.
32
+
33
+ ```ruby
34
+ OpenTelemetry::SDK.configure do |c|
35
+ c.use_all
36
+ end
37
+ ```
38
+
39
+ ## Examples
40
+
41
+ Example usage can be seen in the [`./example/trace_demonstration.ru` file](https://github.com/open-telemetry/opentelemetry-ruby-contrib/blob/main/instrumentation/rage/example/trace_demonstration.ru)
42
+
43
+ ## License
44
+
45
+ The `opentelemetry-instrumentation-rage` gem is distributed under the Apache 2.0 license. See [LICENSE][license-github] for more information.
46
+
47
+ [bundler-home]: https://bundler.io
48
+ [repo-github]: https://github.com/open-telemetry/opentelemetry-ruby
49
+ [license-github]: https://github.com/open-telemetry/opentelemetry-ruby-contrib/blob/main/LICENSE
50
+ [ruby-sig]: https://github.com/open-telemetry/community#ruby-sig
51
+ [community-meetings]: https://github.com/open-telemetry/community#community-meetings
52
+ [slack-channel]: https://cloud-native.slack.com/archives/C01NWKKMKMY
53
+ [discussions-url]: https://github.com/open-telemetry/opentelemetry-ruby/discussions
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "standard/rake"
9
+
10
+ task default: %i[spec standard]
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright The OpenTelemetry Authors
4
+ #
5
+ # SPDX-License-Identifier: Apache-2.0
6
+
7
+ require "bundler/inline"
8
+
9
+ gemfile(true) do
10
+ source "https://rubygems.org"
11
+
12
+ gem "rage-rb"
13
+ gem "opentelemetry-sdk"
14
+ gem "opentelemetry-instrumentation-rage"
15
+ end
16
+
17
+ require "rage/all"
18
+
19
+ ENV["OTEL_TRACES_EXPORTER"] ||= "console"
20
+
21
+ OpenTelemetry::SDK.configure do |c|
22
+ c.use "OpenTelemetry::Instrumentation::Rage"
23
+ end
24
+
25
+ class BaseController < RageController::API
26
+ def index
27
+ render plain: "Hello from OpenTelemetry!"
28
+ end
29
+ end
30
+
31
+ Rage.routes.draw do
32
+ root to: "base#index"
33
+ end
34
+
35
+ run Rage.application
36
+
37
+ # To run this example:
38
+ # 1. Install the `rage-rb` gem:
39
+ # gem install rage-rb
40
+ # 2. Start the server:
41
+ # rage s -c trace_demonstration.ru
42
+ # 3. Navigate to http://localhost:3000/
43
+ # Spans for the requests will appear in the console
@@ -0,0 +1,141 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright The OpenTelemetry Authors
4
+ #
5
+ # SPDX-License-Identifier: Apache-2.0
6
+
7
+ require "opentelemetry/semconv/incubating/code"
8
+ require "opentelemetry/semconv/incubating/messaging"
9
+
10
+ module OpenTelemetry
11
+ module Instrumentation
12
+ module Rage
13
+ module Handlers
14
+ # The class customizes the initial Rack span used for WebSocket handshakes and wraps subsequent
15
+ # Cable connection and action processing in spans linked to the handshake span.
16
+ class Cable < ::Rage::Telemetry::Handler
17
+ HANDSHAKE_CONTEXT = "otel.rage.handshake_context"
18
+ private_constant :HANDSHAKE_CONTEXT
19
+
20
+ HANDSHAKE_LINK = "otel.rage.handshake_link"
21
+ private_constant :HANDSHAKE_LINK
22
+
23
+ handle "cable.websocket.handshake", with: :save_context
24
+
25
+ handle "cable.connection.process", with: :create_connection_span
26
+ handle "cable.action.process", with: :create_channel_span
27
+ handle "cable.stream.broadcast", with: :create_broadcast_span
28
+
29
+ # @param env [Hash] the Rack env
30
+ def self.save_context(env:)
31
+ span = OpenTelemetry::Instrumentation::Rack.current_span
32
+ return yield unless span.recording?
33
+
34
+ request = ::Rack::Request.new(env)
35
+ span.name = "#{request.request_method} #{request.path}"
36
+
37
+ env[HANDSHAKE_CONTEXT] = OpenTelemetry::Context.current
38
+ env[HANDSHAKE_LINK] = [OpenTelemetry::Trace::Link.new(span.context)]
39
+
40
+ yield
41
+ end
42
+
43
+ # @param env [Hash] the Rack env
44
+ # @param action [:connect, :disconnect] the name of the action being processed
45
+ # @param connection [Rage::Cable::Connection] the connection instance
46
+ def self.create_connection_span(env:, action:, connection:)
47
+ handshake_context = env[HANDSHAKE_CONTEXT]
48
+ handshake_link = env[HANDSHAKE_LINK]
49
+
50
+ OpenTelemetry::Context.with_current(handshake_context) do
51
+ attributes = {
52
+ "rage.cable.connection.class" => connection.class.name,
53
+ "rage.cable.connection.action" => action.to_s
54
+ }
55
+
56
+ kind = (action == :connect) ? :server : :internal
57
+
58
+ span = Rage::Instrumentation.instance.tracer.start_root_span(
59
+ "#{connection.class} #{action}",
60
+ links: handshake_link,
61
+ kind:,
62
+ attributes:
63
+ )
64
+
65
+ OpenTelemetry::Trace.with_span(span) do
66
+ result = yield
67
+
68
+ if result.error?
69
+ span.record_exception(result.exception)
70
+ span.status = OpenTelemetry::Trace::Status.error
71
+ end
72
+ ensure
73
+ span.finish
74
+ end
75
+ end
76
+ end
77
+
78
+ # @param env [Hash] the Rack env
79
+ # @param action [Symbol] the name of the action being processed
80
+ # @param channel [Rage::Cable::Channel] the channel instance
81
+ def self.create_channel_span(env:, action:, channel:)
82
+ handshake_context = env[HANDSHAKE_CONTEXT]
83
+ handshake_link = env[HANDSHAKE_LINK]
84
+
85
+ OpenTelemetry::Context.with_current(handshake_context) do
86
+ attributes = {
87
+ "rpc.system" => "rage.cable",
88
+ "rpc.service" => channel.class.name,
89
+ "rpc.method" => action.to_s
90
+ }
91
+
92
+ span_name = if action == :subscribed
93
+ "#{channel.class} subscribe"
94
+ elsif action == :unsubscribed
95
+ "#{channel.class} unsubscribe"
96
+ else
97
+ "#{channel.class} #{action}"
98
+ end
99
+
100
+ kind = (action == :unsubscribed) ? :internal : :server
101
+
102
+ span = Rage::Instrumentation.instance.tracer.start_root_span(
103
+ span_name,
104
+ links: handshake_link,
105
+ kind:,
106
+ attributes:
107
+ )
108
+
109
+ OpenTelemetry::Trace.with_span(span) do
110
+ result = yield
111
+
112
+ if result.error?
113
+ span.record_exception(result.exception)
114
+ span.status = OpenTelemetry::Trace::Status.error
115
+ end
116
+ ensure
117
+ span.finish
118
+ end
119
+ end
120
+ end
121
+
122
+ # @param stream [String] the name of the stream
123
+ def self.create_broadcast_span(stream:)
124
+ attributes = {
125
+ "rage.cable.broadcast.stream" => stream
126
+ }
127
+
128
+ Rage::Instrumentation.instance.tracer.in_span("Rage::Cable broadcast", kind: :producer, attributes:) do |span|
129
+ result = yield
130
+
131
+ if result.error?
132
+ span.record_exception(result.exception)
133
+ span.status = OpenTelemetry::Trace::Status.error
134
+ end
135
+ end
136
+ end
137
+ end
138
+ end
139
+ end
140
+ end
141
+ end
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright The OpenTelemetry Authors
4
+ #
5
+ # SPDX-License-Identifier: Apache-2.0
6
+
7
+ require "opentelemetry/semconv/incubating/code"
8
+ require "opentelemetry/semconv/incubating/messaging"
9
+
10
+ module OpenTelemetry
11
+ module Instrumentation
12
+ module Rage
13
+ module Handlers
14
+ # The class propagates OpenTelemetry context to deferred tasks and wraps the
15
+ # enqueuing and processing of deferred tasks in spans.
16
+ class Deferred < ::Rage::Telemetry::Handler
17
+ handle "deferred.task.enqueue", with: :create_enqueue_span
18
+ handle "deferred.task.process", with: :create_perform_span
19
+
20
+ # @param task_class [Class] the class of the deferred task
21
+ # @param task_context [Hash] the context for the deferred task
22
+ def self.create_enqueue_span(task_class:, task_context:)
23
+ Rage::Instrumentation.instance.tracer.in_span("#{task_class} enqueue", kind: :producer) do |span|
24
+ OpenTelemetry.propagation.inject(task_context)
25
+
26
+ result = yield
27
+
28
+ if result.error?
29
+ span.record_exception(result.exception)
30
+ span.status = OpenTelemetry::Trace::Status.error
31
+ end
32
+ end
33
+ end
34
+
35
+ # @param task_class [Class] the class of the deferred task
36
+ # @param task [Rage::Deferred::Task] the deferred task instance
37
+ # @param task_context [Hash] the context for the deferred task
38
+ def self.create_perform_span(task_class:, task:, task_context:)
39
+ otel_context = OpenTelemetry.propagation.extract(task_context)
40
+
41
+ OpenTelemetry::Context.with_current(otel_context) do
42
+ parent_span_context = OpenTelemetry::Trace.current_span(otel_context).context
43
+ links = [OpenTelemetry::Trace::Link.new(parent_span_context)] if parent_span_context.valid?
44
+
45
+ span = Rage::Instrumentation.instance.tracer.start_root_span(
46
+ "#{task_class} perform",
47
+ links:,
48
+ kind: :consumer
49
+ )
50
+
51
+ OpenTelemetry::Trace.with_span(span) do
52
+ result = yield
53
+
54
+ if result.error?
55
+ span.record_exception(result.exception)
56
+ span.status = OpenTelemetry::Trace::Status.error
57
+ end
58
+ ensure
59
+ span.finish
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright The OpenTelemetry Authors
4
+ #
5
+ # SPDX-License-Identifier: Apache-2.0
6
+
7
+ require "opentelemetry/semconv/incubating/code"
8
+ require "opentelemetry/semconv/incubating/messaging"
9
+
10
+ module OpenTelemetry
11
+ module Instrumentation
12
+ module Rage
13
+ module Handlers
14
+ # The class records the publishing of Rage events and wraps event subscribers in spans.
15
+ class Events < ::Rage::Telemetry::Handler
16
+ handle "events.event.publish", with: :create_publisher_span
17
+ handle "events.subscriber.process", with: :create_subscriber_span
18
+
19
+ # @param event [Object] the event being published
20
+ def self.create_publisher_span(event:, &)
21
+ current_span = OpenTelemetry::Trace.current_span
22
+ return yield unless current_span.recording?
23
+
24
+ kind = :producer
25
+ attributes = {
26
+ SemConv::Incubating::MESSAGING::MESSAGING_SYSTEM => "rage.events",
27
+ SemConv::Incubating::MESSAGING::MESSAGING_OPERATION_TYPE => "publish",
28
+ SemConv::Incubating::MESSAGING::MESSAGING_DESTINATION_NAME => event.class.name
29
+ }
30
+
31
+ Rage::Instrumentation.instance.tracer.in_span("#{event.class} publish", kind:, attributes:, &)
32
+ end
33
+
34
+ # @param event [Object] the event being processed
35
+ # @param subscriber [Rage::Events::Subscriber] the subscriber processing the event
36
+ def self.create_subscriber_span(event:, subscriber:)
37
+ # deferred subscribers will be wrapped into spans by the `Handlers::Deferred` handler
38
+ return yield if subscriber.class.deferred?
39
+
40
+ kind = :consumer
41
+ attributes = {
42
+ SemConv::Incubating::MESSAGING::MESSAGING_SYSTEM => "rage.events",
43
+ SemConv::Incubating::MESSAGING::MESSAGING_OPERATION_TYPE => "process",
44
+ SemConv::Incubating::MESSAGING::MESSAGING_DESTINATION_NAME => event.class.name
45
+ }
46
+
47
+ Rage::Instrumentation.instance.tracer.in_span("#{subscriber.class} process", kind:, attributes:) do |span|
48
+ result = yield
49
+
50
+ if result.error?
51
+ span.record_exception(result.exception)
52
+ span.status = OpenTelemetry::Trace::Status.error
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,36 @@
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 Rage
10
+ module Handlers
11
+ # The class uses Fiber storage to propagate OpenTelemetry context between fibers.
12
+ class Fiber < ::Rage::Telemetry::Handler
13
+ # Save the current OpenTelemetry context into Fiber local storage.
14
+ # Application-level fibers spawned via `Fiber.schedule` will automatically inherit the storage.
15
+ module Patch
16
+ def schedule(&)
17
+ ::Fiber[:__rage_otel_context] = OpenTelemetry::Context.current
18
+ super
19
+ end
20
+ end
21
+
22
+ def initialize
23
+ super
24
+ ::Fiber.singleton_class.prepend(Patch)
25
+ end
26
+
27
+ handle "core.fiber.spawn", with: :propagate_otel_context
28
+
29
+ def propagate_otel_context(&)
30
+ OpenTelemetry::Context.with_current(::Fiber[:__rage_otel_context], &)
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright The OpenTelemetry Authors
4
+ #
5
+ # SPDX-License-Identifier: Apache-2.0
6
+
7
+ require "opentelemetry/semconv/http"
8
+ require "opentelemetry/semconv/incubating/code"
9
+
10
+ module OpenTelemetry
11
+ module Instrumentation
12
+ module Rage
13
+ module Handlers
14
+ # The class updates the name of the Rack span, adds relevant attributes, and records
15
+ # exceptions if any occur during the processing of a controller action.
16
+ class Request < ::Rage::Telemetry::Handler
17
+ handle "controller.action.process", with: :enrich_request_span
18
+
19
+ # @param controller [RageController::API] the controller processing the request
20
+ # @param request [Rage::Request] the request being processed
21
+ def self.enrich_request_span(controller:, request:)
22
+ span = OpenTelemetry::Instrumentation::Rack.current_span
23
+ return yield unless span.recording?
24
+
25
+ http_route = request.route_uri_pattern
26
+ span.name = "#{request.method} #{http_route}"
27
+ span.set_attribute(SemConv::HTTP::HTTP_ROUTE, http_route)
28
+
29
+ result = yield
30
+ return unless result.error?
31
+
32
+ span.record_exception(result.exception)
33
+ span.status = OpenTelemetry::Trace::Status.error
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -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 Rage
10
+ # The Instrumentation class contains logic to detect and install the Rage instrumentation
11
+ class Instrumentation < OpenTelemetry::Instrumentation::Base
12
+ install do |_config|
13
+ unless OpenTelemetry::Instrumentation::Rack::Instrumentation.instance.installed?
14
+ ENV["OTEL_SEMCONV_STABILITY_OPT_IN"] ||= "http"
15
+ OpenTelemetry::Instrumentation::Rack::Instrumentation.instance.install({use_rack_events: false})
16
+ end
17
+
18
+ require_dependencies
19
+ install_instrumentation
20
+ end
21
+
22
+ present do
23
+ defined?(::Rage)
24
+ end
25
+
26
+ compatible do
27
+ is_compatible = gem_version >= Gem::Version.new("1.20.0") && gem_version < Gem::Version.new("2")
28
+ OpenTelemetry.logger.warn("Rage version #{::Rage::VERSION} is not supported by the OpenTelemetry Rage instrumentation. Supported versions are >= 1.20.0 and < 2.0.0.") unless is_compatible
29
+
30
+ is_compatible
31
+ end
32
+
33
+ private
34
+
35
+ def gem_version
36
+ Gem::Version.new(::Rage::VERSION)
37
+ end
38
+
39
+ def require_dependencies
40
+ require_relative "log_context"
41
+
42
+ require_relative "handlers/cable"
43
+ require_relative "handlers/deferred"
44
+ require_relative "handlers/events"
45
+ require_relative "handlers/fiber"
46
+ require_relative "handlers/request"
47
+ end
48
+
49
+ def install_instrumentation
50
+ ::Rage.configure do
51
+ # install Rack middleware that creates spans for incoming requests
52
+ config.middleware.insert_after(0, *OpenTelemetry::Instrumentation::Rack::Instrumentation.instance.middleware_args)
53
+
54
+ # install telemetry handler to update request span name and attributes
55
+ config.telemetry.use Handlers::Request
56
+
57
+ # install telemetry handler to propagate context to application-level fibers
58
+ config.telemetry.use Handlers::Fiber.new
59
+
60
+ # install telemetry handlers for Rage components
61
+ config.telemetry.use Handlers::Cable
62
+ config.telemetry.use Handlers::Deferred
63
+ config.telemetry.use Handlers::Events
64
+
65
+ # install log context to add tracing info to logs
66
+ config.log_context << LogContext
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,30 @@
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 Rage
10
+ # The class provides a custom log context to enrich Rage logs with
11
+ # the current OpenTelemetry trace and span IDs.
12
+ class LogContext
13
+ class << self
14
+ def call
15
+ current_span = OpenTelemetry::Trace.current_span
16
+ return unless current_span.recording?
17
+
18
+ {
19
+ trace_id: current_span.context.hex_trace_id,
20
+ span_id: current_span.context.hex_span_id
21
+ }
22
+ rescue => e
23
+ OpenTelemetry.handle_error(exception: e)
24
+ nil
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,13 @@
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 Rage
10
+ VERSION = "0.1.0"
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright The OpenTelemetry Authors
4
+ #
5
+ # SPDX-License-Identifier: Apache-2.0
6
+
7
+ require "opentelemetry"
8
+ require "opentelemetry-instrumentation-base"
9
+
10
+ module OpenTelemetry
11
+ module Instrumentation
12
+ # Contains the OpenTelemetry instrumentation for the Rage gem
13
+ module Rage
14
+ end
15
+ end
16
+ end
17
+
18
+ require_relative "rage/instrumentation"
19
+ require_relative "rage/version"
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright The OpenTelemetry Authors
4
+ #
5
+ # SPDX-License-Identifier: Apache-2.0
6
+
7
+ # OpenTelemetry is an open source observability framework, providing a
8
+ # general-purpose API, SDK, and related tools required for the instrumentation
9
+ # of cloud-native software, frameworks, and libraries.
10
+ #
11
+ # The OpenTelemetry module provides global accessors for telemetry objects.
12
+ # See the documentation for the `opentelemetry-api` gem for details.
13
+ module OpenTelemetry
14
+ # Instrumentation should be able to handle the case when the library is not installed on a user's system.
15
+ module Instrumentation
16
+ end
17
+ end
18
+
19
+ require "opentelemetry-instrumentation-rack"
20
+ require_relative "instrumentation/rage"
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright The OpenTelemetry Authors
4
+ #
5
+ # SPDX-License-Identifier: Apache-2.0
6
+
7
+ require_relative "opentelemetry/instrumentation"
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/opentelemetry/instrumentation/rage/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "opentelemetry-instrumentation-rage"
7
+ spec.version = OpenTelemetry::Instrumentation::Rage::VERSION
8
+ spec.authors = ["Roman Samoilov"]
9
+ spec.email = ["developers@rage-rb.dev"]
10
+
11
+ spec.summary = "OpenTelemetry instrumentation for the Rage framework"
12
+ spec.homepage = "https://rage-rb.dev"
13
+ spec.license = "Apache-2.0"
14
+ spec.required_ruby_version = ">= 3.2.0"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = "https://github.com/rage-rb/opentelemetry-instrumentation"
18
+
19
+ spec.files = Dir.chdir(__dir__) do
20
+ `git ls-files -z`.split("\x0").reject do |f|
21
+ (File.expand_path(f) == __FILE__) ||
22
+ f.start_with?(*%w[bin/ test/ spec/ features/ .git .github appveyor Gemfile])
23
+ end
24
+ end
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_dependency "opentelemetry-instrumentation-rack", "~> 0.29"
30
+ spec.add_dependency "opentelemetry-semantic_conventions", "~> 1.36"
31
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: opentelemetry-instrumentation-rage
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Roman Samoilov
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 2026-02-01 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: opentelemetry-instrumentation-rack
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '0.29'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '0.29'
26
+ - !ruby/object:Gem::Dependency
27
+ name: opentelemetry-semantic_conventions
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '1.36'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.36'
40
+ email:
41
+ - developers@rage-rb.dev
42
+ executables: []
43
+ extensions: []
44
+ extra_rdoc_files: []
45
+ files:
46
+ - ".rspec"
47
+ - ".standard.yml"
48
+ - CHANGELOG.md
49
+ - CODE_OF_CONDUCT.md
50
+ - LICENSE
51
+ - README.md
52
+ - Rakefile
53
+ - example/trace_demonstration.ru
54
+ - lib/opentelemetry-instrumentation-rage.rb
55
+ - lib/opentelemetry/instrumentation.rb
56
+ - lib/opentelemetry/instrumentation/rage.rb
57
+ - lib/opentelemetry/instrumentation/rage/handlers/cable.rb
58
+ - lib/opentelemetry/instrumentation/rage/handlers/deferred.rb
59
+ - lib/opentelemetry/instrumentation/rage/handlers/events.rb
60
+ - lib/opentelemetry/instrumentation/rage/handlers/fiber.rb
61
+ - lib/opentelemetry/instrumentation/rage/handlers/request.rb
62
+ - lib/opentelemetry/instrumentation/rage/instrumentation.rb
63
+ - lib/opentelemetry/instrumentation/rage/log_context.rb
64
+ - lib/opentelemetry/instrumentation/rage/version.rb
65
+ - opentelemetry-instrumentation.gemspec
66
+ homepage: https://rage-rb.dev
67
+ licenses:
68
+ - Apache-2.0
69
+ metadata:
70
+ homepage_uri: https://rage-rb.dev
71
+ source_code_uri: https://github.com/rage-rb/opentelemetry-instrumentation
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: 3.2.0
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubygems_version: 3.6.2
87
+ specification_version: 4
88
+ summary: OpenTelemetry instrumentation for the Rage framework
89
+ test_files: []