-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathprometheus_metrics_api_test.cc
85 lines (69 loc) · 3.2 KB
/
prometheus_metrics_api_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
//
//
// Tencent is pleased to support the open source community by making tRPC available.
//
// Copyright (C) 2023 THL A29 Limited, a Tencent company.
// All rights reserved.
//
// If you have downloaded a copy of the tRPC source code from Tencent,
// please note that tRPC source code is licensed under the Apache 2.0 License,
// A copy of the Apache 2.0 License is included in this file.
//
//
#ifdef TRPC_BUILD_INCLUDE_PROMETHEUS
#include "trpc/metrics/prometheus/prometheus_metrics_api.h"
#include "gtest/gtest.h"
#include "trpc/common/config/trpc_config.h"
#include "trpc/metrics/metrics_factory.h"
#include "trpc/metrics/prometheus/prometheus_metrics.h"
namespace trpc::testing {
class PrometheusMetricsAPITest : public ::testing::Test {
protected:
static void SetUpTestCase() {
prometheus_metrics_ = MakeRefCounted<PrometheusMetrics>();
TrpcConfig::GetInstance()->Init("./trpc/metrics/prometheus/testing/prometheus_metrics.yaml");
ASSERT_EQ(0, prometheus_metrics_->Init());
}
static void TearDownTestCase() {}
protected:
static PrometheusMetricsPtr prometheus_metrics_;
};
PrometheusMetricsPtr PrometheusMetricsAPITest::prometheus_metrics_;
std::map<std::string, std::string> GetTestLabels(std::string value) {
std::map<std::string, std::string> labels = {{"inter_key1", value + "1"}, {"inter_key2", value + "2"}};
return labels;
}
TEST_F(PrometheusMetricsAPITest, Interfaces) {
// 1. testing report when prometheus plugin is not registered
std::map<std::string, std::string> labels = GetTestLabels("inter_value");
ASSERT_NE(0, trpc::prometheus::ReportSetMetricsInfo(labels, 10));
// register the prometheus plugin
MetricsFactory::GetInstance()->Register(prometheus_metrics_);
// 2. testing report SET metrics data
labels = GetTestLabels("inter_set_value");
ASSERT_EQ(0, trpc::prometheus::ReportSetMetricsInfo(labels, 10));
// 3. testing report SUM metrics data
labels = GetTestLabels("inter_sum_value");
ASSERT_EQ(0, trpc::prometheus::ReportSumMetricsInfo(labels, 10));
// 4. testing report MID metrics data
labels = GetTestLabels("inter_mid_value");
ASSERT_EQ(0, trpc::prometheus::ReportMidMetricsInfo(labels, 10));
// 5. testing report QUANTILES metrics data
labels = GetTestLabels("inter_quantiles_value");
// report failed because quantiles filed is empty
ASSERT_NE(0, trpc::prometheus::ReportQuantilesMetricsInfo(labels, {}, 10));
// report failed because the value in quantiles do not have a size of 2
ASSERT_NE(0, trpc::prometheus::ReportQuantilesMetricsInfo(labels, {{0.5}}, 10));
// report success because the metrics data is valid
ASSERT_EQ(0, trpc::prometheus::ReportQuantilesMetricsInfo(labels, {{0.5, 0.05}, {0.1, 0.05}}, 10));
// 6. testing report HISTOGRAM metrics data
labels = GetTestLabels("inter_histogram_value");
// report failed because bucket filed is empty
ASSERT_NE(0, trpc::prometheus::ReportHistogramMetricsInfo(labels, {}, 10));
// report success because the metrics data is valid
trpc::HistogramBucket bucket = {0.1, 0.5, 1};
ASSERT_EQ(0, trpc::prometheus::ReportHistogramMetricsInfo(labels, bucket, 10));
ASSERT_EQ(0, trpc::prometheus::ReportHistogramMetricsInfo(labels, std::move(bucket), 10));
}
} // namespace trpc::testing
#endif