forked from grpc/grpc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstrument_test.cc
More file actions
1211 lines (1114 loc) · 49.4 KB
/
Copy pathinstrument_test.cc
File metadata and controls
1211 lines (1114 loc) · 49.4 KB
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
991
992
993
994
995
996
997
998
999
1000
// Copyright 2025 gRPC authors.
//
// 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
//
// http://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.
#include "src/core/telemetry/instrument.h"
#include <thread>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/random/random.h"
namespace grpc_core {
using instrument_detail::InstrumentIndex;
MATCHER_P(InstrumentLabelListElementsAreArray, expected, "") {
std::vector<std::string> actual;
actual.reserve(arg.size());
for (size_t i = 0; i < arg.size(); ++i) {
actual.push_back(std::string(arg[i].label()));
}
return ::testing::ExplainMatchResult(::testing::ElementsAreArray(expected),
actual, result_listener);
}
using GetStorageTest = InstrumentTest;
using MetricsQueryTest = InstrumentTest;
using InstrumentIndexDeathTest = InstrumentTest;
using StorageReapingTest = InstrumentTest;
using InstrumentHandleTest = InstrumentTest;
using InstrumentLabelTest = InstrumentTest;
using InstrumentLabelListTest = InstrumentTest;
using InstrumentIndexTest = InstrumentTest;
using DomainStorageTest = InstrumentTest;
using CollectionScopeTest = InstrumentTest;
class MockMetricsSink : public MetricsSink {
public:
virtual ~MockMetricsSink() = default;
MOCK_METHOD(void, Counter,
(InstrumentLabelList label_keys,
absl::Span<const std::string> label, absl::string_view name,
uint64_t value),
(override));
MOCK_METHOD(void, UpDownCounter,
(InstrumentLabelList label_keys,
absl::Span<const std::string> label, absl::string_view name,
uint64_t value),
(override));
MOCK_METHOD(void, Histogram,
(InstrumentLabelList label_keys,
absl::Span<const std::string> label, absl::string_view name,
HistogramBuckets bounds, absl::Span<const uint64_t> counts),
(override));
MOCK_METHOD(void, DoubleGauge,
(InstrumentLabelList label_keys,
absl::Span<const std::string> labels, absl::string_view name,
double value),
(override));
MOCK_METHOD(void, IntGauge,
(InstrumentLabelList label_keys,
absl::Span<const std::string> labels, absl::string_view name,
int64_t value),
(override));
MOCK_METHOD(void, UintGauge,
(InstrumentLabelList label_keys,
absl::Span<const std::string> labels, absl::string_view name,
uint64_t value),
(override));
};
template <typename... LabelNames>
std::vector<std::string> TupleToVector(std::tuple<LabelNames...> labels) {
auto make_vector = [](auto&&... args) {
return std::vector<std::string>{absl::StrCat(args)...};
};
return std::apply(make_vector, std::move(labels));
}
template <size_t N>
std::vector<std::string> TupleToVector(FixedInstrumentLabelList<N> labels) {
auto list = labels.ToList();
std::vector<std::string> result;
result.reserve(list.size());
for (size_t i = 0; i < list.size(); ++i) {
result.push_back(std::string(list[i].label()));
}
return result;
}
std::vector<std::string> LabelListToVector(const InstrumentLabelList& list) {
std::vector<std::string> result;
result.reserve(list.size());
for (size_t i = 0; i < list.size(); ++i) {
result.push_back(std::string(list[i].label()));
}
return result;
}
template <typename Domain>
std::vector<std::string> DomainLabels() {
return TupleToVector(Domain::Labels());
}
class InstrumentTest : public ::testing::Test {
protected:
void SetUp() override { TestOnlyResetInstruments(); }
void TearDown() override { TestOnlyResetInstruments(); }
};
class HighContentionDomain final
: public InstrumentDomain<HighContentionDomain> {
public:
using Backend = HighContentionBackend;
static constexpr absl::string_view kName = "high_contention";
GRPC_INSTRUMENT_DOMAIN_LABELS();
static inline const auto kCounter =
RegisterCounter("high_contention", "Desc", "unit");
};
class LowContentionDomain final : public InstrumentDomain<LowContentionDomain> {
public:
using Backend = LowContentionBackend;
static constexpr absl::string_view kName = "low_contention";
GRPC_INSTRUMENT_DOMAIN_LABELS("grpc.target");
static inline const auto kCounter =
RegisterCounter("low_contention", "Desc", "unit");
static inline const auto kExponentialHistogram =
RegisterHistogram<ExponentialHistogramShape>("exponential_histogram",
"Desc", "unit", 1024, 20);
static inline const auto kDoubleGauge =
RegisterDoubleGauge("double_gauge", "Desc", "unit");
static inline const auto kIntGauge =
RegisterIntGauge("int_gauge", "Desc", "unit");
static inline const auto kUintGauge =
RegisterUintGauge("uint_gauge", "Desc", "unit");
static inline const auto kUpDownCounter =
RegisterUpDownCounter("up_down_counter", "Desc", "unit");
};
class InstanceCounterDomain final
: public InstrumentDomain<InstanceCounterDomain> {
public:
using Backend = LowContentionBackend;
static constexpr absl::string_view kName = "instance_counter";
GRPC_INSTRUMENT_DOMAIN_LABELS("grpc.target");
static inline const auto kInstanceCounter =
RegisterCounter("instance_counter", "Desc", "unit");
};
class TestDomain1 : public InstrumentDomain<TestDomain1> {
public:
using Backend = LowContentionBackend;
static constexpr absl::string_view kName = "test_domain1";
GRPC_INSTRUMENT_DOMAIN_LABELS("label1");
static inline const auto kCounter1 = RegisterCounter("test.counter1", "", "");
};
class TestDomain2 : public InstrumentDomain<TestDomain2> {
public:
using Backend = LowContentionBackend;
static constexpr absl::string_view kName = "test_domain2";
GRPC_INSTRUMENT_DOMAIN_LABELS("label2", "label3");
static inline const auto kCounter2 = RegisterCounter("test.counter2", "", "");
};
class GarbageCollectionTestDomain
: public InstrumentDomain<GarbageCollectionTestDomain> {
public:
using Backend = LowContentionBackend;
static constexpr absl::string_view kName = "gc_test";
GRPC_INSTRUMENT_DOMAIN_LABELS("label");
static inline const auto kTestCounter =
RegisterCounter("gc-test.counter", "", "");
};
using InstrumentIndexTest = InstrumentTest;
class FanOutDomain final : public InstrumentDomain<FanOutDomain> {
public:
using Backend = LowContentionBackend;
static constexpr absl::string_view kName = "fan_out";
GRPC_INSTRUMENT_DOMAIN_LABELS("grpc.target", "grpc.method");
static inline const auto kCounter =
RegisterCounter("fan_out", "Desc", "unit");
static inline const auto kUpDownCounter =
RegisterUpDownCounter("fan_out_up_down", "Desc", "unit");
static inline const auto kDoubleGauge =
RegisterDoubleGauge("fan_out_double", "Desc", "unit");
};
using InstrumentIndexDeathTest = InstrumentTest;
using InstrumentIndexTest = InstrumentTest;
using MetricsQueryTest = InstrumentTest;
// Verifies that instrument metadata can be registered and found via the
// InstrumentIndex.
TEST_F(InstrumentIndexTest, RegisterAndFind) {
InstrumentIndex& index = InstrumentIndex::Get();
const InstrumentMetadata::Description* description = index.Register(
nullptr, 0, "test_metric", "Test description", "units", {});
ASSERT_NE(description, nullptr);
EXPECT_EQ(description->name, "test_metric");
EXPECT_EQ(description->description, "Test description");
EXPECT_EQ(description->unit, "units");
const auto* found = index.Find("test_metric");
EXPECT_EQ(found, description);
const auto* not_found = index.Find("nonexistent");
EXPECT_EQ(not_found, nullptr);
}
// Verifies that registering a metric with a duplicate name returns the same
// description pointer.
TEST_F(InstrumentIndexTest, RegisterDuplicateReturnsSame) {
InstrumentIndex& index = InstrumentIndex::Get();
const auto* desc1 =
index.Register(nullptr, 1, "duplicate_metric", "Desc 1", "units", {});
const auto* desc2 =
index.Register(nullptr, 2, "duplicate_metric", "Desc 2", "units", {});
EXPECT_EQ(desc1, desc2);
EXPECT_EQ(desc1->description, "Desc 1");
}
// Verifies that the accessors on InstrumentHandle return the correct metadata.
TEST_F(InstrumentHandleTest, Accessors) {
EXPECT_EQ(LowContentionDomain::kCounter.name(), "low_contention");
EXPECT_EQ(LowContentionDomain::kCounter.description(), "Desc");
EXPECT_EQ(LowContentionDomain::kCounter.unit(), "unit");
EXPECT_EQ(LowContentionDomain::kExponentialHistogram.name(),
"exponential_histogram");
EXPECT_EQ(LowContentionDomain::kExponentialHistogram.description(), "Desc");
EXPECT_EQ(LowContentionDomain::kExponentialHistogram.unit(), "unit");
EXPECT_EQ(LowContentionDomain::kDoubleGauge.name(), "double_gauge");
EXPECT_EQ(LowContentionDomain::kDoubleGauge.description(), "Desc");
EXPECT_EQ(LowContentionDomain::kDoubleGauge.unit(), "unit");
EXPECT_EQ(LowContentionDomain::kIntGauge.name(), "int_gauge");
EXPECT_EQ(LowContentionDomain::kIntGauge.description(), "Desc");
EXPECT_EQ(LowContentionDomain::kIntGauge.unit(), "unit");
EXPECT_EQ(LowContentionDomain::kUintGauge.name(), "uint_gauge");
EXPECT_EQ(LowContentionDomain::kUintGauge.description(), "Desc");
EXPECT_EQ(LowContentionDomain::kUintGauge.unit(), "unit");
}
// Tests basic counter functionality in a high-contention domain (no labels).
// Verifies that increments are recorded and that storage is reset after being
// released.
TEST_F(MetricsQueryTest, HighContention) {
auto scope = CreateCollectionScope({}, {});
auto storage = HighContentionDomain::GetStorage(scope);
::testing::StrictMock<MockMetricsSink> sink;
EXPECT_CALL(
sink,
Counter(InstrumentLabelListElementsAreArray(std::vector<std::string>{}),
::testing::ElementsAreArray(absl::Span<const std::string>()),
"high_contention", 0));
MetricsQuery().OnlyMetrics({"high_contention"}).Run(scope, sink);
::testing::Mock::VerifyAndClearExpectations(&sink);
storage->Increment(HighContentionDomain::kCounter);
EXPECT_CALL(
sink,
Counter(InstrumentLabelListElementsAreArray(std::vector<std::string>{}),
::testing::ElementsAreArray(absl::Span<const std::string>()),
"high_contention", 1));
MetricsQuery().OnlyMetrics({"high_contention"}).Run(scope, sink);
::testing::Mock::VerifyAndClearExpectations(&sink);
storage.reset();
scope = CreateCollectionScope({}, {});
storage = HighContentionDomain::GetStorage(scope);
EXPECT_CALL(
sink,
Counter(InstrumentLabelListElementsAreArray(std::vector<std::string>{}),
::testing::ElementsAreArray(absl::Span<const std::string>()),
"high_contention", 0));
MetricsQuery().OnlyMetrics({"high_contention"}).Run(scope, sink);
}
// Tests basic counter functionality in a low-contention domain (one label).
// Verifies that increments are recorded for the correct label and that storage
// is reset after being released.
TEST_F(MetricsQueryTest, LowContention) {
auto scope = CreateCollectionScope({}, {"grpc.target"});
auto storage = LowContentionDomain::GetStorage(scope, "example.com");
std::vector<std::string> label_keys = {"grpc.target"};
std::vector<std::string> label = {"example.com"};
::testing::StrictMock<MockMetricsSink> sink;
EXPECT_CALL(sink,
Counter(InstrumentLabelListElementsAreArray(label_keys),
::testing::ElementsAreArray(label), "low_contention", 0));
MetricsQuery().OnlyMetrics({"low_contention"}).Run(scope, sink);
::testing::Mock::VerifyAndClearExpectations(&sink);
storage->Increment(LowContentionDomain::kCounter);
EXPECT_CALL(sink,
Counter(InstrumentLabelListElementsAreArray(label_keys),
::testing::ElementsAreArray(label), "low_contention", 1));
MetricsQuery().OnlyMetrics({"low_contention"}).Run(scope, sink);
::testing::Mock::VerifyAndClearExpectations(&sink);
storage.reset();
scope = CreateCollectionScope({}, {"grpc.target"});
storage = LowContentionDomain::GetStorage(scope, "example.com");
EXPECT_CALL(sink,
Counter(InstrumentLabelListElementsAreArray(label_keys),
::testing::ElementsAreArray(label), "low_contention", 0));
MetricsQuery().OnlyMetrics({"low_contention"}).Run(scope, sink);
}
// Tests histogram functionality in a low-contention domain.
// Verifies that increments are recorded in the correct histogram bucket.
TEST_F(MetricsQueryTest, LowContentionHistogram) {
auto scope = CreateCollectionScope({}, {"grpc.target"});
std::vector<uint64_t> value_before;
auto storage = LowContentionDomain::GetStorage(scope, "example.com");
::testing::StrictMock<MockMetricsSink> sink;
std::vector<std::string> label_keys = {"grpc.target"};
std::vector<std::string> label = {"example.com"};
EXPECT_CALL(sink,
Histogram(InstrumentLabelListElementsAreArray(label_keys),
::testing::ElementsAreArray(label),
"exponential_histogram", ::testing::_, ::testing::_))
.WillOnce([&value_before](auto, auto, auto, auto, auto counts) {
value_before.assign(counts.begin(), counts.end());
});
MetricsQuery()
.OnlyMetrics({"exponential_histogram"})
.WithLabelEq("grpc.target", "example.com")
.Run(scope, sink);
::testing::Mock::VerifyAndClearExpectations(&sink);
ASSERT_FALSE(value_before.empty());
std::vector<uint64_t> expect_value = value_before;
expect_value[0] += 1;
storage->Increment(LowContentionDomain::kExponentialHistogram, 0);
EXPECT_CALL(sink,
Histogram(InstrumentLabelListElementsAreArray(label_keys),
absl::MakeConstSpan(label), "exponential_histogram",
::testing::_, absl::MakeConstSpan(expect_value)))
.Times(1);
MetricsQuery()
.OnlyMetrics({"exponential_histogram"})
.WithLabelEq("grpc.target", "example.com")
.Run(scope, sink);
::testing::Mock::VerifyAndClearExpectations(&sink);
}
// Tests gauge functionality (double, int, uint) in a low-contention domain.
// Verifies that a GaugeProvider can register itself and provide correct values
// during a query.
TEST_F(MetricsQueryTest, LowContentionGauge) {
auto scope = CreateCollectionScope({}, {"grpc.target"});
auto storage = LowContentionDomain::GetStorage(scope, "example.com");
std::vector<std::string> label_keys = {"grpc.target"};
std::vector<std::string> label = {"example.com"};
::testing::StrictMock<MockMetricsSink> sink;
class MyGaugeProvider final : public GaugeProvider<LowContentionDomain> {
public:
explicit MyGaugeProvider(
InstrumentStorageRefPtr<LowContentionDomain> storage)
: GaugeProvider(std::move(storage)) {
ProviderConstructed();
}
~MyGaugeProvider() { ProviderDestructing(); }
void PopulateGaugeData(
GaugeSink<LowContentionDomain>& gauge_sink) override {
gauge_sink.Set(LowContentionDomain::kDoubleGauge, 1.23);
gauge_sink.Set(LowContentionDomain::kIntGauge, -456);
gauge_sink.Set(LowContentionDomain::kUintGauge, 789);
}
};
MyGaugeProvider provider(storage);
EXPECT_CALL(sink, DoubleGauge(InstrumentLabelListElementsAreArray(
DomainLabels<LowContentionDomain>()),
::testing::ElementsAreArray(label),
"double_gauge", 1.23));
EXPECT_CALL(sink,
IntGauge(InstrumentLabelListElementsAreArray(
DomainLabels<LowContentionDomain>()),
::testing::ElementsAreArray(label), "int_gauge", -456));
EXPECT_CALL(sink,
UintGauge(InstrumentLabelListElementsAreArray(
DomainLabels<LowContentionDomain>()),
::testing::ElementsAreArray(label), "uint_gauge", 789));
MetricsQuery()
.OnlyMetrics({"double_gauge", "int_gauge", "uint_gauge"})
.Run(scope, sink);
::testing::Mock::VerifyAndClearExpectations(&sink);
}
// Tests metric collection across multiple label sets ("fan-out").
// Verifies that metrics for different label combinations are reported correctly
// and that collapsing labels aggregates the results as expected.
TEST_F(MetricsQueryTest, FanOut) {
auto scope = CreateCollectionScope({}, {"grpc.target", "grpc.method"});
std::vector<std::string> label_keys = {"grpc.target", "grpc.method"};
auto storage_foo = FanOutDomain::GetStorage(scope, "example.com", "foo");
std::vector<std::string> label_foo = {"example.com", "foo"};
auto storage_bar = FanOutDomain::GetStorage(scope, "example.com", "bar");
std::vector<std::string> label_bar = {"example.com", "bar"};
{
::testing::StrictMock<MockMetricsSink> sink;
EXPECT_CALL(sink,
Counter(InstrumentLabelListElementsAreArray(label_keys),
::testing::ElementsAreArray(label_foo), "fan_out", 0));
EXPECT_CALL(sink,
Counter(InstrumentLabelListElementsAreArray(label_keys),
::testing::ElementsAreArray(label_bar), "fan_out", 0));
MetricsQuery().OnlyMetrics({"fan_out"}).Run(scope, sink);
}
storage_foo->Increment(FanOutDomain::kCounter);
storage_bar->Increment(FanOutDomain::kCounter);
{
::testing::StrictMock<MockMetricsSink> sink;
EXPECT_CALL(sink,
Counter(InstrumentLabelListElementsAreArray(label_keys),
::testing::ElementsAreArray(label_foo), "fan_out", 1));
EXPECT_CALL(sink,
Counter(InstrumentLabelListElementsAreArray(label_keys),
::testing::ElementsAreArray(label_bar), "fan_out", 1));
MetricsQuery().OnlyMetrics({"fan_out"}).Run(scope, sink);
}
{
const std::vector<std::string> label_all = {"example.com"};
::testing::StrictMock<MockMetricsSink> sink;
EXPECT_CALL(sink,
Counter(::testing::ResultOf(
LabelListToVector,
::testing::ElementsAreArray(
std::vector<std::string>({"grpc.target"}))),
::testing::ElementsAreArray(label_all), "fan_out", 2));
MetricsQuery()
.OnlyMetrics({"fan_out"})
.CollapseLabels({InstrumentLabel("grpc.method")})
.Run(scope, sink);
}
storage_foo.reset();
storage_bar.reset();
{
::testing::StrictMock<MockMetricsSink> sink;
EXPECT_CALL(sink,
Counter(InstrumentLabelListElementsAreArray(label_keys),
::testing::ElementsAreArray(label_foo), "fan_out", 1));
EXPECT_CALL(sink,
Counter(InstrumentLabelListElementsAreArray(label_keys),
::testing::ElementsAreArray(label_bar), "fan_out", 1));
MetricsQuery().OnlyMetrics({"fan_out"}).Run(scope, sink);
}
storage_foo = FanOutDomain::GetStorage(scope, "example.com", "foo");
storage_bar = FanOutDomain::GetStorage(scope, "example.com", "bar");
{
::testing::StrictMock<MockMetricsSink> sink;
EXPECT_CALL(sink,
Counter(InstrumentLabelListElementsAreArray(label_keys),
::testing::ElementsAreArray(label_foo), "fan_out", 1));
EXPECT_CALL(sink,
Counter(InstrumentLabelListElementsAreArray(label_keys),
::testing::ElementsAreArray(label_bar), "fan_out", 1));
MetricsQuery().OnlyMetrics({"fan_out"}).Run(scope, sink);
}
{
const std::vector<std::string> label_all = {"example.com"};
::testing::StrictMock<MockMetricsSink> sink;
EXPECT_CALL(sink,
Counter(InstrumentLabelListElementsAreArray(
std::vector<std::string>({"grpc.target"})),
::testing::ElementsAreArray(label_all), "fan_out", 2));
MetricsQuery()
.OnlyMetrics({"fan_out"})
.CollapseLabels({InstrumentLabel("grpc.method")})
.Run(scope, sink);
}
}
// Tests gauge functionality with multiple label sets.
// Verifies that gauges for different label combinations are reported correctly
// and that label filtering works. It also confirms that gauges are not
// aggregated when labels are collapsed.
TEST_F(MetricsQueryTest, FanOutGauge) {
auto scope = CreateCollectionScope({}, {"grpc.target", "grpc.method"});
std::vector<std::string> label_keys = {"grpc.target", "grpc.method"};
auto storage_foo = FanOutDomain::GetStorage(scope, "example.com", "foo");
std::vector<std::string> label_foo = {"example.com", "foo"};
auto storage_bar = FanOutDomain::GetStorage(scope, "example.com", "bar");
std::vector<std::string> label_bar = {"example.com", "bar"};
::testing::StrictMock<MockMetricsSink> sink;
class MyGaugeProvider final : public GaugeProvider<FanOutDomain> {
public:
explicit MyGaugeProvider(InstrumentStorageRefPtr<FanOutDomain> storage,
double value)
: GaugeProvider(std::move(storage)), value_(value) {
ProviderConstructed();
}
~MyGaugeProvider() { ProviderDestructing(); }
void PopulateGaugeData(GaugeSink<FanOutDomain>& gauge_sink) override {
gauge_sink.Set(FanOutDomain::kDoubleGauge, value_);
}
private:
double value_;
};
MyGaugeProvider provider_foo(storage_foo, 1.1);
MyGaugeProvider provider_bar(storage_bar, 2.2);
EXPECT_CALL(sink, DoubleGauge(InstrumentLabelListElementsAreArray(
DomainLabels<FanOutDomain>()),
::testing::ElementsAreArray(label_foo),
"fan_out_double", 1.1));
EXPECT_CALL(sink, DoubleGauge(InstrumentLabelListElementsAreArray(
DomainLabels<FanOutDomain>()),
::testing::ElementsAreArray(label_bar),
"fan_out_double", 2.2));
MetricsQuery().OnlyMetrics({"fan_out_double"}).Run(scope, sink);
::testing::Mock::VerifyAndClearExpectations(&sink);
// Test label equality filter
EXPECT_CALL(
sink, DoubleGauge(
::testing::ResultOf(
LabelListToVector,
::testing::ElementsAreArray(DomainLabels<FanOutDomain>())),
::testing::ElementsAreArray(label_foo), "fan_out_double", 1.1));
MetricsQuery()
.OnlyMetrics({"fan_out_double"})
.WithLabelEq("grpc.method", "foo")
.Run(scope, sink);
::testing::Mock::VerifyAndClearExpectations(&sink);
// Test collapsing - Gauges are not aggregated.
EXPECT_CALL(
sink, DoubleGauge(::testing::_, ::testing::_, ::testing::_, ::testing::_))
.Times(0);
MetricsQuery()
.OnlyMetrics({"fan_out_double"})
.CollapseLabels({InstrumentLabel("grpc.method")})
.Run(scope, sink);
::testing::Mock::VerifyAndClearExpectations(&sink);
}
// Tests the `WithLabelEq` filter in MetricsQuery.
// Verifies that only metrics matching the specified label values are returned.
TEST_F(MetricsQueryTest, LowContentionUpDownCounter) {
const InstrumentLabelSet kLabels = {"grpc.target"};
auto scope = CreateCollectionScope({}, kLabels);
auto storage = LowContentionDomain::GetStorage(scope, "example.com");
std::vector<std::string> label_keys = {"grpc.target"};
std::vector<std::string> label = {"example.com"};
::testing::StrictMock<MockMetricsSink> sink;
EXPECT_CALL(
sink,
UpDownCounter(InstrumentLabelListElementsAreArray(label_keys),
::testing::ElementsAreArray(label), "up_down_counter", 0));
MetricsQuery().OnlyMetrics({"up_down_counter"}).Run(scope, sink);
::testing::Mock::VerifyAndClearExpectations(&sink);
storage->Increment(LowContentionDomain::kUpDownCounter);
EXPECT_CALL(
sink,
UpDownCounter(InstrumentLabelListElementsAreArray(label_keys),
::testing::ElementsAreArray(label), "up_down_counter", 1));
MetricsQuery().OnlyMetrics({"up_down_counter"}).Run(scope, sink);
::testing::Mock::VerifyAndClearExpectations(&sink);
storage->Decrement(LowContentionDomain::kUpDownCounter);
EXPECT_CALL(
sink,
UpDownCounter(InstrumentLabelListElementsAreArray(label_keys),
::testing::ElementsAreArray(label), "up_down_counter", 0));
MetricsQuery().OnlyMetrics({"up_down_counter"}).Run(scope, sink);
::testing::Mock::VerifyAndClearExpectations(&sink);
storage.reset();
scope = CreateCollectionScope({}, kLabels);
storage = LowContentionDomain::GetStorage(scope, "example.com");
EXPECT_CALL(
sink,
UpDownCounter(InstrumentLabelListElementsAreArray(label_keys),
::testing::ElementsAreArray(label), "up_down_counter", 0));
MetricsQuery().OnlyMetrics({"up_down_counter"}).Run(scope, sink);
}
TEST_F(MetricsQueryTest, FanOutUpDownCounter) {
const InstrumentLabelSet kLabels = {"grpc.target", "grpc.method"};
auto scope = CreateCollectionScope({}, kLabels);
std::vector<std::string> label_keys = {"grpc.target", "grpc.method"};
auto storage_foo = FanOutDomain::GetStorage(scope, "example.com", "foo");
std::vector<std::string> label_foo = {"example.com", "foo"};
auto storage_bar = FanOutDomain::GetStorage(scope, "example.com", "bar");
std::vector<std::string> label_bar = {"example.com", "bar"};
storage_foo->Increment(FanOutDomain::kUpDownCounter);
storage_bar->Increment(FanOutDomain::kUpDownCounter);
storage_bar->Increment(FanOutDomain::kUpDownCounter);
{
::testing::StrictMock<MockMetricsSink> sink;
EXPECT_CALL(sink,
UpDownCounter(InstrumentLabelListElementsAreArray(label_keys),
::testing::ElementsAreArray(label_foo),
"fan_out_up_down", 1));
EXPECT_CALL(sink,
UpDownCounter(InstrumentLabelListElementsAreArray(label_keys),
::testing::ElementsAreArray(label_bar),
"fan_out_up_down", 2));
MetricsQuery().OnlyMetrics({"fan_out_up_down"}).Run(scope, sink);
}
{
const std::vector<std::string> label_all = {"example.com"};
::testing::StrictMock<MockMetricsSink> sink;
EXPECT_CALL(
sink,
UpDownCounter(
::testing::ResultOf(LabelListToVector,
::testing::ElementsAreArray(
std::vector<std::string>({"grpc.target"}))),
::testing::ElementsAreArray(label_all), "fan_out_up_down", 3));
MetricsQuery()
.OnlyMetrics({"fan_out_up_down"})
.CollapseLabels({InstrumentLabel("grpc.method")})
.Run(scope, sink);
}
}
TEST_F(MetricsQueryTest, LabelEq) {
auto scope = CreateCollectionScope({}, {"grpc.target", "grpc.method"});
std::vector<std::string> label_keys = {"grpc.target", "grpc.method"};
auto storage_foo = FanOutDomain::GetStorage(scope, "example.com", "foo");
std::vector<std::string> label_foo = {"example.com", "foo"};
auto storage_bar = FanOutDomain::GetStorage(scope, "example.com", "bar");
auto storage_baz = FanOutDomain::GetStorage(scope, "example.org", "baz");
::testing::StrictMock<MockMetricsSink> sink;
EXPECT_CALL(
sink,
Counter(InstrumentLabelListElementsAreArray(DomainLabels<FanOutDomain>()),
::testing::ElementsAreArray(label_foo), "fan_out", 0));
MetricsQuery()
.OnlyMetrics({"fan_out"})
.WithLabelEq("grpc.target", "example.com")
.WithLabelEq("grpc.method", "foo")
.Run(scope, sink);
::testing::Mock::VerifyAndClearExpectations(&sink);
storage_foo->Increment(FanOutDomain::kCounter);
storage_bar->Increment(FanOutDomain::kCounter);
storage_baz->Increment(FanOutDomain::kCounter);
EXPECT_CALL(
sink,
Counter(InstrumentLabelListElementsAreArray(DomainLabels<FanOutDomain>()),
::testing::ElementsAreArray(label_foo), "fan_out", 1));
MetricsQuery()
.OnlyMetrics({"fan_out"})
.WithLabelEq("grpc.target", "example.com")
.WithLabelEq("grpc.method", "foo")
.Run(scope, sink);
::testing::Mock::VerifyAndClearExpectations(&sink);
}
// A stress test that runs multiple threads concurrently, performing metric
// increments, gauge provider registrations, and metric queries.
// This is a "does it crash" test to check for race conditions.
TEST_F(MetricsQueryTest, ThreadStress) {
auto scope = CreateCollectionScope({}, {});
std::vector<std::thread> threads;
std::atomic<bool> done = false;
for (int i = 0; i < 10; ++i) {
threads.emplace_back([&]() {
auto storage = HighContentionDomain::GetStorage(scope);
while (!done.load(std::memory_order_relaxed)) {
storage->Increment(HighContentionDomain::kCounter);
}
});
threads.emplace_back([&]() {
auto storage = LowContentionDomain::GetStorage(scope, "example.com");
while (!done.load(std::memory_order_relaxed)) {
storage->Increment(LowContentionDomain::kCounter);
}
});
threads.emplace_back([&]() {
auto storage = LowContentionDomain::GetStorage(scope, "bar.com");
while (!done.load(std::memory_order_relaxed)) {
storage->Increment(LowContentionDomain::kCounter);
}
});
threads.emplace_back([&]() {
auto storage = LowContentionDomain::GetStorage(scope, "example.com");
absl::BitGen gen;
while (!done.load(std::memory_order_relaxed)) {
storage->Increment(LowContentionDomain::kExponentialHistogram,
absl::Uniform(gen, 0, 1024));
}
});
threads.emplace_back([&]() {
class NoopSink final : public MetricsSink {
public:
void Counter(InstrumentLabelList label_keys,
absl::Span<const std::string> label,
absl::string_view name, uint64_t value) override {}
void UpDownCounter(InstrumentLabelList label_keys,
absl::Span<const std::string> label,
absl::string_view name, uint64_t value) override {}
void Histogram(InstrumentLabelList label_keys,
absl::Span<const std::string> label,
absl::string_view name, HistogramBuckets bounds,
absl::Span<const uint64_t> counts) override {}
void DoubleGauge(InstrumentLabelList label_keys,
absl::Span<const std::string> labels,
absl::string_view name, double value) override {}
void IntGauge(InstrumentLabelList label_keys,
absl::Span<const std::string> labels,
absl::string_view name, int64_t value) override {}
void UintGauge(InstrumentLabelList label_keys,
absl::Span<const std::string> labels,
absl::string_view name, uint64_t value) override {}
};
NoopSink sink;
while (!done.load(std::memory_order_relaxed)) {
MetricsQuery().Run(scope, sink);
}
});
threads.emplace_back([&]() {
auto storage = LowContentionDomain::GetStorage(scope, "gauge_stress.com");
class MyProvider final : public GaugeProvider<LowContentionDomain> {
public:
explicit MyProvider(
InstrumentStorageRefPtr<LowContentionDomain> storage)
: GaugeProvider(std::move(storage)) {
ProviderConstructed();
}
~MyProvider() { ProviderDestructing(); }
void PopulateGaugeData(GaugeSink<LowContentionDomain>& sink) override {
sink.Set(LowContentionDomain::kDoubleGauge, 1.0);
}
};
while (!done.load(std::memory_order_relaxed)) {
MyProvider provider(storage);
}
});
}
absl::SleepFor(absl::Seconds(1));
done.store(true, std::memory_order_relaxed);
for (auto& thread : threads) {
thread.join();
}
}
// Tests that a registered histogram collection hook is called when a histogram
// is incremented.
TEST_F(InstrumentTest, HistogramHook) {
auto scope = CreateCollectionScope({}, {});
::testing::MockFunction<void(
const InstrumentMetadata::Description* instrument,
absl::Span<const std::string> labels, int64_t value)>
hook;
RegisterHistogramCollectionHook(hook.AsStdFunction());
auto storage = LowContentionDomain::GetStorage(scope, "example.com");
std::vector<std::string> label = {std::string(kOmittedLabel)};
EXPECT_CALL(hook, Call(::testing::_, ::testing::ElementsAreArray(label), 10));
storage->Increment(LowContentionDomain::kExponentialHistogram, 10);
::testing::Mock::VerifyAndClearExpectations(&hook);
}
// Tests that multiple registered histogram collection hooks are all called when
// a histogram is incremented.
TEST_F(InstrumentTest, MultipleHistogramHooks) {
auto scope = CreateCollectionScope({}, {});
::testing::MockFunction<void(
const InstrumentMetadata::Description* instrument,
absl::Span<const std::string> labels, int64_t value)>
hook1;
::testing::MockFunction<void(
const InstrumentMetadata::Description* instrument,
absl::Span<const std::string> labels, int64_t value)>
hook2;
RegisterHistogramCollectionHook(hook1.AsStdFunction());
RegisterHistogramCollectionHook(hook2.AsStdFunction());
auto storage = LowContentionDomain::GetStorage(scope, "example.com");
std::vector<std::string> label = {std::string(kOmittedLabel)};
EXPECT_CALL(hook1,
Call(::testing::_, ::testing::ElementsAreArray(label), 10));
EXPECT_CALL(hook2,
Call(::testing::_, ::testing::ElementsAreArray(label), 10));
storage->Increment(LowContentionDomain::kExponentialHistogram, 10);
::testing::Mock::VerifyAndClearExpectations(&hook1);
::testing::Mock::VerifyAndClearExpectations(&hook2);
}
TEST_F(InstrumentLabelListTest, FixedToList) {
FixedInstrumentLabelList<2> fixed("label1", "label2");
InstrumentLabelList list = fixed.ToList();
EXPECT_EQ(list.size(), 2);
EXPECT_EQ(list[0].label(), "label1");
EXPECT_EQ(list[1].label(), "label2");
}
// Verifies that calling GetStorage with the same labels multiple times returns
// a pointer to the same Storage instance, as long as a strong reference is
// held.
TEST_F(GetStorageTest, SameInstanceForRepeatedCalls) {
auto scope = CreateCollectionScope({}, {});
auto storage1 = LowContentionDomain::GetStorage(scope, "test.com");
auto storage2 = LowContentionDomain::GetStorage(scope, "test.com");
EXPECT_EQ(storage1.get(), storage2.get());
}
// Tests that a Storage instance created *after* a CollectionScope has been
// created is still visible and included in the metric query results for that
// scope.
TEST_F(MetricsQueryTest, NewStorageVisibleInQuery) {
::testing::StrictMock<MockMetricsSink> sink;
std::vector<std::string> label = {std::string(kOmittedLabel)};
auto scope = CreateCollectionScope({}, {});
// Initial query, storage doesn't exist yet.
MetricsQuery().OnlyMetrics({"low_contention"}).Run(scope, sink);
::testing::Mock::VerifyAndClearExpectations(&sink);
// Storage created *after* scope.
auto storage = LowContentionDomain::GetStorage(scope, "new_metric.com");
storage->Increment(LowContentionDomain::kCounter);
// Query again with the same scope, new storage should be visible.
EXPECT_CALL(sink,
Counter(InstrumentLabelListElementsAreArray(
DomainLabels<LowContentionDomain>()),
::testing::ElementsAreArray(label), "low_contention", 1));
MetricsQuery().OnlyMetrics({"low_contention"}).Run(scope, sink);
::testing::Mock::VerifyAndClearExpectations(&sink);
}
// Verifies that a CollectionScope created via CreateCollectionScope takes a
// snapshot of the existing metrics, which are then readable via
// MetricsQuery::Run.
TEST_F(InstrumentTest, CollectionScopeSnapshotsExistingMetrics) {
auto scope = CreateCollectionScope({}, {});
// Create some metrics *before* the scope is created.
auto storage1 = LowContentionDomain::GetStorage(scope, "test1.com");
storage1->Increment(LowContentionDomain::kCounter);
auto storage2 = FanOutDomain::GetStorage(scope, "target1", "method1");
for (int i = 0; i < 5; ++i) {
storage2->Increment(FanOutDomain::kCounter);
}
// Query the data.
::testing::StrictMock<MockMetricsSink> sink;
std::vector<std::string> low_contention_label = {std::string(kOmittedLabel)};
std::vector<std::string> fan_out_label = {std::string(kOmittedLabel),
std::string(kOmittedLabel)};
EXPECT_CALL(sink, Counter(::testing::ResultOf(
LabelListToVector,
::testing::ElementsAreArray(
DomainLabels<LowContentionDomain>())),
::testing::ElementsAreArray(low_contention_label),
"low_contention", 1));
EXPECT_CALL(
sink, Counter(::testing::ResultOf(LabelListToVector,
::testing::ElementsAreArray(
DomainLabels<FanOutDomain>())),
::testing::ElementsAreArray(fan_out_label), "fan_out", 5));
MetricsQuery().OnlyMetrics({"low_contention", "fan_out"}).Run(scope, sink);
}
// Verifies that metrics created *after* a CollectionScope is created are still
// visible to that scope, verifying the live-update mechanism.
TEST_F(InstrumentTest, CollectionScopeSeesNewMetrics) {
// Create the scope first.
auto scope = CreateCollectionScope({}, {});
// Create metrics *after* the scope exists.
auto storage1 = LowContentionDomain::GetStorage(scope, "test1.com");
storage1->Increment(LowContentionDomain::kCounter);
auto storage2 = FanOutDomain::GetStorage(scope, "target1", "method1");
for (int i = 0; i < 5; ++i) {
storage2->Increment(FanOutDomain::kCounter);
}
// Query the data using the original scope.
::testing::StrictMock<MockMetricsSink> sink;
std::vector<std::string> low_contention_label = {std::string(kOmittedLabel)};
std::vector<std::string> fan_out_label = {std::string(kOmittedLabel),
std::string(kOmittedLabel)};
EXPECT_CALL(sink, Counter(InstrumentLabelListElementsAreArray(
DomainLabels<LowContentionDomain>()),
::testing::ElementsAreArray(low_contention_label),
"low_contention", 1));
EXPECT_CALL(
sink,
Counter(InstrumentLabelListElementsAreArray(DomainLabels<FanOutDomain>()),
::testing::ElementsAreArray(fan_out_label), "fan_out", 5));
MetricsQuery().OnlyMetrics({"low_contention", "fan_out"}).Run(scope, sink);
}
TEST_F(MetricsQueryTest, ScopedLabels) {
auto scope = CreateCollectionScope({}, {"grpc.target"});
auto s1 = FanOutDomain::GetStorage(scope, "t1", "m1");
auto s2 = FanOutDomain::GetStorage(scope, "t1", "m2");
s1->Increment(FanOutDomain::kCounter);
s2->Increment(FanOutDomain::kCounter);
std::vector<std::string> label = {"t1", std::string(kOmittedLabel)};
::testing::StrictMock<MockMetricsSink> sink;
EXPECT_CALL(
sink,
Counter(InstrumentLabelListElementsAreArray(DomainLabels<FanOutDomain>()),
::testing::ElementsAreArray(label), "fan_out", 2));
MetricsQuery().OnlyMetrics({"fan_out"}).Run(scope, sink);
}
TEST_F(MetricsQueryTest, StorageIsSharedWhenChildLabelsAreSameAsParent) {
auto parent_scope = CreateCollectionScope({}, {"grpc.target"});
auto child_scope = CreateCollectionScope({parent_scope}, {});
auto s1 = FanOutDomain::GetStorage(parent_scope, "t1", "m1");
auto s2 = FanOutDomain::GetStorage(child_scope, "t1", "m1");
EXPECT_EQ(s1.get(), s2.get());
}
TEST_F(MetricsQueryTest, StorageIsNotSharedWhenChildLabelsAreDifferent) {
auto parent_scope = CreateCollectionScope({}, {"grpc.target"});
auto child_scope = CreateCollectionScope({parent_scope}, {"grpc.method"});
auto s1 = FanOutDomain::GetStorage(parent_scope, "t1", "m1");
auto s2 = FanOutDomain::GetStorage(child_scope, "t1", "m1");
EXPECT_NE(s1.get(), s2.get());
EXPECT_THAT(s1->label(),
::testing::ElementsAre("t1", std::string(kOmittedLabel)));
EXPECT_THAT(s2->label(), ::testing::ElementsAre("t1", "m1"));
}
TEST_F(MetricsQueryTest, HierarchicalQuery) {
auto parent_scope = CreateCollectionScope({}, {"grpc.target"});
auto child_scope = CreateCollectionScope({parent_scope}, {"grpc.method"});
auto s1 = FanOutDomain::GetStorage(parent_scope, "t1", "m1");
auto s2 = FanOutDomain::GetStorage(child_scope, "t2", "m2");
s1->Increment(FanOutDomain::kCounter);
s2->Increment(FanOutDomain::kCounter);
std::vector<std::string> label1 = {"t1", std::string(kOmittedLabel)};
std::vector<std::string> label2 = {"t2", "m2"};
::testing::StrictMock<MockMetricsSink> sink;
EXPECT_CALL(
sink,
Counter(InstrumentLabelListElementsAreArray(DomainLabels<FanOutDomain>()),
::testing::ElementsAreArray(label1), "fan_out", 1));
EXPECT_CALL(
sink,
Counter(InstrumentLabelListElementsAreArray(DomainLabels<FanOutDomain>()),
::testing::ElementsAreArray(label2), "fan_out", 1));
MetricsQuery().OnlyMetrics({"fan_out"}).Run(parent_scope, sink);
}
TEST_F(MetricsQueryTest, AggregationOnChildDestruction) {
auto parent_scope = CreateCollectionScope({}, {"grpc.target"});
auto child_scope = CreateCollectionScope({parent_scope}, {"grpc.method"});
auto s_p = FanOutDomain::GetStorage(parent_scope, "t1", "m1");
auto s_c = FanOutDomain::GetStorage(child_scope, "t1", "m1");
s_p->Increment(FanOutDomain::kCounter);
s_c->Increment(FanOutDomain::kCounter);
child_scope.reset();
std::vector<std::string> label = {"t1", std::string(kOmittedLabel)};
::testing::StrictMock<MockMetricsSink> sink;
EXPECT_CALL(
sink,
Counter(InstrumentLabelListElementsAreArray(DomainLabels<FanOutDomain>()),
::testing::ElementsAreArray(label), "fan_out", 2));
MetricsQuery().OnlyMetrics({"fan_out"}).Run(parent_scope, sink);
}
// Verifies that CreateCollectionScope creates a valid scope.
TEST_F(InstrumentTest, CreateCollectionScope) {
auto scope = CreateCollectionScope({}, {});
ASSERT_NE(scope, nullptr);
}
TEST_F(MetricsQueryTest, AggregationToMultipleParents) {
auto p1 = CreateCollectionScope({}, {"grpc.target"});
auto p2 = CreateCollectionScope({}, {"grpc.method"});
auto s_p1 = FanOutDomain::GetStorage(p1, "t", "m");
auto s_p2 = FanOutDomain::GetStorage(p2, "t", "m");
s_p1->Increment(FanOutDomain::kCounter);
s_p2->Increment(FanOutDomain::kCounter);
{
auto child = CreateCollectionScope({p1, p2}, {});
auto s_c = FanOutDomain::GetStorage(child, "t", "m");
s_c->Increment(FanOutDomain::kCounter);
}
// child scope destroyed, s_c should be aggregated to p1 and p2.
::testing::StrictMock<MockMetricsSink> sink1;
::testing::StrictMock<MockMetricsSink> sink2;