-
-
Notifications
You must be signed in to change notification settings - Fork 525
feat: queue time capture for Rack #2838
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
39a875b
8b2e2b8
7829e24
dfea02f
c223fce
597c2e6
9f3bd01
1407b7d
f5e132b
13cd936
b32132c
00cc056
39b76a3
6f0fc54
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -72,7 +72,14 @@ def start_transaction(env, scope) | |||||
| } | ||||||
|
|
||||||
| transaction = Sentry.continue_trace(env, **options) | ||||||
| Sentry.start_transaction(transaction: transaction, custom_sampling_context: { env: env }, **options) | ||||||
| transaction = Sentry.start_transaction(transaction: transaction, custom_sampling_context: { env: env }, **options) | ||||||
|
|
||||||
| # attach queue time if available | ||||||
| if transaction && (queue_time = extract_queue_time(env)) | ||||||
| transaction.set_data(Span::DataConventions::HTTP_QUEUE_TIME_MS, queue_time) | ||||||
| end | ||||||
|
|
||||||
| transaction | ||||||
| end | ||||||
|
|
||||||
|
|
||||||
|
|
@@ -86,6 +93,75 @@ def finish_transaction(transaction, status_code) | |||||
| def mechanism | ||||||
| Sentry::Mechanism.new(type: MECHANISM_TYPE, handled: false) | ||||||
| end | ||||||
|
|
||||||
| # Extracts queue time from the request environment. | ||||||
| # Calculates the time (in milliseconds) the request spent waiting in the | ||||||
| # web server queue before processing began. | ||||||
| # | ||||||
| # Subtracts puma.request_body_wait to account for time spent waiting for | ||||||
| # slow clients to send the request body, isolating actual queue time. | ||||||
| # See: https://github.com/puma/puma/blob/master/docs/architecture.md | ||||||
| # | ||||||
| # @param env [Hash] Rack env | ||||||
| # @return [Float, nil] queue time in milliseconds or nil | ||||||
| def extract_queue_time(env) | ||||||
| return nil unless Sentry.configuration&.capture_queue_time | ||||||
|
|
||||||
| header_value = env["HTTP_X_REQUEST_START"] | ||||||
| return nil unless header_value | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| request_start = parse_request_start_header(header_value) | ||||||
| return nil unless request_start | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| total_time_ms = ((Time.now.to_f - request_start) * 1000).round(2) | ||||||
|
|
||||||
| # reject negative (clock skew between proxy & app server) | ||||||
| return nil unless total_time_ms >= 0 | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| puma_wait_ms = env["puma.request_body_wait"] | ||||||
|
|
||||||
| if puma_wait_ms && puma_wait_ms > 0 | ||||||
| queue_time_ms = total_time_ms - puma_wait_ms | ||||||
| queue_time_ms >= 0 ? queue_time_ms : 0.0 # more sanity check | ||||||
| else | ||||||
| total_time_ms | ||||||
| end | ||||||
| rescue StandardError | ||||||
| nil | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| end | ||||||
|
|
||||||
| # Parses X-Request-Start header value to extract a timestamp. | ||||||
| # Supports multiple formats: | ||||||
| # - Nginx: "t=1234567890.123" (seconds with decimal) | ||||||
| # - Heroku, HAProxy 1.9+: "t=1234567890123456" (microseconds) | ||||||
| # - HAProxy < 1.9: "t=1234567890" (seconds) | ||||||
| # - Generic: "1234567890.123" (raw timestamp) | ||||||
| # | ||||||
| # @param header_value [String] The X-Request-Start header value | ||||||
| # @return [Float, nil] Timestamp in seconds since epoch or nil | ||||||
| def parse_request_start_header(header_value) | ||||||
| return unless header_value | ||||||
|
|
||||||
| timestamp = if header_value.start_with?("t=") | ||||||
| header_value[2..-1].to_f | ||||||
| elsif header_value.match?(/\A\d+(?:\.\d+)?\z/) | ||||||
| header_value.to_f | ||||||
| else | ||||||
| return nil | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| end | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Non-numeric
|
||||||
|
|
||||||
| # normalize: timestamps can be in seconds, milliseconds or microseconds | ||||||
| # any timestamp > 10 trillion = microseconds | ||||||
| if timestamp > 10_000_000_000_000 | ||||||
| timestamp / 1_000_000.0 | ||||||
| # timestamp > 10 billion & < 10 trillion = milliseconds | ||||||
| elsif timestamp > 10_000_000_000 | ||||||
| timestamp / 1_000.0 | ||||||
| else | ||||||
| timestamp # assume seconds | ||||||
| end | ||||||
| rescue StandardError | ||||||
| end | ||||||
| end | ||||||
| end | ||||||
| end | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.