Skip to content

Commit

Permalink
Merge pull request #157 from kaleido-io/metrics-prefix-config
Browse files Browse the repository at this point in the history
[metrics] Configurable Prefix and Avoiding 'ff_' in Tags
  • Loading branch information
EnriqueL8 authored Dec 5, 2024
2 parents 851eeb4 + 9cd0b48 commit 9de497d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
27 changes: 20 additions & 7 deletions pkg/metric/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ import (
var allowedNameStringRegex = `^[a-zA-Z]+[a-zA-Z0-9_]*[a-zA-Z0-9]$`
var ffMetricsPrefix = "ff"

const compulsoryComponentLabel = fireflySystemLabelsPrefix + "component"
const compulsoryComponentLabel = "component"

/** Metrics names should follow the convention documented in https://prometheus.io/docs/practices/naming/. Below is an example breakdown of the term mapping:
* Example metric: ff _ api_server_rest _ requests _ total {ff_component="tm" , method = "Get" ...}
ff _ token _ mint_duration _ seconds {ff_component="" , status = "Success" ...}
* Mapping : <firefly prefix> _ <subsystem> _ <metric target> _ <unit> {ff_component=<component name> , <label name> = <label value> ...}
* Example metric: ff _ api_server_rest _ requests _ total {component="tm" , method = "Get" ...}
ff _ token _ mint_duration _ seconds {component="" , status = "Success" ...}
* Mapping : <firefly prefix> _ <subsystem> _ <metric target> _ <unit> {component=<component name> , <label name> = <label value> ...}
*/

// MetricsRegistry contains all metrics defined in a micro-service.
Expand Down Expand Up @@ -90,16 +90,29 @@ type MetricsManager interface {
ObserveSummaryMetricWithLabels(ctx context.Context, metricName string, number float64, labels map[string]string, defaultLabels *FireflyDefaultLabels)
}

func NewPrometheusMetricsRegistry(fireflyComponentName string /*component name will be added to all metrics as a label*/) MetricsRegistry {
type Options struct {
MetricsPrefix string
}

func NewPrometheusMetricsRegistry(componentName string /*component name will be added to all metrics as a label*/) MetricsRegistry {
return NewPrometheusMetricsRegistryWithOptions(componentName, Options{})
}

func NewPrometheusMetricsRegistryWithOptions(componentName string /*component name will be added to all metrics as a label*/, opts Options) MetricsRegistry {
registry := prometheus.NewRegistry()
registerer := prometheus.WrapRegistererWith(prometheus.Labels{compulsoryComponentLabel: fireflyComponentName}, registry)
registerer := prometheus.WrapRegistererWith(prometheus.Labels{compulsoryComponentLabel: componentName}, registry)

// register default cpu & go metrics by default
registerer.MustRegister(collectors.NewGoCollector())
registerer.MustRegister(collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}))

metricsPrefix := ffMetricsPrefix
if opts.MetricsPrefix != "" {
metricsPrefix = opts.MetricsPrefix
}

return &prometheusMetricsRegistry{
namespace: ffMetricsPrefix,
namespace: metricsPrefix,
registry: registry,
registerer: registerer,
managerMap: make(map[string]MetricsManager),
Expand Down
10 changes: 10 additions & 0 deletions pkg/metric/metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ func TestMetricsRegistry(t *testing.T) {
assert.NotNil(t, httpHandler)
}

func TestMetricsRegistryWithOptions(t *testing.T) {
mr := NewPrometheusMetricsRegistryWithOptions("test", Options{
MetricsPrefix: "foobar",
})

reg := mr.(*prometheusMetricsRegistry)

assert.Equal(t, "foobar", reg.namespace)
}

func TestMetricsManager(t *testing.T) {
mr := NewPrometheusMetricsRegistry("test")
ctx, cancel := context.WithCancel(context.Background())
Expand Down

0 comments on commit 9de497d

Please sign in to comment.