forked from m3db/m3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
integration_data.go
990 lines (909 loc) · 27.9 KB
/
integration_data.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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
// Copyright (c) 2016 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package integration
import (
"errors"
"fmt"
"sort"
"testing"
"time"
"github.com/m3db/m3/src/aggregator/aggregation"
"github.com/m3db/m3/src/aggregator/aggregator"
maggregation "github.com/m3db/m3/src/metrics/aggregation"
"github.com/m3db/m3/src/metrics/metadata"
"github.com/m3db/m3/src/metrics/metric"
"github.com/m3db/m3/src/metrics/metric/aggregated"
metricid "github.com/m3db/m3/src/metrics/metric/id"
"github.com/m3db/m3/src/metrics/metric/unaggregated"
"github.com/m3db/m3/src/metrics/pipeline/applied"
"github.com/m3db/m3/src/metrics/policy"
xtime "github.com/m3db/m3x/time"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/stretchr/testify/require"
)
var (
testPoliciesList = policy.PoliciesList{
policy.NewStagedPolicies(
0,
false,
[]policy.Policy{
policy.NewPolicy(policy.NewStoragePolicy(time.Second, xtime.Second, time.Hour), maggregation.DefaultID),
policy.NewPolicy(policy.NewStoragePolicy(2*time.Second, xtime.Second, 6*time.Hour), maggregation.DefaultID),
},
),
}
testUpdatedPoliciesList = policy.PoliciesList{
policy.NewStagedPolicies(
0,
false,
[]policy.Policy{
policy.NewPolicy(policy.NewStoragePolicy(time.Second, xtime.Second, time.Hour), maggregation.DefaultID),
policy.NewPolicy(policy.NewStoragePolicy(3*time.Second, xtime.Second, 24*time.Hour), maggregation.DefaultID),
},
),
}
testPoliciesListWithCustomAggregation1 = policy.PoliciesList{
policy.NewStagedPolicies(
0,
false,
[]policy.Policy{
policy.NewPolicy(policy.NewStoragePolicy(time.Second, xtime.Second, time.Hour), maggregation.MustCompressTypes(maggregation.Min)),
policy.NewPolicy(policy.NewStoragePolicy(2*time.Second, xtime.Second, 6*time.Hour), maggregation.MustCompressTypes(maggregation.Min)),
},
),
}
testPoliciesListWithCustomAggregation2 = policy.PoliciesList{
policy.NewStagedPolicies(
0,
false,
[]policy.Policy{
policy.NewPolicy(policy.NewStoragePolicy(time.Second, xtime.Second, time.Hour), maggregation.MustCompressTypes(maggregation.Min, maggregation.Max)),
policy.NewPolicy(policy.NewStoragePolicy(3*time.Second, xtime.Second, 24*time.Hour), maggregation.MustCompressTypes(maggregation.Min, maggregation.Max)),
},
),
}
testStagedMetadatas = metadata.StagedMetadatas{
{
CutoverNanos: 0,
Tombstoned: false,
Metadata: metadata.Metadata{
Pipelines: []metadata.PipelineMetadata{
{
AggregationID: maggregation.DefaultID,
StoragePolicies: []policy.StoragePolicy{
policy.NewStoragePolicy(time.Second, xtime.Second, time.Hour),
policy.NewStoragePolicy(2*time.Second, xtime.Second, 6*time.Hour),
},
},
{
AggregationID: maggregation.MustCompressTypes(maggregation.Sum),
StoragePolicies: []policy.StoragePolicy{
policy.NewStoragePolicy(time.Second, xtime.Second, 2*time.Hour),
},
},
},
},
},
}
testStagedMetadatasWithCustomAggregation1 = metadata.StagedMetadatas{
{
CutoverNanos: 0,
Tombstoned: false,
Metadata: metadata.Metadata{
Pipelines: []metadata.PipelineMetadata{
{
AggregationID: maggregation.MustCompressTypes(maggregation.Min),
StoragePolicies: []policy.StoragePolicy{
policy.NewStoragePolicy(time.Second, xtime.Second, time.Hour),
policy.NewStoragePolicy(2*time.Second, xtime.Second, 6*time.Hour),
},
},
},
},
},
}
testStagedMetadatasWithCustomAggregation2 = metadata.StagedMetadatas{
{
CutoverNanos: 0,
Tombstoned: false,
Metadata: metadata.Metadata{
Pipelines: []metadata.PipelineMetadata{
{
AggregationID: maggregation.MustCompressTypes(maggregation.Min, maggregation.Max),
StoragePolicies: []policy.StoragePolicy{
policy.NewStoragePolicy(time.Second, xtime.Second, time.Hour),
policy.NewStoragePolicy(3*time.Second, xtime.Second, 24*time.Hour),
},
},
},
},
},
}
testUpdatedStagedMetadatas = metadata.StagedMetadatas{
{
CutoverNanos: 0,
Tombstoned: false,
Metadata: metadata.Metadata{
Pipelines: []metadata.PipelineMetadata{
{
AggregationID: maggregation.MustCompressTypes(maggregation.Mean),
StoragePolicies: []policy.StoragePolicy{
policy.NewStoragePolicy(time.Second, xtime.Second, time.Hour),
policy.NewStoragePolicy(3*time.Second, xtime.Second, 6*time.Hour),
},
},
{
AggregationID: maggregation.DefaultID,
StoragePolicies: []policy.StoragePolicy{
policy.NewStoragePolicy(2*time.Second, xtime.Second, 2*time.Hour),
},
},
},
},
},
}
testCmpOpts = []cmp.Option{
cmpopts.EquateEmpty(),
cmpopts.EquateNaNs(),
cmp.AllowUnexported(policy.StoragePolicy{}),
}
)
func generateTestIDs(prefix string, numIDs int) []string {
ids := make([]string, numIDs)
for i := 0; i < numIDs; i++ {
ids[i] = fmt.Sprintf("%s%d", prefix, i)
}
return ids
}
func mustGenerateTestDataset(t *testing.T, opts datasetGenOpts) testDataset {
ds, err := generateTestDataset(opts)
require.NoError(t, err)
return ds
}
func generateTestDataset(opts datasetGenOpts) (testDataset, error) {
var (
testDataset []testData
intervalIdx int
)
for timestamp := opts.start; timestamp.Before(opts.stop); timestamp = timestamp.Add(opts.interval) {
metricWithMetadatas := make([]metricWithMetadataUnion, 0, len(opts.ids))
for i := 0; i < len(opts.ids); i++ {
var (
metricType = opts.typeFn(timestamp, i)
metadata = opts.metadataFn(i)
mu metricUnion
)
switch opts.category {
case untimedMetric:
var err error
mu, err = generateTestUntimedMetric(metricType, opts.ids[i], intervalIdx, i, opts.valueGenOpts.untimed)
if err != nil {
return nil, err
}
case forwardedMetric:
mu = generateTestForwardedMetric(metricType, opts.ids[i], timestamp.UnixNano(), intervalIdx, i, opts.valueGenOpts.forwarded)
case timedMetric:
mu = generateTestTimedMetric(metricType, opts.ids[i], timestamp.UnixNano(), intervalIdx, i, opts.valueGenOpts.timed)
default:
return nil, fmt.Errorf("unrecognized metric category: %v", opts.category)
}
metricWithMetadatas = append(metricWithMetadatas, metricWithMetadataUnion{
metric: mu,
metadata: metadata,
})
}
testDataset = append(testDataset, testData{
timestamp: timestamp,
metricWithMetadatas: metricWithMetadatas,
})
intervalIdx++
}
return testDataset, nil
}
func generateTestUntimedMetric(
metricType metric.Type,
id string,
intervalIdx, idIdx int,
valueGenOpts untimedValueGenOpts,
) (metricUnion, error) {
mu := metricUnion{category: untimedMetric}
switch metricType {
case metric.CounterType:
mu.untimed = unaggregated.MetricUnion{
Type: metricType,
ID: metricid.RawID(id),
CounterVal: valueGenOpts.counterValueGenFn(intervalIdx, idIdx),
}
case metric.TimerType:
mu.untimed = unaggregated.MetricUnion{
Type: metricType,
ID: metricid.RawID(id),
BatchTimerVal: valueGenOpts.timerValueGenFn(intervalIdx, idIdx),
}
case metric.GaugeType:
mu.untimed = unaggregated.MetricUnion{
Type: metricType,
ID: metricid.RawID(id),
GaugeVal: valueGenOpts.gaugeValueGenFn(intervalIdx, idIdx),
}
default:
return metricUnion{}, fmt.Errorf("unrecognized untimed metric type: %v", metricType)
}
return mu, nil
}
func generateTestTimedMetric(
metricType metric.Type,
id string,
timeNanos int64,
intervalIdx, idIdx int,
valueGenOpts timedValueGenOpts,
) metricUnion {
return metricUnion{
category: timedMetric,
timed: aggregated.Metric{
Type: metricType,
ID: metricid.RawID(id),
TimeNanos: timeNanos,
Value: valueGenOpts.timedValueGenFn(intervalIdx, idIdx),
},
}
}
func generateTestForwardedMetric(
metricType metric.Type,
id string,
timeNanos int64,
intervalIdx, idIdx int,
valueGenOpts forwardedValueGenOpts,
) metricUnion {
return metricUnion{
category: forwardedMetric,
forwarded: aggregated.ForwardedMetric{
Type: metricType,
ID: metricid.RawID(id),
TimeNanos: timeNanos,
Values: valueGenOpts.forwardedValueGenFn(intervalIdx, idIdx),
},
}
}
func mustComputeExpectedResults(
t *testing.T,
now time.Time,
dataset testDataset,
opts aggregator.Options,
) []aggregated.MetricWithStoragePolicy {
res, err := computeExpectedResults(now, dataset, opts)
require.NoError(t, err)
return res
}
func computeExpectedResults(
now time.Time,
dataset testDataset,
opts aggregator.Options,
) ([]aggregated.MetricWithStoragePolicy, error) {
buckets, err := computeExpectedAggregationBuckets(now, dataset, opts)
if err != nil {
return nil, err
}
return computeExpectedAggregationOutput(now, buckets, opts)
}
// computeExpectedAggregationBuckets computes the expected aggregation buckets for the given
// dataset and the aggregation keys, assuming each metric in the given dataset is associated
// with the full set of aggregation keys passed in.
func computeExpectedAggregationBuckets(
now time.Time,
dataset testDataset,
opts aggregator.Options,
) ([]aggregationBucket, error) {
var (
buckets = make([]aggregationBucket, 0)
defaultStoragePolicies = opts.DefaultStoragePolicies()
)
for _, dataValues := range dataset {
for _, mm := range dataValues.metricWithMetadatas {
keys, err := mm.metadata.expectedAggregationKeys(now, defaultStoragePolicies)
if err != nil {
return nil, err
}
for _, key := range keys {
// Find or create the corresponding bucket.
var bucket *aggregationBucket
for _, b := range buckets {
if b.key.Equal(key) {
bucket = &b
break
}
}
if bucket == nil {
buckets = append(buckets, aggregationBucket{key: key, data: make(datapointsByID)})
bucket = &buckets[len(buckets)-1]
}
// Add metric to the list of metrics aggregated by the aggregation bucket if necessary.
mu := mm.metric
key := metricKey{category: mu.category, typ: mu.Type(), id: string(mu.ID()), storagePolicy: key.storagePolicy}
datapoints, metricExists := bucket.data[key]
if !metricExists {
datapoints = make(valuesByTime)
bucket.data[key] = datapoints
}
// Add metric to the time bucket associated with the aggregation bucket if necessary.
resolution := bucket.key.storagePolicy.Resolution()
alignedStartNanos := dataValues.timestamp.Truncate(resolution.Window).UnixNano()
values, timeBucketExists := datapoints[alignedStartNanos]
if !timeBucketExists {
var (
aggTypeOpts = opts.AggregationTypesOptions()
aggTypes = maggregation.NewIDDecompressor().MustDecompress(bucket.key.aggregationID)
aggregationOpts = aggregation.NewOptions()
)
switch mu.Type() {
case metric.CounterType:
if aggTypes.IsDefault() {
aggTypes = aggTypeOpts.DefaultCounterAggregationTypes()
}
aggregationOpts.ResetSetData(aggTypes)
values = aggregation.NewCounter(aggregationOpts)
case metric.TimerType:
if aggTypes.IsDefault() {
aggTypes = aggTypeOpts.DefaultTimerAggregationTypes()
}
aggregationOpts.ResetSetData(aggTypes)
values = aggregation.NewTimer(aggTypeOpts.Quantiles(), opts.StreamOptions(), aggregationOpts)
case metric.GaugeType:
if aggTypes.IsDefault() {
aggTypes = aggTypeOpts.DefaultGaugeAggregationTypes()
}
aggregationOpts.ResetSetData(aggTypes)
values = aggregation.NewGauge(aggregationOpts)
default:
return nil, fmt.Errorf("unrecognized metric type %v", mu.Type())
}
}
// Add metric value to the corresponding time bucket.
var err error
switch mu.category {
case untimedMetric:
values, err = addUntimedMetricToAggregation(values, mu.untimed)
case forwardedMetric:
values, err = addForwardedMetricToAggregation(values, mu.forwarded)
case timedMetric:
values, err = addTimedMetricToAggregation(values, mu.timed)
default:
err = fmt.Errorf("unrecognized metric category: %v", mu.category)
}
if err != nil {
return nil, err
}
datapoints[alignedStartNanos] = values
}
}
}
return buckets, nil
}
func addUntimedMetricToAggregation(
values interface{},
mu unaggregated.MetricUnion,
) (interface{}, error) {
switch mu.Type {
case metric.CounterType:
v := values.(aggregation.Counter)
v.Update(mu.CounterVal)
return v, nil
case metric.TimerType:
v := values.(aggregation.Timer)
v.AddBatch(mu.BatchTimerVal)
return v, nil
case metric.GaugeType:
v := values.(aggregation.Gauge)
v.Update(mu.GaugeVal)
return v, nil
default:
return nil, fmt.Errorf("unrecognized untimed metric type %v", mu.Type)
}
}
func addTimedMetricToAggregation(
values interface{},
mu aggregated.Metric,
) (interface{}, error) {
switch mu.Type {
case metric.CounterType:
v := values.(aggregation.Counter)
v.Update(int64(mu.Value))
return v, nil
case metric.TimerType:
v := values.(aggregation.Timer)
v.AddBatch([]float64{mu.Value})
return v, nil
case metric.GaugeType:
v := values.(aggregation.Gauge)
v.Update(mu.Value)
return v, nil
default:
return nil, fmt.Errorf("unrecognized timed metric type %v", mu.Type)
}
}
func addForwardedMetricToAggregation(
values interface{},
mu aggregated.ForwardedMetric,
) (interface{}, error) {
switch mu.Type {
case metric.CounterType:
v := values.(aggregation.Counter)
for _, val := range mu.Values {
v.Update(int64(val))
}
return v, nil
case metric.TimerType:
v := values.(aggregation.Timer)
v.AddBatch(mu.Values)
return v, nil
case metric.GaugeType:
v := values.(aggregation.Gauge)
for _, val := range mu.Values {
v.Update(val)
}
return v, nil
default:
return nil, fmt.Errorf("unrecognized forwarded metric type %v", mu.Type)
}
}
// computeExpectedAggregationOutput computes the expected aggregation output given
// the current time and the populated aggregation buckets.
func computeExpectedAggregationOutput(
now time.Time,
buckets []aggregationBucket,
opts aggregator.Options,
) ([]aggregated.MetricWithStoragePolicy, error) {
var expected []aggregated.MetricWithStoragePolicy
for _, bucket := range buckets {
var (
aggregationTypes = maggregation.NewIDDecompressor().MustDecompress(bucket.key.aggregationID)
storagePolicy = bucket.key.storagePolicy
resolutionWindow = storagePolicy.Resolution().Window
alignedCutoffNanos = now.Truncate(resolutionWindow).UnixNano()
)
for key, datapoints := range bucket.data {
timestampNanosFn := key.category.TimestampNanosFn()
for windowStartAtNanos, values := range datapoints {
timestampNanos := timestampNanosFn(windowStartAtNanos, resolutionWindow)
// The end time must be no later than the aligned cutoff time
// for the data to be flushed.
if timestampNanos > alignedCutoffNanos {
continue
}
outputs, err := computeExpectedAggregatedMetrics(
key,
timestampNanos,
values,
storagePolicy,
aggregationTypes,
opts,
)
if err != nil {
return nil, err
}
expected = append(expected, outputs...)
}
}
}
// Sort the aggregated metrics.
sort.Sort(byTimeIDPolicyAscending(expected))
return expected, nil
}
// computeExpectedAggregatedMetrics computes the expected set of aggregated metrics
// given the metric key, timestamp, metric aggregation, and related aggregation metadata.
func computeExpectedAggregatedMetrics(
key metricKey,
timeNanos int64,
metricAgg interface{},
sp policy.StoragePolicy,
aggTypes maggregation.Types,
opts aggregator.Options,
) ([]aggregated.MetricWithStoragePolicy, error) {
var results []aggregated.MetricWithStoragePolicy
fn := func(
prefix []byte,
id string,
suffix []byte,
timeNanos int64,
value float64,
sp policy.StoragePolicy,
) {
results = append(results, aggregated.MetricWithStoragePolicy{
Metric: aggregated.Metric{
ID: metricid.RawID(string(prefix) + id + string(suffix)),
TimeNanos: timeNanos,
Value: value,
},
StoragePolicy: sp,
})
}
id := key.id
aggTypeOpts := opts.AggregationTypesOptions()
switch metricAgg := metricAgg.(type) {
case aggregation.Counter:
if aggTypes.IsDefault() {
aggTypes = aggTypeOpts.DefaultCounterAggregationTypes()
}
for _, aggType := range aggTypes {
if key.category == timedMetric {
fn(nil, id, nil, timeNanos, metricAgg.ValueOf(aggType), sp)
continue
}
fn(opts.FullCounterPrefix(), id, aggTypeOpts.TypeStringForCounter(aggType), timeNanos, metricAgg.ValueOf(aggType), sp)
}
case aggregation.Timer:
if aggTypes.IsDefault() {
aggTypes = aggTypeOpts.DefaultTimerAggregationTypes()
}
for _, aggType := range aggTypes {
if key.category == timedMetric {
fn(nil, id, nil, timeNanos, metricAgg.ValueOf(aggType), sp)
continue
}
fn(opts.FullTimerPrefix(), id, aggTypeOpts.TypeStringForTimer(aggType), timeNanos, metricAgg.ValueOf(aggType), sp)
}
case aggregation.Gauge:
if aggTypes.IsDefault() {
aggTypes = aggTypeOpts.DefaultGaugeAggregationTypes()
}
for _, aggType := range aggTypes {
if key.category == timedMetric {
fn(nil, id, nil, timeNanos, metricAgg.ValueOf(aggType), sp)
continue
}
fn(opts.FullGaugePrefix(), id, aggTypeOpts.TypeStringForGauge(aggType), timeNanos, metricAgg.ValueOf(aggType), sp)
}
default:
return nil, fmt.Errorf("unrecognized aggregation type %T", metricAgg)
}
return results, nil
}
func roundRobinMetricTypeFn(_ time.Time, idx int) metric.Type {
switch idx % 3 {
case 0:
return metric.CounterType
case 1:
return metric.TimerType
default:
return metric.GaugeType
}
}
func constantMetricTypeFnFactory(typ metric.Type) metricTypeFn {
return func(time.Time, int) metric.Type { return typ }
}
type byTimeIDPolicyAscending []aggregated.MetricWithStoragePolicy
func (a byTimeIDPolicyAscending) Len() int { return len(a) }
func (a byTimeIDPolicyAscending) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byTimeIDPolicyAscending) Less(i, j int) bool {
if a[i].TimeNanos != a[j].TimeNanos {
return a[i].TimeNanos < a[j].TimeNanos
}
id1, id2 := string(a[i].ID), string(a[j].ID)
if id1 != id2 {
return id1 < id2
}
resolution1, resolution2 := a[i].Resolution().Window, a[j].Resolution().Window
if resolution1 != resolution2 {
return resolution1 < resolution2
}
retention1, retention2 := a[i].Retention(), a[j].Retention()
return retention1 < retention2
}
type metricTypeFn func(ts time.Time, idx int) metric.Type
type metricKey struct {
category metricCategory
typ metric.Type
id string
storagePolicy policy.StoragePolicy
}
type valuesByTime map[int64]interface{}
type datapointsByID map[metricKey]valuesByTime
type aggregationKey struct {
aggregationID maggregation.ID
storagePolicy policy.StoragePolicy
pipeline applied.Pipeline
}
func (k aggregationKey) Equal(other aggregationKey) bool {
return k.aggregationID == other.aggregationID &&
k.storagePolicy == other.storagePolicy &&
k.pipeline.Equal(other.pipeline)
}
type aggregationKeys []aggregationKey
func (keys *aggregationKeys) add(newKey aggregationKey) {
for _, k := range *keys {
if k.Equal(newKey) {
return
}
}
*keys = append(*keys, newKey)
}
type aggregationBucket struct {
key aggregationKey
data datapointsByID
}
// timestampNanosFn computes the timestamp in nanoseconds of metrics in a given time window.
type timestampNanosFn func(windowStartAtNanos int64, resolution time.Duration) int64
type metricCategory int
const (
untimedMetric metricCategory = iota
forwardedMetric
timedMetric
)
func (c metricCategory) TimestampNanosFn() timestampNanosFn {
switch c {
case untimedMetric:
return func(windowStartAtNanos int64, resolution time.Duration) int64 {
return windowStartAtNanos + resolution.Nanoseconds()
}
case forwardedMetric:
return func(windowStartAtNanos int64, _ time.Duration) int64 {
return windowStartAtNanos
}
case timedMetric:
return func(windowStartAtNanos int64, resolution time.Duration) int64 {
return windowStartAtNanos + resolution.Nanoseconds()
}
default:
panic(fmt.Errorf("unknown category type: %v", c))
}
}
type metricUnion struct {
category metricCategory
untimed unaggregated.MetricUnion
forwarded aggregated.ForwardedMetric
timed aggregated.Metric
}
func (mu metricUnion) Type() metric.Type {
switch mu.category {
case untimedMetric:
return mu.untimed.Type
case forwardedMetric:
return mu.forwarded.Type
case timedMetric:
return mu.timed.Type
default:
panic(fmt.Errorf("unknown category type: %v", mu.category))
}
}
func (mu metricUnion) ID() metricid.RawID {
switch mu.category {
case untimedMetric:
return mu.untimed.ID
case forwardedMetric:
return mu.forwarded.ID
case timedMetric:
return mu.timed.ID
default:
panic(fmt.Errorf("unknown category type: %v", mu.category))
}
}
type metadataType int
const (
policiesListType metadataType = iota
stagedMetadatasType
forwardMetadataType
timedMetadataType
)
type metadataFn func(idx int) metadataUnion
type metadataUnion struct {
mType metadataType
policiesList policy.PoliciesList
stagedMetadatas metadata.StagedMetadatas
forwardMetadata metadata.ForwardMetadata
timedMetadata metadata.TimedMetadata
}
func (mu metadataUnion) expectedAggregationKeys(
now time.Time,
defaultStoragePolicies []policy.StoragePolicy,
) (aggregationKeys, error) {
switch mu.mType {
case policiesListType:
return computeExpectedAggregationKeysFromPoliciesList(now, mu.policiesList, defaultStoragePolicies)
case stagedMetadatasType:
return computeExpectedAggregationKeysFromStagedMetadatas(now, mu.stagedMetadatas, defaultStoragePolicies)
case forwardMetadataType:
return computeExpectedAggregationKeysFromForwardMetadata(mu.forwardMetadata), nil
case timedMetadataType:
return computeExpectedAggregationKeysFromTimedMetadata(mu.timedMetadata), nil
default:
return nil, fmt.Errorf("unexpected metadata type: %v", mu.mType)
}
}
// computeExpectedAggregationKeysFromPoliciesList computes the expected set of aggregation keys
// from the given time and the policies list.
func computeExpectedAggregationKeysFromPoliciesList(
now time.Time,
policiesList policy.PoliciesList,
defaultStoragePolices []policy.StoragePolicy,
) (aggregationKeys, error) {
// Find the staged policy that is currently active.
nowNanos := now.UnixNano()
i := len(policiesList) - 1
for i >= 0 {
if policiesList[i].CutoverNanos <= nowNanos {
break
}
i--
}
if i < 0 {
return nil, errors.New("no active staged policy")
}
// If the active policies are the default policies, create the aggregation keys
// from them.
policies, useDefault := policiesList[i].Policies()
if useDefault {
res := make(aggregationKeys, 0, len(defaultStoragePolices))
for _, sp := range defaultStoragePolices {
key := aggregationKey{storagePolicy: sp}
res = append(res, key)
}
return res, nil
}
// Otherwise create the aggregation keys from the staged policies.
res := make(aggregationKeys, 0, len(policies))
for _, p := range policies {
newKey := aggregationKey{
aggregationID: p.AggregationID,
storagePolicy: p.StoragePolicy,
}
res.add(newKey)
}
return res, nil
}
func computeExpectedAggregationKeysFromStagedMetadatas(
now time.Time,
metadatas metadata.StagedMetadatas,
defaultStoragePolices []policy.StoragePolicy,
) (aggregationKeys, error) {
// Find the staged policy that is currently active.
nowNanos := now.UnixNano()
i := len(metadatas) - 1
for i >= 0 {
if metadatas[i].CutoverNanos <= nowNanos {
break
}
i--
}
if i < 0 {
return nil, errors.New("no active staged metadata")
}
res := make(aggregationKeys, 0, len(metadatas[i].Pipelines))
for _, pipeline := range metadatas[i].Pipelines {
storagePolicies := pipeline.StoragePolicies
if storagePolicies.IsDefault() {
storagePolicies = defaultStoragePolices
}
for _, sp := range storagePolicies {
newKey := aggregationKey{
aggregationID: pipeline.AggregationID,
storagePolicy: sp,
pipeline: pipeline.Pipeline,
}
res.add(newKey)
}
}
return res, nil
}
func computeExpectedAggregationKeysFromTimedMetadata(
metadata metadata.TimedMetadata,
) aggregationKeys {
return aggregationKeys{
{
aggregationID: metadata.AggregationID,
storagePolicy: metadata.StoragePolicy,
},
}
}
func computeExpectedAggregationKeysFromForwardMetadata(
metadata metadata.ForwardMetadata,
) aggregationKeys {
return aggregationKeys{
{
aggregationID: metadata.AggregationID,
storagePolicy: metadata.StoragePolicy,
pipeline: metadata.Pipeline,
},
}
}
type metricWithMetadataUnion struct {
metric metricUnion
metadata metadataUnion
}
type testData struct {
timestamp time.Time
metricWithMetadatas []metricWithMetadataUnion
}
type testDataset []testData
type counterValueGenFn func(intervalIdx, idIdx int) int64
type timerValueGenFn func(intervalIdx, idIdx int) []float64
type gaugeValueGenFn func(intervalIdx, idIdx int) float64
func defaultCounterValueGenFn(intervalIdx, _ int) int64 {
testCounterVal := int64(123)
return testCounterVal + int64(intervalIdx)
}
func defaultTimerValueGenFn(intervalIdx, _ int) []float64 {
testBatchTimerVals := []float64{1.5, 2.5, 3.5, 4.5, 5.5}
vals := make([]float64, len(testBatchTimerVals))
for idx, v := range testBatchTimerVals {
vals[idx] = v + float64(intervalIdx)
}
return vals
}
func defaultGaugeValueGenFn(intervalIdx, _ int) float64 {
testGaugeVal := 456.789
return testGaugeVal + float64(intervalIdx)
}
type untimedValueGenOpts struct {
counterValueGenFn counterValueGenFn
timerValueGenFn timerValueGenFn
gaugeValueGenFn gaugeValueGenFn
}
var defaultUntimedValueGenOpts = untimedValueGenOpts{
counterValueGenFn: defaultCounterValueGenFn,
timerValueGenFn: defaultTimerValueGenFn,
gaugeValueGenFn: defaultGaugeValueGenFn,
}
type timedValueGenFn func(intervalIdx, idIdx int) float64
func defaultTimedValueGenFn(intervalIdx, _ int) float64 {
testVal := 456.789
return testVal + float64(intervalIdx)
}
type timedValueGenOpts struct {
timedValueGenFn timedValueGenFn
}
var defaultTimedValueGenOpts = timedValueGenOpts{
timedValueGenFn: defaultTimedValueGenFn,
}
type forwardedValueGenFn func(intervalIdx, idIdx int) []float64
func defaultForwardedValueGenFn(intervalIdx, _ int) []float64 {
testForwardedVals := []float64{1.2, 3.4, 5.6}
vals := make([]float64, len(testForwardedVals))
for idx, v := range testForwardedVals {
vals[idx] = v + float64(intervalIdx)
}
return vals
}
type forwardedValueGenOpts struct {
forwardedValueGenFn forwardedValueGenFn
}
var defaultForwardedValueGenOpts = forwardedValueGenOpts{
forwardedValueGenFn: defaultForwardedValueGenFn,
}
type valueGenOpts struct {
untimed untimedValueGenOpts
timed timedValueGenOpts
forwarded forwardedValueGenOpts
}
var defaultValueGenOpts = valueGenOpts{
untimed: defaultUntimedValueGenOpts,
timed: defaultTimedValueGenOpts,
forwarded: defaultForwardedValueGenOpts,
}
type datasetGenOpts struct {
start time.Time
stop time.Time
interval time.Duration
ids []string
category metricCategory
typeFn metricTypeFn
valueGenOpts valueGenOpts
metadataFn metadataFn
}