feat(serializers.prometheus): Provide option to reduce payload size by removing HELP from payload (#11690)

This commit is contained in:
Conor Evans 2022-08-18 08:22:40 +01:00 committed by GitHub
parent 1cc24efd3b
commit b76d794968
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 42 additions and 5 deletions

View File

@ -342,10 +342,9 @@ func (c *Config) LoadDirectory(path string) error {
} }
// Try to find a default config file at these locations (in order): // Try to find a default config file at these locations (in order):
// 1. $TELEGRAF_CONFIG_PATH // 1. $TELEGRAF_CONFIG_PATH
// 2. $HOME/.telegraf/telegraf.conf // 2. $HOME/.telegraf/telegraf.conf
// 3. /etc/telegraf/telegraf.conf // 3. /etc/telegraf/telegraf.conf
//
func getDefaultConfigPath() (string, error) { func getDefaultConfigPath() (string, error) {
envfile := os.Getenv("TELEGRAF_CONFIG_PATH") envfile := os.Getenv("TELEGRAF_CONFIG_PATH")
homefile := os.ExpandEnv("${HOME}/.telegraf/telegraf.conf") homefile := os.ExpandEnv("${HOME}/.telegraf/telegraf.conf")
@ -1165,6 +1164,7 @@ func (c *Config) buildSerializer(tbl *ast.Table) (serializers.Serializer, error)
c.getFieldBool(tbl, "prometheus_export_timestamp", &sc.PrometheusExportTimestamp) c.getFieldBool(tbl, "prometheus_export_timestamp", &sc.PrometheusExportTimestamp)
c.getFieldBool(tbl, "prometheus_sort_metrics", &sc.PrometheusSortMetrics) c.getFieldBool(tbl, "prometheus_sort_metrics", &sc.PrometheusSortMetrics)
c.getFieldBool(tbl, "prometheus_string_as_label", &sc.PrometheusStringAsLabel) c.getFieldBool(tbl, "prometheus_string_as_label", &sc.PrometheusStringAsLabel)
c.getFieldBool(tbl, "prometheus_compact_encoding", &sc.PrometheusCompactEncoding)
if c.hasErrs() { if c.hasErrs() {
return nil, c.firstErr() return nil, c.firstErr()
@ -1233,6 +1233,7 @@ func (c *Config) missingTomlField(_ reflect.Type, key string) error {
"influx_max_line_bytes", "influx_sort_fields", "influx_uint_support", "influx_max_line_bytes", "influx_sort_fields", "influx_uint_support",
"json_timestamp_format", "json_timestamp_units", "json_timestamp_format", "json_timestamp_units",
"prometheus_export_timestamp", "prometheus_sort_metrics", "prometheus_string_as_label", "prometheus_export_timestamp", "prometheus_sort_metrics", "prometheus_string_as_label",
"prometheus_compact_encoding",
"splunkmetric_hec_routing", "splunkmetric_multimetric", "splunkmetric_hec_routing", "splunkmetric_multimetric",
"wavefront_disable_prefix_conversion", "wavefront_source_override", "wavefront_use_strict": "wavefront_disable_prefix_conversion", "wavefront_source_override", "wavefront_use_strict":
default: default:

View File

@ -31,6 +31,10 @@ reporting others every bucket/quantile will continue to exist.
## discarded. ## discarded.
prometheus_string_as_label = false prometheus_string_as_label = false
## Encode metrics without HELP metadata. This helps reduce the payload
## size.
prometheus_compact_encoding = false
## Data format to output. ## Data format to output.
## Each data format has its own unique set of configuration options, read ## Each data format has its own unique set of configuration options, read
## more about them here: ## more about them here:

View File

@ -412,10 +412,13 @@ func (c *Collection) GetProto() []*dto.MetricFamily {
for _, entry := range c.GetEntries(c.config.MetricSortOrder) { for _, entry := range c.GetEntries(c.config.MetricSortOrder) {
mf := &dto.MetricFamily{ mf := &dto.MetricFamily{
Name: proto.String(entry.Family.Name), Name: proto.String(entry.Family.Name),
Help: proto.String(helpString),
Type: MetricType(entry.Family.Type), Type: MetricType(entry.Family.Type),
} }
if !c.config.CompactEncoding {
mf.Help = proto.String(helpString)
}
for _, metric := range c.GetMetrics(entry, c.config.MetricSortOrder) { for _, metric := range c.GetMetrics(entry, c.config.MetricSortOrder) {
l := make([]*dto.LabelPair, 0, len(metric.Labels)) l := make([]*dto.LabelPair, 0, len(metric.Labels))
for _, label := range metric.Labels { for _, label := range metric.Labels {

View File

@ -36,6 +36,10 @@ type FormatConfig struct {
TimestampExport TimestampExport TimestampExport TimestampExport
MetricSortOrder MetricSortOrder MetricSortOrder MetricSortOrder
StringHandling StringHandling StringHandling StringHandling
// CompactEncoding defines whether to include
// HELP metadata in Prometheus payload. Setting to true
// helps to reduce payload size.
CompactEncoding bool
} }
type Serializer struct { type Serializer struct {

View File

@ -156,6 +156,26 @@ http_request_duration_seconds_count 0
# HELP cpu_time_idle Telegraf collected metric # HELP cpu_time_idle Telegraf collected metric
# TYPE cpu_time_idle untyped # TYPE cpu_time_idle untyped
cpu_time_idle{host="example.org"} 42 1574279268000 cpu_time_idle{host="example.org"} 42 1574279268000
`),
},
{
name: "simple with CompactEncoding",
config: FormatConfig{
CompactEncoding: true,
},
metric: testutil.MustMetric(
"cpu",
map[string]string{
"host": "example.org",
},
map[string]interface{}{
"time_idle": 42.0,
},
time.Unix(1574279268, 0),
),
expected: []byte(`
# TYPE cpu_time_idle untyped
cpu_time_idle{host="example.org"} 42
`), `),
}, },
} }
@ -165,6 +185,7 @@ cpu_time_idle{host="example.org"} 42 1574279268000
MetricSortOrder: SortMetrics, MetricSortOrder: SortMetrics,
TimestampExport: tt.config.TimestampExport, TimestampExport: tt.config.TimestampExport,
StringHandling: tt.config.StringHandling, StringHandling: tt.config.StringHandling,
CompactEncoding: tt.config.CompactEncoding,
}) })
require.NoError(t, err) require.NoError(t, err)
actual, err := s.Serialize(tt.metric) actual, err := s.Serialize(tt.metric)

View File

@ -130,6 +130,9 @@ type Config struct {
// Output string fields as metric labels; when false string fields are // Output string fields as metric labels; when false string fields are
// discarded. // discarded.
PrometheusStringAsLabel bool `toml:"prometheus_string_as_label"` PrometheusStringAsLabel bool `toml:"prometheus_string_as_label"`
// Encode metrics without HELP metadata. This helps reduce the payload size.
PrometheusCompactEncoding bool `toml:"prometheus_compact_encoding"`
} }
// NewSerializer a Serializer interface based on the given config. // NewSerializer a Serializer interface based on the given config.
@ -206,6 +209,7 @@ func NewPrometheusSerializer(config *Config) (Serializer, error) {
TimestampExport: exportTimestamp, TimestampExport: exportTimestamp,
MetricSortOrder: sortMetrics, MetricSortOrder: sortMetrics,
StringHandling: stringAsLabels, StringHandling: stringAsLabels,
CompactEncoding: config.PrometheusCompactEncoding,
}) })
} }