@@ -22,18 +22,186 @@ import (
22
22
23
23
resourcepb "github.com/census-instrumentation/opencensus-proto/gen-go/resource/v1"
24
24
"github.com/golang/protobuf/ptypes/timestamp"
25
+ "google.golang.org/api/option"
25
26
distributionpb "google.golang.org/genproto/googleapis/api/distribution"
26
27
labelpb "google.golang.org/genproto/googleapis/api/label"
27
28
googlemetricpb "google.golang.org/genproto/googleapis/api/metric"
28
29
monitoredrespb "google.golang.org/genproto/googleapis/api/monitoredres"
29
30
monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3"
31
+ "google.golang.org/grpc"
30
32
31
33
metricspb "github.com/census-instrumentation/opencensus-proto/gen-go/metrics/v1"
32
34
"github.com/golang/protobuf/ptypes/wrappers"
33
35
"github.com/google/go-cmp/cmp"
34
36
"go.opencensus.io/resource/resourcekeys"
35
37
)
36
38
39
+ func TestExportTimeSeriesWithDifferentLabels (t * testing.T ) {
40
+ server , addr , doneFn := createFakeServer (t )
41
+ defer doneFn ()
42
+
43
+ // Now create a gRPC connection to the agent.
44
+ conn , err := grpc .Dial (addr , grpc .WithInsecure ())
45
+ if err != nil {
46
+ t .Fatalf ("Failed to make a gRPC connection to the agent: %v" , err )
47
+ }
48
+ defer conn .Close ()
49
+
50
+ // Finally create the OpenCensus stats exporter
51
+ exporterOptions := Options {
52
+ ProjectID : "equivalence" ,
53
+ MonitoringClientOptions : []option.ClientOption {option .WithGRPCConn (conn )},
54
+
55
+ // Set empty labels to avoid the opencensus-task
56
+ DefaultMonitoringLabels : & Labels {},
57
+ }
58
+ se , err := NewExporter (exporterOptions )
59
+ if err != nil {
60
+ t .Fatalf ("Failed to create the statsExporter: %v" , err )
61
+ }
62
+
63
+ startTimestamp := & timestamp.Timestamp {
64
+ Seconds : 1543160298 ,
65
+ Nanos : 100000090 ,
66
+ }
67
+ endTimestamp := & timestamp.Timestamp {
68
+ Seconds : 1543160298 ,
69
+ Nanos : 100000997 ,
70
+ }
71
+
72
+ // Generate the proto Metrics.
73
+ var metricPbs []* metricspb.Metric
74
+ metricPbs = append (metricPbs ,
75
+ & metricspb.Metric {
76
+ MetricDescriptor : & metricspb.MetricDescriptor {
77
+ Name : "ocagent.io/calls" ,
78
+ Description : "The number of the various calls" ,
79
+ LabelKeys : []* metricspb.LabelKey {
80
+ {
81
+ Key : "empty_key" ,
82
+ },
83
+ {
84
+ Key : "operation_type" ,
85
+ },
86
+ },
87
+ Unit : "1" ,
88
+ Type : metricspb .MetricDescriptor_CUMULATIVE_INT64 ,
89
+ },
90
+ Timeseries : []* metricspb.TimeSeries {
91
+ {
92
+ StartTimestamp : startTimestamp ,
93
+ LabelValues : []* metricspb.LabelValue {
94
+ {
95
+ Value : "" ,
96
+ HasValue : true ,
97
+ },
98
+ {
99
+ Value : "test_1" ,
100
+ HasValue : true ,
101
+ },
102
+ },
103
+ Points : []* metricspb.Point {
104
+ {
105
+ Timestamp : endTimestamp ,
106
+ Value : & metricspb.Point_Int64Value {Int64Value : int64 (1 )},
107
+ },
108
+ },
109
+ },
110
+ {
111
+ StartTimestamp : startTimestamp ,
112
+ LabelValues : []* metricspb.LabelValue {
113
+ {
114
+ Value : "" ,
115
+ HasValue : true ,
116
+ },
117
+ {
118
+ Value : "test_2" ,
119
+ HasValue : true ,
120
+ },
121
+ },
122
+ Points : []* metricspb.Point {
123
+ {
124
+ Timestamp : endTimestamp ,
125
+ Value : & metricspb.Point_Int64Value {Int64Value : int64 (1 )},
126
+ },
127
+ },
128
+ },
129
+ },
130
+ })
131
+
132
+ var wantTimeSeries []* monitoringpb.CreateTimeSeriesRequest
133
+ wantTimeSeries = append (wantTimeSeries , & monitoringpb.CreateTimeSeriesRequest {
134
+ Name : "projects/equivalence" ,
135
+ TimeSeries : []* monitoringpb.TimeSeries {
136
+ {
137
+ Metric : & googlemetricpb.Metric {
138
+ Type : "custom.googleapis.com/opencensus/ocagent.io/calls" ,
139
+ Labels : map [string ]string {
140
+ "empty_key" : "" ,
141
+ "operation_type" : "test_1" ,
142
+ },
143
+ },
144
+ Resource : & monitoredrespb.MonitoredResource {
145
+ Type : "global" ,
146
+ },
147
+ MetricKind : googlemetricpb .MetricDescriptor_CUMULATIVE ,
148
+ ValueType : googlemetricpb .MetricDescriptor_INT64 ,
149
+ Points : []* monitoringpb.Point {
150
+ {
151
+ Interval : & monitoringpb.TimeInterval {
152
+ StartTime : startTimestamp ,
153
+ EndTime : endTimestamp ,
154
+ },
155
+ Value : & monitoringpb.TypedValue {
156
+ Value : & monitoringpb.TypedValue_Int64Value {
157
+ Int64Value : 1 ,
158
+ },
159
+ },
160
+ },
161
+ },
162
+ },
163
+ {
164
+ Metric : & googlemetricpb.Metric {
165
+ Type : "custom.googleapis.com/opencensus/ocagent.io/calls" ,
166
+ Labels : map [string ]string {
167
+ "empty_key" : "" ,
168
+ "operation_type" : "test_2" ,
169
+ },
170
+ },
171
+ Resource : & monitoredrespb.MonitoredResource {
172
+ Type : "global" ,
173
+ },
174
+ MetricKind : googlemetricpb .MetricDescriptor_CUMULATIVE ,
175
+ ValueType : googlemetricpb .MetricDescriptor_INT64 ,
176
+ Points : []* monitoringpb.Point {
177
+ {
178
+ Interval : & monitoringpb.TimeInterval {
179
+ StartTime : startTimestamp ,
180
+ EndTime : endTimestamp ,
181
+ },
182
+ Value : & monitoringpb.TypedValue {
183
+ Value : & monitoringpb.TypedValue_Int64Value {
184
+ Int64Value : 1 ,
185
+ },
186
+ },
187
+ },
188
+ },
189
+ },
190
+ },
//<metric:<type:"custom.googleapis.com/opencensus/ocagent.io/calls" labels:<key:"opencensus_task" value:"[email protected] " > > resource:<type:"global" > metric_kind:CUMULATIVE value_type:INT64 points:<interval:<end_time:<seconds:1001 > start_time:<seconds:1000 > > value:<int64_value:8 > > > time_series:<metric:<type:"custom.googleapis.com/opencensus/ocagent.io/calls" labels:<key:"opencensus_task" value:"[email protected] " > > resource:<type:"global" > metric_kind:CUMULATIVE value_type:INT64 points:<interval:<end_time:<seconds:1001 > start_time:<seconds:1000 > > value:<int64_value:8 > > > `,
191
+ })
192
+
193
+ // Export the proto Metrics to the Stackdriver backend.
194
+ se .PushMetricsProto (context .Background (), nil , nil , metricPbs )
195
+ se .Flush ()
196
+
197
+ var gotTimeSeries []* monitoringpb.CreateTimeSeriesRequest
198
+ server .forEachStackdriverTimeSeries (func (sdt * monitoringpb.CreateTimeSeriesRequest ) {
199
+ gotTimeSeries = append (gotTimeSeries , sdt )
200
+ })
201
+
202
+ requireTimeSeriesRequestEqual (t , gotTimeSeries , wantTimeSeries )
203
+ }
204
+
37
205
func TestProtoMetricToCreateTimeSeriesRequest (t * testing.T ) {
38
206
startTimestamp := & timestamp.Timestamp {
39
207
Seconds : 1543160298 ,
0 commit comments