Metrics.h 8.2 KB
Newer Older
Y
yu yunfeng 已提交
1 2 3 4 5 6 7
/*******************************************************************************
 * Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved
 * Unauthorized copying of this file, via any medium is strictly prohibited.
 * Proprietary and confidential.
 ******************************************************************************/
#pragma once

Y
yu yunfeng 已提交
8
#include "MetricBase.h"
Y
Yu Kun 已提交
9
#include "db/meta/MetaTypes.h"
Y
yudong.cai 已提交
10

Y
yu yunfeng 已提交
11 12

namespace zilliz {
J
jinhai 已提交
13
namespace milvus {
Y
yu yunfeng 已提交
14 15
namespace server {

Y
yu yunfeng 已提交
16 17
#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 已提交
18

Y
yu yunfeng 已提交
19
enum class MetricCollectorType {
Y
yu yunfeng 已提交
20 21 22 23 24
    INVALID,
    PROMETHEUS,
    ZABBIX
};

Y
yu yunfeng 已提交
25
class Metrics {
Y
yu yunfeng 已提交
26
 public:
Y
yudong.cai 已提交
27
    static MetricsBase &GetInstance();
Y
yu yunfeng 已提交
28

Y
yudong.cai 已提交
29 30
 private:
    static MetricsBase &CreateMetricsCollector();
Y
yu yunfeng 已提交
31 32
};

Y
Yu Kun 已提交
33 34
class CollectInsertMetrics {
public:
35 36
    CollectInsertMetrics(size_t n, engine::Status& status) : n_(n), status_(status) {
        start_time_ = METRICS_NOW_TIME;
Y
Yu Kun 已提交
37 38 39
    }

    ~CollectInsertMetrics() {
S
starlord 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
        if(n_ > 0) {
            auto end_time = METRICS_NOW_TIME;
            auto total_time = METRICS_MICROSECONDS(start_time_, end_time);
            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 已提交
56 57 58 59 60 61 62
        }
    }

private:
    using TIME_POINT = std::chrono::system_clock::time_point;
    TIME_POINT start_time_;
    size_t n_;
63
    engine::Status& status_;
Y
Yu Kun 已提交
64 65 66 67 68 69 70 71 72
};

class CollectQueryMetrics {
public:
    CollectQueryMetrics(size_t nq) : nq_(nq) {
        start_time_ = METRICS_NOW_TIME;
    }

    ~CollectQueryMetrics() {
S
starlord 已提交
73 74 75 76 77 78 79 80 81
        if(nq_ > 0) {
            auto end_time = METRICS_NOW_TIME;
            auto total_time = METRICS_MICROSECONDS(start_time_, end_time);
            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 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
        }
    }

private:
    using TIME_POINT = std::chrono::system_clock::time_point;
    TIME_POINT start_time_;
    size_t nq_;
};

class CollectMergeFilesMetrics {
public:
    CollectMergeFilesMetrics() {
        start_time_ = METRICS_NOW_TIME;
    }

    ~CollectMergeFilesMetrics() {
        auto end_time = METRICS_NOW_TIME;
        auto total_time = METRICS_MICROSECONDS(start_time_, end_time);
        server::Metrics::GetInstance().MemTableMergeDurationSecondsHistogramObserve(total_time);
    }

private:
    using TIME_POINT = std::chrono::system_clock::time_point;
    TIME_POINT start_time_;
};

class CollectBuildIndexMetrics {
public:
    CollectBuildIndexMetrics() {
        start_time_ = METRICS_NOW_TIME;
    }

    ~CollectBuildIndexMetrics() {
        auto end_time = METRICS_NOW_TIME;
        auto total_time = METRICS_MICROSECONDS(start_time_, end_time);
        server::Metrics::GetInstance().BuildIndexDurationSecondsHistogramObserve(total_time);
    }
private:
    using TIME_POINT = std::chrono::system_clock::time_point;
    TIME_POINT start_time_;
};

class CollectExecutionEngineMetrics {
public:
Y
Yu Kun 已提交
126
    CollectExecutionEngineMetrics(double physical_size) : physical_size_(physical_size) {
Y
Yu Kun 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
        start_time_ = METRICS_NOW_TIME;
    }

    ~CollectExecutionEngineMetrics() {
        auto end_time = METRICS_NOW_TIME;
        auto total_time = METRICS_MICROSECONDS(start_time_, end_time);

        server::Metrics::GetInstance().FaissDiskLoadDurationSecondsHistogramObserve(total_time);

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

private:
    using TIME_POINT = std::chrono::system_clock::time_point;
    TIME_POINT start_time_;
Y
Yu Kun 已提交
143
    double physical_size_;
Y
Yu Kun 已提交
144 145 146 147
};

class CollectSerializeMetrics {
public:
Y
Yu Kun 已提交
148
    CollectSerializeMetrics(size_t size) : size_(size) {
Y
Yu Kun 已提交
149 150 151 152 153 154 155 156 157 158 159
        start_time_ = METRICS_NOW_TIME;
    }

    ~CollectSerializeMetrics() {
        auto end_time = METRICS_NOW_TIME;
        auto total_time = METRICS_MICROSECONDS(start_time_, end_time);
        server::Metrics::GetInstance().DiskStoreIOSpeedGaugeSet((double) size_ / total_time);
    }
private:
    using TIME_POINT = std::chrono::system_clock::time_point;
    TIME_POINT start_time_;
Y
Yu Kun 已提交
160
    size_t size_;
Y
Yu Kun 已提交
161 162
};

Y
Yu Kun 已提交
163
class CollectAddMetrics {
Y
Yu Kun 已提交
164
public:
Y
Yu Kun 已提交
165
    CollectAddMetrics(size_t n, uint16_t dimension) : n_(n), dimension_(dimension) {
Y
Yu Kun 已提交
166 167 168
        start_time_ = METRICS_NOW_TIME;
    }

Y
Yu Kun 已提交
169
    ~CollectAddMetrics() {
Y
Yu Kun 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182
        auto end_time = METRICS_NOW_TIME;
        auto total_time = METRICS_MICROSECONDS(start_time_, end_time);
        server::Metrics::GetInstance().AddVectorsPerSecondGaugeSet(static_cast<int>(n_),
                                                                   static_cast<int>(dimension_),
                                                                   total_time);
    }
private:
    using TIME_POINT = std::chrono::system_clock::time_point;
    TIME_POINT start_time_;
    size_t n_;
    uint16_t dimension_;
};

Y
Yu Kun 已提交
183
class CollectDurationMetrics {
Y
Yu Kun 已提交
184
public:
Y
Yu Kun 已提交
185
    CollectDurationMetrics(int index_type) : index_type_(index_type) {
Y
Yu Kun 已提交
186 187 188
        start_time_ = METRICS_NOW_TIME;
    }

Y
Yu Kun 已提交
189
    ~CollectDurationMetrics() {
Y
Yu Kun 已提交
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
        auto end_time = METRICS_NOW_TIME;
        auto total_time = METRICS_MICROSECONDS(start_time_, end_time);
        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;
            }
        }
    }
private:
    using TIME_POINT = std::chrono::system_clock::time_point;
    TIME_POINT start_time_;
    int index_type_;
};

class CollectSearchTaskMetrics {
public:
    CollectSearchTaskMetrics(int index_type) : index_type_(index_type) {
        start_time_ = METRICS_NOW_TIME;
    }

    ~CollectSearchTaskMetrics() {
        auto end_time = METRICS_NOW_TIME;
        auto total_time = METRICS_MICROSECONDS(start_time_, end_time);
        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;
            }
        }
    }

private:
    using TIME_POINT = std::chrono::system_clock::time_point;
    TIME_POINT start_time_;
    int index_type_;
};

class MetricCollector {
public:
    MetricCollector() {
        server::Metrics::GetInstance().MetaAccessTotalIncrement();
        start_time_ = METRICS_NOW_TIME;
    }

    ~MetricCollector() {
        auto end_time = METRICS_NOW_TIME;
        auto total_time = METRICS_MICROSECONDS(start_time_, end_time);
        server::Metrics::GetInstance().MetaAccessDurationSecondsHistogramObserve(total_time);
    }

private:
    using TIME_POINT = std::chrono::system_clock::time_point;
    TIME_POINT start_time_;
};


Y
yu yunfeng 已提交
263 264 265 266 267 268 269

}
}
}