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

Commit

Permalink
metric type is not set for gauges. (#1082)
Browse files Browse the repository at this point in the history
  • Loading branch information
rghetia authored Mar 27, 2019
1 parent ec71c97 commit 41e54b8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
21 changes: 21 additions & 0 deletions metric/gauge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func TestGauge(t *testing.T) {
Descriptor: metricdata.Descriptor{
Name: "TestGauge",
LabelKeys: []string{"k1", "k2"},
Type: metricdata.TypeGaugeFloat64,
},
TimeSeries: []*metricdata.TimeSeries{
{
Expand Down Expand Up @@ -79,6 +80,26 @@ func TestGauge(t *testing.T) {
}
}

func TestGaugeMetricDescriptor(t *testing.T) {
unit := metricdata.UnitDimensionless
r := NewRegistry()

gf, _ := r.AddFloat64Gauge("float64_gauge", "", unit)
compareType(gf.g.desc.Type, metricdata.TypeGaugeFloat64, t)
gi, _ := r.AddInt64Gauge("int64_gauge", "", unit)
compareType(gi.g.desc.Type, metricdata.TypeGaugeInt64, t)
dgf, _ := r.AddFloat64DerivedGauge("derived_float64_gauge", "", unit)
compareType(dgf.g.desc.Type, metricdata.TypeGaugeFloat64, t)
dgi, _ := r.AddInt64DerivedGauge("derived_int64_gauge", "", unit)
compareType(dgi.g.desc.Type, metricdata.TypeGaugeInt64, t)
}

func compareType(got, want metricdata.Type, t *testing.T) {
if got != want {
t.Errorf("metricdata type: got %v, want %v\n", got, want)
}
}

func TestFloat64Entry_Add(t *testing.T) {
r := NewRegistry()
g, _ := r.AddFloat64Gauge("g", "", metricdata.UnitDimensionless)
Expand Down
16 changes: 16 additions & 0 deletions metric/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,21 @@ func (r *Registry) AddFloat64DerivedGauge(name, description string, unit metricd
return f, nil
}

func gTypeToMetricType(g *gauge) metricdata.Type {
switch g.gType {
case derivedGaugeFloat64:
return metricdata.TypeGaugeFloat64
case derivedGaugeInt64:
return metricdata.TypeGaugeInt64
case gaugeFloat64:
return metricdata.TypeGaugeFloat64
case gaugeInt64:
return metricdata.TypeGaugeInt64
default:
panic("unsupported gauge type")
}
}

func (r *Registry) initGauge(g *gauge, labelKeys []string, name string, description string, unit metricdata.Unit) (*gauge, error) {
val, ok := r.gauges.Load(name)
if ok {
Expand All @@ -117,6 +132,7 @@ func (r *Registry) initGauge(g *gauge, labelKeys []string, name string, descript
Description: description,
Unit: unit,
LabelKeys: labelKeys,
Type: gTypeToMetricType(g),
}
r.gauges.Store(name, g)
return g, nil
Expand Down

0 comments on commit 41e54b8

Please sign in to comment.