Skip to content

Commit

Permalink
metrics: add an interface to get the metrics definitions (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gandem committed Sep 13, 2024
1 parent 6270dc8 commit ce5afa2
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
20 changes: 20 additions & 0 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
package metrics

import (
"bytes"
_ "embed"
"encoding/json"
"sync"

log "github.com/sirupsen/logrus"
Expand All @@ -31,6 +34,9 @@ var (

// mutex serializes the concurrent calls to AddSlice()
mutex sync.RWMutex

//go:embed metrics.json
metricsJSON []byte
)

// reporterImpl allows swapping out the global metrics reporter.
Expand Down Expand Up @@ -151,3 +157,17 @@ func Add(id MetricID, value MetricValue) {
// If these assumptions change, we can address that by regularly calling AddSlice() with
// an empty slice. Code can be found in commit 1d01d1ff841891010afaf8d64d4c21a05f19d168
// and earlier.

// GetDefinitions returns the metric definitions from the embedded metrics.json file.
func GetDefinitions() ([]MetricDefinition, error) {
var definitions []MetricDefinition

dec := json.NewDecoder(bytes.NewReader(metricsJSON))
dec.DisallowUnknownFields()

err := dec.Decode(&definitions)
if err != nil {
return nil, err
}
return definitions, nil
}
7 changes: 7 additions & 0 deletions metrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type fakeReporter struct {
Expand Down Expand Up @@ -65,3 +66,9 @@ func TestMetrics(t *testing.T) {
assert.Fail(t, "timeout - no metrics received in time")
}
}

func TestGetDefinitions(t *testing.T) {
defs, err := GetDefinitions()
require.NoError(t, err)
assert.Greater(t, len(defs), 1)
}
17 changes: 17 additions & 0 deletions metrics/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,20 @@ type Metric struct {
// Summary helps summarizing metrics of the same ID from different sources before
// processing it further.
type Summary map[MetricID]MetricValue

type MetricDefinition struct {
ID MetricID `json:"id"`
Type MetricType `json:"type"`
Description string `json:"description"`
Name string `json:"name"`
Field string `json:"field"`
Unit string `json:"unit"`
Obsolete bool `json:"obsolete"`
}

type MetricType string

const (
MetricTypeGauge MetricType = "gauge"
MetricTypeCounter MetricType = "counter"
)

0 comments on commit ce5afa2

Please sign in to comment.