Skip to content
This repository has been archived by the owner on Jul 31, 2023. It is now read-only.

Commit

Permalink
Add description field to LabelKey
Browse files Browse the repository at this point in the history
  • Loading branch information
rghetia committed Apr 18, 2019
1 parent 6a5e7ee commit e2363a3
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 27 deletions.
4 changes: 2 additions & 2 deletions exporter/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,9 @@ func (me *descExporter) ExportMetrics(ctx context.Context, metrics []*metricdata
return nil
}

func toPromLabels(mls []string) (labels []string) {
func toPromLabels(mls []metricdata.LabelKey) (labels []string) {
for _, ml := range mls {
labels = append(labels, internal.Sanitize(ml))
labels = append(labels, internal.Sanitize(ml.Key))
}
return labels
}
Expand Down
2 changes: 1 addition & 1 deletion metric/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type baseMetric struct {
vals sync.Map
desc metricdata.Descriptor
start time.Time
keys []string
keys []metricdata.LabelKey
bmType baseMetricType
}

Expand Down
9 changes: 6 additions & 3 deletions metric/cumulative_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,12 @@ func TestCumulative(t *testing.T) {
want := []*metricdata.Metric{
{
Descriptor: metricdata.Descriptor{
Name: "TestCumulative",
LabelKeys: []string{"k1", "k2"},
Type: metricdata.TypeCumulativeFloat64,
Name: "TestCumulative",
LabelKeys: []metricdata.LabelKey{
{Key: "k1", Description: "k1"},
{Key: "k2", Description: "k2"},
},
Type: metricdata.TypeCumulativeFloat64,
},
TimeSeries: []*metricdata.TimeSeries{
{
Expand Down
41 changes: 34 additions & 7 deletions metric/gauge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,12 @@ func TestGauge(t *testing.T) {
want := []*metricdata.Metric{
{
Descriptor: metricdata.Descriptor{
Name: "TestGauge",
LabelKeys: []string{"k1", "k2"},
Type: metricdata.TypeGaugeFloat64,
Name: "TestGauge",
LabelKeys: []metricdata.LabelKey{
{Key: "k1", Description: "k1"},
{Key: "k2", Description: "k2"},
},
Type: metricdata.TypeGaugeFloat64,
},
TimeSeries: []*metricdata.TimeSeries{
{
Expand Down Expand Up @@ -136,9 +139,33 @@ func TestGaugeMetricOptionLabelKeys(t *testing.T) {
name := "testOptUnit"
gf, _ := r.AddFloat64Gauge(name, WithLabelKeys("k1", "k3"))
want := metricdata.Descriptor{
Name: name,
LabelKeys: []string{"k1", "k3"},
Type: metricdata.TypeGaugeFloat64,
Name: name,
LabelKeys: []metricdata.LabelKey{
{Key: "k1", Description: "k1"},
{Key: "k3", Description: "k3"},
},
Type: metricdata.TypeGaugeFloat64,
}
got := gf.bm.desc
if !cmp.Equal(got, want) {
t.Errorf("metric descriptor: got %v, want %v\n", got, want)
}
}

func TestGaugeMetricOptionLabelKeysAndDesc(t *testing.T) {
r := NewRegistry()
name := "testOptUnit"
lks := []metricdata.LabelKey{}
lks = append(lks, metricdata.LabelKey{Key: "k1", Description: "desc k1"},
metricdata.LabelKey{Key: "k3", Description: "desc k3"})
gf, _ := r.AddFloat64Gauge(name, WithLabelKeysAndDescription(lks...))
want := metricdata.Descriptor{
Name: name,
LabelKeys: []metricdata.LabelKey{
{Key: "k1", Description: "desc k1"},
{Key: "k3", Description: "desc k3"},
},
Type: metricdata.TypeGaugeFloat64,
}
got := gf.bm.desc
if !cmp.Equal(got, want) {
Expand Down Expand Up @@ -263,7 +290,7 @@ func TestMapKey(t *testing.T) {
for i, tc := range cases {
t.Run(fmt.Sprintf("case %d", i), func(t *testing.T) {
g := &baseMetric{
keys: make([]string, len(tc)),
keys: make([]metricdata.LabelKey, len(tc)),
}
mk := g.encodeLabelVals(tc)
vals := g.decodeLabelVals(mk)
Expand Down
7 changes: 7 additions & 0 deletions metric/metricdata/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@

package metricdata

// LabelKey represents key of a label. It has optional
// description attribute.
type LabelKey struct {
Key string
Description string
}

// LabelValue represents the value of a label.
// The zero value represents a missing label value, which may be treated
// differently to an empty string value by some back ends.
Expand Down
10 changes: 5 additions & 5 deletions metric/metricdata/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ import (

// Descriptor holds metadata about a metric.
type Descriptor struct {
Name string // full name of the metric
Description string // human-readable description
Unit Unit // units for the measure
Type Type // type of measure
LabelKeys []string // label keys
Name string // full name of the metric
Description string // human-readable description
Unit Unit // units for the measure
Type Type // type of measure
LabelKeys []LabelKey // label keys
}

// Metric represents a quantity measured against a resource with different
Expand Down
15 changes: 13 additions & 2 deletions metric/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type Registry struct {
//TODO: [rghetia] add constant labels.
type metricOptions struct {
unit metricdata.Unit
labelkeys []string
labelkeys []metricdata.LabelKey
desc string
}

Expand All @@ -53,7 +53,18 @@ func WithUnit(unit metricdata.Unit) Options {
}

// WithLabelKeys applies provided label.
func WithLabelKeys(labelKeys ...string) Options {
func WithLabelKeys(keys ...string) Options {
return func(mo *metricOptions) {
labelKeys := make([]metricdata.LabelKey, 0)
for _, key := range keys {
labelKeys = append(labelKeys, metricdata.LabelKey{Key: key, Description: key})
}
mo.labelkeys = labelKeys
}
}

// WithLabelKeysAndDescription applies provided label.
func WithLabelKeysAndDescription(labelKeys ...metricdata.LabelKey) Options {
return func(mo *metricOptions) {
mo.labelkeys = labelKeys
}
Expand Down
10 changes: 5 additions & 5 deletions stats/view/view_to_metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ func getType(v *View) metricdata.Type {
}
}

func getLableKeys(v *View) []string {
labelKeys := []string{}
func getLableKeys(v *View) []metricdata.LabelKey {
labelKeys := []metricdata.LabelKey{}
for _, k := range v.TagKeys {
labelKeys = append(labelKeys, k.Name())
labelKeys = append(labelKeys, metricdata.LabelKey{Key: k.Name(), Description: k.Name()})
}
return labelKeys
}
Expand All @@ -91,15 +91,15 @@ func viewToMetricDescriptor(v *View) *metricdata.Descriptor {
}
}

func toLabelValues(row *Row, expectedKeys []string) []metricdata.LabelValue {
func toLabelValues(row *Row, expectedKeys []metricdata.LabelKey) []metricdata.LabelValue {
labelValues := []metricdata.LabelValue{}
tagMap := make(map[string]string)
for _, tag := range row.Tags {
tagMap[tag.Key.Name()] = tag.Value
}

for _, key := range expectedKeys {
if val, ok := tagMap[key]; ok {
if val, ok := tagMap[key.Key]; ok {
labelValues = append(labelValues, metricdata.NewLabelValue(val))
} else {
labelValues = append(labelValues, metricdata.LabelValue{})
Expand Down
7 changes: 5 additions & 2 deletions stats/view/view_to_metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ var (
labelValues []metricdata.LabelValue
emptyLabelValues []metricdata.LabelValue

labelKeys []string
labelKeys []metricdata.LabelKey

recordsInt64 []recordValWithTag
recordsFloat64 []recordValWithTag
Expand Down Expand Up @@ -125,7 +125,10 @@ func initTags() {
{Value: "", Present: false},
{Value: "", Present: false},
}
labelKeys = []string{tk1.Name(), tk2.Name()}
labelKeys = []metricdata.LabelKey{
{Key: tk1.Name(), Description: tk1.Name()},
{Key: tk2.Name(), Description: tk2.Name()},
}

recordsInt64 = []recordValWithTag{
{tags: tags, value: int64(2)},
Expand Down

0 comments on commit e2363a3

Please sign in to comment.