Scrape protocol content negotiation
Abstract
This document specifies the protocol negotiation mechanism used by Prometheus when scraping metrics from targets. It defines the Accept header format, supported Content Types, and the negotiation process for determining the best available format for metric exposition.
Introduction
Prometheus supports multiple formats for scraping metrics, including both text-based and binary protobuf formats. Based on the value of the Accept header, the target will pick the best available Content Type for its reply.
Protocol Types
Supported Protocols
The following protocols are be supported by Prometheus:
PrometheusProto
- Binary protobuf formatPrometheusText0.0.4
- Prometheus text format version 0.0.4PrometheusText1.0.0
- Prometheus text format version 1.0.0OpenMetricsText0.0.1
- OpenMetrics text format version 0.0.1OpenMetricsText1.0.0
- OpenMetrics text format version 1.0.0
Protocol Headers
Each protocol MUST be associated with a specific MIME type and version:
Protocol | MIME Type | Parameters |
---|---|---|
PrometheusProto | application/vnd.google.protobuf | proto=io.prometheus.client.MetricFamily;encoding=delimited |
PrometheusText0.0.4 | text/plain | version=0.0.4 |
PrometheusText1.0.0 | text/plain | version=1.0.0 |
OpenMetricsText0.0.1 | application/openmetrics-text | version=0.0.1 |
OpenMetricsText1.0.0 | application/openmetrics-text | version=1.0.0 |
Accept Header Construction
The Accept header is constructed by Prometheus to indicate what formats it supports.
Basic Format
The Accept header MUST be constructed as follows:
- For each protocol supported by the target:
- The protocol's MIME type and parameters MUST be specified.
- For protobuf protocols, an encoding of "delimited" MUST be specified.
- For PrometheusText1.0.0 and OpenMetricsText1.0.0, the escaping scheme parameter SHOULD be appended.
- A quality value (q) parameter SHOULD be appended.
- A catch-all
*/*
with the lowest quality value SHOULD be appended.
Quality Values
Quality values SHOULD be assigned in descending order based on the protocol's position in the Accept header:
- First protocol: q=0.{n+1}
- Second protocol: q=0.{n}
- And so on, where n is the number of supported protocols
Escaping Scheme
For PrometheusText1.0.0 and OpenMetricsText1.0.0 protocols, the Accept header
SHOULD include an escaping scheme parameter: escaping=<scheme>
Where <scheme>
MUST be one of:
allow-utf8
underscores
dots
values
See Escaping Schemes spec for details on how the escaping schemes function.
Compression
The Accept-Encoding header SHOULD be set to:
gzip
if compression is enabledidentity
if compression is disabled
Selection of Format
The scrape target SHOULD use the following process to select an appropriate Content-Type based on the list of protocols in the Accept header generated by Prometheus:
- It MUST use the protocol in the Accept header with the highest weighting that is supported by Prometheus.
- If no protocols are supported, the target MAY use a user-configured fallback scrape protocol.
- If no fallback is specified, the target MUST use PrometheusText0.0.4 as a last resort.
Content-Type Response
Targets SHOULD respond with a Content-Type header that matches one of the accepted formats. The Content-Type header MUST include:
- The appropriate MIME type.
- The version parameter.
- For text formats version 1.0.0 and above, the escaping scheme parameter.
Security Considerations
- Targets MUST validate the Accept header to prevent potential injection attacks
- The escaping scheme parameter MUST be validated to prevent protocol confusion
- Content-Type headers MUST be properly sanitized to prevent MIME type confusion
Examples
Default Accept Header
Accept: application/openmetrics-text;version=1.0.0;escaping=allow-utf8;q=0.5,application/openmetrics-text;version=0.0.1;q=0.4,text/plain;version=1.0.0;escaping=allow-utf8;q=0.3,text/plain;version=0.0.4;q=0.2,/;q=0.1
Protobuf-First Accept Header
Accept: application/vnd.google.protobuf;proto=io.prometheus.client.MetricFamily;encoding=delimited;q=0.5,application/
openmetrics-text;version=1.0.0;escaping=allow-utf8;q=0.4,application/openmetrics-text;version=0.0.1;q=0.3,text/plain;version=1.0.0;escaping=allow-utf8;q=0.2,text/plain;version=0.0.4;q=0.1,/;q=0.0