diff --git a/docs/clients/promtail/stages/metrics.md b/docs/clients/promtail/stages/metrics.md index 2c90a46815533..419c01fade584 100644 --- a/docs/clients/promtail/stages/metrics.md +++ b/docs/clients/promtail/stages/metrics.md @@ -26,6 +26,9 @@ type: Counter # Describes the metric. [description: ] +# Defines custom prefix name for the metric. If undefined, default name "promtail_custom_" will be prefixed. +[prefix: ] + # Key from the extracted data map to use for the mtric, # defaulting to the metric's name if not present. [source: ] @@ -55,6 +58,9 @@ type: Gauge # Describes the metric. [description: ] +# Defines custom prefix name for the metric. If undefined, default name "promtail_custom_" will be prefixed. +[prefix: ] + # Key from the extracted data map to use for the mtric, # defaulting to the metric's name if not present. [source: ] @@ -83,6 +89,9 @@ type: Histogram # Describes the metric. [description: ] +# Defines custom prefix name for the metric. If undefined, default name "promtail_custom_" will be prefixed. +[prefix: ] + # Key from the extracted data map to use for the mtric, # defaulting to the metric's name if not present. [source: ] @@ -114,6 +123,7 @@ config: log_lines_total: type: Counter description: "total number of log lines" + prefix: my_promtail_custom_ source: time config: action: inc diff --git a/pkg/logentry/stages/metrics.go b/pkg/logentry/stages/metrics.go index d831083c07264..63abba205c8fb 100644 --- a/pkg/logentry/stages/metrics.go +++ b/pkg/logentry/stages/metrics.go @@ -18,8 +18,6 @@ import ( "github.com/grafana/loki/pkg/logentry/metric" ) -const customPrefix = "promtail_custom_" - const ( MetricTypeCounter = "counter" MetricTypeGauge = "gauge" @@ -34,6 +32,7 @@ type MetricConfig struct { MetricType string `mapstructure:"type"` Description string `mapstructure:"description"` Source *string `mapstructure:"source"` + Prefix string `mapstructure:"prefix"` Config interface{} `mapstructure:"config"` } @@ -78,6 +77,13 @@ func newMetricStage(logger log.Logger, config interface{}, registry prometheus.R for name, cfg := range *cfgs { var collector prometheus.Collector + customPrefix := "" + if cfg.Prefix != "" { + customPrefix = cfg.Prefix + } else { + customPrefix = "promtail_custom_" + } + switch strings.ToLower(cfg.MetricType) { case MetricTypeCounter: collector, err = metric.NewCounters(customPrefix+name, cfg.Description, cfg.Config) diff --git a/pkg/logentry/stages/metrics_test.go b/pkg/logentry/stages/metrics_test.go index 4c4ad4e8adde7..427dc647da2be 100644 --- a/pkg/logentry/stages/metrics_test.go +++ b/pkg/logentry/stages/metrics_test.go @@ -24,6 +24,7 @@ pipeline_stages: loki_count: type: Counter description: uhhhhhhh + prefix: my_promtail_custom_ source: app config: value: loki @@ -65,9 +66,9 @@ var testMetricLogLine2 = ` const expectedMetrics = `# HELP promtail_custom_bloki_count blerrrgh # TYPE promtail_custom_bloki_count gauge promtail_custom_bloki_count -1.0 -# HELP promtail_custom_loki_count uhhhhhhh -# TYPE promtail_custom_loki_count counter -promtail_custom_loki_count 1.0 +# HELP my_promtail_custom_loki_count uhhhhhhh +# TYPE my_promtail_custom_loki_count counter +my_promtail_custom_loki_count 1.0 # HELP promtail_custom_payload_size_bytes grrrragh # TYPE promtail_custom_payload_size_bytes histogram promtail_custom_payload_size_bytes_bucket{le="10.0"} 1.0 @@ -262,7 +263,13 @@ func TestMetricStage_Process(t *testing.T) { func metricNames(cfg MetricsConfig) []string { result := make([]string, 0, len(cfg)) - for name := range cfg { + for name, config := range cfg { + customPrefix := "" + if config.Prefix != "" { + customPrefix = config.Prefix + } else { + customPrefix = "promtail_custom_" + } result = append(result, customPrefix+name) } return result