Skip to content

Commit

Permalink
JSC-62132: Add support to configure extra labels
Browse files Browse the repository at this point in the history
  • Loading branch information
kasparjarek authored Nov 6, 2024
1 parent c736e53 commit d6f3fb3
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,17 @@ saramaprom.ExportMetrics(metricRegistry, saramaprom.Options{})
```

`saramaprom.Options` enables you to for example configure how often the metrics are refreshed, namespace of the metrics, etc.

Multiple metric registries can be defined to not mix values of several Kafka cluster.
But in that case the prometheus metrics have to be distinguished by labels. Use `ExtraLabels` option for that:
```
// create new registry and export metrics for cluster Foo
configFoo := sarama.NewConfig()
configFoo.MetricRegistry = metrics.NewRegistry()
saramaprom.ExportMetrics(configFoo.MetricRegistry, saramaprom.Options{ExtraLabels: map[string]string{"cluster": "foo"}})
// create new registry and export metrics for cluster Bar
configBar := sarama.NewConfig()
configBar.MetricRegistry = metrics.NewRegistry()
saramaprom.ExportMetrics(configBar.MetricRegistry, saramaprom.Options{ExtraLabels: map[string]string{"cluster": "bar"}})
```
6 changes: 6 additions & 0 deletions exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ func (c *exporter) gaugeFromNameAndValue(name string, val float64) error {
if skip {
return nil
}
for n, v := range c.opt.ExtraLabels {
labels[n] = v
}

if _, exists := c.gauges[name]; !exists {
labelNames := make([]string, 0, len(labels))
Expand Down Expand Up @@ -151,6 +154,9 @@ func (c *exporter) summaryFromNameAndMetric(name string, goMetric interface{}, q
if skip {
return nil
}
for n, v := range c.opt.ExtraLabels {
labels[n] = v
}

desc := prometheus.NewDesc(
prometheus.BuildFQName(
Expand Down
3 changes: 3 additions & 0 deletions saramaprom.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ type Options struct {
Namespace string
Subsystem string

// Extra labels to be added to all metrics. Beware, this will override the default label value if configured with same name.
ExtraLabels prometheus.Labels

// RefreshInterval specifies interval between updating metrics. Default 1s.
RefreshInterval time.Duration
}
Expand Down
5 changes: 3 additions & 2 deletions saramaprom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,13 @@ func TestLabels(t *testing.T) {
Namespace: "test",
Subsystem: "subsys",
PrometheusRegistry: promRegistry,
ExtraLabels: map[string]string{"extra": "foo"},
})

t.Run("counter1-for-broker-123", func(t *testing.T) {
want := []gaugeDetails{{
name: "test_subsys_counter1",
labels: map[string]string{"broker": "123", "topic": ""},
labels: map[string]string{"broker": "123", "topic": "", "extra": "foo"},
gaugeValues: []float64{0},
}}
got := getMetricDetails(promRegistry, "test_subsys_counter1")
Expand All @@ -68,7 +69,7 @@ func TestLabels(t *testing.T) {
t.Run("counter2-for-topic-abc", func(t *testing.T) {
want := []gaugeDetails{{
name: "test_subsys_counter2",
labels: map[string]string{"broker": "", "topic": "abc"},
labels: map[string]string{"broker": "", "topic": "abc", "extra": "foo"},
gaugeValues: []float64{0},
}}
got := getMetricDetails(promRegistry, "test_subsys_counter2")
Expand Down

0 comments on commit d6f3fb3

Please sign in to comment.