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

Commit

Permalink
Refactor gauge api with options. (#1086)
Browse files Browse the repository at this point in the history
* Refactor gauge api with options.

* fixed review comments.
  • Loading branch information
rghetia authored Apr 1, 2019
1 parent 41e54b8 commit 948b0cb
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 39 deletions.
5 changes: 4 additions & 1 deletion metric/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ func ExampleRegistry_AddInt64Gauge() {
r := metric.NewRegistry()
// TODO: allow exporting from a registry

g, _ := r.AddInt64Gauge("active_request", "Number of active requests, per method.", metricdata.UnitDimensionless, "method")
g, _ := r.AddInt64Gauge("active_request",
metric.WithDescription("Number of active requests, per method."),
metric.WithUnit(metricdata.UnitDimensionless),
metric.WithLabelKeys("method"))

http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
e, _ := g.GetEntry(metricdata.NewLabelValue(request.Method))
Expand Down
108 changes: 84 additions & 24 deletions metric/gauge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ import (

func TestGauge(t *testing.T) {
r := NewRegistry()
f, _ := r.AddFloat64Gauge("TestGauge", "", "", "k1", "k2")

f, _ := r.AddFloat64Gauge("TestGauge",
WithLabelKeys("k1", "k2"))
e, _ := f.GetEntry(metricdata.LabelValue{}, metricdata.LabelValue{})
e.Set(5)
e, _ = f.GetEntry(metricdata.NewLabelValue("k1v1"), metricdata.LabelValue{})
Expand Down Expand Up @@ -81,16 +83,15 @@ func TestGauge(t *testing.T) {
}

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

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

Expand All @@ -100,9 +101,68 @@ func compareType(got, want metricdata.Type, t *testing.T) {
}
}

func TestGaugeMetricOptionDesc(t *testing.T) {
r := NewRegistry()
name := "testOptDesc"
gf, _ := r.AddFloat64Gauge(name, WithDescription("test"))
want := metricdata.Descriptor{
Name: name,
Description: "test",
Type: metricdata.TypeGaugeFloat64,
}
got := gf.g.desc
if !cmp.Equal(got, want) {
t.Errorf("metric option description: got %v, want %v\n", got, want)
}
}

func TestGaugeMetricOptionUnit(t *testing.T) {
r := NewRegistry()
name := "testOptUnit"
gf, _ := r.AddFloat64Gauge(name, WithUnit(metricdata.UnitMilliseconds))
want := metricdata.Descriptor{
Name: name,
Unit: metricdata.UnitMilliseconds,
Type: metricdata.TypeGaugeFloat64,
}
got := gf.g.desc
if !cmp.Equal(got, want) {
t.Errorf("metric descriptor: got %v, want %v\n", got, want)
}
}

func TestGaugeMetricOptionLabelKeys(t *testing.T) {
r := NewRegistry()
name := "testOptUnit"
gf, _ := r.AddFloat64Gauge(name, WithLabelKeys("k1", "k3"))
want := metricdata.Descriptor{
Name: name,
LabelKeys: []string{"k1", "k3"},
Type: metricdata.TypeGaugeFloat64,
}
got := gf.g.desc
if !cmp.Equal(got, want) {
t.Errorf("metric descriptor: got %v, want %v\n", got, want)
}
}

func TestGaugeMetricOptionDefault(t *testing.T) {
r := NewRegistry()
name := "testOptUnit"
gf, _ := r.AddFloat64Gauge(name)
want := metricdata.Descriptor{
Name: name,
Type: metricdata.TypeGaugeFloat64,
}
got := gf.g.desc
if !cmp.Equal(got, want) {
t.Errorf("metric descriptor: got %v, want %v\n", got, want)
}
}

func TestFloat64Entry_Add(t *testing.T) {
r := NewRegistry()
g, _ := r.AddFloat64Gauge("g", "", metricdata.UnitDimensionless)
g, _ := r.AddFloat64Gauge("g")
e, _ := g.GetEntry()
e.Add(0)
ms := r.Read()
Expand All @@ -125,7 +185,7 @@ func TestFloat64Entry_Add(t *testing.T) {

func TestFloat64Gauge_Add_NegativeTotals(t *testing.T) {
r := NewRegistry()
g, _ := r.AddFloat64Gauge("g", "", metricdata.UnitDimensionless)
g, _ := r.AddFloat64Gauge("g")
e, _ := g.GetEntry()
e.Add(-1.0)
ms := r.Read()
Expand All @@ -136,7 +196,7 @@ func TestFloat64Gauge_Add_NegativeTotals(t *testing.T) {

func TestInt64GaugeEntry_Add(t *testing.T) {
r := NewRegistry()
g, _ := r.AddInt64Gauge("g", "", metricdata.UnitDimensionless)
g, _ := r.AddInt64Gauge("g")
e, _ := g.GetEntry()
e.Add(0)
ms := r.Read()
Expand All @@ -153,7 +213,7 @@ func TestInt64GaugeEntry_Add(t *testing.T) {

func TestInt64Gauge_Add_NegativeTotals(t *testing.T) {
r := NewRegistry()
g, _ := r.AddInt64Gauge("g", "", metricdata.UnitDimensionless)
g, _ := r.AddInt64Gauge("g")
e, _ := g.GetEntry()
e.Add(-1)
ms := r.Read()
Expand All @@ -164,24 +224,24 @@ func TestInt64Gauge_Add_NegativeTotals(t *testing.T) {

func TestGaugeWithSameNameDiffType(t *testing.T) {
r := NewRegistry()
r.AddInt64Gauge("g", "", metricdata.UnitDimensionless)
_, gotErr := r.AddFloat64Gauge("g", "", metricdata.UnitDimensionless)
r.AddInt64Gauge("g")
_, gotErr := r.AddFloat64Gauge("g")
if gotErr == nil {
t.Errorf("got: nil, want error: %v", errGaugeExistsWithDiffType)
}
_, gotErr = r.AddInt64DerivedGauge("g", "", metricdata.UnitDimensionless)
_, gotErr = r.AddInt64DerivedGauge("g")
if gotErr == nil {
t.Errorf("got: nil, want error: %v", errGaugeExistsWithDiffType)
}
_, gotErr = r.AddFloat64DerivedGauge("g", "", metricdata.UnitDimensionless)
_, gotErr = r.AddFloat64DerivedGauge("g")
if gotErr == nil {
t.Errorf("got: nil, want error: %v", errGaugeExistsWithDiffType)
}
}

func TestGaugeWithLabelMismatch(t *testing.T) {
r := NewRegistry()
g, _ := r.AddInt64Gauge("g", "", metricdata.UnitDimensionless, "k1")
g, _ := r.AddInt64Gauge("g", WithLabelKeys("k1"))
_, gotErr := g.GetEntry(metricdata.NewLabelValue("k1v2"), metricdata.NewLabelValue("k2v2"))
if gotErr == nil {
t.Errorf("got: nil, want error: %v", errKeyValueMismatch)
Expand Down Expand Up @@ -222,7 +282,7 @@ func TestRaceCondition(t *testing.T) {
for i := 0; i < 5; i++ {
go func(k int) {
for j := 0; j < 5; j++ {
g, _ := r.AddInt64Gauge(fmt.Sprintf("g%d%d", k, j), "", metricdata.UnitDimensionless)
g, _ := r.AddInt64Gauge(fmt.Sprintf("g%d%d", k, j))
e, _ := g.GetEntry()
e.Add(1)
}
Expand Down Expand Up @@ -272,7 +332,7 @@ func (q *queueInt64) ToInt64() int64 {
func TestInt64DerivedGaugeEntry_Add(t *testing.T) {
r := NewRegistry()
q := &queueInt64{3}
g, _ := r.AddInt64DerivedGauge("g", "", metricdata.UnitDimensionless, "k1", "k2")
g, _ := r.AddInt64DerivedGauge("g", WithLabelKeys("k1", "k2"))
err := g.UpsertEntry(q.ToInt64, metricdata.NewLabelValue("k1v1"), metricdata.LabelValue{})
if err != nil {
t.Errorf("want: nil, got: %v", err)
Expand All @@ -290,7 +350,7 @@ func TestInt64DerivedGaugeEntry_Add(t *testing.T) {

func TestInt64DerivedGaugeEntry_AddWithNilObj(t *testing.T) {
r := NewRegistry()
g, _ := r.AddInt64DerivedGauge("g", "", metricdata.UnitDimensionless, "k1", "k2")
g, _ := r.AddInt64DerivedGauge("g", WithLabelKeys("k1", "k2"))
gotErr := g.UpsertEntry(nil, metricdata.NewLabelValue("k1v1"), metricdata.LabelValue{})
if gotErr == nil {
t.Errorf("expected error but got nil")
Expand All @@ -300,7 +360,7 @@ func TestInt64DerivedGaugeEntry_AddWithNilObj(t *testing.T) {
func TestInt64DerivedGaugeEntry_AddWithInvalidLabels(t *testing.T) {
r := NewRegistry()
q := &queueInt64{3}
g, _ := r.AddInt64DerivedGauge("g", "", metricdata.UnitDimensionless, "k1", "k2")
g, _ := r.AddInt64DerivedGauge("g", WithLabelKeys("k1", "k2"))
gotErr := g.UpsertEntry(q.ToInt64, metricdata.NewLabelValue("k1v1"))
if gotErr == nil {
t.Errorf("expected error but got nil")
Expand All @@ -311,7 +371,7 @@ func TestInt64DerivedGaugeEntry_Update(t *testing.T) {
r := NewRegistry()
q := &queueInt64{3}
q2 := &queueInt64{5}
g, _ := r.AddInt64DerivedGauge("g", "", metricdata.UnitDimensionless, "k1", "k2")
g, _ := r.AddInt64DerivedGauge("g", WithLabelKeys("k1", "k2"))
g.UpsertEntry(q.ToInt64, metricdata.NewLabelValue("k1v1"), metricdata.LabelValue{})
gotErr := g.UpsertEntry(q2.ToInt64, metricdata.NewLabelValue("k1v1"), metricdata.LabelValue{})
if gotErr != nil {
Expand All @@ -334,7 +394,7 @@ func (q *queueFloat64) ToFloat64() float64 {
func TestFloat64DerivedGaugeEntry_Add(t *testing.T) {
r := NewRegistry()
q := &queueFloat64{5.0}
g, _ := r.AddFloat64DerivedGauge("g", "", metricdata.UnitDimensionless, "k1", "k2")
g, _ := r.AddFloat64DerivedGauge("g", WithLabelKeys("k1", "k2"))
err := g.UpsertEntry(q.ToFloat64, metricdata.NewLabelValue("k1v1"), metricdata.LabelValue{})
if err != nil {
t.Errorf("want: nil, got: %v", err)
Expand All @@ -352,7 +412,7 @@ func TestFloat64DerivedGaugeEntry_Add(t *testing.T) {

func TestFloat64DerivedGaugeEntry_AddWithNilObj(t *testing.T) {
r := NewRegistry()
g, _ := r.AddFloat64DerivedGauge("g", "", metricdata.UnitDimensionless, "k1", "k2")
g, _ := r.AddFloat64DerivedGauge("g", WithLabelKeys("k1", "k2"))
gotErr := g.UpsertEntry(nil, metricdata.NewLabelValue("k1v1"), metricdata.LabelValue{})
if gotErr == nil {
t.Errorf("expected error but got nil")
Expand All @@ -362,7 +422,7 @@ func TestFloat64DerivedGaugeEntry_AddWithNilObj(t *testing.T) {
func TestFloat64DerivedGaugeEntry_AddWithInvalidLabels(t *testing.T) {
r := NewRegistry()
q := &queueFloat64{3}
g, _ := r.AddFloat64DerivedGauge("g", "", metricdata.UnitDimensionless, "k1", "k2")
g, _ := r.AddFloat64DerivedGauge("g", WithLabelKeys("k1", "k2"))
gotErr := g.UpsertEntry(q.ToFloat64, metricdata.NewLabelValue("k1v1"))
if gotErr == nil {
t.Errorf("expected error but got nil")
Expand All @@ -373,7 +433,7 @@ func TestFloat64DerivedGaugeEntry_Update(t *testing.T) {
r := NewRegistry()
q := &queueFloat64{3.0}
q2 := &queueFloat64{5.0}
g, _ := r.AddFloat64DerivedGauge("g", "", metricdata.UnitDimensionless, "k1", "k2")
g, _ := r.AddFloat64DerivedGauge("g", WithLabelKeys("k1", "k2"))
g.UpsertEntry(q.ToFloat64, metricdata.NewLabelValue("k1v1"), metricdata.LabelValue{})
gotErr := g.UpsertEntry(q2.ToFloat64, metricdata.NewLabelValue("k1v1"), metricdata.LabelValue{})
if gotErr != nil {
Expand Down
5 changes: 4 additions & 1 deletion metric/metricexport/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ func (e *metricExporter) ExportMetrics(ctx context.Context, metrics []*metricdat
func init() {
r := metric.NewRegistry()
metricproducer.GlobalManager().AddProducer(r)
g, _ := r.AddInt64Gauge("active_request", "Number of active requests, per method.", metricdata.UnitDimensionless, "method")
g, _ := r.AddInt64Gauge("active_request",
metric.WithDescription("Number of active requests, per method."),
metric.WithUnit(metricdata.UnitDimensionless),
metric.WithLabelKeys("method"))
gaugeEntry, _ = g.GetEntry(metricdata.NewLabelValue("foo"))
}

Expand Down
Loading

0 comments on commit 948b0cb

Please sign in to comment.