@@ -44,10 +44,7 @@ func TestWorkers(t *testing.T) {
44
44
}
45
45
m2 := newMetricsBatcher (ctx , "test" , 2 , c2 , defaultTimeout ) // batcher with 2 workers
46
46
47
- tss := make ([]* monitoringpb.TimeSeries , 0 , 500 ) // make 500 time series, should be split to 3 reqs
48
- for i := 0 ; i < 500 ; i ++ {
49
- tss = append (tss , makeTs (i ))
50
- }
47
+ tss := makeTs (500 , false ) // make 500 time series, should be split to 3 reqs
51
48
52
49
for _ , ts := range tss {
53
50
m1 .addTimeSeries (ts )
@@ -89,69 +86,97 @@ func makeClient(addr string) (*monitoring.MetricClient, error) {
89
86
return monitoring .NewMetricClient (context .Background (), option .WithGRPCConn (conn ))
90
87
}
91
88
92
- func makeTs (i int ) * monitoringpb.TimeSeries {
93
- return & monitoringpb.TimeSeries {
94
- Metric : & googlemetricpb.Metric {
95
- Type : fmt .Sprintf ("custom.googleapis.com/opencensus/test/metric/%v" , i ),
96
- Labels : map [string ]string {
97
- "key" : fmt .Sprintf ("test_%v" , i ),
89
+ // makeTs returns a list of n *monitoringpb.TimeSeries. The metric type (service/non-service)
90
+ // is determined by serviceMetric
91
+ func makeTs (n int , serviceMetric bool ) []* monitoringpb.TimeSeries {
92
+ var tsl []* monitoringpb.TimeSeries
93
+ for i := 0 ; i < n ; i ++ {
94
+ metricType := fmt .Sprintf ("custom.googleapis.com/opencensus/test/metric/%v" , i )
95
+ if serviceMetric {
96
+ metricType = fmt .Sprintf ("kubernetes.io/opencensus/test/metric/%v" , i )
97
+ }
98
+ tsl = append (tsl , & monitoringpb.TimeSeries {
99
+ Metric : & googlemetricpb.Metric {
100
+ Type : metricType ,
101
+ Labels : map [string ]string {
102
+ "key" : fmt .Sprintf ("test_%v" , i ),
103
+ },
98
104
},
99
- } ,
100
- MetricKind : googlemetricpb .MetricDescriptor_CUMULATIVE ,
101
- ValueType : googlemetricpb . MetricDescriptor_INT64 ,
102
- Points : [] * monitoringpb. Point {
103
- {
104
- Value : & monitoringpb.TypedValue {
105
- Value : & monitoringpb. TypedValue_Int64Value {
106
- Int64Value : int64 ( i ) ,
105
+ MetricKind : googlemetricpb . MetricDescriptor_CUMULATIVE ,
106
+ ValueType : googlemetricpb .MetricDescriptor_INT64 ,
107
+ Points : [] * monitoringpb. Point {
108
+ {
109
+ Value : & monitoringpb. TypedValue {
110
+ Value : & monitoringpb.TypedValue_Int64Value {
111
+ Int64Value : int64 ( i ),
112
+ } ,
107
113
},
108
114
},
109
115
},
110
- },
116
+ })
111
117
}
118
+ return tsl
112
119
}
113
120
114
121
func TestSendReqAndParseDropped (t * testing.T ) {
115
122
type testCase struct {
116
- name string
117
- timeseriesCount int
118
- createTimeSeriesFunc func (ctx context.Context , c * monitoring.MetricClient , ts * monitoringpb.CreateTimeSeriesRequest ) error
119
- expectedErr bool
120
- expectedDropped int
123
+ name string
124
+ nonServiceTimeSeriesCount int
125
+ serviceTimeSeriesCount int
126
+ createTimeSeriesFunc func (ctx context.Context , c * monitoring.MetricClient , ts * monitoringpb.CreateTimeSeriesRequest ) error
127
+ createServiceTimeSeriesFunc func (ctx context.Context , c * monitoring.MetricClient , ts * monitoringpb.CreateTimeSeriesRequest ) error
128
+ expectedErr bool
129
+ expectedDropped int
121
130
}
122
131
123
132
testCases := []testCase {
124
133
{
125
- name : "No error" ,
126
- timeseriesCount : 75 ,
134
+ name : "No error" ,
135
+ serviceTimeSeriesCount : 75 ,
136
+ nonServiceTimeSeriesCount : 75 ,
127
137
createTimeSeriesFunc : func (ctx context.Context , c * monitoring.MetricClient , ts * monitoringpb.CreateTimeSeriesRequest ) error {
128
138
return nil
129
139
},
140
+ createServiceTimeSeriesFunc : func (ctx context.Context , c * monitoring.MetricClient , ts * monitoringpb.CreateTimeSeriesRequest ) error {
141
+ return nil
142
+ },
130
143
expectedErr : false ,
131
144
expectedDropped : 0 ,
132
145
},
133
146
{
134
- name : "Partial error" ,
135
- timeseriesCount : 75 ,
147
+ name : "Partial error" ,
148
+ serviceTimeSeriesCount : 75 ,
149
+ nonServiceTimeSeriesCount : 75 ,
136
150
createTimeSeriesFunc : func (ctx context.Context , c * monitoring.MetricClient , ts * monitoringpb.CreateTimeSeriesRequest ) error {
137
151
return errors .New ("One or more TimeSeries could not be written: Internal error encountered. Please retry after a few seconds. If internal errors persist, contact support at https://cloud.google.com/support/docs.: timeSeries[0-16,25-44,46-74]; Unknown metric: agent.googleapis.com/system.swap.page_faults: timeSeries[45]" )
138
152
},
153
+ createServiceTimeSeriesFunc : func (ctx context.Context , c * monitoring.MetricClient , ts * monitoringpb.CreateTimeSeriesRequest ) error {
154
+ return errors .New ("One or more TimeSeries could not be written: Internal error encountered. Please retry after a few seconds. If internal errors persist, contact support at https://cloud.google.com/support/docs.: timeSeries[0-16,25-44,46-74]; Unknown metric: agent.googleapis.com/system.swap.page_faults: timeSeries[45]" )
155
+ },
139
156
expectedErr : true ,
140
- expectedDropped : 67 ,
157
+ expectedDropped : 67 * 2 ,
141
158
},
142
159
{
143
- name : "Incorrectly formatted error" ,
144
- timeseriesCount : 75 ,
160
+ name : "Incorrectly formatted error" ,
161
+ nonServiceTimeSeriesCount : 75 ,
162
+ serviceTimeSeriesCount : 75 ,
145
163
createTimeSeriesFunc : func (ctx context.Context , c * monitoring.MetricClient , ts * monitoringpb.CreateTimeSeriesRequest ) error {
146
164
return errors .New ("One or more TimeSeries could not be written: Internal error encountered. Please retry after a few seconds. If internal errors persist, contact support at https://cloud.google.com/support/docs.: timeSeries[0-16,25-44,,46-74]; Unknown metric: agent.googleapis.com/system.swap.page_faults: timeSeries[45x]" )
147
165
},
166
+ createServiceTimeSeriesFunc : func (ctx context.Context , c * monitoring.MetricClient , ts * monitoringpb.CreateTimeSeriesRequest ) error {
167
+ return nil
168
+ },
148
169
expectedErr : true ,
149
170
expectedDropped : 75 ,
150
171
},
151
172
{
152
- name : "Unexpected error format" ,
153
- timeseriesCount : 75 ,
173
+ name : "Unexpected error format" ,
174
+ nonServiceTimeSeriesCount : 75 ,
175
+ serviceTimeSeriesCount : 75 ,
154
176
createTimeSeriesFunc : func (ctx context.Context , c * monitoring.MetricClient , ts * monitoringpb.CreateTimeSeriesRequest ) error {
177
+ return nil
178
+ },
179
+ createServiceTimeSeriesFunc : func (ctx context.Context , c * monitoring.MetricClient , ts * monitoringpb.CreateTimeSeriesRequest ) error {
155
180
return errors .New ("err1" )
156
181
},
157
182
expectedErr : true ,
@@ -162,21 +187,28 @@ func TestSendReqAndParseDropped(t *testing.T) {
162
187
for _ , test := range testCases {
163
188
t .Run (test .name , func (t * testing.T ) {
164
189
persistedCreateTimeSeries := createTimeSeries
190
+ persistedCreateServiceTimeSeries := createServiceTimeSeries
165
191
createTimeSeries = test .createTimeSeriesFunc
192
+ createServiceTimeSeries = test .createServiceTimeSeriesFunc
193
+ defer func () {
194
+ createTimeSeries = persistedCreateTimeSeries
195
+ createServiceTimeSeries = persistedCreateServiceTimeSeries
196
+ }()
166
197
167
198
mc , _ := monitoring .NewMetricClient (context .Background ())
168
- d , err := sendReq (context .Background (), mc , & monitoringpb.CreateTimeSeriesRequest {TimeSeries : make ([]* monitoringpb.TimeSeries , test .timeseriesCount )})
169
- if ! test .expectedErr && err != nil {
170
- t .Fatal ("Expected no err" )
199
+ var tsl []* monitoringpb.TimeSeries
200
+ tsl = append (tsl , makeTs (test .serviceTimeSeriesCount , true )... )
201
+ tsl = append (tsl , makeTs (test .nonServiceTimeSeriesCount , false )... )
202
+ d , errors := sendReq (context .Background (), mc , & monitoringpb.CreateTimeSeriesRequest {TimeSeries : tsl })
203
+ if ! test .expectedErr && len (errors ) > 0 {
204
+ t .Fatalf ("Expected no errors, got %v" , errors )
171
205
}
172
- if test .expectedErr && err == nil {
173
- t .Fatal ("Expected noerr" )
206
+ if test .expectedErr && len ( errors ) == 0 {
207
+ t .Fatalf ("Expected errors, got %v" , errors )
174
208
}
175
209
if d != test .expectedDropped {
176
210
t .Fatalf ("Want %v dropped, got %v" , test .expectedDropped , d )
177
211
}
178
-
179
- createTimeSeries = persistedCreateTimeSeries
180
212
})
181
213
}
182
214
}
0 commit comments