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

metrics: add an interface to get the metrics definitions #155

Merged
merged 2 commits into from
Sep 13, 2024
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
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
Comment on lines +168 to +172
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: This could be simplified:

Suggested change
err := dec.Decode(&definitions)
if err != nil {
return nil, err
}
return definitions, nil
return definitions, dec.Decode(&definitions)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC, unfortunately this linter rule disallows this: https://go-critic.com/overview.html#evalorder

}
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"
)