Skip to content

Commit 258b587

Browse files
committed
refactor: Assign attributes using select
1 parent 3eef72a commit 258b587

File tree

1 file changed

+5
-8
lines changed
  • instrumentation/rack/lib/opentelemetry/instrumentation/rack/middlewares

1 file changed

+5
-8
lines changed

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,8 @@ def create_span(parent_context, request)
266266
end
267267

268268
# Metrics stuff
269+
HTTP_SERVER_REQUEST_DURATION_ATTRS_FROM_SPAN = %w[http.method http.scheme http.route http.status_code http.host].freeze
270+
269271
def metrics_enabled?
270272
OpenTelemetry::Instrumentation::Rack::Instrumentation.instance.metrics_enabled?
271273
end
@@ -287,14 +289,9 @@ def record_http_server_request_duration_metric(span)
287289
# find span duration
288290
# end - start / a billion to convert nanoseconds to seconds
289291
duration = (span.end_timestamp - span.start_timestamp) / Float(10**9)
290-
# Create attributes
291-
#
292-
attrs = {}
293-
attrs['http.method'] = span.attributes['http.method']
294-
attrs['http.scheme'] = span.attributes['http.scheme']
295-
attrs['http.route'] = span.attributes['http.route']
296-
attrs['http.status_code'] = span.attributes['http.status_code']
297-
attrs['http.host'] = span.attributes['http.host']
292+
# glean attributes
293+
attrs = span.attributes.select {|k, _v| HTTP_SERVER_REQUEST_DURATION_ATTRS_FROM_SPAN.include?(k) }
294+
# set error
298295
attrs['error.type'] = span.status.description if span.status.code == OpenTelemetry::Trace::Status::ERROR
299296

300297
http_server_request_duration_histogram.record(duration, attributes: attrs)

0 commit comments

Comments
 (0)