Skip to content

Commit b41a13c

Browse files
committed
fix: add readme and rubocop fix
1 parent 131de59 commit b41a13c

File tree

4 files changed

+53
-37
lines changed

4 files changed

+53
-37
lines changed

instrumentation/rack/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,19 @@ The `opentelemetry-instrumentation-rack` gem is distributed under the Apache 2.0
101101
[community-meetings]: https://github.com/open-telemetry/community#community-meetings
102102
[slack-channel]: https://cloud-native.slack.com/archives/C01NWKKMKMY
103103
[discussions-url]: https://github.com/open-telemetry/opentelemetry-ruby/discussions
104+
105+
## HTTP semantic convention stability
106+
107+
In the OpenTelemetry ecosystem, HTTP semantic conventions have now reached a stable state. However, the initial Rack instrumentation was introduced before this stability was achieved, which resulted in HTTP attributes being based on an older version of the semantic conventions.
108+
109+
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.
110+
111+
When setting the value for `OTEL_SEMCONV_STABILITY_OPT_IN`, you can specify which conventions you wish to adopt:
112+
113+
- `http` - Emits the stable HTTP and networking conventions and ceases emitting the old conventions previously emitted by the instrumentation.
114+
- `http/dup` - Emits both the old and stable HTTP and networking conventions, enabling a phased rollout of the stable semantic conventions.
115+
- Default behavior (in the absence of either value) is to continue emitting the old HTTP and networking conventions the instrumentation previously emitted.
116+
117+
During the transition from old to stable conventions, Rack instrumentation code comes in three patch versions: `dup`, `old`, and `stable`. These versions are identical except for the attributes they send. Any changes to Rack instrumentation should consider all three patches.
118+
119+
For additional information on migration, please refer to our [documentation](https://opentelemetry.io/docs/specs/semconv/non-normative/http-migration/).

instrumentation/rack/lib/opentelemetry/instrumentation/rack/middlewares/dup/event_handler.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def extract_request_headers(env)
128128
end
129129

130130
def extract_response_attributes(response)
131-
attributes = {
131+
attributes = {
132132
'http.status_code' => response.status.to_i,
133133
'http.response.status_code' => response.status.to_i
134134
}

instrumentation/rack/lib/opentelemetry/instrumentation/rack/middlewares/old/event_handler.rb

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ module Old
4242
# @see OpenTelemetry::Instrumentation::Rack.current_span
4343
class EventHandler
4444
include ::Rack::Events::Abstract
45-
45+
4646
OTEL_TOKEN_AND_SPAN = 'otel.rack.token_and_span'
4747
EMPTY_HASH = {}.freeze
48-
48+
4949
# Creates a server span for this current request using the incoming parent context
5050
# and registers them as the {current_span}
5151
#
@@ -58,15 +58,15 @@ def on_start(request, _)
5858
else
5959
extract_remote_context(request)
6060
end
61-
61+
6262
span = create_span(parent_context, request)
6363
span_ctx = OpenTelemetry::Trace.context_with_span(span, parent_context: parent_context)
6464
rack_ctx = OpenTelemetry::Instrumentation::Rack.context_with_span(span, parent_context: span_ctx)
6565
request.env[OTEL_TOKEN_AND_SPAN] = [OpenTelemetry::Context.attach(rack_ctx), span]
6666
rescue StandardError => e
6767
OpenTelemetry.handle_error(exception: e)
6868
end
69-
69+
7070
# Optionally adds debugging response headers injected from {response_propagators}
7171
#
7272
# @param [Rack::Request] The current HTTP request
@@ -75,7 +75,7 @@ def on_start(request, _)
7575
def on_commit(request, response)
7676
span = OpenTelemetry::Instrumentation::Rack.current_span
7777
return unless span.recording?
78-
78+
7979
response_propagators&.each do |propagator|
8080
propagator.inject(response.headers)
8181
rescue StandardError => e
@@ -84,7 +84,7 @@ def on_commit(request, response)
8484
rescue StandardError => e
8585
OpenTelemetry.handle_error(exception: e)
8686
end
87-
87+
8888
# Records Unexpected Exceptions on the Rack span and set the Span Status to Error
8989
#
9090
# @note does nothing if the span is a non-recording span
@@ -94,13 +94,13 @@ def on_commit(request, response)
9494
def on_error(request, _, error)
9595
span = OpenTelemetry::Instrumentation::Rack.current_span
9696
return unless span.recording?
97-
97+
9898
span.record_exception(error)
9999
span.status = OpenTelemetry::Trace::Status.error(error.class.name)
100100
rescue StandardError => e
101101
OpenTelemetry.handle_error(exception: e)
102102
end
103-
103+
104104
# Finishes the span making it eligible to be exported and cleans up existing contexts
105105
#
106106
# @note does nothing if the span is a non-recording span
@@ -109,33 +109,33 @@ def on_error(request, _, error)
109109
def on_finish(request, response)
110110
span = OpenTelemetry::Instrumentation::Rack.current_span
111111
return unless span.recording?
112-
112+
113113
add_response_attributes(span, response) if response
114114
rescue StandardError => e
115115
OpenTelemetry.handle_error(exception: e)
116116
ensure
117117
detach_context(request)
118118
end
119-
119+
120120
private
121-
121+
122122
def extract_request_headers(env)
123123
return EMPTY_HASH if allowed_request_headers.empty?
124-
124+
125125
allowed_request_headers.each_with_object({}) do |(key, value), result|
126126
result[value] = env[key] if env.key?(key)
127127
end
128128
end
129-
129+
130130
def extract_response_attributes(response)
131131
attributes = { 'http.status_code' => response.status.to_i }
132132
attributes.merge!(extract_response_headers(response.headers))
133133
attributes
134134
end
135-
135+
136136
def extract_response_headers(headers)
137137
return EMPTY_HASH if allowed_response_headers.empty?
138-
138+
139139
allowed_response_headers.each_with_object({}) do |(key, value), result|
140140
if headers.key?(key)
141141
result[value] = headers[key]
@@ -150,14 +150,14 @@ def extract_response_headers(headers)
150150
end
151151
end
152152
end
153-
153+
154154
def untraced_request?(env)
155155
return true if untraced_endpoints.include?(env['PATH_INFO'])
156156
return true if untraced_requests&.call(env)
157-
157+
158158
false
159159
end
160-
160+
161161
# https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/data-http.md#name
162162
#
163163
# recommendation: span.name(s) should be low-cardinality (e.g.,
@@ -167,90 +167,90 @@ def untraced_request?(env)
167167
def create_request_span_name(request)
168168
# NOTE: dd-trace-rb has implemented 'quantization' (which lowers url cardinality)
169169
# see Datadog::Quantization::HTTP.url
170-
170+
171171
if (implementation = url_quantization)
172172
request_uri_or_path_info = request.env['REQUEST_URI'] || request.path_info
173173
implementation.call(request_uri_or_path_info, request.env)
174174
else
175175
"HTTP #{request.request_method}"
176176
end
177177
end
178-
178+
179179
def extract_remote_context(request, context = Context.current)
180180
OpenTelemetry.propagation.extract(
181181
request.env,
182182
context: context,
183183
getter: OpenTelemetry::Common::Propagation.rack_env_getter
184184
)
185185
end
186-
186+
187187
def request_span_attributes(env)
188188
attributes = {
189189
'http.method' => env['REQUEST_METHOD'],
190190
'http.host' => env['HTTP_HOST'] || 'unknown',
191191
'http.scheme' => env['rack.url_scheme'],
192192
'http.target' => env['QUERY_STRING'].empty? ? env['PATH_INFO'] : "#{env['PATH_INFO']}?#{env['QUERY_STRING']}"
193193
}
194-
194+
195195
attributes['http.user_agent'] = env['HTTP_USER_AGENT'] if env['HTTP_USER_AGENT']
196196
attributes.merge!(extract_request_headers(env))
197197
attributes
198198
end
199-
199+
200200
def detach_context(request)
201201
return nil unless request.env[OTEL_TOKEN_AND_SPAN]
202-
202+
203203
token, span = request.env[OTEL_TOKEN_AND_SPAN]
204204
span.finish
205205
OpenTelemetry::Context.detach(token)
206206
rescue StandardError => e
207207
OpenTelemetry.handle_error(exception: e)
208208
end
209-
209+
210210
def add_response_attributes(span, response)
211211
span.status = OpenTelemetry::Trace::Status.error if response.server_error?
212212
attributes = extract_response_attributes(response)
213213
span.add_attributes(attributes)
214214
rescue StandardError => e
215215
OpenTelemetry.handle_error(exception: e)
216216
end
217-
217+
218218
def record_frontend_span?
219219
config[:record_frontend_span] == true
220220
end
221-
221+
222222
def untraced_endpoints
223223
config[:untraced_endpoints]
224224
end
225-
225+
226226
def untraced_requests
227227
config[:untraced_requests]
228228
end
229-
229+
230230
def url_quantization
231231
config[:url_quantization]
232232
end
233-
233+
234234
def response_propagators
235235
config[:response_propagators]
236236
end
237-
237+
238238
def allowed_request_headers
239239
config[:allowed_rack_request_headers]
240240
end
241-
241+
242242
def allowed_response_headers
243243
config[:allowed_rack_response_headers]
244244
end
245-
245+
246246
def tracer
247247
OpenTelemetry::Instrumentation::Rack::Instrumentation.instance.tracer
248248
end
249-
249+
250250
def config
251251
OpenTelemetry::Instrumentation::Rack::Instrumentation.instance.config
252252
end
253-
253+
254254
def create_span(parent_context, request)
255255
span = tracer.start_span(
256256
create_request_span_name(request),

instrumentation/rack/test/opentelemetry/instrumentation/rack/middlewares/dup/event_handler_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@
234234

235235
root_span = finished_spans.find { |s| s.attributes['http.target'] == '/' }
236236
root_span_stable = finished_spans.find { |s| s.attributes['url.path'] == '/' }
237-
237+
238238
_(root_span).wont_be_nil
239239
_(root_span_stable).wont_be_nil
240240
end

0 commit comments

Comments
 (0)