...

Source file src/go.opencensus.io/metric/cumulative_test.go

Documentation: go.opencensus.io/metric

     1  // Copyright 2019, OpenCensus Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package metric
    16  
    17  import (
    18  	"testing"
    19  	"time"
    20  
    21  	"github.com/google/go-cmp/cmp"
    22  
    23  	"go.opencensus.io/metric/metricdata"
    24  )
    25  
    26  func TestCumulative(t *testing.T) {
    27  	r := NewRegistry()
    28  
    29  	f, _ := r.AddFloat64Cumulative("TestCumulative",
    30  		WithLabelKeys("k1", "k2"))
    31  	e, _ := f.GetEntry(metricdata.LabelValue{}, metricdata.LabelValue{})
    32  	e.Inc(5)
    33  	e, _ = f.GetEntry(metricdata.NewLabelValue("k1v1"), metricdata.LabelValue{})
    34  	e.Inc(1)
    35  	e, _ = f.GetEntry(metricdata.NewLabelValue("k1v1"), metricdata.LabelValue{})
    36  	e.Inc(1)
    37  	e, _ = f.GetEntry(metricdata.NewLabelValue("k1v2"), metricdata.NewLabelValue("k2v2"))
    38  	e.Inc(1)
    39  	m := r.Read()
    40  	want := []*metricdata.Metric{
    41  		{
    42  			Descriptor: metricdata.Descriptor{
    43  				Name: "TestCumulative",
    44  				LabelKeys: []metricdata.LabelKey{
    45  					{Key: "k1"},
    46  					{Key: "k2"},
    47  				},
    48  				Type: metricdata.TypeCumulativeFloat64,
    49  			},
    50  			TimeSeries: []*metricdata.TimeSeries{
    51  				{
    52  					LabelValues: []metricdata.LabelValue{
    53  						{}, {},
    54  					},
    55  					Points: []metricdata.Point{
    56  						metricdata.NewFloat64Point(time.Time{}, 5),
    57  					},
    58  				},
    59  				{
    60  					LabelValues: []metricdata.LabelValue{
    61  						metricdata.NewLabelValue("k1v1"),
    62  						{},
    63  					},
    64  					Points: []metricdata.Point{
    65  						metricdata.NewFloat64Point(time.Time{}, 2),
    66  					},
    67  				},
    68  				{
    69  					LabelValues: []metricdata.LabelValue{
    70  						metricdata.NewLabelValue("k1v2"),
    71  						metricdata.NewLabelValue("k2v2"),
    72  					},
    73  					Points: []metricdata.Point{
    74  						metricdata.NewFloat64Point(time.Time{}, 1),
    75  					},
    76  				},
    77  			},
    78  		},
    79  	}
    80  	canonicalize(m)
    81  	canonicalize(want)
    82  	if diff := cmp.Diff(m, want, cmp.Comparer(ignoreTimes)); diff != "" {
    83  		t.Errorf("-got +want: %s", diff)
    84  	}
    85  }
    86  
    87  func TestCumulativeConstLabel(t *testing.T) {
    88  	r := NewRegistry()
    89  
    90  	f, _ := r.AddFloat64Cumulative("TestCumulativeWithConstLabel",
    91  		WithLabelKeys("k1"),
    92  		WithConstLabel(map[metricdata.LabelKey]metricdata.LabelValue{
    93  			{Key: "const"}:  metricdata.NewLabelValue("same"),
    94  			{Key: "const2"}: metricdata.NewLabelValue("same2"),
    95  		}))
    96  
    97  	e, _ := f.GetEntry(metricdata.LabelValue{})
    98  	e.Inc(5)
    99  	e, _ = f.GetEntry(metricdata.NewLabelValue("k1v1"))
   100  	e.Inc(1)
   101  	m := r.Read()
   102  	want := []*metricdata.Metric{
   103  		{
   104  			Descriptor: metricdata.Descriptor{
   105  				Name: "TestCumulativeWithConstLabel",
   106  				LabelKeys: []metricdata.LabelKey{
   107  					{Key: "const"},
   108  					{Key: "const2"},
   109  					{Key: "k1"}},
   110  				Type: metricdata.TypeCumulativeFloat64,
   111  			},
   112  			TimeSeries: []*metricdata.TimeSeries{
   113  				{
   114  					LabelValues: []metricdata.LabelValue{
   115  						metricdata.NewLabelValue("same"),
   116  						metricdata.NewLabelValue("same2"),
   117  						{}},
   118  					Points: []metricdata.Point{
   119  						metricdata.NewFloat64Point(time.Time{}, 5),
   120  					},
   121  				},
   122  				{
   123  					LabelValues: []metricdata.LabelValue{
   124  						metricdata.NewLabelValue("same"),
   125  						metricdata.NewLabelValue("same2"),
   126  						metricdata.NewLabelValue("k1v1"),
   127  					},
   128  					Points: []metricdata.Point{
   129  						metricdata.NewFloat64Point(time.Time{}, 1),
   130  					},
   131  				},
   132  			},
   133  		},
   134  	}
   135  	canonicalize(m)
   136  	canonicalize(want)
   137  	if diff := cmp.Diff(m, want, cmp.Comparer(ignoreTimes)); diff != "" {
   138  		t.Errorf("-got +want: %s", diff)
   139  	}
   140  }
   141  
   142  func TestCumulativeMetricDescriptor(t *testing.T) {
   143  	r := NewRegistry()
   144  
   145  	gf, _ := r.AddFloat64Cumulative("float64_gauge")
   146  	compareType(gf.bm.desc.Type, metricdata.TypeCumulativeFloat64, t)
   147  	gi, _ := r.AddInt64Cumulative("int64_gauge")
   148  	compareType(gi.bm.desc.Type, metricdata.TypeCumulativeInt64, t)
   149  	dgf, _ := r.AddFloat64DerivedCumulative("derived_float64_gauge")
   150  	compareType(dgf.bm.desc.Type, metricdata.TypeCumulativeFloat64, t)
   151  	dgi, _ := r.AddInt64DerivedCumulative("derived_int64_gauge")
   152  	compareType(dgi.bm.desc.Type, metricdata.TypeCumulativeInt64, t)
   153  }
   154  
   155  func readAndCompareInt64Val(testname string, r *Registry, want int64, t *testing.T) {
   156  	ms := r.Read()
   157  	if got := ms[0].TimeSeries[0].Points[0].Value.(int64); got != want {
   158  		t.Errorf("testname: %s, got = %v, want %v\n", testname, got, want)
   159  	}
   160  }
   161  
   162  func TestInt64CumulativeEntry_IncNegative(t *testing.T) {
   163  	r := NewRegistry()
   164  	g, _ := r.AddInt64Cumulative("bm")
   165  	e, _ := g.GetEntry()
   166  	e.Inc(5)
   167  	readAndCompareInt64Val("inc", r, 5, t)
   168  	e.Inc(-2)
   169  	readAndCompareInt64Val("inc negative", r, 5, t)
   170  }
   171  
   172  func readAndCompareFloat64Val(testname string, r *Registry, want float64, t *testing.T) {
   173  	ms := r.Read()
   174  	if got := ms[0].TimeSeries[0].Points[0].Value.(float64); got != want {
   175  		t.Errorf("testname: %s, got = %v, want %v\n", testname, got, want)
   176  	}
   177  }
   178  
   179  func TestFloat64CumulativeEntry_IncNegative(t *testing.T) {
   180  	r := NewRegistry()
   181  	g, _ := r.AddFloat64Cumulative("bm")
   182  	e, _ := g.GetEntry()
   183  	e.Inc(5.0)
   184  	readAndCompareFloat64Val("inc", r, 5.0, t)
   185  	e.Inc(-2.0)
   186  	readAndCompareFloat64Val("inc negative", r, 5.0, t)
   187  }
   188  
   189  func TestCumulativeWithSameNameDiffType(t *testing.T) {
   190  	r := NewRegistry()
   191  	r.AddInt64Cumulative("bm")
   192  	_, gotErr := r.AddFloat64Cumulative("bm")
   193  	if gotErr == nil {
   194  		t.Errorf("got: nil, want error: %v", errMetricExistsWithDiffType)
   195  	}
   196  	_, gotErr = r.AddInt64DerivedCumulative("bm")
   197  	if gotErr == nil {
   198  		t.Errorf("got: nil, want error: %v", errMetricExistsWithDiffType)
   199  	}
   200  	_, gotErr = r.AddFloat64DerivedCumulative("bm")
   201  	if gotErr == nil {
   202  		t.Errorf("got: nil, want error: %v", errMetricExistsWithDiffType)
   203  	}
   204  }
   205  
   206  func TestCumulativeWithLabelMismatch(t *testing.T) {
   207  	r := NewRegistry()
   208  	g, _ := r.AddInt64Cumulative("bm", WithLabelKeys("k1"))
   209  	_, gotErr := g.GetEntry(metricdata.NewLabelValue("k1v2"), metricdata.NewLabelValue("k2v2"))
   210  	if gotErr == nil {
   211  		t.Errorf("got: nil, want error: %v", errKeyValueMismatch)
   212  	}
   213  }
   214  
   215  type sysUpTimeInNanoSecs struct {
   216  	size int64
   217  }
   218  
   219  func (q *sysUpTimeInNanoSecs) ToInt64() int64 {
   220  	return q.size
   221  }
   222  
   223  func TestInt64DerivedCumulativeEntry_Inc(t *testing.T) {
   224  	r := NewRegistry()
   225  	q := &sysUpTimeInNanoSecs{3}
   226  	g, _ := r.AddInt64DerivedCumulative("bm", WithLabelKeys("k1", "k2"))
   227  	err := g.UpsertEntry(q.ToInt64, metricdata.NewLabelValue("k1v1"), metricdata.LabelValue{})
   228  	if err != nil {
   229  		t.Errorf("want: nil, got: %v", err)
   230  	}
   231  	ms := r.Read()
   232  	if got, want := ms[0].TimeSeries[0].Points[0].Value.(int64), int64(3); got != want {
   233  		t.Errorf("value = %v, want %v", got, want)
   234  	}
   235  	q.size = 5
   236  	ms = r.Read()
   237  	if got, want := ms[0].TimeSeries[0].Points[0].Value.(int64), int64(5); got != want {
   238  		t.Errorf("value = %v, want %v", got, want)
   239  	}
   240  }
   241  
   242  func TestInt64DerivedCumulativeEntry_IncWithNilObj(t *testing.T) {
   243  	r := NewRegistry()
   244  	g, _ := r.AddInt64DerivedCumulative("bm", WithLabelKeys("k1", "k2"))
   245  	gotErr := g.UpsertEntry(nil, metricdata.NewLabelValue("k1v1"), metricdata.LabelValue{})
   246  	if gotErr == nil {
   247  		t.Errorf("expected error but got nil")
   248  	}
   249  }
   250  
   251  func TestInt64DerivedCumulativeEntry_IncWithInvalidLabels(t *testing.T) {
   252  	r := NewRegistry()
   253  	q := &sysUpTimeInNanoSecs{3}
   254  	g, _ := r.AddInt64DerivedCumulative("bm", WithLabelKeys("k1", "k2"))
   255  	gotErr := g.UpsertEntry(q.ToInt64, metricdata.NewLabelValue("k1v1"))
   256  	if gotErr == nil {
   257  		t.Errorf("expected error but got nil")
   258  	}
   259  }
   260  
   261  func TestInt64DerivedCumulativeEntry_Update(t *testing.T) {
   262  	r := NewRegistry()
   263  	q := &sysUpTimeInNanoSecs{3}
   264  	q2 := &sysUpTimeInNanoSecs{5}
   265  	g, _ := r.AddInt64DerivedCumulative("bm", WithLabelKeys("k1", "k2"))
   266  	g.UpsertEntry(q.ToInt64, metricdata.NewLabelValue("k1v1"), metricdata.LabelValue{})
   267  	gotErr := g.UpsertEntry(q2.ToInt64, metricdata.NewLabelValue("k1v1"), metricdata.LabelValue{})
   268  	if gotErr != nil {
   269  		t.Errorf("got: %v, want: nil", gotErr)
   270  	}
   271  	ms := r.Read()
   272  	if got, want := ms[0].TimeSeries[0].Points[0].Value.(int64), int64(5); got != want {
   273  		t.Errorf("value = %v, want %v", got, want)
   274  	}
   275  }
   276  
   277  type sysUpTimeInSeconds struct {
   278  	size float64
   279  }
   280  
   281  func (q *sysUpTimeInSeconds) ToFloat64() float64 {
   282  	return q.size
   283  }
   284  
   285  func TestFloat64DerivedCumulativeEntry_Inc(t *testing.T) {
   286  	r := NewRegistry()
   287  	q := &sysUpTimeInSeconds{5.0}
   288  	g, _ := r.AddFloat64DerivedCumulative("bm", WithLabelKeys("k1", "k2"))
   289  	err := g.UpsertEntry(q.ToFloat64, metricdata.NewLabelValue("k1v1"), metricdata.LabelValue{})
   290  	if err != nil {
   291  		t.Errorf("want: nil, got: %v", err)
   292  	}
   293  	ms := r.Read()
   294  	if got, want := ms[0].TimeSeries[0].Points[0].Value.(float64), float64(5.0); got != want {
   295  		t.Errorf("value = %v, want %v", got, want)
   296  	}
   297  	q.size = 7
   298  	ms = r.Read()
   299  	if got, want := ms[0].TimeSeries[0].Points[0].Value.(float64), float64(7.0); got != want {
   300  		t.Errorf("value = %v, want %v", got, want)
   301  	}
   302  }
   303  
   304  func TestFloat64DerivedCumulativeEntry_IncWithNilObj(t *testing.T) {
   305  	r := NewRegistry()
   306  	g, _ := r.AddFloat64DerivedCumulative("bm", WithLabelKeys("k1", "k2"))
   307  	gotErr := g.UpsertEntry(nil, metricdata.NewLabelValue("k1v1"), metricdata.LabelValue{})
   308  	if gotErr == nil {
   309  		t.Errorf("expected error but got nil")
   310  	}
   311  }
   312  
   313  func TestFloat64DerivedCumulativeEntry_IncWithInvalidLabels(t *testing.T) {
   314  	r := NewRegistry()
   315  	q := &sysUpTimeInSeconds{3}
   316  	g, _ := r.AddFloat64DerivedCumulative("bm", WithLabelKeys("k1", "k2"))
   317  	gotErr := g.UpsertEntry(q.ToFloat64, metricdata.NewLabelValue("k1v1"))
   318  	if gotErr == nil {
   319  		t.Errorf("expected error but got nil")
   320  	}
   321  }
   322  
   323  func TestFloat64DerivedCumulativeEntry_Update(t *testing.T) {
   324  	r := NewRegistry()
   325  	q := &sysUpTimeInSeconds{3.0}
   326  	q2 := &sysUpTimeInSeconds{5.0}
   327  	g, _ := r.AddFloat64DerivedCumulative("bm", WithLabelKeys("k1", "k2"))
   328  	g.UpsertEntry(q.ToFloat64, metricdata.NewLabelValue("k1v1"), metricdata.LabelValue{})
   329  	gotErr := g.UpsertEntry(q2.ToFloat64, metricdata.NewLabelValue("k1v1"), metricdata.LabelValue{})
   330  	if gotErr != nil {
   331  		t.Errorf("got: %v, want: nil", gotErr)
   332  	}
   333  	ms := r.Read()
   334  	if got, want := ms[0].TimeSeries[0].Points[0].Value.(float64), float64(5.0); got != want {
   335  		t.Errorf("value = %v, want %v", got, want)
   336  	}
   337  }
   338  

View as plain text