diff --git a/config/config.go b/config/config.go index c6407f624..52bf6b2d4 100644 --- a/config/config.go +++ b/config/config.go @@ -342,10 +342,9 @@ func (c *Config) LoadDirectory(path string) error { } // Try to find a default config file at these locations (in order): -// 1. $TELEGRAF_CONFIG_PATH -// 2. $HOME/.telegraf/telegraf.conf -// 3. /etc/telegraf/telegraf.conf -// +// 1. $TELEGRAF_CONFIG_PATH +// 2. $HOME/.telegraf/telegraf.conf +// 3. /etc/telegraf/telegraf.conf func getDefaultConfigPath() (string, error) { envfile := os.Getenv("TELEGRAF_CONFIG_PATH") 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_sort_metrics", &sc.PrometheusSortMetrics) c.getFieldBool(tbl, "prometheus_string_as_label", &sc.PrometheusStringAsLabel) + c.getFieldBool(tbl, "prometheus_compact_encoding", &sc.PrometheusCompactEncoding) if c.hasErrs() { 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", "json_timestamp_format", "json_timestamp_units", "prometheus_export_timestamp", "prometheus_sort_metrics", "prometheus_string_as_label", + "prometheus_compact_encoding", "splunkmetric_hec_routing", "splunkmetric_multimetric", "wavefront_disable_prefix_conversion", "wavefront_source_override", "wavefront_use_strict": default: diff --git a/plugins/serializers/prometheus/README.md b/plugins/serializers/prometheus/README.md index 5bf509c97..0fbcc5eaf 100644 --- a/plugins/serializers/prometheus/README.md +++ b/plugins/serializers/prometheus/README.md @@ -31,6 +31,10 @@ reporting others every bucket/quantile will continue to exist. ## discarded. prometheus_string_as_label = false + ## Encode metrics without HELP metadata. This helps reduce the payload + ## size. + prometheus_compact_encoding = false + ## Data format to output. ## Each data format has its own unique set of configuration options, read ## more about them here: diff --git a/plugins/serializers/prometheus/collection.go b/plugins/serializers/prometheus/collection.go index 9f23544d6..a8b6d2f2f 100644 --- a/plugins/serializers/prometheus/collection.go +++ b/plugins/serializers/prometheus/collection.go @@ -412,10 +412,13 @@ func (c *Collection) GetProto() []*dto.MetricFamily { for _, entry := range c.GetEntries(c.config.MetricSortOrder) { mf := &dto.MetricFamily{ Name: proto.String(entry.Family.Name), - Help: proto.String(helpString), Type: MetricType(entry.Family.Type), } + if !c.config.CompactEncoding { + mf.Help = proto.String(helpString) + } + for _, metric := range c.GetMetrics(entry, c.config.MetricSortOrder) { l := make([]*dto.LabelPair, 0, len(metric.Labels)) for _, label := range metric.Labels { diff --git a/plugins/serializers/prometheus/prometheus.go b/plugins/serializers/prometheus/prometheus.go index 9e5df5882..62a03ca1e 100644 --- a/plugins/serializers/prometheus/prometheus.go +++ b/plugins/serializers/prometheus/prometheus.go @@ -36,6 +36,10 @@ type FormatConfig struct { TimestampExport TimestampExport MetricSortOrder MetricSortOrder 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 { diff --git a/plugins/serializers/prometheus/prometheus_test.go b/plugins/serializers/prometheus/prometheus_test.go index a2a95482d..8f275b124 100644 --- a/plugins/serializers/prometheus/prometheus_test.go +++ b/plugins/serializers/prometheus/prometheus_test.go @@ -156,6 +156,26 @@ http_request_duration_seconds_count 0 # HELP cpu_time_idle Telegraf collected metric # TYPE cpu_time_idle untyped 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, TimestampExport: tt.config.TimestampExport, StringHandling: tt.config.StringHandling, + CompactEncoding: tt.config.CompactEncoding, }) require.NoError(t, err) actual, err := s.Serialize(tt.metric) diff --git a/plugins/serializers/registry.go b/plugins/serializers/registry.go index 9fefef2e4..61d48dabf 100644 --- a/plugins/serializers/registry.go +++ b/plugins/serializers/registry.go @@ -130,6 +130,9 @@ type Config struct { // Output string fields as metric labels; when false string fields are // discarded. 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. @@ -206,6 +209,7 @@ func NewPrometheusSerializer(config *Config) (Serializer, error) { TimestampExport: exportTimestamp, MetricSortOrder: sortMetrics, StringHandling: stringAsLabels, + CompactEncoding: config.PrometheusCompactEncoding, }) }