Class: Protocol::HTTP::Header::ServerTiming

Inherits:
Split
  • Object
show all
Defined in:
lib/protocol/http/header/server_timing.rb

Overview

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

“‘ruby 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"” “`

Defined Under Namespace

Classes: Metric

Constant Summary collapse

ParseError =
Class.new(Error)
METRIC =
/\A(?<name>[a-zA-Z0-9][a-zA-Z0-9_\-]*)(;(?<parameters>.*))?\z/
PARAMETER =
/(?<key>dur|desc)=((?<value>#{TOKEN})|(?<quoted_value>#{QUOTED_STRING}))/

Constants inherited from Split

Protocol::HTTP::Header::Split::COMMA

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Split

#<<, #initialize, #to_s

Constructor Details

This class inherits a constructor from Protocol::HTTP::Header::Split

Class Method Details

.trailer?Boolean

Whether this header is acceptable in HTTP trailers.

Returns:

  • (Boolean)


86
87
88
# File 'lib/protocol/http/header/server_timing.rb', line 86

def self.trailer?
  true
end

Instance Method Details

#metricsObject

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



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/protocol/http/header/server_timing.rb', line 57

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