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 bvar metrics header
19 */
20
21#include "metrics/bvar_metrics_collector.h"
22#include <ailego/utility/string_helper.h>
23
24namespace proxima {
25namespace be {
26namespace metrics {
27
28int BvarMetricsCollector::init(
29 const ::proxima::be::proto::MetricsConfig & /*config*/) {
30 auto query_type_enum_descriptor = proto::QueryRequest_QueryType_descriptor();
31 query_type_counter_.resize(proto::QueryRequest_QueryType_QueryType_ARRAYSIZE);
32 query_type_counter_second_.resize(
33 proto::QueryRequest_QueryType_QueryType_ARRAYSIZE);
34 for (int i = proto::QueryRequest_QueryType_QueryType_MIN;
35 i <= proto::QueryRequest_QueryType_QueryType_MAX; i++) {
36 if (proto::QueryRequest_QueryType_IsValid(i)) {
37 query_type_counter_[i].reset(new LongAdder{});
38 query_type_counter_second_[i].reset(new WindowedLongAdder{
39 MODULE_QUERY,
40 ailego::StringHelper::Concat(
41 "type", query_type_enum_descriptor->FindValueByNumber(i)->name(),
42 "_count_second"),
43 query_type_counter_[i].get(), 1});
44 }
45 }
46
47 query_latency_by_protocol_.resize(kProtocolTypeSize);
48 get_document_latency_by_protocol_.resize(kProtocolTypeSize);
49 write_latency_by_protocol_.resize(kProtocolTypeSize);
50 for (size_t i = 0; i < kProtocolTypeSize; i++) {
51 query_latency_by_protocol_[i].reset(new LatencyRecorder{
52 MODULE_QUERY,
53 ailego::StringHelper::Concat(kProtocolName[i], "_request")});
54
55 get_document_latency_by_protocol_[i].reset(new LatencyRecorder{
56 MODULE_GET_DOCUMENT,
57 ailego::StringHelper::Concat(kProtocolName[i], "_request")});
58
59 write_latency_by_protocol_[i].reset(new LatencyRecorder{
60 MODULE_WRITE,
61 ailego::StringHelper::Concat(kProtocolName[i], "_request")});
62 }
63
64 auto operation_type_enum_descriptor = proto::OperationType_descriptor();
65 write_doc_count_by_operation_type_.resize(proto::OperationType_ARRAYSIZE);
66 write_doc_count_by_operation_type_second_.resize(
67 proto::OperationType_ARRAYSIZE);
68 for (int i = proto::OperationType_MIN; i <= proto::OperationType_MAX; i++) {
69 if (proto::OperationType_IsValid(i)) {
70 write_doc_count_by_operation_type_[i].reset(new LongAdder{});
71 write_doc_count_by_operation_type_second_[i].reset(new WindowedLongAdder{
72 MODULE_WRITE,
73 ailego::StringHelper::Concat(
74 operation_type_enum_descriptor->FindValueByNumber(i)->name(),
75 "_count_second"),
76 write_doc_count_by_operation_type_[i].get(), 1});
77 }
78 }
79
80
81 return 0;
82}
83
84void BvarMetricsCollector::report_query_count_by_type(
85 proto::QueryRequest::QueryType query_type, uint32_t batch) {
86 if (query_type < 0 ||
87 query_type >= static_cast<int>(query_type_counter_.size()) ||
88 !query_type_counter_[query_type]) {
89 return;
90 }
91 (*query_type_counter_[query_type]) << batch;
92}
93
94void BvarMetricsCollector::report_write_doc_count_by_operation_type(
95 proto::OperationType type, size_t doc_count) {
96 if (type < 0 ||
97 type >= static_cast<int>(write_doc_count_by_operation_type_.size()) ||
98 !write_doc_count_by_operation_type_[type]) {
99 return;
100 }
101 (*write_doc_count_by_operation_type_[type]) << doc_count;
102}
103
104METRICS_REGISTER(bvar, BvarMetricsCollector);
105
106} // namespace metrics
107} // namespace be
108} // namespace proxima
109