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

Commit f67cd4b

Browse files
authored
Add description field to LabelKey (#1114)
* Add description field to LabelKey * keep description as "" when not specified.
1 parent f216f87 commit f67cd4b

File tree

9 files changed

+78
-27
lines changed

9 files changed

+78
-27
lines changed

exporter/prometheus/prometheus.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,9 @@ func (me *descExporter) ExportMetrics(ctx context.Context, metrics []*metricdata
192192
return nil
193193
}
194194

195-
func toPromLabels(mls []string) (labels []string) {
195+
func toPromLabels(mls []metricdata.LabelKey) (labels []string) {
196196
for _, ml := range mls {
197-
labels = append(labels, internal.Sanitize(ml))
197+
labels = append(labels, internal.Sanitize(ml.Key))
198198
}
199199
return labels
200200
}

metric/common.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type baseMetric struct {
3333
vals sync.Map
3434
desc metricdata.Descriptor
3535
start time.Time
36-
keys []string
36+
keys []metricdata.LabelKey
3737
bmType baseMetricType
3838
}
3939

metric/cumulative_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,12 @@ func TestCumulative(t *testing.T) {
3939
want := []*metricdata.Metric{
4040
{
4141
Descriptor: metricdata.Descriptor{
42-
Name: "TestCumulative",
43-
LabelKeys: []string{"k1", "k2"},
44-
Type: metricdata.TypeCumulativeFloat64,
42+
Name: "TestCumulative",
43+
LabelKeys: []metricdata.LabelKey{
44+
{Key: "k1"},
45+
{Key: "k2"},
46+
},
47+
Type: metricdata.TypeCumulativeFloat64,
4548
},
4649
TimeSeries: []*metricdata.TimeSeries{
4750
{

metric/gauge_test.go

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,12 @@ func TestGauge(t *testing.T) {
4141
want := []*metricdata.Metric{
4242
{
4343
Descriptor: metricdata.Descriptor{
44-
Name: "TestGauge",
45-
LabelKeys: []string{"k1", "k2"},
46-
Type: metricdata.TypeGaugeFloat64,
44+
Name: "TestGauge",
45+
LabelKeys: []metricdata.LabelKey{
46+
{Key: "k1"},
47+
{Key: "k2"},
48+
},
49+
Type: metricdata.TypeGaugeFloat64,
4750
},
4851
TimeSeries: []*metricdata.TimeSeries{
4952
{
@@ -136,9 +139,33 @@ func TestGaugeMetricOptionLabelKeys(t *testing.T) {
136139
name := "testOptUnit"
137140
gf, _ := r.AddFloat64Gauge(name, WithLabelKeys("k1", "k3"))
138141
want := metricdata.Descriptor{
139-
Name: name,
140-
LabelKeys: []string{"k1", "k3"},
141-
Type: metricdata.TypeGaugeFloat64,
142+
Name: name,
143+
LabelKeys: []metricdata.LabelKey{
144+
{Key: "k1"},
145+
{Key: "k3"},
146+
},
147+
Type: metricdata.TypeGaugeFloat64,
148+
}
149+
got := gf.bm.desc
150+
if !cmp.Equal(got, want) {
151+
t.Errorf("metric descriptor: got %v, want %v\n", got, want)
152+
}
153+
}
154+
155+
func TestGaugeMetricOptionLabelKeysAndDesc(t *testing.T) {
156+
r := NewRegistry()
157+
name := "testOptUnit"
158+
lks := []metricdata.LabelKey{}
159+
lks = append(lks, metricdata.LabelKey{Key: "k1", Description: "desc k1"},
160+
metricdata.LabelKey{Key: "k3", Description: "desc k3"})
161+
gf, _ := r.AddFloat64Gauge(name, WithLabelKeysAndDescription(lks...))
162+
want := metricdata.Descriptor{
163+
Name: name,
164+
LabelKeys: []metricdata.LabelKey{
165+
{Key: "k1", Description: "desc k1"},
166+
{Key: "k3", Description: "desc k3"},
167+
},
168+
Type: metricdata.TypeGaugeFloat64,
142169
}
143170
got := gf.bm.desc
144171
if !cmp.Equal(got, want) {
@@ -263,7 +290,7 @@ func TestMapKey(t *testing.T) {
263290
for i, tc := range cases {
264291
t.Run(fmt.Sprintf("case %d", i), func(t *testing.T) {
265292
g := &baseMetric{
266-
keys: make([]string, len(tc)),
293+
keys: make([]metricdata.LabelKey, len(tc)),
267294
}
268295
mk := g.encodeLabelVals(tc)
269296
vals := g.decodeLabelVals(mk)

metric/metricdata/label.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414

1515
package metricdata
1616

17+
// LabelKey represents key of a label. It has optional
18+
// description attribute.
19+
type LabelKey struct {
20+
Key string
21+
Description string
22+
}
23+
1724
// LabelValue represents the value of a label.
1825
// The zero value represents a missing label value, which may be treated
1926
// differently to an empty string value by some back ends.

metric/metricdata/metric.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ import (
2222

2323
// Descriptor holds metadata about a metric.
2424
type Descriptor struct {
25-
Name string // full name of the metric
26-
Description string // human-readable description
27-
Unit Unit // units for the measure
28-
Type Type // type of measure
29-
LabelKeys []string // label keys
25+
Name string // full name of the metric
26+
Description string // human-readable description
27+
Unit Unit // units for the measure
28+
Type Type // type of measure
29+
LabelKeys []LabelKey // label keys
3030
}
3131

3232
// Metric represents a quantity measured against a resource with different

metric/registry.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type Registry struct {
3131
//TODO: [rghetia] add constant labels.
3232
type metricOptions struct {
3333
unit metricdata.Unit
34-
labelkeys []string
34+
labelkeys []metricdata.LabelKey
3535
desc string
3636
}
3737

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

5555
// WithLabelKeys applies provided label.
56-
func WithLabelKeys(labelKeys ...string) Options {
56+
func WithLabelKeys(keys ...string) Options {
57+
return func(mo *metricOptions) {
58+
labelKeys := make([]metricdata.LabelKey, 0)
59+
for _, key := range keys {
60+
labelKeys = append(labelKeys, metricdata.LabelKey{Key: key})
61+
}
62+
mo.labelkeys = labelKeys
63+
}
64+
}
65+
66+
// WithLabelKeysAndDescription applies provided label.
67+
func WithLabelKeysAndDescription(labelKeys ...metricdata.LabelKey) Options {
5768
return func(mo *metricOptions) {
5869
mo.labelkeys = labelKeys
5970
}

stats/view/view_to_metric.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ func getType(v *View) metricdata.Type {
7373
}
7474
}
7575

76-
func getLableKeys(v *View) []string {
77-
labelKeys := []string{}
76+
func getLableKeys(v *View) []metricdata.LabelKey {
77+
labelKeys := []metricdata.LabelKey{}
7878
for _, k := range v.TagKeys {
79-
labelKeys = append(labelKeys, k.Name())
79+
labelKeys = append(labelKeys, metricdata.LabelKey{Key: k.Name()})
8080
}
8181
return labelKeys
8282
}
@@ -91,15 +91,15 @@ func viewToMetricDescriptor(v *View) *metricdata.Descriptor {
9191
}
9292
}
9393

94-
func toLabelValues(row *Row, expectedKeys []string) []metricdata.LabelValue {
94+
func toLabelValues(row *Row, expectedKeys []metricdata.LabelKey) []metricdata.LabelValue {
9595
labelValues := []metricdata.LabelValue{}
9696
tagMap := make(map[string]string)
9797
for _, tag := range row.Tags {
9898
tagMap[tag.Key.Name()] = tag.Value
9999
}
100100

101101
for _, key := range expectedKeys {
102-
if val, ok := tagMap[key]; ok {
102+
if val, ok := tagMap[key.Key]; ok {
103103
labelValues = append(labelValues, metricdata.NewLabelValue(val))
104104
} else {
105105
labelValues = append(labelValues, metricdata.LabelValue{})

stats/view/view_to_metric_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ var (
5050
labelValues []metricdata.LabelValue
5151
emptyLabelValues []metricdata.LabelValue
5252

53-
labelKeys []string
53+
labelKeys []metricdata.LabelKey
5454

5555
recordsInt64 []recordValWithTag
5656
recordsFloat64 []recordValWithTag
@@ -125,7 +125,10 @@ func initTags() {
125125
{Value: "", Present: false},
126126
{Value: "", Present: false},
127127
}
128-
labelKeys = []string{tk1.Name(), tk2.Name()}
128+
labelKeys = []metricdata.LabelKey{
129+
{Key: tk1.Name()},
130+
{Key: tk2.Name()},
131+
}
129132

130133
recordsInt64 = []recordValWithTag{
131134
{tags: tags, value: int64(2)},

0 commit comments

Comments
 (0)