aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/monlib/encode/buffered/buffered_encoder_base.h
blob: fe3714e58f08b2f1b6883390a30a3ac58b13f45f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
#pragma once

#include "string_pool.h"

#include <library/cpp/monlib/encode/encoder.h>
#include <library/cpp/monlib/encode/encoder_state.h>
#include <library/cpp/monlib/encode/format.h>
#include <library/cpp/monlib/metrics/metric_value.h>

#include <util/datetime/base.h>
#include <util/digest/numeric.h>


namespace NMonitoring {

class TBufferedEncoderBase : public IMetricEncoder {
public:
    void OnStreamBegin() override;
    void OnStreamEnd() override;

    void OnCommonTime(TInstant time) override;

    void OnMetricBegin(EMetricType type) override;
    void OnMetricEnd() override;

    void OnLabelsBegin() override;
    void OnLabelsEnd() override;
    void OnLabel(TStringBuf name, TStringBuf value) override;
    void OnLabel(ui32 name, ui32 value) override;
    std::pair<ui32, ui32> PrepareLabel(TStringBuf name, TStringBuf value) override;

    void OnDouble(TInstant time, double value) override;
    void OnInt64(TInstant time, i64 value) override;
    void OnUint64(TInstant time, ui64 value) override;

    void OnHistogram(TInstant time, IHistogramSnapshotPtr snapshot) override;
    void OnSummaryDouble(TInstant time, ISummaryDoubleSnapshotPtr snapshot) override;
    void OnLogHistogram(TInstant, TLogHistogramSnapshotPtr) override;

protected:
    using TPooledStr = TStringPoolBuilder::TValue;

    struct TPooledLabel {
        TPooledLabel(const TPooledStr* key, const TPooledStr* value)
            : Key{key}
            , Value{value}
        {
        }

        bool operator==(const TPooledLabel& other) const {
            return std::tie(Key, Value) == std::tie(other.Key, other.Value);
        }

        bool operator!=(const TPooledLabel& other) const {
            return !(*this == other);
        }

        const TPooledStr* Key;
        const TPooledStr* Value;
    };

    using TPooledLabels = TVector<TPooledLabel>;

    struct TPooledLabelsHash {
        size_t operator()(const TPooledLabels& val) const {
            size_t hash{0};

            for (auto v : val) {
                hash = CombineHashes<size_t>(hash, reinterpret_cast<size_t>(v.Key));
                hash = CombineHashes<size_t>(hash, reinterpret_cast<size_t>(v.Value));
            }

            return hash;
        }
    };

    using TMetricMap = THashMap<TPooledLabels, size_t, TPooledLabelsHash>;

    struct TMetric {
        EMetricType MetricType = EMetricType::UNKNOWN;
        TPooledLabels Labels;
        TMetricTimeSeries TimeSeries;
    };

protected:
    TString FormatLabels(const TPooledLabels& labels) const;

protected:
    TEncoderState State_;

    TStringPoolBuilder LabelNamesPool_;
    TStringPoolBuilder LabelValuesPool_;
    TInstant CommonTime_ = TInstant::Zero();
    TPooledLabels CommonLabels_;
    TVector<TMetric> Metrics_;
    TMetricMap MetricMap_;
    EMetricsMergingMode MetricsMergingMode_ = EMetricsMergingMode::DEFAULT;
};

}