Skip to content
Newer
Older
100644 55 lines (49 sloc) 1.95 KB
c7bf1c3 @jaggederest queue time, initial header work
jaggederest authored
1 module NewRelic
2 module Agent
3 module Instrumentation
1e068f4 @gnarg RUBY-1024 fixed and simplified WebFrontend/QueueTime metric recording
gnarg authored
4 # https://newrelic.com/docs/features/tracking-front-end-time
5 # Record queue time metrics based on any of three headers
6 # which can be set on the request.
c7bf1c3 @jaggederest queue time, initial header work
jaggederest authored
7 module QueueTime
f223dd4 @gnarg RUBY-1024 make sure not to define the queue time constants more than …
gnarg authored
8 unless defined?(REQUEST_START_HEADER)
9 REQUEST_START_HEADER = 'HTTP_X_REQUEST_START'
10 QUEUE_START_HEADER = 'HTTP_X_QUEUE_START'
11 QUEUE_DURATION_HEADER = 'HTTP_X_QUEUE_TIME'
12 MIDDLEWARE_START_HEADER = 'HTTP_X_MIDDLEWARE_START'
13 ALL_QUEUE_METRIC = 'WebFrontend/QueueTime'
59711d7 @gnarg RUBY-1024 make sure not to define the queue time constants more than …
gnarg authored
14 # any timestamps before this are thrown out and the parser
15 # will try again with a larger unit (2000/1/1 UTC)
16 EARLIEST_ACCEPTABLE_TIMESTAMP = 946684800
f223dd4 @gnarg RUBY-1024 make sure not to define the queue time constants more than …
gnarg authored
17 end
1e068f4 @gnarg RUBY-1024 fixed and simplified WebFrontend/QueueTime metric recording
gnarg authored
18
19 module_function
20
21 def parse_frontend_timestamp(headers)
22 candidate_headers = [ REQUEST_START_HEADER, QUEUE_START_HEADER,
23 MIDDLEWARE_START_HEADER ]
24 candidate_headers.map do |header|
25 if headers[header]
26 parse_timestamp(timestamp_string_from_header_value(headers[header]))
27 end
28 end.compact.min
29 end
30
31 def record_frontend_metrics(start_time, now=Time.now)
32 NewRelic::Agent.instance.stats_engine.get_stats(ALL_QUEUE_METRIC) \
33 .record_data_point((now - start_time).to_f)
34 end
35
36 def timestamp_string_from_header_value(value)
37 case value
38 when /^\s*([\d+\.]+)\s*$/ then $1
39 # following regexp intentionally unanchored to handle
40 # (ie ignore) leading server names
41 when /t=([\d+\.]+)/ then $1
aca6854 @jaggederest queue time finally actually measuring queue time, wip
jaggederest authored
42 end
c7bf1c3 @jaggederest queue time, initial header work
jaggederest authored
43 end
44
1e068f4 @gnarg RUBY-1024 fixed and simplified WebFrontend/QueueTime metric recording
gnarg authored
45 def parse_timestamp(string)
46 cut_off = Time.at(EARLIEST_ACCEPTABLE_TIMESTAMP)
47 [1_000_000, 1_000, 1].map do |divisor|
48 Time.at(string.to_f / divisor)
49 end.find {|candidate| candidate > cut_off }
c7bf1c3 @jaggederest queue time, initial header work
jaggederest authored
50 end
51 end
52 end
53 end
54 end
Something went wrong with that request. Please try again.