Metrics.h 8.6 KB
Newer Older
J
jinhai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

Y
yu yunfeng 已提交
18 19
#pragma once

Y
yu yunfeng 已提交
20
#include "MetricBase.h"
Y
Yu Kun 已提交
21
#include "db/meta/MetaTypes.h"
Y
yudong.cai 已提交
22

J
jinhai 已提交
23
namespace milvus {
Y
yu yunfeng 已提交
24 25
namespace server {

Y
yu yunfeng 已提交
26
#define METRICS_NOW_TIME std::chrono::system_clock::now()
S
starlord 已提交
27
#define METRICS_MICROSECONDS(a, b) (std::chrono::duration_cast<std::chrono::microseconds>(b - a)).count();
Y
yu yunfeng 已提交
28

S
starlord 已提交
29
enum class MetricCollectorType { INVALID, PROMETHEUS, ZABBIX };
Y
yu yunfeng 已提交
30

Y
yu yunfeng 已提交
31
class Metrics {
Y
yu yunfeng 已提交
32
 public:
S
starlord 已提交
33 34
    static MetricsBase&
    GetInstance();
Y
yu yunfeng 已提交
35

Y
yudong.cai 已提交
36
 private:
S
starlord 已提交
37 38
    static MetricsBase&
    CreateMetricsCollector();
Y
yu yunfeng 已提交
39
};
S
starlord 已提交
40 41
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class CollectMetricsBase {
S
starlord 已提交
42
 protected:
S
starlord 已提交
43 44 45 46 47 48
    CollectMetricsBase() {
        start_time_ = METRICS_NOW_TIME;
    }

    virtual ~CollectMetricsBase() = default;

S
starlord 已提交
49 50
    double
    TimeFromBegine() {
S
starlord 已提交
51 52 53 54
        auto end_time = METRICS_NOW_TIME;
        return METRICS_MICROSECONDS(start_time_, end_time);
    }

S
starlord 已提交
55
 protected:
S
starlord 已提交
56 57 58
    using TIME_POINT = std::chrono::system_clock::time_point;
    TIME_POINT start_time_;
};
Y
yu yunfeng 已提交
59

S
starlord 已提交
60 61
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class CollectInsertMetrics : CollectMetricsBase {
S
starlord 已提交
62
 public:
S
starlord 已提交
63
    CollectInsertMetrics(size_t n, Status& status) : n_(n), status_(status) {
Y
Yu Kun 已提交
64 65 66
    }

    ~CollectInsertMetrics() {
S
starlord 已提交
67
        if (n_ > 0) {
S
starlord 已提交
68
            auto total_time = TimeFromBegine();
S
starlord 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81
            double avg_time = total_time / n_;
            for (int i = 0; i < n_; ++i) {
                Metrics::GetInstance().AddVectorsDurationHistogramOberve(avg_time);
            }

            //    server::Metrics::GetInstance().add_vector_duration_seconds_quantiles().Observe((average_time));
            if (status_.ok()) {
                server::Metrics::GetInstance().AddVectorsSuccessTotalIncrement(n_);
                server::Metrics::GetInstance().AddVectorsSuccessGaugeSet(n_);
            } else {
                server::Metrics::GetInstance().AddVectorsFailTotalIncrement(n_);
                server::Metrics::GetInstance().AddVectorsFailGaugeSet(n_);
            }
Y
Yu Kun 已提交
82 83 84
        }
    }

S
starlord 已提交
85
 private:
Y
Yu Kun 已提交
86
    size_t n_;
S
starlord 已提交
87
    Status& status_;
Y
Yu Kun 已提交
88 89
};

S
starlord 已提交
90 91
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class CollectQueryMetrics : CollectMetricsBase {
S
starlord 已提交
92 93
 public:
    explicit CollectQueryMetrics(size_t nq) : nq_(nq) {
Y
Yu Kun 已提交
94 95 96
    }

    ~CollectQueryMetrics() {
S
starlord 已提交
97
        if (nq_ > 0) {
S
starlord 已提交
98
            auto total_time = TimeFromBegine();
S
starlord 已提交
99 100 101 102 103 104
            for (int i = 0; i < nq_; ++i) {
                server::Metrics::GetInstance().QueryResponseSummaryObserve(total_time);
            }
            auto average_time = total_time / nq_;
            server::Metrics::GetInstance().QueryVectorResponseSummaryObserve(average_time, nq_);
            server::Metrics::GetInstance().QueryVectorResponsePerSecondGaugeSet(double(nq_) / total_time);
Y
Yu Kun 已提交
105 106 107
        }
    }

S
starlord 已提交
108
 private:
Y
Yu Kun 已提交
109 110 111
    size_t nq_;
};

S
starlord 已提交
112 113
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class CollectMergeFilesMetrics : CollectMetricsBase {
S
starlord 已提交
114
 public:
Y
Yu Kun 已提交
115 116 117 118
    CollectMergeFilesMetrics() {
    }

    ~CollectMergeFilesMetrics() {
S
starlord 已提交
119
        auto total_time = TimeFromBegine();
Y
Yu Kun 已提交
120 121 122 123
        server::Metrics::GetInstance().MemTableMergeDurationSecondsHistogramObserve(total_time);
    }
};

S
starlord 已提交
124 125
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class CollectBuildIndexMetrics : CollectMetricsBase {
S
starlord 已提交
126
 public:
Y
Yu Kun 已提交
127 128 129 130
    CollectBuildIndexMetrics() {
    }

    ~CollectBuildIndexMetrics() {
S
starlord 已提交
131
        auto total_time = TimeFromBegine();
Y
Yu Kun 已提交
132 133 134 135
        server::Metrics::GetInstance().BuildIndexDurationSecondsHistogramObserve(total_time);
    }
};

S
starlord 已提交
136 137
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class CollectExecutionEngineMetrics : CollectMetricsBase {
S
starlord 已提交
138 139
 public:
    explicit CollectExecutionEngineMetrics(double physical_size) : physical_size_(physical_size) {
Y
Yu Kun 已提交
140 141 142
    }

    ~CollectExecutionEngineMetrics() {
S
starlord 已提交
143
        auto total_time = TimeFromBegine();
Y
Yu Kun 已提交
144 145 146 147 148 149

        server::Metrics::GetInstance().FaissDiskLoadDurationSecondsHistogramObserve(total_time);
        server::Metrics::GetInstance().FaissDiskLoadSizeBytesHistogramObserve(physical_size_);
        server::Metrics::GetInstance().FaissDiskLoadIOSpeedGaugeSet(physical_size_ / double(total_time));
    }

S
starlord 已提交
150
 private:
Y
Yu Kun 已提交
151
    double physical_size_;
Y
Yu Kun 已提交
152 153
};

S
starlord 已提交
154 155
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class CollectSerializeMetrics : CollectMetricsBase {
S
starlord 已提交
156 157
 public:
    explicit CollectSerializeMetrics(size_t size) : size_(size) {
Y
Yu Kun 已提交
158 159 160
    }

    ~CollectSerializeMetrics() {
S
starlord 已提交
161
        auto total_time = TimeFromBegine();
S
starlord 已提交
162
        server::Metrics::GetInstance().DiskStoreIOSpeedGaugeSet((double)size_ / total_time);
Y
Yu Kun 已提交
163
    }
S
starlord 已提交
164 165

 private:
Y
Yu Kun 已提交
166
    size_t size_;
Y
Yu Kun 已提交
167 168
};

S
starlord 已提交
169 170
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class CollectAddMetrics : CollectMetricsBase {
S
starlord 已提交
171
 public:
Y
Yu Kun 已提交
172
    CollectAddMetrics(size_t n, uint16_t dimension) : n_(n), dimension_(dimension) {
Y
Yu Kun 已提交
173 174
    }

Y
Yu Kun 已提交
175
    ~CollectAddMetrics() {
S
starlord 已提交
176
        auto total_time = TimeFromBegine();
S
starlord 已提交
177
        server::Metrics::GetInstance().AddVectorsPerSecondGaugeSet(static_cast<int>(n_), static_cast<int>(dimension_),
Y
Yu Kun 已提交
178 179
                                                                   total_time);
    }
S
starlord 已提交
180 181

 private:
Y
Yu Kun 已提交
182 183 184 185
    size_t n_;
    uint16_t dimension_;
};

S
starlord 已提交
186 187
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class CollectDurationMetrics : CollectMetricsBase {
S
starlord 已提交
188 189
 public:
    explicit CollectDurationMetrics(int index_type) : index_type_(index_type) {
Y
Yu Kun 已提交
190 191
    }

Y
Yu Kun 已提交
192
    ~CollectDurationMetrics() {
S
starlord 已提交
193
        auto total_time = TimeFromBegine();
Y
Yu Kun 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
        switch (index_type_) {
            case engine::meta::TableFileSchema::RAW: {
                server::Metrics::GetInstance().SearchRawDataDurationSecondsHistogramObserve(total_time);
                break;
            }
            case engine::meta::TableFileSchema::TO_INDEX: {
                server::Metrics::GetInstance().SearchRawDataDurationSecondsHistogramObserve(total_time);
                break;
            }
            default: {
                server::Metrics::GetInstance().SearchIndexDataDurationSecondsHistogramObserve(total_time);
                break;
            }
        }
    }
S
starlord 已提交
209 210

 private:
Y
Yu Kun 已提交
211 212 213
    int index_type_;
};

S
starlord 已提交
214 215
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class CollectSearchTaskMetrics : CollectMetricsBase {
S
starlord 已提交
216 217
 public:
    explicit CollectSearchTaskMetrics(int index_type) : index_type_(index_type) {
Y
Yu Kun 已提交
218 219 220
    }

    ~CollectSearchTaskMetrics() {
S
starlord 已提交
221
        auto total_time = TimeFromBegine();
S
starlord 已提交
222
        switch (index_type_) {
Y
Yu Kun 已提交
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
            case engine::meta::TableFileSchema::RAW: {
                server::Metrics::GetInstance().SearchRawDataDurationSecondsHistogramObserve(total_time);
                break;
            }
            case engine::meta::TableFileSchema::TO_INDEX: {
                server::Metrics::GetInstance().SearchRawDataDurationSecondsHistogramObserve(total_time);
                break;
            }
            default: {
                server::Metrics::GetInstance().SearchIndexDataDurationSecondsHistogramObserve(total_time);
                break;
            }
        }
    }

S
starlord 已提交
238
 private:
Y
Yu Kun 已提交
239 240 241
    int index_type_;
};

S
starlord 已提交
242 243
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class MetricCollector : CollectMetricsBase {
S
starlord 已提交
244
 public:
Y
Yu Kun 已提交
245 246 247 248 249
    MetricCollector() {
        server::Metrics::GetInstance().MetaAccessTotalIncrement();
    }

    ~MetricCollector() {
S
starlord 已提交
250
        auto total_time = TimeFromBegine();
Y
Yu Kun 已提交
251 252 253 254
        server::Metrics::GetInstance().MetaAccessDurationSecondsHistogramObserve(total_time);
    }
};

S
starlord 已提交
255 256
}  // namespace server
}  // namespace milvus