Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom prefix name in metrics stage #1664

Merged
merged 1 commit into from
Feb 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/clients/promtail/stages/metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ type: Counter
# Describes the metric.
[description: <string>]

# Defines custom prefix name for the metric. If undefined, default name "promtail_custom_" will be prefixed.
[prefix: <string>]

# Key from the extracted data map to use for the mtric,
# defaulting to the metric's name if not present.
[source: <string>]
Expand Down Expand Up @@ -55,6 +58,9 @@ type: Gauge
# Describes the metric.
[description: <string>]

# Defines custom prefix name for the metric. If undefined, default name "promtail_custom_" will be prefixed.
[prefix: <string>]

# Key from the extracted data map to use for the mtric,
# defaulting to the metric's name if not present.
[source: <string>]
Expand Down Expand Up @@ -83,6 +89,9 @@ type: Histogram
# Describes the metric.
[description: <string>]

# Defines custom prefix name for the metric. If undefined, default name "promtail_custom_" will be prefixed.
[prefix: <string>]

# Key from the extracted data map to use for the mtric,
# defaulting to the metric's name if not present.
[source: <string>]
Expand Down Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions pkg/logentry/stages/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import (
"github.com/grafana/loki/pkg/logentry/metric"
)

const customPrefix = "promtail_custom_"

const (
MetricTypeCounter = "counter"
MetricTypeGauge = "gauge"
Expand All @@ -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"`
}

Expand Down Expand Up @@ -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)
Expand Down
15 changes: 11 additions & 4 deletions pkg/logentry/stages/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pipeline_stages:
loki_count:
type: Counter
description: uhhhhhhh
prefix: my_promtail_custom_
source: app
config:
value: loki
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down