Skip to content
This repository was archived by the owner on Dec 3, 2024. It is now read-only.

Commit 202ca3d

Browse files
authoredDec 23, 2019
Merge pull request #7 from google/better-test
Test Coverage
2 parents da1b5e1 + 3d5d4f3 commit 202ca3d

File tree

1 file changed

+670
-38
lines changed

1 file changed

+670
-38
lines changed
 

‎stackdriver_test.go

Lines changed: 670 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ import (
2525
monitoring "cloud.google.com/go/monitoring/apiv3"
2626
metrics "github.com/armon/go-metrics"
2727
emptypb "github.com/golang/protobuf/ptypes/empty"
28+
"github.com/google/go-cmp/cmp"
2829
"google.golang.org/api/option"
30+
distributionpb "google.golang.org/genproto/googleapis/api/distribution"
31+
"google.golang.org/genproto/googleapis/api/metric"
32+
metricpb "google.golang.org/genproto/googleapis/api/metric"
2933
monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3"
3034
"google.golang.org/grpc"
3135
"google.golang.org/grpc/test/bufconn"
@@ -102,11 +106,73 @@ func TestSample(t *testing.T) {
102106
},
103107
createFn: func(t *testing.T) func(context.Context, *monitoringpb.CreateTimeSeriesRequest) (*emptypb.Empty, error) {
104108
return func(_ context.Context, req *monitoringpb.CreateTimeSeriesRequest) (*emptypb.Empty, error) {
105-
if req.TimeSeries[0].Points[0].Value.GetDistributionValue().BucketCounts[0] == 1 {
106-
return &emptypb.Empty{}, nil
109+
want := &monitoringpb.CreateTimeSeriesRequest{
110+
Name: "projects/foo",
111+
TimeSeries: []*monitoringpb.TimeSeries{
112+
&monitoringpb.TimeSeries{
113+
Metric: &metricpb.Metric{
114+
Type: "custom.googleapis.com/go-metrics/foo_bar",
115+
},
116+
MetricKind: metric.MetricDescriptor_CUMULATIVE,
117+
Points: []*monitoringpb.Point{
118+
&monitoringpb.Point{
119+
Value: &monitoringpb.TypedValue{
120+
Value: &monitoringpb.TypedValue_DistributionValue{
121+
DistributionValue: &distributionpb.Distribution{
122+
BucketCounts: []int64{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
123+
Count: 1,
124+
},
125+
},
126+
},
127+
},
128+
},
129+
},
130+
},
107131
}
108-
t.Errorf("unexpected CreateTimeSeriesRequest\nwant: %s\ngot: %v", "bucket 0 count 1", req)
109-
return nil, errors.New("unexpected CreateTimeSeriesRequest")
132+
if diff := diffCreateMsg(want, req); diff != "" {
133+
t.Errorf("unexpected CreateTimeSeriesRequest (-want +got):\n%s", diff)
134+
}
135+
return &emptypb.Empty{}, nil
136+
}
137+
},
138+
},
139+
{
140+
name: "histogram with label",
141+
collect: func() {
142+
ss.AddSampleWithLabels([]string{"foo", "bar"}, 5.0, []metrics.Label{metrics.Label{Name: "env", Value: "dev"}})
143+
},
144+
createFn: func(t *testing.T) func(context.Context, *monitoringpb.CreateTimeSeriesRequest) (*emptypb.Empty, error) {
145+
return func(_ context.Context, req *monitoringpb.CreateTimeSeriesRequest) (*emptypb.Empty, error) {
146+
want := &monitoringpb.CreateTimeSeriesRequest{
147+
Name: "projects/foo",
148+
TimeSeries: []*monitoringpb.TimeSeries{
149+
&monitoringpb.TimeSeries{
150+
Metric: &metricpb.Metric{
151+
Type: "custom.googleapis.com/go-metrics/foo_bar",
152+
Labels: map[string]string{
153+
"env": "dev",
154+
},
155+
},
156+
MetricKind: metric.MetricDescriptor_CUMULATIVE,
157+
Points: []*monitoringpb.Point{
158+
&monitoringpb.Point{
159+
Value: &monitoringpb.TypedValue{
160+
Value: &monitoringpb.TypedValue_DistributionValue{
161+
DistributionValue: &distributionpb.Distribution{
162+
BucketCounts: []int64{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
163+
Count: 1,
164+
},
165+
},
166+
},
167+
},
168+
},
169+
},
170+
},
171+
}
172+
if diff := diffCreateMsg(want, req); diff != "" {
173+
t.Errorf("unexpected CreateTimeSeriesRequest (-want +got):\n%s", diff)
174+
}
175+
return &emptypb.Empty{}, nil
110176
}
111177
},
112178
},
@@ -116,14 +182,37 @@ func TestSample(t *testing.T) {
116182
ss.AddSample([]string{"foo", "bar"}, 5.0)
117183
ss.AddSample([]string{"foo", "bar"}, 100.0)
118184
ss.AddSample([]string{"foo", "bar"}, 500.0)
185+
ss.AddSample([]string{"foo", "bar"}, 50000000.0)
119186
},
120187
createFn: func(t *testing.T) func(context.Context, *monitoringpb.CreateTimeSeriesRequest) (*emptypb.Empty, error) {
121188
return func(_ context.Context, req *monitoringpb.CreateTimeSeriesRequest) (*emptypb.Empty, error) {
122-
if req.TimeSeries[0].Points[0].Value.GetDistributionValue().BucketCounts[0] == 1 {
123-
return &emptypb.Empty{}, nil
189+
want := &monitoringpb.CreateTimeSeriesRequest{
190+
Name: "projects/foo",
191+
TimeSeries: []*monitoringpb.TimeSeries{
192+
&monitoringpb.TimeSeries{
193+
Metric: &metricpb.Metric{
194+
Type: "custom.googleapis.com/go-metrics/foo_bar",
195+
},
196+
MetricKind: metric.MetricDescriptor_CUMULATIVE,
197+
Points: []*monitoringpb.Point{
198+
&monitoringpb.Point{
199+
Value: &monitoringpb.TypedValue{
200+
Value: &monitoringpb.TypedValue_DistributionValue{
201+
DistributionValue: &distributionpb.Distribution{
202+
BucketCounts: []int64{1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1},
203+
Count: 4,
204+
},
205+
},
206+
},
207+
},
208+
},
209+
},
210+
},
124211
}
125-
t.Errorf("unexpected CreateTimeSeriesRequest\nwant: %s\ngot: %v", "bucket 0 count 1", req)
126-
return nil, errors.New("unexpected CreateTimeSeriesRequest")
212+
if diff := diffCreateMsg(want, req); diff != "" {
213+
t.Errorf("unexpected CreateTimeSeriesRequest (-want +got):\n%s", diff)
214+
}
215+
return &emptypb.Empty{}, nil
127216
}
128217
},
129218
},
@@ -136,11 +225,33 @@ func TestSample(t *testing.T) {
136225
},
137226
createFn: func(t *testing.T) func(context.Context, *monitoringpb.CreateTimeSeriesRequest) (*emptypb.Empty, error) {
138227
return func(_ context.Context, req *monitoringpb.CreateTimeSeriesRequest) (*emptypb.Empty, error) {
139-
if req.TimeSeries[0].Points[0].Value.GetDistributionValue().BucketCounts[0] == 3 {
140-
return &emptypb.Empty{}, nil
228+
want := &monitoringpb.CreateTimeSeriesRequest{
229+
Name: "projects/foo",
230+
TimeSeries: []*monitoringpb.TimeSeries{
231+
&monitoringpb.TimeSeries{
232+
Metric: &metricpb.Metric{
233+
Type: "custom.googleapis.com/go-metrics/foo_bar",
234+
},
235+
MetricKind: metric.MetricDescriptor_CUMULATIVE,
236+
Points: []*monitoringpb.Point{
237+
&monitoringpb.Point{
238+
Value: &monitoringpb.TypedValue{
239+
Value: &monitoringpb.TypedValue_DistributionValue{
240+
DistributionValue: &distributionpb.Distribution{
241+
BucketCounts: []int64{3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
242+
Count: 3,
243+
},
244+
},
245+
},
246+
},
247+
},
248+
},
249+
},
141250
}
142-
t.Errorf("unexpected CreateTimeSeriesRequest\nwant: %s\ngot: %v", "bucket 0 count 3", req)
143-
return nil, errors.New("unexpected CreateTimeSeriesRequest")
251+
if diff := diffCreateMsg(want, req); diff != "" {
252+
t.Errorf("unexpected CreateTimeSeriesRequest (-want +got):\n%s", diff)
253+
}
254+
return &emptypb.Empty{}, nil
144255
}
145256
},
146257
},
@@ -151,11 +262,67 @@ func TestSample(t *testing.T) {
151262
},
152263
createFn: func(t *testing.T) func(context.Context, *monitoringpb.CreateTimeSeriesRequest) (*emptypb.Empty, error) {
153264
return func(_ context.Context, req *monitoringpb.CreateTimeSeriesRequest) (*emptypb.Empty, error) {
154-
if req.TimeSeries[0].Points[0].Value.GetDoubleValue() == 1.0 {
155-
return &emptypb.Empty{}, nil
265+
want := &monitoringpb.CreateTimeSeriesRequest{
266+
Name: "projects/foo",
267+
TimeSeries: []*monitoringpb.TimeSeries{
268+
&monitoringpb.TimeSeries{
269+
Metric: &metricpb.Metric{
270+
Type: "custom.googleapis.com/go-metrics/foo_bar_counter",
271+
},
272+
MetricKind: metric.MetricDescriptor_GAUGE,
273+
Points: []*monitoringpb.Point{
274+
&monitoringpb.Point{
275+
Value: &monitoringpb.TypedValue{
276+
Value: &monitoringpb.TypedValue_DoubleValue{
277+
DoubleValue: 1.0,
278+
},
279+
},
280+
},
281+
},
282+
},
283+
},
156284
}
157-
t.Errorf("unexpected CreateTimeSeriesRequest\nwant: %s\ngot: %v", "value 1.0", req)
158-
return nil, errors.New("unexpected CreateTimeSeriesRequest")
285+
if diff := diffCreateMsg(want, req); diff != "" {
286+
t.Errorf("unexpected CreateTimeSeriesRequest (-want +got):\n%s", diff)
287+
}
288+
return &emptypb.Empty{}, nil
289+
}
290+
},
291+
},
292+
{
293+
name: "counter with label",
294+
collect: func() {
295+
ss.IncrCounterWithLabels([]string{"foo", "bar"}, 1.0, []metrics.Label{metrics.Label{Name: "env", Value: "dev"}})
296+
},
297+
createFn: func(t *testing.T) func(context.Context, *monitoringpb.CreateTimeSeriesRequest) (*emptypb.Empty, error) {
298+
return func(_ context.Context, req *monitoringpb.CreateTimeSeriesRequest) (*emptypb.Empty, error) {
299+
want := &monitoringpb.CreateTimeSeriesRequest{
300+
Name: "projects/foo",
301+
TimeSeries: []*monitoringpb.TimeSeries{
302+
&monitoringpb.TimeSeries{
303+
Metric: &metricpb.Metric{
304+
Type: "custom.googleapis.com/go-metrics/foo_bar_counter",
305+
Labels: map[string]string{
306+
"env": "dev",
307+
},
308+
},
309+
MetricKind: metric.MetricDescriptor_GAUGE,
310+
Points: []*monitoringpb.Point{
311+
&monitoringpb.Point{
312+
Value: &monitoringpb.TypedValue{
313+
Value: &monitoringpb.TypedValue_DoubleValue{
314+
DoubleValue: 1.0,
315+
},
316+
},
317+
},
318+
},
319+
},
320+
},
321+
}
322+
if diff := diffCreateMsg(want, req); diff != "" {
323+
t.Errorf("unexpected CreateTimeSeriesRequest (-want +got):\n%s", diff)
324+
}
325+
return &emptypb.Empty{}, nil
159326
}
160327
},
161328
},
@@ -168,11 +335,30 @@ func TestSample(t *testing.T) {
168335
},
169336
createFn: func(t *testing.T) func(context.Context, *monitoringpb.CreateTimeSeriesRequest) (*emptypb.Empty, error) {
170337
return func(_ context.Context, req *monitoringpb.CreateTimeSeriesRequest) (*emptypb.Empty, error) {
171-
if req.TimeSeries[0].Points[0].Value.GetDoubleValue() == 3.0 {
172-
return &emptypb.Empty{}, nil
338+
want := &monitoringpb.CreateTimeSeriesRequest{
339+
Name: "projects/foo",
340+
TimeSeries: []*monitoringpb.TimeSeries{
341+
&monitoringpb.TimeSeries{
342+
Metric: &metricpb.Metric{
343+
Type: "custom.googleapis.com/go-metrics/foo_bar_counter",
344+
},
345+
MetricKind: metric.MetricDescriptor_GAUGE,
346+
Points: []*monitoringpb.Point{
347+
&monitoringpb.Point{
348+
Value: &monitoringpb.TypedValue{
349+
Value: &monitoringpb.TypedValue_DoubleValue{
350+
DoubleValue: 3.0,
351+
},
352+
},
353+
},
354+
},
355+
},
356+
},
173357
}
174-
t.Errorf("unexpected CreateTimeSeriesRequest\nwant: %s\ngot: %v", "value 3.0", req)
175-
return nil, errors.New("unexpected CreateTimeSeriesRequest")
358+
if diff := diffCreateMsg(want, req); diff != "" {
359+
t.Errorf("unexpected CreateTimeSeriesRequest (-want +got):\n%s", diff)
360+
}
361+
return &emptypb.Empty{}, nil
176362
}
177363
},
178364
},
@@ -183,11 +369,67 @@ func TestSample(t *testing.T) {
183369
},
184370
createFn: func(t *testing.T) func(context.Context, *monitoringpb.CreateTimeSeriesRequest) (*emptypb.Empty, error) {
185371
return func(_ context.Context, req *monitoringpb.CreateTimeSeriesRequest) (*emptypb.Empty, error) {
186-
if req.TimeSeries[0].Points[0].Value.GetDoubleValue() == 50.0 {
187-
return &emptypb.Empty{}, nil
372+
want := &monitoringpb.CreateTimeSeriesRequest{
373+
Name: "projects/foo",
374+
TimeSeries: []*monitoringpb.TimeSeries{
375+
&monitoringpb.TimeSeries{
376+
Metric: &metricpb.Metric{
377+
Type: "custom.googleapis.com/go-metrics/foo_bar_gauge",
378+
},
379+
MetricKind: metric.MetricDescriptor_GAUGE,
380+
Points: []*monitoringpb.Point{
381+
&monitoringpb.Point{
382+
Value: &monitoringpb.TypedValue{
383+
Value: &monitoringpb.TypedValue_DoubleValue{
384+
DoubleValue: 50.0,
385+
},
386+
},
387+
},
388+
},
389+
},
390+
},
188391
}
189-
t.Errorf("unexpected CreateTimeSeriesRequest\nwant: %s\ngot: %v", "value 50.0", req)
190-
return nil, errors.New("unexpected CreateTimeSeriesRequest")
392+
if diff := diffCreateMsg(want, req); diff != "" {
393+
t.Errorf("unexpected CreateTimeSeriesRequest (-want +got):\n%s", diff)
394+
}
395+
return &emptypb.Empty{}, nil
396+
}
397+
},
398+
},
399+
{
400+
name: "gauge with label",
401+
collect: func() {
402+
ss.SetGaugeWithLabels([]string{"foo", "bar"}, 1.0, []metrics.Label{metrics.Label{Name: "env", Value: "dev"}})
403+
},
404+
createFn: func(t *testing.T) func(context.Context, *monitoringpb.CreateTimeSeriesRequest) (*emptypb.Empty, error) {
405+
return func(_ context.Context, req *monitoringpb.CreateTimeSeriesRequest) (*emptypb.Empty, error) {
406+
want := &monitoringpb.CreateTimeSeriesRequest{
407+
Name: "projects/foo",
408+
TimeSeries: []*monitoringpb.TimeSeries{
409+
&monitoringpb.TimeSeries{
410+
Metric: &metricpb.Metric{
411+
Type: "custom.googleapis.com/go-metrics/foo_bar_gauge",
412+
Labels: map[string]string{
413+
"env": "dev",
414+
},
415+
},
416+
MetricKind: metric.MetricDescriptor_GAUGE,
417+
Points: []*monitoringpb.Point{
418+
&monitoringpb.Point{
419+
Value: &monitoringpb.TypedValue{
420+
Value: &monitoringpb.TypedValue_DoubleValue{
421+
DoubleValue: 1.0,
422+
},
423+
},
424+
},
425+
},
426+
},
427+
},
428+
}
429+
if diff := diffCreateMsg(want, req); diff != "" {
430+
t.Errorf("unexpected CreateTimeSeriesRequest (-want +got):\n%s", diff)
431+
}
432+
return &emptypb.Empty{}, nil
191433
}
192434
},
193435
},
@@ -200,11 +442,30 @@ func TestSample(t *testing.T) {
200442
},
201443
createFn: func(t *testing.T) func(context.Context, *monitoringpb.CreateTimeSeriesRequest) (*emptypb.Empty, error) {
202444
return func(_ context.Context, req *monitoringpb.CreateTimeSeriesRequest) (*emptypb.Empty, error) {
203-
if req.TimeSeries[0].Points[0].Value.GetDoubleValue() == 50.0 {
204-
return &emptypb.Empty{}, nil
445+
want := &monitoringpb.CreateTimeSeriesRequest{
446+
Name: "projects/foo",
447+
TimeSeries: []*monitoringpb.TimeSeries{
448+
&monitoringpb.TimeSeries{
449+
Metric: &metricpb.Metric{
450+
Type: "custom.googleapis.com/go-metrics/foo_bar_gauge",
451+
},
452+
MetricKind: metric.MetricDescriptor_GAUGE,
453+
Points: []*monitoringpb.Point{
454+
&monitoringpb.Point{
455+
Value: &monitoringpb.TypedValue{
456+
Value: &monitoringpb.TypedValue_DoubleValue{
457+
DoubleValue: 50.0,
458+
},
459+
},
460+
},
461+
},
462+
},
463+
},
205464
}
206-
t.Errorf("unexpected CreateTimeSeriesRequest\nwant: %s\ngot: %v", "value 50.0", req)
207-
return nil, errors.New("unexpected CreateTimeSeriesRequest")
465+
if diff := diffCreateMsg(want, req); diff != "" {
466+
t.Errorf("unexpected CreateTimeSeriesRequest (-want +got):\n%s", diff)
467+
}
468+
return &emptypb.Empty{}, nil
208469
}
209470
},
210471
},
@@ -217,11 +478,30 @@ func TestSample(t *testing.T) {
217478
},
218479
createFn: func(t *testing.T) func(context.Context, *monitoringpb.CreateTimeSeriesRequest) (*emptypb.Empty, error) {
219480
return func(_ context.Context, req *monitoringpb.CreateTimeSeriesRequest) (*emptypb.Empty, error) {
220-
if req.TimeSeries[0].Points[0].Value.GetDoubleValue() == 52.0 {
221-
return &emptypb.Empty{}, nil
481+
want := &monitoringpb.CreateTimeSeriesRequest{
482+
Name: "projects/foo",
483+
TimeSeries: []*monitoringpb.TimeSeries{
484+
&monitoringpb.TimeSeries{
485+
Metric: &metricpb.Metric{
486+
Type: "custom.googleapis.com/go-metrics/foo_bar_gauge",
487+
},
488+
MetricKind: metric.MetricDescriptor_GAUGE,
489+
Points: []*monitoringpb.Point{
490+
&monitoringpb.Point{
491+
Value: &monitoringpb.TypedValue{
492+
Value: &monitoringpb.TypedValue_DoubleValue{
493+
DoubleValue: 52.0,
494+
},
495+
},
496+
},
497+
},
498+
},
499+
},
222500
}
223-
t.Errorf("unexpected CreateTimeSeriesRequest\nwant: %s\ngot: %v", "value 52.0", req)
224-
return nil, errors.New("unexpected CreateTimeSeriesRequest")
501+
if diff := diffCreateMsg(want, req); diff != "" {
502+
t.Errorf("unexpected CreateTimeSeriesRequest (-want +got):\n%s", diff)
503+
}
504+
return &emptypb.Empty{}, nil
225505
}
226506
},
227507
},
@@ -282,6 +562,9 @@ func TestSample(t *testing.T) {
282562
func TestExtract(t *testing.T) {
283563
ss := newTestSink(0*time.Second, nil)
284564
ss.extractor = func(key []string, kind string) ([]string, []metrics.Label, error) {
565+
if len(key) < 2 {
566+
return nil, nil, errors.New("bad extraction")
567+
}
285568
return key[:1], []metrics.Label{
286569
{
287570
Name: "method",
@@ -302,12 +585,306 @@ func TestExtract(t *testing.T) {
302585
},
303586
createFn: func(t *testing.T) func(context.Context, *monitoringpb.CreateTimeSeriesRequest) (*emptypb.Empty, error) {
304587
return func(_ context.Context, req *monitoringpb.CreateTimeSeriesRequest) (*emptypb.Empty, error) {
305-
metric := req.TimeSeries[0].GetMetric()
306-
if metric.GetType() == "custom.googleapis.com/go-metrics/foo" && metric.GetLabels()["method"] == "bar" && req.TimeSeries[0].Points[0].Value.GetDistributionValue().BucketCounts[0] == 1 {
307-
return &emptypb.Empty{}, nil
588+
want := &monitoringpb.CreateTimeSeriesRequest{
589+
Name: "projects/foo",
590+
TimeSeries: []*monitoringpb.TimeSeries{
591+
&monitoringpb.TimeSeries{
592+
Metric: &metricpb.Metric{
593+
Type: "custom.googleapis.com/go-metrics/foo",
594+
Labels: map[string]string{
595+
"method": "bar",
596+
},
597+
},
598+
MetricKind: metric.MetricDescriptor_CUMULATIVE,
599+
Points: []*monitoringpb.Point{
600+
&monitoringpb.Point{
601+
Value: &monitoringpb.TypedValue{
602+
Value: &monitoringpb.TypedValue_DistributionValue{
603+
DistributionValue: &distributionpb.Distribution{
604+
BucketCounts: []int64{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
605+
Count: 1,
606+
},
607+
},
608+
},
609+
},
610+
},
611+
},
612+
},
308613
}
309-
t.Errorf("unexpected CreateTimeSeriesRequest\nwant: %s\ngot: %v", "bucket 0 count 1", req)
310-
return nil, errors.New("unexpected CreateTimeSeriesRequest")
614+
if diff := diffCreateMsg(want, req); diff != "" {
615+
t.Errorf("unexpected CreateTimeSeriesRequest (-want +got):\n%s", diff)
616+
}
617+
return &emptypb.Empty{}, nil
618+
}
619+
},
620+
},
621+
{
622+
name: "histogram with label",
623+
collect: func() {
624+
ss.AddSampleWithLabels([]string{"foo", "bar"}, 5.0, []metrics.Label{metrics.Label{Name: "env", Value: "dev"}})
625+
},
626+
createFn: func(t *testing.T) func(context.Context, *monitoringpb.CreateTimeSeriesRequest) (*emptypb.Empty, error) {
627+
return func(_ context.Context, req *monitoringpb.CreateTimeSeriesRequest) (*emptypb.Empty, error) {
628+
want := &monitoringpb.CreateTimeSeriesRequest{
629+
Name: "projects/foo",
630+
TimeSeries: []*monitoringpb.TimeSeries{
631+
&monitoringpb.TimeSeries{
632+
Metric: &metricpb.Metric{
633+
Type: "custom.googleapis.com/go-metrics/foo",
634+
Labels: map[string]string{
635+
"env": "dev",
636+
"method": "bar",
637+
},
638+
},
639+
MetricKind: metric.MetricDescriptor_CUMULATIVE,
640+
Points: []*monitoringpb.Point{
641+
&monitoringpb.Point{
642+
Value: &monitoringpb.TypedValue{
643+
Value: &monitoringpb.TypedValue_DistributionValue{
644+
DistributionValue: &distributionpb.Distribution{
645+
BucketCounts: []int64{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
646+
Count: 1,
647+
},
648+
},
649+
},
650+
},
651+
},
652+
},
653+
},
654+
}
655+
if diff := diffCreateMsg(want, req); diff != "" {
656+
t.Errorf("unexpected CreateTimeSeriesRequest (-want +got):\n%s", diff)
657+
}
658+
return &emptypb.Empty{}, nil
659+
}
660+
},
661+
},
662+
{
663+
name: "counter with label",
664+
collect: func() {
665+
ss.IncrCounterWithLabels([]string{"foo", "bar"}, 1.0, []metrics.Label{metrics.Label{Name: "env", Value: "dev"}})
666+
},
667+
createFn: func(t *testing.T) func(context.Context, *monitoringpb.CreateTimeSeriesRequest) (*emptypb.Empty, error) {
668+
return func(_ context.Context, req *monitoringpb.CreateTimeSeriesRequest) (*emptypb.Empty, error) {
669+
want := &monitoringpb.CreateTimeSeriesRequest{
670+
Name: "projects/foo",
671+
TimeSeries: []*monitoringpb.TimeSeries{
672+
&monitoringpb.TimeSeries{
673+
Metric: &metricpb.Metric{
674+
Type: "custom.googleapis.com/go-metrics/foo",
675+
Labels: map[string]string{
676+
"env": "dev",
677+
"method": "bar",
678+
},
679+
},
680+
MetricKind: metric.MetricDescriptor_GAUGE,
681+
Points: []*monitoringpb.Point{
682+
&monitoringpb.Point{
683+
Value: &monitoringpb.TypedValue{
684+
Value: &monitoringpb.TypedValue_DoubleValue{
685+
DoubleValue: 1.0,
686+
},
687+
},
688+
},
689+
},
690+
},
691+
},
692+
}
693+
if diff := diffCreateMsg(want, req); diff != "" {
694+
t.Errorf("unexpected CreateTimeSeriesRequest (-want +got):\n%s", diff)
695+
}
696+
return &emptypb.Empty{}, nil
697+
}
698+
},
699+
},
700+
{
701+
name: "gauge with label",
702+
collect: func() {
703+
ss.SetGaugeWithLabels([]string{"foo", "bar"}, 1.0, []metrics.Label{metrics.Label{Name: "env", Value: "dev"}})
704+
},
705+
createFn: func(t *testing.T) func(context.Context, *monitoringpb.CreateTimeSeriesRequest) (*emptypb.Empty, error) {
706+
return func(_ context.Context, req *monitoringpb.CreateTimeSeriesRequest) (*emptypb.Empty, error) {
707+
want := &monitoringpb.CreateTimeSeriesRequest{
708+
Name: "projects/foo",
709+
TimeSeries: []*monitoringpb.TimeSeries{
710+
&monitoringpb.TimeSeries{
711+
Metric: &metricpb.Metric{
712+
Type: "custom.googleapis.com/go-metrics/foo",
713+
Labels: map[string]string{
714+
"env": "dev",
715+
"method": "bar",
716+
},
717+
},
718+
MetricKind: metric.MetricDescriptor_GAUGE,
719+
Points: []*monitoringpb.Point{
720+
&monitoringpb.Point{
721+
Value: &monitoringpb.TypedValue{
722+
Value: &monitoringpb.TypedValue_DoubleValue{
723+
DoubleValue: 1.0,
724+
},
725+
},
726+
},
727+
},
728+
},
729+
},
730+
}
731+
if diff := diffCreateMsg(want, req); diff != "" {
732+
t.Errorf("unexpected CreateTimeSeriesRequest (-want +got):\n%s", diff)
733+
}
734+
return &emptypb.Empty{}, nil
735+
}
736+
},
737+
},
738+
{
739+
name: "histogram error, no requests",
740+
collect: func() {
741+
ss.AddSample([]string{"foo"}, 5.0)
742+
},
743+
createFn: func(t *testing.T) func(context.Context, *monitoringpb.CreateTimeSeriesRequest) (*emptypb.Empty, error) {
744+
return func(_ context.Context, req *monitoringpb.CreateTimeSeriesRequest) (*emptypb.Empty, error) {
745+
t.Errorf("unexpected CreateTimeSeriesRequest")
746+
return &emptypb.Empty{}, nil
747+
}
748+
},
749+
},
750+
{
751+
name: "histogram error, metric skipped",
752+
collect: func() {
753+
ss.AddSample([]string{"foo"}, 5.0)
754+
ss.AddSample([]string{"bar", "baz"}, 5.0)
755+
},
756+
createFn: func(t *testing.T) func(context.Context, *monitoringpb.CreateTimeSeriesRequest) (*emptypb.Empty, error) {
757+
return func(_ context.Context, req *monitoringpb.CreateTimeSeriesRequest) (*emptypb.Empty, error) {
758+
want := &monitoringpb.CreateTimeSeriesRequest{
759+
Name: "projects/foo",
760+
TimeSeries: []*monitoringpb.TimeSeries{
761+
&monitoringpb.TimeSeries{
762+
Metric: &metricpb.Metric{
763+
Type: "custom.googleapis.com/go-metrics/bar",
764+
Labels: map[string]string{
765+
"method": "baz",
766+
},
767+
},
768+
MetricKind: metric.MetricDescriptor_CUMULATIVE,
769+
Points: []*monitoringpb.Point{
770+
&monitoringpb.Point{
771+
Value: &monitoringpb.TypedValue{
772+
Value: &monitoringpb.TypedValue_DistributionValue{
773+
DistributionValue: &distributionpb.Distribution{
774+
BucketCounts: []int64{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
775+
Count: 1,
776+
},
777+
},
778+
},
779+
},
780+
},
781+
},
782+
},
783+
}
784+
if diff := diffCreateMsg(want, req); diff != "" {
785+
t.Errorf("unexpected CreateTimeSeriesRequest (-want +got):\n%s", diff)
786+
}
787+
return &emptypb.Empty{}, nil
788+
}
789+
},
790+
},
791+
{
792+
name: "counter error, no requests",
793+
collect: func() {
794+
ss.IncrCounter([]string{"foo"}, 5.0)
795+
},
796+
createFn: func(t *testing.T) func(context.Context, *monitoringpb.CreateTimeSeriesRequest) (*emptypb.Empty, error) {
797+
return func(_ context.Context, req *monitoringpb.CreateTimeSeriesRequest) (*emptypb.Empty, error) {
798+
t.Errorf("unexpected CreateTimeSeriesRequest")
799+
return &emptypb.Empty{}, nil
800+
}
801+
},
802+
},
803+
{
804+
name: "counter error, metric skipped",
805+
collect: func() {
806+
ss.IncrCounter([]string{"foo"}, 5.0)
807+
ss.IncrCounter([]string{"bar", "baz"}, 5.0)
808+
},
809+
createFn: func(t *testing.T) func(context.Context, *monitoringpb.CreateTimeSeriesRequest) (*emptypb.Empty, error) {
810+
return func(_ context.Context, req *monitoringpb.CreateTimeSeriesRequest) (*emptypb.Empty, error) {
811+
want := &monitoringpb.CreateTimeSeriesRequest{
812+
Name: "projects/foo",
813+
TimeSeries: []*monitoringpb.TimeSeries{
814+
&monitoringpb.TimeSeries{
815+
Metric: &metricpb.Metric{
816+
Type: "custom.googleapis.com/go-metrics/bar",
817+
Labels: map[string]string{
818+
"method": "baz",
819+
},
820+
},
821+
MetricKind: metric.MetricDescriptor_GAUGE,
822+
Points: []*monitoringpb.Point{
823+
&monitoringpb.Point{
824+
Value: &monitoringpb.TypedValue{
825+
Value: &monitoringpb.TypedValue_DoubleValue{
826+
DoubleValue: 5.0,
827+
},
828+
},
829+
},
830+
},
831+
},
832+
},
833+
}
834+
if diff := diffCreateMsg(want, req); diff != "" {
835+
t.Errorf("unexpected CreateTimeSeriesRequest (-want +got):\n%s", diff)
836+
}
837+
return &emptypb.Empty{}, nil
838+
}
839+
},
840+
},
841+
{
842+
name: "gauge error, no requests",
843+
collect: func() {
844+
ss.SetGauge([]string{"foo"}, 5.0)
845+
},
846+
createFn: func(t *testing.T) func(context.Context, *monitoringpb.CreateTimeSeriesRequest) (*emptypb.Empty, error) {
847+
return func(_ context.Context, req *monitoringpb.CreateTimeSeriesRequest) (*emptypb.Empty, error) {
848+
t.Errorf("unexpected CreateTimeSeriesRequest")
849+
return &emptypb.Empty{}, nil
850+
}
851+
},
852+
},
853+
{
854+
name: "gauge error, metric skipped",
855+
collect: func() {
856+
ss.SetGauge([]string{"foo"}, 5.0)
857+
ss.SetGauge([]string{"bar", "baz"}, 5.0)
858+
},
859+
createFn: func(t *testing.T) func(context.Context, *monitoringpb.CreateTimeSeriesRequest) (*emptypb.Empty, error) {
860+
return func(_ context.Context, req *monitoringpb.CreateTimeSeriesRequest) (*emptypb.Empty, error) {
861+
want := &monitoringpb.CreateTimeSeriesRequest{
862+
Name: "projects/foo",
863+
TimeSeries: []*monitoringpb.TimeSeries{
864+
&monitoringpb.TimeSeries{
865+
Metric: &metricpb.Metric{
866+
Type: "custom.googleapis.com/go-metrics/bar",
867+
Labels: map[string]string{
868+
"method": "baz",
869+
},
870+
},
871+
MetricKind: metric.MetricDescriptor_GAUGE,
872+
Points: []*monitoringpb.Point{
873+
&monitoringpb.Point{
874+
Value: &monitoringpb.TypedValue{
875+
Value: &monitoringpb.TypedValue_DoubleValue{
876+
DoubleValue: 5.0,
877+
},
878+
},
879+
},
880+
},
881+
},
882+
},
883+
}
884+
if diff := diffCreateMsg(want, req); diff != "" {
885+
t.Errorf("unexpected CreateTimeSeriesRequest (-want +got):\n%s", diff)
886+
}
887+
return &emptypb.Empty{}, nil
311888
}
312889
},
313890
},
@@ -363,11 +940,66 @@ func (s *mockMetricServer) CreateTimeSeries(ctx context.Context, req *monitoring
363940
// Skips defaults that are not appropriate for tests.
364941
func newTestSink(interval time.Duration, client *monitoring.MetricClient) *Sink {
365942
s := &Sink{}
366-
s.taskInfo = &taskInfo{}
943+
s.taskInfo = &taskInfo{
944+
ProjectID: "foo",
945+
}
367946
s.interval = interval
368947
s.bucketer = DefaultBucketer
369948
s.extractor = DefaultLabelExtractor
370949
s.reset()
371950
go s.flushMetrics(context.Background())
372951
return s
373952
}
953+
954+
func diffCreateMsg(want, got *monitoringpb.CreateTimeSeriesRequest) string {
955+
out := ""
956+
if want.GetName() != "" && (want.GetName() != got.GetName()) {
957+
out += fmt.Sprintf("Unexpected Name, got: %s, want:%s\n", got.GetName(), want.GetName())
958+
}
959+
960+
for i := range want.GetTimeSeries() {
961+
w := want.GetTimeSeries()[i]
962+
g := got.GetTimeSeries()[i]
963+
964+
if w.GetMetricKind() != g.GetMetricKind() {
965+
out += fmt.Sprintf("Unexpected MetricKind, got: %s, want:%s\n", g.GetMetricKind(), w.GetMetricKind())
966+
}
967+
968+
if w.GetMetric().GetType() != g.GetMetric().GetType() {
969+
out += fmt.Sprintf("Unexpected Metric Type, got: %s, want:%s\n", g.GetMetric().GetType(), w.GetMetric().GetType())
970+
}
971+
972+
if len(w.GetMetric().GetLabels()) != 0 {
973+
d := cmp.Diff(g.GetMetric().GetLabels(), w.GetMetric().GetLabels())
974+
if d != "" {
975+
out += fmt.Sprintf("Unexpected metric labels diff:%s \n", d)
976+
}
977+
}
978+
979+
for j := range w.GetPoints() {
980+
wp := w.GetPoints()[j]
981+
gp := g.GetPoints()[j]
982+
983+
// TODO: support diffing the start/end times
984+
985+
// gauge/count
986+
if wp.GetValue().GetDoubleValue() != gp.GetValue().GetDoubleValue() {
987+
out += fmt.Sprintf("Unexpected value (@point %d), got: %v, want:%v\n", j, gp.GetValue().GetDoubleValue(), wp.GetValue().GetDoubleValue())
988+
}
989+
990+
// distribution
991+
if wd := wp.GetValue().GetDistributionValue(); wd != nil {
992+
gd := gp.GetValue().GetDistributionValue()
993+
// TODO: support diffing custom buckets
994+
d := cmp.Diff(gd.GetBucketCounts(), wd.GetBucketCounts())
995+
if d != "" {
996+
out += fmt.Sprintf("Unexpected bucket counts diff (@point %d):%s \n", j, d)
997+
}
998+
if gd.GetCount() != wd.GetCount() {
999+
out += fmt.Sprintf("Unexpected count (@point %d), got: %v, want: %v\n", j, gd.GetCount(), wd.GetCount())
1000+
}
1001+
}
1002+
}
1003+
}
1004+
return out
1005+
}

0 commit comments

Comments
 (0)
This repository has been archived.