-
Notifications
You must be signed in to change notification settings - Fork 98
/
testcase.go
541 lines (488 loc) · 17.8 KB
/
testcase.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package testcases
import (
"bytes"
"encoding/json"
"fmt"
"os"
"sort"
"testing"
"time"
"cloud.google.com/go/monitoring/apiv3/v2/monitoringpb"
"go.opentelemetry.io/collector/pdata/ptrace"
distributionpb "google.golang.org/genproto/googleapis/api/distribution"
googlemetricpb "google.golang.org/genproto/googleapis/api/metric"
"github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/collector/integrationtest/protos"
"github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/metric"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/collector/pdata/pmetric"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/collector"
"github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/collector/internal/logsutil"
)
var (
// selfObsMetricsToNormalize is the set of self-observability metrics which may not record
// the same value every time due to side effects. The values of these metrics get cleared
// and are not checked in the fixture. Their labels and types are still checked.
selfObsMetricsToNormalize = map[string]struct{}{
"custom.googleapis.com/opencensus/grpc.io/client/roundtrip_latency": {},
"custom.googleapis.com/opencensus/grpc.io/client/sent_bytes_per_rpc": {},
"custom.googleapis.com/opencensus/grpc.io/client/received_bytes_per_rpc": {},
}
)
const SecondProjectEnv = "SECOND_PROJECT_ID"
type TestCase struct {
// ConfigureCollector will be called to modify the default configuration for this test case. Optional.
ConfigureCollector func(cfg *collector.Config)
// ConfigureLogsExporter uses internal types to add extra post-init config to an exporter object.
ConfigureLogsExporter *logsutil.ExporterConfig
// Name of the test case
Name string
// OTLPInputFixturePath is the path to the JSON encoded OTLP
// ExportMetricsServiceRequest input metrics fixture.
OTLPInputFixturePath string
// ExpectFixturePath is the path to the JSON encoded MetricExpectFixture
// (see fixtures.proto) that contains request messages the exporter is expected to send.
ExpectFixturePath string
// CompareFixturePath is a second output fixture that should be equal to this test's output fixture.
// Used for cross-referencing multiple tests that should have the same output, without overwriting the same fixtures.
CompareFixturePath string
// When testing the SDK metrics exporter (not collector), this is the options to use. Optional.
MetricSDKExporterOptions []metric.Option
// Skip, if true, skips this test case
Skip bool
// SkipForSDK, if true, skips this test case when testing the SDK
SkipForSDK bool
// ExpectErr sets whether the test is expected to fail
ExpectErr bool
// ExpectRetries sets whether the test expects the server to report multiple attempts
ExpectRetries bool
}
func (tc *TestCase) LoadOTLPTracesInput(
t testing.TB,
startTimestamp time.Time,
endTimestamp time.Time,
) ptrace.Traces {
fixtureBytes, err := os.ReadFile(tc.OTLPInputFixturePath)
require.NoError(t, err)
unmarshaler := &ptrace.JSONUnmarshaler{}
traces, err := unmarshaler.UnmarshalTraces(fixtureBytes)
require.NoError(t, err)
for i := 0; i < traces.ResourceSpans().Len(); i++ {
rs := traces.ResourceSpans().At(i)
for j := 0; j < rs.ScopeSpans().Len(); j++ {
sss := rs.ScopeSpans().At(j)
for k := 0; k < sss.Spans().Len(); k++ {
span := sss.Spans().At(k)
if span.StartTimestamp() != 0 {
span.SetStartTimestamp(pcommon.NewTimestampFromTime(startTimestamp))
}
if span.EndTimestamp() != 0 {
span.SetEndTimestamp(pcommon.NewTimestampFromTime(endTimestamp))
}
}
}
}
return traces
}
func (tc *TestCase) LoadTraceExpectFixture(
t testing.TB,
startTimestamp time.Time,
endTimestamp time.Time,
) *protos.TraceExpectFixture {
fixtureBytes, err := os.ReadFile(tc.ExpectFixturePath)
require.NoError(t, err)
fixture := &protos.TraceExpectFixture{}
require.NoError(t, protojson.Unmarshal(fixtureBytes, fixture))
for _, request := range fixture.BatchWriteSpansRequest {
for _, span := range request.Spans {
span.StartTime = timestamppb.New(startTimestamp)
span.EndTime = timestamppb.New(endTimestamp)
}
}
return fixture
}
func (tc *TestCase) SaveRecordedTraceFixtures(
t testing.TB,
fixture *protos.TraceExpectFixture,
) {
NormalizeTraceFixture(t, fixture)
jsonBytes, err := protojson.Marshal(fixture)
require.NoError(t, err)
formatted := bytes.Buffer{}
require.NoError(t, json.Indent(&formatted, jsonBytes, "", " "))
_, err = formatted.WriteString("\n")
require.NoError(t, err)
require.NoError(t, os.WriteFile(tc.ExpectFixturePath, formatted.Bytes(), 0640))
t.Logf("Updated fixture %v", tc.ExpectFixturePath)
}
func NormalizeTraceFixture(t testing.TB, fixture *protos.TraceExpectFixture) {
for _, req := range fixture.BatchWriteSpansRequest {
for _, span := range req.Spans {
if span.GetStartTime() != nil {
span.StartTime = ×tamppb.Timestamp{}
}
if span.GetEndTime() != nil {
span.EndTime = ×tamppb.Timestamp{}
}
}
}
}
func (tc *TestCase) CreateTraceConfig() collector.Config {
cfg := collector.DefaultConfig()
cfg.ProjectID = "fake-project"
if tc.ConfigureCollector != nil {
tc.ConfigureCollector(&cfg)
}
return cfg
}
func (tc *TestCase) LoadOTLPLogsInput(
t testing.TB,
timestamp time.Time,
) plog.Logs {
fixtureBytes, err := os.ReadFile(tc.OTLPInputFixturePath)
require.NoError(t, err)
unmarshaler := &plog.JSONUnmarshaler{}
logs, err := unmarshaler.UnmarshalLogs(fixtureBytes)
require.NoError(t, err)
for i := 0; i < logs.ResourceLogs().Len(); i++ {
rl := logs.ResourceLogs().At(i)
for j := 0; j < rl.ScopeLogs().Len(); j++ {
sls := rl.ScopeLogs().At(j)
for k := 0; k < sls.LogRecords().Len(); k++ {
log := sls.LogRecords().At(k)
if log.Timestamp() != 0 {
log.SetTimestamp(pcommon.NewTimestampFromTime(timestamp))
}
}
}
}
return logs
}
func (tc *TestCase) CreateLogConfig() collector.Config {
cfg := collector.DefaultConfig()
cfg.ProjectID = "fake-project"
if tc.ConfigureCollector != nil {
tc.ConfigureCollector(&cfg)
}
return cfg
}
func (tc *TestCase) LoadLogExpectFixture(
t testing.TB,
timestamp time.Time,
) *protos.LogExpectFixture {
fixtureBytes, err := os.ReadFile(tc.ExpectFixturePath)
require.NoError(t, err)
fixture := &protos.LogExpectFixture{}
require.NoError(t, protojson.Unmarshal(fixtureBytes, fixture))
for _, request := range fixture.WriteLogEntriesRequests {
for _, entry := range request.Entries {
entry.Timestamp = timestamppb.New(timestamp)
}
}
return fixture
}
func (tc *TestCase) SaveRecordedLogFixtures(
t testing.TB,
fixture *protos.LogExpectFixture,
) {
NormalizeLogFixture(t, fixture)
jsonBytes, err := protojson.Marshal(fixture)
require.NoError(t, err)
formatted := bytes.Buffer{}
require.NoError(t, json.Indent(&formatted, jsonBytes, "", " "))
_, err = formatted.WriteString("\n")
require.NoError(t, err)
require.NoError(t, os.WriteFile(tc.ExpectFixturePath, formatted.Bytes(), 0640))
t.Logf("Updated fixture %v", tc.ExpectFixturePath)
}
// Normalizes timestamps which create noise in the fixture because they can
// vary each test run.
func NormalizeLogFixture(t testing.TB, fixture *protos.LogExpectFixture) {
for listIndex, req := range fixture.WriteLogEntriesRequests {
// sort the entries in each request
sort.Slice(fixture.WriteLogEntriesRequests[listIndex].Entries, func(i, j int) bool {
if fixture.WriteLogEntriesRequests[listIndex].Entries[i].LogName < fixture.WriteLogEntriesRequests[listIndex].Entries[j].LogName {
return fixture.WriteLogEntriesRequests[listIndex].Entries[i].LogName < fixture.WriteLogEntriesRequests[listIndex].Entries[j].LogName
}
return fixture.WriteLogEntriesRequests[listIndex].Entries[i].String() < fixture.WriteLogEntriesRequests[listIndex].Entries[j].String()
})
for _, entry := range req.Entries {
// Normalize timestamps if they are set
if entry.GetTimestamp() != nil {
entry.Timestamp = ×tamppb.Timestamp{}
}
}
}
// sort each request. if the requests have the same name (or just as likely, they both have no name set at the request level),
// peek at the first entry's logname in the request
sort.Slice(fixture.WriteLogEntriesRequests, func(i, j int) bool {
if fixture.WriteLogEntriesRequests[i].LogName != fixture.WriteLogEntriesRequests[j].LogName {
return fixture.WriteLogEntriesRequests[i].LogName < fixture.WriteLogEntriesRequests[j].LogName
}
return fixture.WriteLogEntriesRequests[i].Entries[0].LogName < fixture.WriteLogEntriesRequests[j].Entries[0].LogName
})
}
// Load OTLP metric fixture, test expectation fixtures and modify them so they're suitable for
// testing. Currently, this just updates the timestamps.
//
//nolint:revive
func (tc *TestCase) LoadOTLPMetricsInput(
t testing.TB,
startTime time.Time,
endTime time.Time,
) pmetric.Metrics {
fixtureBytes, err := os.ReadFile(tc.OTLPInputFixturePath)
require.NoError(t, err)
unmarshaler := &pmetric.JSONUnmarshaler{}
metrics, err := unmarshaler.UnmarshalMetrics(fixtureBytes)
require.NoError(t, err)
// Interface with common fields that pdata metric points have
type point interface {
StartTimestamp() pcommon.Timestamp
Timestamp() pcommon.Timestamp
SetStartTimestamp(pcommon.Timestamp)
SetTimestamp(pcommon.Timestamp)
}
type pointWithExemplars interface {
point
Exemplars() pmetric.ExemplarSlice
}
updatePoint := func(p point) {
if p.StartTimestamp() != 0 {
p.SetStartTimestamp(pcommon.NewTimestampFromTime(startTime))
}
if p.Timestamp() != 0 {
p.SetTimestamp(pcommon.NewTimestampFromTime(endTime))
}
}
updatePointWithExemplars := func(p pointWithExemplars) {
updatePoint(p)
for i := 0; i < p.Exemplars().Len(); i++ {
p.Exemplars().At(i).SetTimestamp(pcommon.NewTimestampFromTime(endTime))
}
}
for i := 0; i < metrics.ResourceMetrics().Len(); i++ {
rm := metrics.ResourceMetrics().At(i)
for i := 0; i < rm.ScopeMetrics().Len(); i++ {
sms := rm.ScopeMetrics().At(i)
for i := 0; i < sms.Metrics().Len(); i++ {
m := sms.Metrics().At(i)
switch m.Type() {
case pmetric.MetricTypeGauge:
for i := 0; i < m.Gauge().DataPoints().Len(); i++ {
updatePointWithExemplars(m.Gauge().DataPoints().At(i))
}
case pmetric.MetricTypeSum:
for i := 0; i < m.Sum().DataPoints().Len(); i++ {
updatePointWithExemplars(m.Sum().DataPoints().At(i))
}
case pmetric.MetricTypeHistogram:
for i := 0; i < m.Histogram().DataPoints().Len(); i++ {
updatePointWithExemplars(m.Histogram().DataPoints().At(i))
}
case pmetric.MetricTypeSummary:
for i := 0; i < m.Summary().DataPoints().Len(); i++ {
updatePoint(m.Summary().DataPoints().At(i))
}
case pmetric.MetricTypeExponentialHistogram:
for i := 0; i < m.ExponentialHistogram().DataPoints().Len(); i++ {
updatePointWithExemplars(m.ExponentialHistogram().DataPoints().At(i))
}
}
}
}
}
return metrics
}
func (tc *TestCase) LoadMetricFixture(
t testing.TB,
path string,
startTime time.Time,
endTime time.Time,
) *protos.MetricExpectFixture {
fixtureBytes, err := os.ReadFile(path)
require.NoError(t, err)
fixture := &protos.MetricExpectFixture{}
require.NoError(t, protojson.Unmarshal(fixtureBytes, fixture))
tc.updateMetricExpectFixture(t, startTime, endTime, fixture)
return fixture
}
func (tc *TestCase) updateMetricExpectFixture(
t testing.TB,
startTime time.Time,
endTime time.Time,
fixture *protos.MetricExpectFixture,
) {
reqs := append(
fixture.GetCreateTimeSeriesRequests(),
fixture.GetCreateServiceTimeSeriesRequests()...,
)
for _, req := range reqs {
for _, ts := range req.GetTimeSeries() {
for _, p := range ts.GetPoints() {
if p.GetInterval().GetStartTime() != nil {
p.GetInterval().StartTime = timestamppb.New(startTime)
}
if p.GetInterval().GetEndTime() != nil {
p.GetInterval().EndTime = timestamppb.New(endTime)
}
if ts.GetValueType() == googlemetricpb.MetricDescriptor_DISTRIBUTION {
for _, ex := range p.GetValue().GetDistributionValue().GetExemplars() {
ex.Timestamp = timestamppb.New(endTime)
}
}
}
}
}
}
func (tc *TestCase) SaveRecordedMetricFixtures(
t testing.TB,
fixture *protos.MetricExpectFixture,
) {
NormalizeMetricFixture(t, fixture)
jsonBytes, err := protojson.Marshal(fixture)
require.NoError(t, err)
formatted := bytes.Buffer{}
require.NoError(t, json.Indent(&formatted, jsonBytes, "", " "))
_, err = formatted.WriteString("\n")
require.NoError(t, err)
require.NoError(t, os.WriteFile(tc.ExpectFixturePath, formatted.Bytes(), 0640))
t.Logf("Updated fixture %v", tc.ExpectFixturePath)
}
// Normalizes timestamps which create noise in the fixture because they can
// vary each test run.
func NormalizeMetricFixture(t testing.TB, fixture *protos.MetricExpectFixture) {
normalizeTimeSeriesReqs(t, fixture.CreateTimeSeriesRequests...)
normalizeTimeSeriesReqs(t, fixture.CreateServiceTimeSeriesRequests...)
normalizeMetricDescriptorReqs(t, fixture.CreateMetricDescriptorRequests...)
normalizeSelfObs(t, fixture.SelfObservabilityMetrics)
sort.Slice(fixture.CreateTimeSeriesRequests, func(i, j int) bool {
return fixture.CreateTimeSeriesRequests[i].Name < fixture.CreateTimeSeriesRequests[j].Name
})
sort.Slice(fixture.CreateMetricDescriptorRequests, func(i, j int) bool {
if fixture.CreateMetricDescriptorRequests[i].Name != fixture.CreateMetricDescriptorRequests[j].Name {
return fixture.CreateMetricDescriptorRequests[i].Name < fixture.CreateMetricDescriptorRequests[j].Name
}
return fixture.CreateMetricDescriptorRequests[i].MetricDescriptor.Name < fixture.CreateMetricDescriptorRequests[j].MetricDescriptor.Name
})
sort.Slice(fixture.CreateServiceTimeSeriesRequests, func(i, j int) bool {
return fixture.CreateServiceTimeSeriesRequests[i].Name < fixture.CreateServiceTimeSeriesRequests[j].Name
})
}
func normalizeTimeSeriesReqs(t testing.TB, reqs ...*monitoringpb.CreateTimeSeriesRequest) {
for _, req := range reqs {
for _, ts := range req.TimeSeries {
for _, p := range ts.Points {
// Normalize timestamps if they are set
if p.GetInterval().GetStartTime() != nil {
p.GetInterval().StartTime = ×tamppb.Timestamp{}
}
if p.GetInterval().GetEndTime() != nil {
p.GetInterval().EndTime = ×tamppb.Timestamp{}
}
if ts.GetValueType() == googlemetricpb.MetricDescriptor_DISTRIBUTION {
for _, ex := range p.GetValue().GetDistributionValue().GetExemplars() {
ex.Timestamp = ×tamppb.Timestamp{}
}
}
}
// clear project ID from monitored resource
delete(ts.GetResource().GetLabels(), "project_id")
}
}
}
func normalizeMetricDescriptorReqs(t testing.TB, reqs ...*monitoringpb.CreateMetricDescriptorRequest) {
for _, req := range reqs {
if req.MetricDescriptor == nil {
continue
}
md := req.MetricDescriptor
sort.Slice(md.Labels, func(i, j int) bool {
return md.Labels[i].Key < md.Labels[j].Key
})
}
}
func normalizeSelfObs(t testing.TB, selfObs *protos.SelfObservabilityMetric) {
if selfObs == nil {
return
}
for _, req := range selfObs.CreateTimeSeriesRequests {
normalizeTimeSeriesReqs(t, req)
tss := req.TimeSeries
for _, ts := range tss {
if _, ok := selfObsMetricsToNormalize[ts.Metric.Type]; ok {
// zero out the specific value type
switch value := ts.Points[0].Value.Value.(type) {
case *monitoringpb.TypedValue_Int64Value:
value.Int64Value = 0
case *monitoringpb.TypedValue_DoubleValue:
value.DoubleValue = 0
case *monitoringpb.TypedValue_DistributionValue:
// Only preserve the bucket options and zeroed out counts
for i := range value.DistributionValue.BucketCounts {
value.DistributionValue.BucketCounts[i] = 0
}
value.DistributionValue = &distributionpb.Distribution{
BucketOptions: value.DistributionValue.BucketOptions,
BucketCounts: value.DistributionValue.BucketCounts,
}
default:
t.Logf("Do not know how to normalize typed value type %T", value)
}
}
}
// sort time series by (type, labelset)
sort.Slice(tss, func(i, j int) bool {
iMetric := tss[i].Metric
jMetric := tss[j].Metric
if iMetric.Type == jMetric.Type {
// Doesn't need to sorted correctly, just consistently
return fmt.Sprint(iMetric.Labels) < fmt.Sprint(jMetric.Labels)
}
return iMetric.Type < jMetric.Type
})
}
normalizeMetricDescriptorReqs(t, selfObs.CreateMetricDescriptorRequests...)
// sort descriptors by type
sort.Slice(selfObs.CreateMetricDescriptorRequests, func(i, j int) bool {
return selfObs.CreateMetricDescriptorRequests[i].MetricDescriptor.Type <
selfObs.CreateMetricDescriptorRequests[j].MetricDescriptor.Type
})
}
func (tc *TestCase) SkipIfNeeded(t testing.TB) {
if tc.Skip {
t.Skip("Test case is marked to skip")
}
}
func (tc *TestCase) SkipIfNeededForSDK(t testing.TB) {
if tc.SkipForSDK {
t.Skip("Test case is marked to skip")
}
}
func (tc *TestCase) CreateCollectorMetricConfig() collector.Config {
cfg := collector.DefaultConfig()
cfg.ProjectID = "fakeprojectid"
// Set a big buffer to capture all CMD requests without dropping
cfg.MetricConfig.CreateMetricDescriptorBufferSize = 500
cfg.MetricConfig.InstrumentationLibraryLabels = false
if tc.ConfigureCollector != nil {
tc.ConfigureCollector(&cfg)
}
return cfg
}