aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/monlib/deprecated/json/writer.h
blob: b365d30e5943f522e976ee143a0ecbff700bdde5 (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
#pragma once

#include <library/cpp/json/json_writer.h> 

namespace NMonitoring {
    /**
     * Deprecated writer of Solomon JSON format
     * https://wiki.yandex-team.ru/solomon/api/dataformat/json
     *
     * This writer will be deleted soon, so please consider to use
     * high level library library/cpp/monlib/encode which is decoupled from the
     * particular format.
     */
    class TDeprecatedJsonWriter {
    private:
        NJson::TJsonWriter JsonWriter;
        enum EState {
            STATE_ROOT,
            STATE_DOCUMENT,
            STATE_COMMON_LABELS,
            STATE_METRICS,
            STATE_METRIC,
            STATE_LABELS,
        };
        EState State;

    public:
        explicit TDeprecatedJsonWriter(IOutputStream* out);

        void OpenDocument();
        void CloseDocument();

        void OpenCommonLabels();
        void CloseCommonLabels();

        void WriteCommonLabel(TStringBuf name, TStringBuf value);

        void OpenMetrics();
        void CloseMetrics();

        void OpenMetric();
        void CloseMetric();

        void OpenLabels();
        void CloseLabels();

        void WriteLabel(TStringBuf name, TStringBuf value);

        template <typename... T>
        void WriteLabels(T... pairs) {
            OpenLabels();
            WriteLabelsInner(pairs...);
            CloseLabels();
        }

        void WriteModeDeriv();

        void WriteValue(long long value);
        void WriteDoubleValue(double d);

        void WriteTs(ui64 ts);

    private:
        void WriteLabelsInner(TStringBuf name, TStringBuf value) {
            WriteLabel(name, value);
        }

        template <typename... T>
        void WriteLabelsInner(TStringBuf name, TStringBuf value, T... pairs) {
            WriteLabel(name, value);
            WriteLabelsInner(pairs...);
        }

        inline void TransitionState(EState current, EState next);
    };
}