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
};
S
starlord 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class CollectMetricsBase {
protected:
    CollectMetricsBase() {
        start_time_ = METRICS_NOW_TIME;
    }

    virtual ~CollectMetricsBase() = default;

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

protected:
    using TIME_POINT = std::chrono::system_clock::time_point;
    TIME_POINT start_time_;
};
Y
yu yunfeng 已提交
50

S
starlord 已提交
51 52
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class CollectInsertMetrics : CollectMetricsBase {
Y
Yu Kun 已提交
53
public:
54
    CollectInsertMetrics(size_t n, engine::Status& status) : n_(n), status_(status) {
Y
Yu Kun 已提交
55 56 57
    }

    ~CollectInsertMetrics() {
S
starlord 已提交
58
        if(n_ > 0) {
S
starlord 已提交
59
            auto total_time = TimeFromBegine();
S
starlord 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72
            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 已提交
73 74 75 76 77
        }
    }

private:
    size_t n_;
78
    engine::Status& status_;
Y
Yu Kun 已提交
79 80
};

S
starlord 已提交
81 82
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class CollectQueryMetrics : CollectMetricsBase {
Y
Yu Kun 已提交
83 84 85 86 87
public:
    CollectQueryMetrics(size_t nq) : nq_(nq) {
    }

    ~CollectQueryMetrics() {
S
starlord 已提交
88
        if(nq_ > 0) {
S
starlord 已提交
89
            auto total_time = TimeFromBegine();
S
starlord 已提交
90 91 92 93 94 95
            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 已提交
96 97 98 99 100 101 102
        }
    }

private:
    size_t nq_;
};

S
starlord 已提交
103 104
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class CollectMergeFilesMetrics : CollectMetricsBase {
Y
Yu Kun 已提交
105 106 107 108 109
public:
    CollectMergeFilesMetrics() {
    }

    ~CollectMergeFilesMetrics() {
S
starlord 已提交
110
        auto total_time = TimeFromBegine();
Y
Yu Kun 已提交
111 112 113 114
        server::Metrics::GetInstance().MemTableMergeDurationSecondsHistogramObserve(total_time);
    }
};

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

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

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

    ~CollectExecutionEngineMetrics() {
S
starlord 已提交
134
        auto total_time = TimeFromBegine();
Y
Yu Kun 已提交
135 136 137 138 139 140 141

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

private:
Y
Yu Kun 已提交
142
    double physical_size_;
Y
Yu Kun 已提交
143 144
};

S
starlord 已提交
145 146
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class CollectSerializeMetrics : CollectMetricsBase {
Y
Yu Kun 已提交
147
public:
Y
Yu Kun 已提交
148
    CollectSerializeMetrics(size_t size) : size_(size) {
Y
Yu Kun 已提交
149 150 151
    }

    ~CollectSerializeMetrics() {
S
starlord 已提交
152
        auto total_time = TimeFromBegine();
Y
Yu Kun 已提交
153 154 155
        server::Metrics::GetInstance().DiskStoreIOSpeedGaugeSet((double) size_ / total_time);
    }
private:
Y
Yu Kun 已提交
156
    size_t size_;
Y
Yu Kun 已提交
157 158
};

S
starlord 已提交
159 160
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class CollectAddMetrics : CollectMetricsBase {
Y
Yu Kun 已提交
161
public:
Y
Yu Kun 已提交
162
    CollectAddMetrics(size_t n, uint16_t dimension) : n_(n), dimension_(dimension) {
Y
Yu Kun 已提交
163 164
    }

Y
Yu Kun 已提交
165
    ~CollectAddMetrics() {
S
starlord 已提交
166
        auto total_time = TimeFromBegine();
Y
Yu Kun 已提交
167 168 169 170 171 172 173 174 175
        server::Metrics::GetInstance().AddVectorsPerSecondGaugeSet(static_cast<int>(n_),
                                                                   static_cast<int>(dimension_),
                                                                   total_time);
    }
private:
    size_t n_;
    uint16_t dimension_;
};

S
starlord 已提交
176 177
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class CollectDurationMetrics : CollectMetricsBase {
Y
Yu Kun 已提交
178
public:
Y
Yu Kun 已提交
179
    CollectDurationMetrics(int index_type) : index_type_(index_type) {
Y
Yu Kun 已提交
180 181
    }

Y
Yu Kun 已提交
182
    ~CollectDurationMetrics() {
S
starlord 已提交
183
        auto total_time = TimeFromBegine();
Y
Yu Kun 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
        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:
    int index_type_;
};

S
starlord 已提交
203 204
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class CollectSearchTaskMetrics : CollectMetricsBase {
Y
Yu Kun 已提交
205 206 207 208 209
public:
    CollectSearchTaskMetrics(int index_type) : index_type_(index_type) {
    }

    ~CollectSearchTaskMetrics() {
S
starlord 已提交
210
        auto total_time = TimeFromBegine();
Y
Yu Kun 已提交
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
        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:
    int index_type_;
};

S
starlord 已提交
231 232
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class MetricCollector : CollectMetricsBase {
Y
Yu Kun 已提交
233 234 235 236 237 238
public:
    MetricCollector() {
        server::Metrics::GetInstance().MetaAccessTotalIncrement();
    }

    ~MetricCollector() {
S
starlord 已提交
239
        auto total_time = TimeFromBegine();
Y
Yu Kun 已提交
240 241 242 243 244
        server::Metrics::GetInstance().MetaAccessDurationSecondsHistogramObserve(total_time);
    }
};


Y
yu yunfeng 已提交
245 246 247 248 249 250 251

}
}
}