Documentation
¶
Overview ¶
Package http_metrics provides Prometheus metric collectors for HTTP server and client operations. It tracks request latencies, body sizes, status codes, and error counts for both incoming HTTP requests and outgoing HTTP client calls.
Example usage for HTTP server:
reg := metrics.NewRegistry() serverStorage := http_metrics.NewServerStorage(reg) // In your HTTP handler: start := time.Now() // ... handle request ... serverStorage.ObserveDuration(method, endpoint, time.Since(start)) serverStorage.CountStatusCode(method, endpoint, statusCode)
Example usage for HTTP client:
clientStorage := http_metrics.NewClientStorage(reg) clientStorage.ObserveDuration(endpoint, duration) clientStorage.CountError(endpoint, err)
Index ¶
- func ClientEndpoint(ctx context.Context) string
- func ClientEndpointToContext(ctx context.Context, endpoint string) context.Context
- func ServerEndpoint(ctx context.Context) string
- func ServerEndpointToContext(ctx context.Context, endpoint string) context.Context
- type ClientStorage
- func (s *ClientStorage) CountError(endpoint string, err error)
- func (s *ClientStorage) CountStatusCode(endpoint string, code int)
- func (s *ClientStorage) ObserveConnEstablishment(endpoint string, duration time.Duration)
- func (s *ClientStorage) ObserveDnsLookup(endpoint string, duration time.Duration)
- func (s *ClientStorage) ObserveDuration(endpoint string, duration time.Duration)
- func (s *ClientStorage) ObserveRequestWriting(endpoint string, duration time.Duration)
- func (s *ClientStorage) ObserveResponseReading(endpoint string, duration time.Duration)
- type ServerStorage
- func (s *ServerStorage) CountStatusCode(method string, endpoint string, code int)
- func (s *ServerStorage) ObserveDuration(method string, endpoint string, duration time.Duration)
- func (s *ServerStorage) ObserveRequestBodySize(method string, endpoint string, size int)
- func (s *ServerStorage) ObserveResponseBodySize(method string, endpoint string, size int)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ClientEndpoint ¶
ClientEndpoint retrieves the HTTP client endpoint name from the context. Returns an empty string if no endpoint was set.
func ClientEndpointToContext ¶
ClientEndpointToContext stores the HTTP client endpoint name in the context.
func ServerEndpoint ¶
ServerEndpoint retrieves the HTTP endpoint name from the context. Returns an empty string if no endpoint was set.
Types ¶
type ClientStorage ¶
type ClientStorage struct {
// contains filtered or unexported fields
}
ClientStorage collects metrics for HTTP client operations, including request latency, connection establishment, DNS lookup, and error counts.
func NewClientStorage ¶
func NewClientStorage(reg *metrics.Registry) *ClientStorage
NewClientStorage creates a new ClientStorage instance and registers its metrics with the provided registry. Metrics are labeled by the client endpoint.
func (*ClientStorage) CountError ¶ added in v1.45.2
func (s *ClientStorage) CountError(endpoint string, err error)
CountError increments the error counter for a specific error type. The error message is sanitized to remove sensitive information like URLs and IPs.
func (*ClientStorage) CountStatusCode ¶ added in v1.45.2
func (s *ClientStorage) CountStatusCode(endpoint string, code int)
CountStatusCode increments the counter for a specific HTTP status code.
func (*ClientStorage) ObserveConnEstablishment ¶ added in v1.38.0
func (s *ClientStorage) ObserveConnEstablishment(endpoint string, duration time.Duration)
ObserveConnEstablishment records the time taken to establish a connection.
func (*ClientStorage) ObserveDnsLookup ¶ added in v1.38.0
func (s *ClientStorage) ObserveDnsLookup(endpoint string, duration time.Duration)
ObserveDnsLookup records the time taken for DNS resolution.
func (*ClientStorage) ObserveDuration ¶
func (s *ClientStorage) ObserveDuration(endpoint string, duration time.Duration)
ObserveDuration records the total duration of an HTTP client request.
func (*ClientStorage) ObserveRequestWriting ¶ added in v1.38.0
func (s *ClientStorage) ObserveRequestWriting(endpoint string, duration time.Duration)
ObserveRequestWriting records the time taken to write the request body.
func (*ClientStorage) ObserveResponseReading ¶ added in v1.38.0
func (s *ClientStorage) ObserveResponseReading(endpoint string, duration time.Duration)
ObserveResponseReading records the time taken to read the response.
type ServerStorage ¶
type ServerStorage struct {
// contains filtered or unexported fields
}
ServerStorage collects metrics for HTTP server operations, including request latency, request/response body sizes, and HTTP status code counts.
func NewServerStorage ¶
func NewServerStorage(reg *metrics.Registry) *ServerStorage
NewServerStorage creates a new ServerStorage instance and registers its metrics with the provided registry. The metrics are labeled by HTTP method and endpoint.
func (*ServerStorage) CountStatusCode ¶
func (s *ServerStorage) CountStatusCode(method string, endpoint string, code int)
CountStatusCode increments the counter for a specific HTTP status code, labeled by method, endpoint, and status code.
func (*ServerStorage) ObserveDuration ¶
func (s *ServerStorage) ObserveDuration(method string, endpoint string, duration time.Duration)
ObserveDuration records the latency of an HTTP request, labeled by method and endpoint.
func (*ServerStorage) ObserveRequestBodySize ¶
func (s *ServerStorage) ObserveRequestBodySize(method string, endpoint string, size int)
ObserveRequestBodySize records the size of an HTTP request body in bytes.
func (*ServerStorage) ObserveResponseBodySize ¶
func (s *ServerStorage) ObserveResponseBodySize(method string, endpoint string, size int)
ObserveResponseBodySize records the size of an HTTP response body in bytes.