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\""

Definitions

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

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

Metric

A single metric in the Server-Timing header.

Implementation

Metric = Struct.new(:name, :duration, :description) do
	# Create a new server timing metric.
	#
	# @parameter name [String] the name of the metric.
	# @parameter duration [Float | Nil] the duration in milliseconds.
	# @parameter description [String | Nil] the description of the metric.
	def initialize(name, duration = nil, description = nil)
		super(name, duration, description)
	end
	
	# Convert the metric to its string representation.
	#
	# @returns [String] the formatted metric string.
	def to_s
		result = name.dup
		result << ";dur=#{duration}" if duration
		result << ";desc=\"#{description}\"" if description
		result
	end
end

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