-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
grpc_metrics_service_impl_test.cc
395 lines (324 loc) · 15.3 KB
/
grpc_metrics_service_impl_test.cc
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
#include "envoy/grpc/async_client.h"
#include "envoy/service/metrics/v3/metrics_service.pb.h"
#include "source/extensions/stat_sinks/metrics_service/grpc_metrics_service_impl.h"
#include "test/mocks/common.h"
#include "test/mocks/grpc/mocks.h"
#include "test/mocks/local_info/mocks.h"
#include "test/mocks/stats/mocks.h"
#include "test/mocks/thread_local/mocks.h"
#include "test/test_common/simulated_time_system.h"
#include "io/prometheus/client/metrics.pb.h"
using namespace std::chrono_literals;
using testing::_;
using testing::InSequence;
using testing::Invoke;
using testing::NiceMock;
namespace Envoy {
namespace Extensions {
namespace StatSinks {
namespace MetricsService {
namespace {
class GrpcMetricsStreamerImplTest : public testing::Test {
public:
using MockMetricsStream = Grpc::MockAsyncStream;
using MetricsServiceCallbacks =
Grpc::AsyncStreamCallbacks<envoy::service::metrics::v3::StreamMetricsResponse>;
GrpcMetricsStreamerImplTest() {
streamer_ = std::make_unique<GrpcMetricsStreamerImpl>(
Grpc::RawAsyncClientSharedPtr{async_client_}, local_info_);
}
void expectStreamStart(MockMetricsStream& stream, MetricsServiceCallbacks** callbacks_to_set) {
EXPECT_CALL(*async_client_, startRaw(_, _, _, _))
.WillOnce(Invoke([&stream, callbacks_to_set](absl::string_view, absl::string_view,
Grpc::RawAsyncStreamCallbacks& callbacks,
const Http::AsyncClient::StreamOptions&) {
*callbacks_to_set = dynamic_cast<MetricsServiceCallbacks*>(&callbacks);
return &stream;
}));
}
LocalInfo::MockLocalInfo local_info_;
Grpc::MockAsyncClient* async_client_{new NiceMock<Grpc::MockAsyncClient>};
GrpcMetricsStreamerImplPtr streamer_;
};
// Test basic metrics streaming flow.
TEST_F(GrpcMetricsStreamerImplTest, BasicFlow) {
InSequence s;
// Start a stream and send first message.
MockMetricsStream stream1;
MetricsServiceCallbacks* callbacks1;
expectStreamStart(stream1, &callbacks1);
EXPECT_CALL(local_info_, node());
EXPECT_CALL(stream1, sendMessageRaw_(_, false));
auto metrics =
std::make_unique<Envoy::Protobuf::RepeatedPtrField<io::prometheus::client::MetricFamily>>();
streamer_->send(std::move(metrics));
// Verify that sending an empty response message doesn't do anything bad.
callbacks1->onReceiveMessage(
std::make_unique<envoy::service::metrics::v3::StreamMetricsResponse>());
}
// Test that stream failure is handled correctly.
TEST_F(GrpcMetricsStreamerImplTest, StreamFailure) {
InSequence s;
EXPECT_CALL(*async_client_, startRaw(_, _, _, _))
.WillOnce(
Invoke([](absl::string_view, absl::string_view, Grpc::RawAsyncStreamCallbacks& callbacks,
const Http::AsyncClient::StreamOptions&) {
callbacks.onRemoteClose(Grpc::Status::Internal, "bad");
return nullptr;
}));
EXPECT_CALL(local_info_, node());
auto metrics =
std::make_unique<Envoy::Protobuf::RepeatedPtrField<io::prometheus::client::MetricFamily>>();
streamer_->send(std::move(metrics));
}
class MockGrpcMetricsStreamer
: public GrpcMetricsStreamer<envoy::service::metrics::v3::StreamMetricsMessage,
envoy::service::metrics::v3::StreamMetricsResponse> {
public:
MockGrpcMetricsStreamer(Grpc::RawAsyncClientSharedPtr async_client)
: GrpcMetricsStreamer<envoy::service::metrics::v3::StreamMetricsMessage,
envoy::service::metrics::v3::StreamMetricsResponse>(async_client) {}
// GrpcMetricsStreamer
MOCK_METHOD(void, send, (MetricsPtr && metrics));
};
class MetricsServiceSinkTest : public testing::Test {
public:
void addCounterToSnapshot(const std::string& name, uint64_t delta, uint64_t value,
bool used = true) {
counter_storage_.emplace_back(std::make_unique<NiceMock<Stats::MockCounter>>());
counter_storage_.back()->name_ = name;
counter_storage_.back()->value_ = value;
counter_storage_.back()->used_ = used;
snapshot_.counters_.push_back({delta, *counter_storage_.back()});
}
void addGaugeToSnapshot(const std::string& name, uint64_t value, bool used = true) {
gauge_storage_.emplace_back(std::make_unique<NiceMock<Stats::MockGauge>>());
gauge_storage_.back()->name_ = name;
gauge_storage_.back()->value_ = value;
gauge_storage_.back()->used_ = used;
snapshot_.gauges_.push_back(*gauge_storage_.back());
}
void addHistogramToSnapshot(const std::string& name, bool used = true) {
histogram_storage_.emplace_back(std::make_unique<NiceMock<Stats::MockParentHistogram>>());
histogram_storage_.back()->name_ = name;
histogram_storage_.back()->used_ = used;
snapshot_.histograms_.push_back(*histogram_storage_.back());
}
NiceMock<Stats::MockMetricSnapshot> snapshot_;
std::vector<std::unique_ptr<NiceMock<Stats::MockCounter>>> counter_storage_;
std::vector<std::unique_ptr<NiceMock<Stats::MockGauge>>> gauge_storage_;
std::vector<std::unique_ptr<NiceMock<Stats::MockParentHistogram>>> histogram_storage_;
std::shared_ptr<MockGrpcMetricsStreamer> streamer_{new MockGrpcMetricsStreamer(
Grpc::RawAsyncClientSharedPtr{new NiceMock<Grpc::MockAsyncClient>()})};
};
TEST_F(MetricsServiceSinkTest, CheckSendCall) {
MetricsServiceSink<envoy::service::metrics::v3::StreamMetricsMessage,
envoy::service::metrics::v3::StreamMetricsResponse>
sink(streamer_, false, false,
envoy::config::metrics::v3::HistogramEmitMode::SUMMARY_AND_HISTOGRAM);
addCounterToSnapshot("test_counter", 1, 1);
addGaugeToSnapshot("test_gauge", 1);
addHistogramToSnapshot("test_histogram");
EXPECT_CALL(*streamer_, send(_));
sink.flush(snapshot_);
}
TEST_F(MetricsServiceSinkTest, CheckStatsCount) {
MetricsServiceSink<envoy::service::metrics::v3::StreamMetricsMessage,
envoy::service::metrics::v3::StreamMetricsResponse>
sink(streamer_, false, false,
envoy::config::metrics::v3::HistogramEmitMode::SUMMARY_AND_HISTOGRAM);
addCounterToSnapshot("test_counter", 1, 100);
addGaugeToSnapshot("test_gauge", 1);
EXPECT_CALL(*streamer_, send(_)).WillOnce(Invoke([](MetricsPtr&& metrics) {
EXPECT_EQ(2, metrics->size());
}));
sink.flush(snapshot_);
// Verify only newly added metrics come after endFlush call.
gauge_storage_.back()->used_ = false;
EXPECT_CALL(*streamer_, send(_)).WillOnce(Invoke([](MetricsPtr&& metrics) {
EXPECT_EQ(1, metrics->size());
}));
sink.flush(snapshot_);
}
// Test that verifies counters are correctly reported as current value when configured to do so.
TEST_F(MetricsServiceSinkTest, ReportCountersValues) {
MetricsServiceSink<envoy::service::metrics::v3::StreamMetricsMessage,
envoy::service::metrics::v3::StreamMetricsResponse>
sink(streamer_, false, false,
envoy::config::metrics::v3::HistogramEmitMode::SUMMARY_AND_HISTOGRAM);
addCounterToSnapshot("test_counter", 1, 100);
EXPECT_CALL(*streamer_, send(_)).WillOnce(Invoke([](MetricsPtr&& metrics) {
EXPECT_EQ(1, metrics->size());
EXPECT_EQ(100, (*metrics)[0].metric(0).counter().value());
}));
sink.flush(snapshot_);
}
// Test that verifies counters are reported as the delta between flushes when configured to do so.
TEST_F(MetricsServiceSinkTest, ReportCountersAsDeltas) {
addCounterToSnapshot("test_counter", 1, 100);
counter_storage_.back()->setTagExtractedName("tag-counter-name");
counter_storage_.back()->setTags({{"a", "b"}});
{
// This test won't emit any labels.
MetricsServiceSink<envoy::service::metrics::v3::StreamMetricsMessage,
envoy::service::metrics::v3::StreamMetricsResponse>
sink(streamer_, true, false,
envoy::config::metrics::v3::HistogramEmitMode::SUMMARY_AND_HISTOGRAM);
EXPECT_CALL(*streamer_, send(_)).WillOnce(Invoke([](MetricsPtr&& metrics) {
ASSERT_EQ(1, metrics->size());
EXPECT_EQ("test_counter", (*metrics)[0].name());
const auto& metric = (*metrics)[0].metric(0);
EXPECT_EQ(1, metric.counter().value());
EXPECT_EQ(0, metric.label().size());
}));
sink.flush(snapshot_);
}
{
// This test will emit labels.
MetricsServiceSink<envoy::service::metrics::v3::StreamMetricsMessage,
envoy::service::metrics::v3::StreamMetricsResponse>
sink(streamer_, true, true,
envoy::config::metrics::v3::HistogramEmitMode::SUMMARY_AND_HISTOGRAM);
EXPECT_CALL(*streamer_, send(_)).WillOnce(Invoke([](MetricsPtr&& metrics) {
ASSERT_EQ(1, metrics->size());
EXPECT_EQ("tag-counter-name", (*metrics)[0].name());
const auto& metric = (*metrics)[0].metric(0);
EXPECT_EQ(1, metric.counter().value());
EXPECT_EQ(1, metric.label().size());
}));
sink.flush(snapshot_);
}
}
// Test the behavior of tag emission based on the emit_tags_as_label flag.
TEST_F(MetricsServiceSinkTest, ReportMetricsWithTags) {
addCounterToSnapshot("full-counter-name", 1, 100);
counter_storage_.back()->setTagExtractedName("tag-counter-name");
counter_storage_.back()->setTags({{"a", "b"}});
addGaugeToSnapshot("full-gauge-name", 100);
gauge_storage_.back()->setTagExtractedName("tag-gauge-name");
gauge_storage_.back()->setTags({{"a", "b"}});
addHistogramToSnapshot("full-histogram-name");
histogram_storage_.back()->setTagExtractedName("tag-histogram-name");
histogram_storage_.back()->setTags({{"a", "b"}});
{
// When the emit_tags flag is false, we don't emit the tags and use the full name.
MetricsServiceSink<envoy::service::metrics::v3::StreamMetricsMessage,
envoy::service::metrics::v3::StreamMetricsResponse>
sink(streamer_, false, false,
envoy::config::metrics::v3::HistogramEmitMode::SUMMARY_AND_HISTOGRAM);
EXPECT_CALL(*streamer_, send(_)).WillOnce(Invoke([](MetricsPtr&& metrics) {
EXPECT_EQ(4, metrics->size());
EXPECT_EQ("full-counter-name", (*metrics)[0].name());
EXPECT_EQ(0, (*metrics)[0].metric(0).label().size());
EXPECT_EQ("full-gauge-name", (*metrics)[1].name());
EXPECT_EQ(0, (*metrics)[1].metric(0).label().size());
EXPECT_EQ("full-histogram-name", (*metrics)[2].name());
EXPECT_EQ(0, (*metrics)[2].metric(0).label().size());
EXPECT_EQ("full-histogram-name", (*metrics)[3].name());
EXPECT_EQ(0, (*metrics)[3].metric(0).label().size());
}));
sink.flush(snapshot_);
}
io::prometheus::client::LabelPair expected_label_pair;
expected_label_pair.set_name("a");
expected_label_pair.set_value("b");
// When the emit_tags flag is true, we emit the tags as labels and use the tag extracted name.
MetricsServiceSink<envoy::service::metrics::v3::StreamMetricsMessage,
envoy::service::metrics::v3::StreamMetricsResponse>
sink(streamer_, false, true,
envoy::config::metrics::v3::HistogramEmitMode::SUMMARY_AND_HISTOGRAM);
EXPECT_CALL(*streamer_, send(_)).WillOnce(Invoke([&expected_label_pair](MetricsPtr&& metrics) {
EXPECT_EQ(4, metrics->size());
EXPECT_EQ("tag-counter-name", (*metrics)[0].name());
EXPECT_EQ(1, (*metrics)[0].metric(0).label().size());
EXPECT_TRUE(TestUtility::protoEqual(expected_label_pair, (*metrics)[0].metric(0).label()[0]));
EXPECT_EQ("tag-gauge-name", (*metrics)[1].name());
EXPECT_EQ(1, (*metrics)[1].metric(0).label().size());
EXPECT_TRUE(TestUtility::protoEqual(expected_label_pair, (*metrics)[0].metric(0).label()[0]));
EXPECT_EQ("tag-histogram-name", (*metrics)[2].name());
EXPECT_EQ(1, (*metrics)[2].metric(0).label().size());
EXPECT_TRUE(TestUtility::protoEqual(expected_label_pair, (*metrics)[0].metric(0).label()[0]));
EXPECT_EQ("tag-histogram-name", (*metrics)[3].name());
EXPECT_EQ(1, (*metrics)[3].metric(0).label().size());
EXPECT_TRUE(TestUtility::protoEqual(expected_label_pair, (*metrics)[0].metric(0).label()[0]));
}));
sink.flush(snapshot_);
}
TEST_F(MetricsServiceSinkTest, FlushPredicate) {
addCounterToSnapshot("used_counter", 100, 1);
addCounterToSnapshot("unused_counter", 100, 1, false);
// Default predicate only accepts used metrics.
{
MetricsFlusher flusher(true, true,
envoy::config::metrics::v3::HistogramEmitMode::SUMMARY_AND_HISTOGRAM);
auto metrics = flusher.flush(snapshot_);
EXPECT_EQ(1, metrics->size());
}
// Using a predicate that accepts all metrics, we'd flush both metrics.
{
MetricsFlusher flusher(true, true,
envoy::config::metrics::v3::HistogramEmitMode::SUMMARY_AND_HISTOGRAM,
[](const auto&) { return true; });
auto metrics = flusher.flush(snapshot_);
EXPECT_EQ(2, metrics->size());
}
// Using a predicate that rejects all metrics, we'd flush no metrics.
MetricsFlusher flusher(true, true,
envoy::config::metrics::v3::HistogramEmitMode::SUMMARY_AND_HISTOGRAM,
[](const auto&) { return false; });
auto metrics = flusher.flush(snapshot_);
EXPECT_EQ(0, metrics->size());
}
// This test will emit summary and histogram.
TEST_F(MetricsServiceSinkTest, HistogramEmitModeBoth) {
addHistogramToSnapshot("test_histogram");
MetricsServiceSink<envoy::service::metrics::v3::StreamMetricsMessage,
envoy::service::metrics::v3::StreamMetricsResponse>
sink(streamer_, true, false,
envoy::config::metrics::v3::HistogramEmitMode::SUMMARY_AND_HISTOGRAM);
EXPECT_CALL(*streamer_, send(_)).WillOnce(Invoke([](MetricsPtr&& metrics) {
ASSERT_EQ(2, metrics->size());
EXPECT_EQ("test_histogram", (*metrics)[0].name());
EXPECT_EQ("test_histogram", (*metrics)[1].name());
const auto& metric1 = (*metrics)[0].metric(0);
EXPECT_TRUE(metric1.has_summary());
EXPECT_TRUE(metric1.summary().has_sample_sum());
const auto& metric2 = (*metrics)[1].metric(0);
EXPECT_TRUE(metric2.has_histogram());
}));
sink.flush(snapshot_);
}
// This test will only summary.
TEST_F(MetricsServiceSinkTest, HistogramEmitModeSummary) {
addHistogramToSnapshot("test_histogram");
MetricsServiceSink<envoy::service::metrics::v3::StreamMetricsMessage,
envoy::service::metrics::v3::StreamMetricsResponse>
sink(streamer_, true, false, envoy::config::metrics::v3::HistogramEmitMode::SUMMARY);
EXPECT_CALL(*streamer_, send(_)).WillOnce(Invoke([](MetricsPtr&& metrics) {
ASSERT_EQ(1, metrics->size());
EXPECT_EQ("test_histogram", (*metrics)[0].name());
const auto& metric1 = (*metrics)[0].metric(0);
EXPECT_TRUE(metric1.has_summary());
EXPECT_TRUE(metric1.summary().has_sample_sum());
}));
sink.flush(snapshot_);
}
// This test will only histogram.
TEST_F(MetricsServiceSinkTest, HistogramEmitModeHistogram) {
addHistogramToSnapshot("test_histogram");
MetricsServiceSink<envoy::service::metrics::v3::StreamMetricsMessage,
envoy::service::metrics::v3::StreamMetricsResponse>
sink(streamer_, true, false, envoy::config::metrics::v3::HistogramEmitMode::HISTOGRAM);
EXPECT_CALL(*streamer_, send(_)).WillOnce(Invoke([](MetricsPtr&& metrics) {
ASSERT_EQ(1, metrics->size());
EXPECT_EQ("test_histogram", (*metrics)[0].name());
const auto& metric1 = (*metrics)[0].metric(0);
EXPECT_TRUE(metric1.has_histogram());
}));
sink.flush(snapshot_);
}
} // namespace
} // namespace MetricsService
} // namespace StatSinks
} // namespace Extensions
} // namespace Envoy