Protocol::HTTPSourceProtocolHTTPHeaderServerTiming

class ServerTiming

The server-timing header communicates performance metrics about the request-response cycle to the client.

This header allows servers to send timing information about various server-side operations, which can be useful for performance monitoring and debugging. Each metric can include a name, optional duration, and optional description.

Examples

server_timing = ServerTiming.new("db;dur=53.2")
server_timing << "cache;dur=12.1;desc=\"Redis lookup\""
puts server_timing.to_s
# => "db;dur=53.2, cache;dur=12.1;desc=\"Redis lookup\""

Nested

Definitions

METRIC = /\A(?<name>[a-zA-Z0-9][a-zA-Z0-9_\-]*)(;(?<parameters>.*))?\z/

https://www.w3.org/TR/server-timing/

def metrics

Parse the server-timing header value into a list of metrics.

Signature

returns Array(Metric)

the list of metrics with their names, durations, and descriptions.

Implementation

def metrics
	self.map do |value|
		if match = value.match(METRIC)
			name = match[:name]
			parameters = match[:parameters] || ""
			
			duration = nil
			description = nil
			
			parameters.scan(PARAMETER) do |key, value, quoted_value|
				value = QuotedString.unquote(quoted_value) if quoted_value
				
				case key
				when "dur"
					duration = value.to_f
				when "desc"
					description = value
				end
			end
			
			Metric.new(name, duration, description)
		else
			raise ParseError.new("Could not parse server timing metric: #{value.inspect}")
		end
	end
end

def self.trailer?

Whether this header is acceptable in HTTP trailers.

Signature

returns Boolean

true, as server-timing headers contain performance metrics that are typically calculated during response generation.

Implementation

def self.trailer?
	true
end