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 interface of metrics collector
19 */
20
21#pragma once
22
23#include <string>
24#include <ailego/pattern/factory.h>
25#include <ailego/utility/time_helper.h>
26#include "proto/config.pb.h"
27#include "proto/proxima_be.pb.h"
28
29namespace proxima {
30namespace be {
31namespace metrics {
32
33enum class ProtocolType { kHttp = 0, kGrpc, kProtocolSize };
34constexpr const size_t kProtocolTypeSize =
35 static_cast<size_t>(ProtocolType::kProtocolSize);
36constexpr const char *kProtocolName[] = {"http", "grpc"};
37
38//! Metrics Collector interface
39class MetricsCollector {
40 public:
41 static int CreateAndInitMetrics(
42 const proxima::be::proto::MetricsConfig &config);
43
44 static MetricsCollector &GetInstance();
45
46 virtual int init(const proxima::be::proto::MetricsConfig & /*config*/) {
47 return 0;
48 }
49
50 virtual ~MetricsCollector() = default;
51
52 //! report rt per vector
53 virtual void report_query_rt(ProtocolType /*type*/, uint32_t /*batch*/,
54 uint64_t /*us*/) {}
55
56 virtual void report_query_success_count(uint32_t /*batch*/) {}
57
58 virtual void report_query_failure_count(uint32_t /*batch*/) {}
59
60 virtual void report_query_batch(uint32_t /*batch*/) {}
61
62 virtual void report_query_count_by_type(
63 proto::QueryRequest::QueryType /*query_type*/, uint32_t /*batch*/) {}
64
65 virtual void report_get_document_rt(ProtocolType /*type*/, uint64_t /*us*/) {}
66
67 virtual void report_get_document_success_count() {}
68
69 virtual void report_get_document_failure_count() {}
70
71 virtual void report_write_rt(ProtocolType /*type*/, uint32_t /*batch*/,
72 uint64_t /*us*/) {}
73
74 virtual void report_write_success_count(uint32_t /*batch*/) {}
75
76 virtual void report_write_failure_count(uint32_t /*batch*/) {}
77
78 virtual void report_write_doc_count_by_operation_type(
79 proto::OperationType /*type*/, size_t /*doc_count*/) {}
80
81 virtual void report_write_batch(uint32_t /*batch*/) {}
82
83 private:
84 static std::string metrics_name_;
85};
86
87#define METRICS_REGISTER(__NAME__, __IMPL__, ...) \
88 AILEGO_FACTORY_REGISTER(__NAME__, MetricsCollector, __IMPL__, ##__VA_ARGS__)
89
90} // namespace metrics
91} // namespace be
92} // namespace proxima
93