@@ -23,6 +23,7 @@ import (
2323
2424 "cloud.google.com/go/monitoring/apiv3"
2525 "github.com/golang/protobuf/ptypes/timestamp"
26+ "github.com/google/go-cmp/cmp"
2627 "go.opencensus.io/stats"
2728 "go.opencensus.io/stats/view"
2829 "go.opencensus.io/tag"
@@ -58,7 +59,7 @@ func TestExporter_makeReq(t *testing.T) {
5859 }
5960
6061 v := & view.View {
61- Name : "testview" ,
62+ Name : "example.com/views/ testview" ,
6263 Description : "desc" ,
6364 TagKeys : []tag.Key {key },
6465 Measure : m ,
@@ -95,6 +96,7 @@ func TestExporter_makeReq(t *testing.T) {
9596 projID string
9697 vd * view.Data
9798 want []* monitoringpb.CreateTimeSeriesRequest
99+ opts Options
98100 }{
99101 {
100102 name : "count agg + timeline" ,
@@ -105,7 +107,7 @@ func TestExporter_makeReq(t *testing.T) {
105107 TimeSeries : []* monitoringpb.TimeSeries {
106108 {
107109 Metric : & metricpb.Metric {
108- Type : "custom.googleapis.com/opencensus/testview" ,
110+ Type : "custom.googleapis.com/opencensus/example.com/views/ testview" ,
109111 Labels : map [string ]string {
110112 "test_key" : "test-value-1" ,
111113 opencensusTaskKey : taskValue ,
@@ -134,7 +136,7 @@ func TestExporter_makeReq(t *testing.T) {
134136 },
135137 {
136138 Metric : & metricpb.Metric {
137- Type : "custom.googleapis.com/opencensus/testview" ,
139+ Type : "custom.googleapis.com/opencensus/example.com/views/ testview" ,
138140 Labels : map [string ]string {
139141 "test_key" : "test-value-2" ,
140142 opencensusTaskKey : taskValue ,
@@ -164,6 +166,79 @@ func TestExporter_makeReq(t *testing.T) {
164166 },
165167 }},
166168 },
169+ {
170+ name : "metric type formatter" ,
171+ projID : "proj-id" ,
172+ vd : newTestViewData (v , start , end , sum1 , sum2 ),
173+ opts : Options {
174+ GetMetricType : func (v * view.View ) string {
175+ return fmt .Sprintf ("external.googleapis.com/%s" , v .Name )
176+ },
177+ },
178+ want : []* monitoringpb.CreateTimeSeriesRequest {{
179+ Name : monitoring .MetricProjectPath ("proj-id" ),
180+ TimeSeries : []* monitoringpb.TimeSeries {
181+ {
182+ Metric : & metricpb.Metric {
183+ Type : "external.googleapis.com/example.com/views/testview" ,
184+ Labels : map [string ]string {
185+ "test_key" : "test-value-1" ,
186+ opencensusTaskKey : taskValue ,
187+ },
188+ },
189+ Resource : & monitoredrespb.MonitoredResource {
190+ Type : "global" ,
191+ },
192+ Points : []* monitoringpb.Point {
193+ {
194+ Interval : & monitoringpb.TimeInterval {
195+ StartTime : & timestamp.Timestamp {
196+ Seconds : start .Unix (),
197+ Nanos : int32 (start .Nanosecond ()),
198+ },
199+ EndTime : & timestamp.Timestamp {
200+ Seconds : end .Unix (),
201+ Nanos : int32 (end .Nanosecond ()),
202+ },
203+ },
204+ Value : & monitoringpb.TypedValue {Value : & monitoringpb.TypedValue_DoubleValue {
205+ DoubleValue : 5.5 ,
206+ }},
207+ },
208+ },
209+ },
210+ {
211+ Metric : & metricpb.Metric {
212+ Type : "external.googleapis.com/example.com/views/testview" ,
213+ Labels : map [string ]string {
214+ "test_key" : "test-value-2" ,
215+ opencensusTaskKey : taskValue ,
216+ },
217+ },
218+ Resource : & monitoredrespb.MonitoredResource {
219+ Type : "global" ,
220+ },
221+ Points : []* monitoringpb.Point {
222+ {
223+ Interval : & monitoringpb.TimeInterval {
224+ StartTime : & timestamp.Timestamp {
225+ Seconds : start .Unix (),
226+ Nanos : int32 (start .Nanosecond ()),
227+ },
228+ EndTime : & timestamp.Timestamp {
229+ Seconds : end .Unix (),
230+ Nanos : int32 (end .Nanosecond ()),
231+ },
232+ },
233+ Value : & monitoringpb.TypedValue {Value : & monitoringpb.TypedValue_DoubleValue {
234+ DoubleValue : - 11.1 ,
235+ }},
236+ },
237+ },
238+ },
239+ },
240+ }},
241+ },
167242 {
168243 name : "sum agg + timeline" ,
169244 projID : "proj-id" ,
@@ -173,7 +248,7 @@ func TestExporter_makeReq(t *testing.T) {
173248 TimeSeries : []* monitoringpb.TimeSeries {
174249 {
175250 Metric : & metricpb.Metric {
176- Type : "custom.googleapis.com/opencensus/testview" ,
251+ Type : "custom.googleapis.com/opencensus/example.com/views/ testview" ,
177252 Labels : map [string ]string {
178253 "test_key" : "test-value-1" ,
179254 opencensusTaskKey : taskValue ,
@@ -202,7 +277,7 @@ func TestExporter_makeReq(t *testing.T) {
202277 },
203278 {
204279 Metric : & metricpb.Metric {
205- Type : "custom.googleapis.com/opencensus/testview" ,
280+ Type : "custom.googleapis.com/opencensus/example.com/views/ testview" ,
206281 Labels : map [string ]string {
207282 "test_key" : "test-value-2" ,
208283 opencensusTaskKey : taskValue ,
@@ -341,7 +416,10 @@ func TestExporter_makeReq(t *testing.T) {
341416 }
342417 for _ , tt := range tests {
343418 t .Run (tt .name , func (t * testing.T ) {
344- e , err := newStatsExporter (Options {ProjectID : tt .projID , MonitoringClientOptions : authOptions })
419+ opts := tt .opts
420+ opts .ProjectID = tt .projID
421+ opts .MonitoringClientOptions = authOptions
422+ e , err := newStatsExporter (opts )
345423 if err != nil {
346424 t .Fatal (err )
347425 }
@@ -352,8 +430,8 @@ func TestExporter_makeReq(t *testing.T) {
352430 if len (tt .want ) == 0 {
353431 return
354432 }
355- if ! reflect . DeepEqual (resps , tt .want ) {
356- t .Errorf ("%v: Exporter.makeReq() = %v, want %v " , tt . name , resps , tt . want )
433+ if diff := cmp . Diff (resps , tt .want ); diff != "" {
434+ t .Errorf ("Values differ -got + want: %s " , diff )
357435 }
358436 })
359437 }
0 commit comments