Metrics.h 8.7 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.

S
starlord 已提交
18

Y
yu yunfeng 已提交
19 20
#pragma once

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

Y
yu yunfeng 已提交
24
namespace zilliz {
J
jinhai 已提交
25
namespace milvus {
Y
yu yunfeng 已提交
26 27
namespace server {

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

Y
yu yunfeng 已提交
31
enum class MetricCollectorType {
Y
yu yunfeng 已提交
32 33 34 35 36
    INVALID,
    PROMETHEUS,
    ZABBIX
};

Y
yu yunfeng 已提交
37
class Metrics {
Y
yu yunfeng 已提交
38
 public:
Y
yudong.cai 已提交
39
    static MetricsBase &GetInstance();
Y
yu yunfeng 已提交
40

Y
yudong.cai 已提交
41 42
 private:
    static MetricsBase &CreateMetricsCollector();
Y
yu yunfeng 已提交
43
};
S
starlord 已提交
44 45
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class CollectMetricsBase {
S
starlord 已提交
46
 protected:
S
starlord 已提交
47 48 49 50 51 52 53 54 55 56 57
    CollectMetricsBase() {
        start_time_ = METRICS_NOW_TIME;
    }

    virtual ~CollectMetricsBase() = default;

    double TimeFromBegine() {
        auto end_time = METRICS_NOW_TIME;
        return METRICS_MICROSECONDS(start_time_, end_time);
    }

S
starlord 已提交
58
 protected:
S
starlord 已提交
59 60 61
    using TIME_POINT = std::chrono::system_clock::time_point;
    TIME_POINT start_time_;
};
Y
yu yunfeng 已提交
62

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

    ~CollectInsertMetrics() {
S
starlord 已提交
70
        if (n_ > 0) {
S
starlord 已提交
71
            auto total_time = TimeFromBegine();
S
starlord 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84
            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 已提交
85 86 87
        }
    }

S
starlord 已提交
88
 private:
Y
Yu Kun 已提交
89
    size_t n_;
S
starlord 已提交
90
    Status &status_;
Y
Yu Kun 已提交
91 92
};

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

    ~CollectQueryMetrics() {
S
starlord 已提交
100
        if (nq_ > 0) {
S
starlord 已提交
101
            auto total_time = TimeFromBegine();
S
starlord 已提交
102 103 104 105 106 107
            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 已提交
108 109 110
        }
    }

S
starlord 已提交
111
 private:
Y
Yu Kun 已提交
112 113 114
    size_t nq_;
};

S
starlord 已提交
115 116
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class CollectMergeFilesMetrics : CollectMetricsBase {
S
starlord 已提交
117
 public:
Y
Yu Kun 已提交
118 119 120 121
    CollectMergeFilesMetrics() {
    }

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

S
starlord 已提交
127 128
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class CollectBuildIndexMetrics : CollectMetricsBase {
S
starlord 已提交
129
 public:
Y
Yu Kun 已提交
130 131 132 133
    CollectBuildIndexMetrics() {
    }

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

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

    ~CollectExecutionEngineMetrics() {
S
starlord 已提交
146
        auto total_time = TimeFromBegine();
Y
Yu Kun 已提交
147 148 149 150 151 152

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

S
starlord 已提交
153
 private:
Y
Yu Kun 已提交
154
    double physical_size_;
Y
Yu Kun 已提交
155 156
};

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

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

 private:
Y
Yu Kun 已提交
169
    size_t size_;
Y
Yu Kun 已提交
170 171
};

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

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

 private:
Y
Yu Kun 已提交
186 187 188 189
    size_t n_;
    uint16_t dimension_;
};

S
starlord 已提交
190 191
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class CollectDurationMetrics : CollectMetricsBase {
S
starlord 已提交
192 193
 public:
    explicit CollectDurationMetrics(int index_type) : index_type_(index_type) {
Y
Yu Kun 已提交
194 195
    }

Y
Yu Kun 已提交
196
    ~CollectDurationMetrics() {
S
starlord 已提交
197
        auto total_time = TimeFromBegine();
Y
Yu Kun 已提交
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
        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 已提交
213 214

 private:
Y
Yu Kun 已提交
215 216 217
    int index_type_;
};

S
starlord 已提交
218 219
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class CollectSearchTaskMetrics : CollectMetricsBase {
S
starlord 已提交
220 221
 public:
    explicit CollectSearchTaskMetrics(int index_type) : index_type_(index_type) {
Y
Yu Kun 已提交
222 223 224
    }

    ~CollectSearchTaskMetrics() {
S
starlord 已提交
225
        auto total_time = TimeFromBegine();
S
starlord 已提交
226
        switch (index_type_) {
Y
Yu Kun 已提交
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
            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 已提交
242
 private:
Y
Yu Kun 已提交
243 244 245
    int index_type_;
};

S
starlord 已提交
246 247
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class MetricCollector : CollectMetricsBase {
S
starlord 已提交
248
 public:
Y
Yu Kun 已提交
249 250 251 252 253
    MetricCollector() {
        server::Metrics::GetInstance().MetaAccessTotalIncrement();
    }

    ~MetricCollector() {
S
starlord 已提交
254
        auto total_time = TimeFromBegine();
Y
Yu Kun 已提交
255 256 257 258
        server::Metrics::GetInstance().MetaAccessDurationSecondsHistogramObserve(total_time);
    }
};

S
starlord 已提交
259 260 261
} // namespace server
} // namespace milvus
} // namespace zilliz