1 | /** |
2 | * Copyright 2021 Alibaba, Inc. and its affiliates. All Rights Reserved. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | |
16 | * \author jiliang.ljl |
17 | * \date Feb 2021 |
18 | * \brief implementation of metrics collector |
19 | */ |
20 | |
21 | #include "metrics/metrics_collector.h" |
22 | #include "common/logger.h" |
23 | |
24 | namespace proxima { |
25 | namespace be { |
26 | namespace metrics { |
27 | |
28 | std::string MetricsCollector::metrics_name_; |
29 | static MetricsCollector &CreateMetrics(const std::string &name) { |
30 | if (name.empty()) { |
31 | static MetricsCollector empty_metrics; |
32 | return empty_metrics; |
33 | } |
34 | static auto obj = ailego::Factory<MetricsCollector>::MakeShared(name.c_str()); |
35 | if (obj) { |
36 | LOG_INFO("Create Metrics with name:%s" , name.c_str()); |
37 | return *obj; |
38 | } else { |
39 | std::string registered; |
40 | for (const auto &s : ailego::Factory<MetricsCollector>::Classes()) { |
41 | registered += s; |
42 | registered += ", " ; |
43 | } |
44 | LOG_FATAL("Cannot create Metrics with name=%s, registered names=%s" , |
45 | name.c_str(), registered.c_str()); |
46 | // In case LOG(FATAL) only print log and do not abort process |
47 | static MetricsCollector empty_metrics; |
48 | return empty_metrics; |
49 | } |
50 | } |
51 | |
52 | MetricsCollector &MetricsCollector::GetInstance() { |
53 | static MetricsCollector &obj = CreateMetrics(metrics_name_); |
54 | return obj; |
55 | } |
56 | |
57 | int MetricsCollector::CreateAndInitMetrics( |
58 | const proxima::be::proto::MetricsConfig &config) { |
59 | metrics_name_ = config.name(); |
60 | auto &obj = GetInstance(); |
61 | int ret = 0; |
62 | ret = obj.init(config); |
63 | if (ret != 0) { |
64 | LOG_FATAL("init metrics failed, config=%s" , config.DebugString().c_str()); |
65 | return ret; |
66 | } |
67 | return ret; |
68 | } |
69 | |
70 | METRICS_REGISTER(default, MetricsCollector); |
71 | |
72 | } // namespace metrics |
73 | } // namespace be |
74 | } // namespace proxima |
75 | |