Skip to content

Commit 980f442

Browse files
Simplify queue time calculation
- Remove abbreviations (timestamp_str -> timestamp_string) - Always assume timestamps are in seconds (remove millisecond/microsecond detection) - Remove corresponding millisecond test case
1 parent 4ebb8d1 commit 980f442

File tree

2 files changed

+3
-39
lines changed

2 files changed

+3
-39
lines changed

lib/metrics/provider/async/http/server.rb

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,31 +38,17 @@ def call(request)
3838
private
3939

4040
# Parse x-request-start header and calculate queue time in seconds.
41-
# Supports multiple formats:
41+
# Supports formats:
4242
# - "t=1234567890.123" (nginx format with 't=' prefix)
4343
# - "1234567890.123" (Unix timestamp in seconds)
44-
# - "1234567890123" (Unix timestamp in milliseconds)
4544
def calculate_queue_time(header_value)
4645
return nil unless header_value
4746

4847
# Remove 't=' prefix if present (nginx format)
49-
timestamp_str = header_value.sub(/^t=/, "")
48+
timestamp_string = header_value.sub(/^t=/, "")
5049

5150
begin
52-
timestamp = Float(timestamp_str)
53-
54-
# If timestamp is very large, it's likely in milliseconds or microseconds
55-
# Convert to seconds if necessary
56-
if timestamp > 10_000_000_000
57-
# Likely milliseconds (13 digits) or microseconds (16 digits)
58-
if timestamp > 10_000_000_000_000
59-
# Microseconds (16 digits)
60-
timestamp = timestamp / 1_000_000.0
61-
else
62-
# Milliseconds (13 digits)
63-
timestamp = timestamp / 1000.0
64-
end
65-
end
51+
timestamp = Float(timestamp_string)
6652

6753
current_time = Process.clock_gettime(Process::CLOCK_REALTIME)
6854
queue_time = current_time - timestamp

test/metrics/provider/async/http/server.rb

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -94,28 +94,6 @@
9494
server_task.stop
9595
end
9696

97-
it "handles millisecond timestamp format" do
98-
server_task = server.run
99-
bound_endpoint = server_task.wait_until_ready
100-
101-
request_start_ms = (Process.clock_gettime(Process::CLOCK_REALTIME) * 1000).to_i - 50
102-
103-
expect(Async::HTTP::Server::ASYNC_HTTP_SERVER_REQUEST_QUEUE_TIME).to receive(:emit) do |value, tags:|
104-
expect(value).to be > 0
105-
expect(value).to be < 1
106-
end
107-
108-
client = Async::HTTP::Client.new(bound_endpoint)
109-
headers = [["x-request-start", request_start_ms.to_s]]
110-
response = client.get("/", headers)
111-
112-
expect(response.status).to be == 200
113-
response.finish
114-
115-
client.close
116-
server_task.stop
117-
end
118-
11997
it "does not emit queue time metric when x-request-start header is missing" do
12098
server_task = server.run
12199
bound_endpoint = server_task.wait_until_ready

0 commit comments

Comments
 (0)