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

Commit 41e54b8

Browse files
authored
metric type is not set for gauges. (#1082)
1 parent ec71c97 commit 41e54b8

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

metric/gauge_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ func TestGauge(t *testing.T) {
4141
Descriptor: metricdata.Descriptor{
4242
Name: "TestGauge",
4343
LabelKeys: []string{"k1", "k2"},
44+
Type: metricdata.TypeGaugeFloat64,
4445
},
4546
TimeSeries: []*metricdata.TimeSeries{
4647
{
@@ -79,6 +80,26 @@ func TestGauge(t *testing.T) {
7980
}
8081
}
8182

83+
func TestGaugeMetricDescriptor(t *testing.T) {
84+
unit := metricdata.UnitDimensionless
85+
r := NewRegistry()
86+
87+
gf, _ := r.AddFloat64Gauge("float64_gauge", "", unit)
88+
compareType(gf.g.desc.Type, metricdata.TypeGaugeFloat64, t)
89+
gi, _ := r.AddInt64Gauge("int64_gauge", "", unit)
90+
compareType(gi.g.desc.Type, metricdata.TypeGaugeInt64, t)
91+
dgf, _ := r.AddFloat64DerivedGauge("derived_float64_gauge", "", unit)
92+
compareType(dgf.g.desc.Type, metricdata.TypeGaugeFloat64, t)
93+
dgi, _ := r.AddInt64DerivedGauge("derived_int64_gauge", "", unit)
94+
compareType(dgi.g.desc.Type, metricdata.TypeGaugeInt64, t)
95+
}
96+
97+
func compareType(got, want metricdata.Type, t *testing.T) {
98+
if got != want {
99+
t.Errorf("metricdata type: got %v, want %v\n", got, want)
100+
}
101+
}
102+
82103
func TestFloat64Entry_Add(t *testing.T) {
83104
r := NewRegistry()
84105
g, _ := r.AddFloat64Gauge("g", "", metricdata.UnitDimensionless)

metric/registry.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,21 @@ func (r *Registry) AddFloat64DerivedGauge(name, description string, unit metricd
102102
return f, nil
103103
}
104104

105+
func gTypeToMetricType(g *gauge) metricdata.Type {
106+
switch g.gType {
107+
case derivedGaugeFloat64:
108+
return metricdata.TypeGaugeFloat64
109+
case derivedGaugeInt64:
110+
return metricdata.TypeGaugeInt64
111+
case gaugeFloat64:
112+
return metricdata.TypeGaugeFloat64
113+
case gaugeInt64:
114+
return metricdata.TypeGaugeInt64
115+
default:
116+
panic("unsupported gauge type")
117+
}
118+
}
119+
105120
func (r *Registry) initGauge(g *gauge, labelKeys []string, name string, description string, unit metricdata.Unit) (*gauge, error) {
106121
val, ok := r.gauges.Load(name)
107122
if ok {
@@ -117,6 +132,7 @@ func (r *Registry) initGauge(g *gauge, labelKeys []string, name string, descript
117132
Description: description,
118133
Unit: unit,
119134
LabelKeys: labelKeys,
135+
Type: gTypeToMetricType(g),
120136
}
121137
r.gauges.Store(name, g)
122138
return g, nil

0 commit comments

Comments
 (0)