-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathprometheus_common.cc
99 lines (89 loc) · 3.64 KB
/
prometheus_common.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
//
//
// 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_common.h"
#include <algorithm>
#include <string_view>
namespace trpc::prometheus {
namespace detail {
void SplitName(const std::string& str, char separator, std::vector<std::string>& output) {
output.clear();
std::size_t cur_index = 0; // the index of the current segment being processed.
std::size_t start_pos = 0, next_pos = 0;
for (; start_pos < str.size(); start_pos = next_pos + 1) {
next_pos = str.find(separator, start_pos);
if (next_pos == std::string::npos || cur_index++ == kServiceIndex) {
output.emplace_back(str.substr(start_pos));
return;
}
output.emplace_back(str.substr(start_pos, next_pos - start_pos));
}
}
std::string GetServiceName(const std::string& str, char separator) {
std::size_t cur_index = 0;
std::size_t pos = 0;
while ((pos = str.find(separator, pos)) != std::string::npos) {
if (++cur_index == kServiceIndex) {
return str.substr(pos + 1);
}
++pos;
}
return "";
}
void SetCallerServiceInfo(const std::string& service_name, std::map<std::string, std::string>& infos) {
std::vector<std::string> sv;
sv.reserve(4);
trpc::prometheus::detail::SplitName(service_name, '.', sv);
if (sv.size() > 3) {
infos.emplace(trpc::prometheus::kCallerAppKey, std::move(sv[1]));
infos.emplace(trpc::prometheus::kCallerServerKey, std::move(sv[2]));
infos.emplace(trpc::prometheus::kCallerServiceKey, std::move(sv[3]));
} else if (sv.size() > 2) {
infos.emplace(trpc::prometheus::kCallerAppKey, std::move(sv[1]));
infos.emplace(trpc::prometheus::kCallerServerKey, std::move(sv[2]));
infos.emplace(trpc::prometheus::kCallerServiceKey, "");
} else if (sv.size() > 1) {
infos.emplace(trpc::prometheus::kCallerAppKey, std::move(sv[1]));
infos.emplace(trpc::prometheus::kCallerServerKey, "");
infos.emplace(trpc::prometheus::kCallerServiceKey, "");
} else {
infos.emplace(trpc::prometheus::kCallerAppKey, service_name);
infos.emplace(trpc::prometheus::kCallerServerKey, "");
infos.emplace(trpc::prometheus::kCallerServiceKey, "");
}
}
void SetCalleeServiceInfo(const std::string& service_name, std::map<std::string, std::string>& infos) {
std::vector<std::string> sv;
sv.reserve(4);
trpc::prometheus::detail::SplitName(service_name, '.', sv);
if (sv.size() > 3) {
infos.emplace(trpc::prometheus::kCalleeAppKey, std::move(sv[1]));
infos.emplace(trpc::prometheus::kCalleeServerKey, std::move(sv[2]));
infos.emplace(trpc::prometheus::kCalleeServiceKey, std::move(sv[3]));
} else if (sv.size() > 2) {
infos.emplace(trpc::prometheus::kCalleeAppKey, std::move(sv[1]));
infos.emplace(trpc::prometheus::kCalleeServerKey, std::move(sv[2]));
infos.emplace(trpc::prometheus::kCalleeServiceKey, "");
} else if (sv.size() > 1) {
infos.emplace(trpc::prometheus::kCalleeAppKey, std::move(sv[1]));
infos.emplace(trpc::prometheus::kCalleeServerKey, "");
infos.emplace(trpc::prometheus::kCalleeServiceKey, "");
} else {
infos.emplace(trpc::prometheus::kCalleeAppKey, service_name);
infos.emplace(trpc::prometheus::kCalleeServerKey, "");
infos.emplace(trpc::prometheus::kCalleeServiceKey, "");
}
}
} // namespace detail
} // namespace trpc::prometheus
#endif