Metrics.h 8.1 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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 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
class CollectInsertMetrics {
public:
    CollectInsertMetrics(std::chrono::system_clock::time_point start_time,
                         size_t n, bool succeed) : start_time_(start_time), n_(n), succeed_(succeed) {
    }

    ~CollectInsertMetrics() {
        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 (succeed_) {
            server::Metrics::GetInstance().AddVectorsSuccessTotalIncrement(n_);
            server::Metrics::GetInstance().AddVectorsSuccessGaugeSet(n_);
        }
        else {
            server::Metrics::GetInstance().AddVectorsFailTotalIncrement(n_);
            server::Metrics::GetInstance().AddVectorsFailGaugeSet(n_);
        }
    }

private:
    using TIME_POINT = std::chrono::system_clock::time_point;
    TIME_POINT start_time_;
    size_t n_;
    bool succeed_;
};

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

    ~CollectQueryMetrics() {
        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);
    }

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:
    CollectExecutionEngineMetrics(double& physical_size) : physical_size_(physical_size) {
        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_;
    double& physical_size_;
};

class CollectSerializeMetrics {
public:
    CollectSerializeMetrics(size_t& size) : size_(size) {
        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_;
    size_t& size_;
};

class CollectorAddMetrics {
public:
    CollectorAddMetrics(size_t n, uint16_t dimension) : n_(n), dimension_(dimension) {
        start_time_ = METRICS_NOW_TIME;
    }

    ~CollectorAddMetrics() {
        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_;
};

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

    ~CollectorDurationMetrics() {
        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 已提交
260 261 262 263 264 265 266

}
}
}