diff options
author | Devtools Arcadia <arcadia-devtools@yandex-team.ru> | 2022-02-07 18:08:42 +0300 |
---|---|---|
committer | Devtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net> | 2022-02-07 18:08:42 +0300 |
commit | 1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch) | |
tree | e26c9fed0de5d9873cce7e00bc214573dc2195b7 /library/cpp/monlib/deprecated | |
download | ydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/monlib/deprecated')
-rw-r--r-- | library/cpp/monlib/deprecated/json/ut/ya.make | 12 | ||||
-rw-r--r-- | library/cpp/monlib/deprecated/json/writer.cpp | 100 | ||||
-rw-r--r-- | library/cpp/monlib/deprecated/json/writer.h | 76 | ||||
-rw-r--r-- | library/cpp/monlib/deprecated/json/writer_ut.cpp | 32 | ||||
-rw-r--r-- | library/cpp/monlib/deprecated/json/ya.make | 26 | ||||
-rw-r--r-- | library/cpp/monlib/deprecated/ya.make | 8 |
6 files changed, 254 insertions, 0 deletions
diff --git a/library/cpp/monlib/deprecated/json/ut/ya.make b/library/cpp/monlib/deprecated/json/ut/ya.make new file mode 100644 index 0000000000..18315993b5 --- /dev/null +++ b/library/cpp/monlib/deprecated/json/ut/ya.make @@ -0,0 +1,12 @@ +UNITTEST_FOR(library/cpp/monlib/deprecated/json) + +OWNER( + jamel + g:solomon +) + +SRCS( + writer_ut.cpp +) + +END() diff --git a/library/cpp/monlib/deprecated/json/writer.cpp b/library/cpp/monlib/deprecated/json/writer.cpp new file mode 100644 index 0000000000..a581f2e07a --- /dev/null +++ b/library/cpp/monlib/deprecated/json/writer.cpp @@ -0,0 +1,100 @@ +#include "writer.h" + +namespace NMonitoring { + TDeprecatedJsonWriter::TDeprecatedJsonWriter(IOutputStream* out) + : JsonWriter(out, false) + , State(STATE_ROOT) + { + } + + void TDeprecatedJsonWriter::TransitionState(EState current, EState next) { + if (State != current) { + ythrow yexception() << "wrong state"; + } + State = next; + } + + void TDeprecatedJsonWriter::OpenDocument() { + TransitionState(STATE_ROOT, STATE_DOCUMENT); + JsonWriter.OpenMap(); + } + + void TDeprecatedJsonWriter::CloseDocument() { + TransitionState(STATE_DOCUMENT, STATE_ROOT); + JsonWriter.CloseMap(); + JsonWriter.Flush(); + } + + void TDeprecatedJsonWriter::OpenCommonLabels() { + TransitionState(STATE_DOCUMENT, STATE_COMMON_LABELS); + JsonWriter.Write("commonLabels"); + JsonWriter.OpenMap(); + } + + void TDeprecatedJsonWriter::CloseCommonLabels() { + TransitionState(STATE_COMMON_LABELS, STATE_DOCUMENT); + JsonWriter.CloseMap(); + } + + void TDeprecatedJsonWriter::WriteCommonLabel(TStringBuf name, TStringBuf value) { + TransitionState(STATE_COMMON_LABELS, STATE_COMMON_LABELS); + JsonWriter.Write(name, value); + } + + void TDeprecatedJsonWriter::OpenMetrics() { + TransitionState(STATE_DOCUMENT, STATE_METRICS); + JsonWriter.Write("sensors"); + JsonWriter.OpenArray(); + } + + void TDeprecatedJsonWriter::CloseMetrics() { + TransitionState(STATE_METRICS, STATE_DOCUMENT); + JsonWriter.CloseArray(); + } + + void TDeprecatedJsonWriter::OpenMetric() { + TransitionState(STATE_METRICS, STATE_METRIC); + JsonWriter.OpenMap(); + } + + void TDeprecatedJsonWriter::CloseMetric() { + TransitionState(STATE_METRIC, STATE_METRICS); + JsonWriter.CloseMap(); + } + + void TDeprecatedJsonWriter::OpenLabels() { + TransitionState(STATE_METRIC, STATE_LABELS); + JsonWriter.Write("labels"); + JsonWriter.OpenMap(); + } + + void TDeprecatedJsonWriter::CloseLabels() { + TransitionState(STATE_LABELS, STATE_METRIC); + JsonWriter.CloseMap(); + } + + void TDeprecatedJsonWriter::WriteLabel(TStringBuf name, TStringBuf value) { + TransitionState(STATE_LABELS, STATE_LABELS); + JsonWriter.Write(name, value); + } + + void TDeprecatedJsonWriter::WriteModeDeriv() { + TransitionState(STATE_METRIC, STATE_METRIC); + JsonWriter.Write("mode", "deriv"); + } + + void TDeprecatedJsonWriter::WriteValue(long long value) { + TransitionState(STATE_METRIC, STATE_METRIC); + JsonWriter.Write("value", value); + } + + void TDeprecatedJsonWriter::WriteDoubleValue(double value) { + TransitionState(STATE_METRIC, STATE_METRIC); + JsonWriter.Write("value", value); + } + + void TDeprecatedJsonWriter::WriteTs(ui64 ts) { + TransitionState(STATE_METRIC, STATE_METRIC); + JsonWriter.Write("ts", ts); + } +} diff --git a/library/cpp/monlib/deprecated/json/writer.h b/library/cpp/monlib/deprecated/json/writer.h new file mode 100644 index 0000000000..183288143c --- /dev/null +++ b/library/cpp/monlib/deprecated/json/writer.h @@ -0,0 +1,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); + }; +} diff --git a/library/cpp/monlib/deprecated/json/writer_ut.cpp b/library/cpp/monlib/deprecated/json/writer_ut.cpp new file mode 100644 index 0000000000..1f9fc8f393 --- /dev/null +++ b/library/cpp/monlib/deprecated/json/writer_ut.cpp @@ -0,0 +1,32 @@ +#include "writer.h" + +#include <library/cpp/testing/unittest/registar.h> + +using namespace NMonitoring; + +Y_UNIT_TEST_SUITE(JsonWriterTests) { + Y_UNIT_TEST(One) { + TStringStream ss; + TDeprecatedJsonWriter w(&ss); + w.OpenDocument(); + w.OpenMetrics(); + + for (int i = 0; i < 5; ++i) { + w.OpenMetric(); + w.OpenLabels(); + w.WriteLabel("user", TString("") + (char)('a' + i)); + w.WriteLabel("name", "NWrites"); + w.CloseLabels(); + if (i % 2 == 0) { + w.WriteModeDeriv(); + } + w.WriteValue(10l); + w.CloseMetric(); + } + + w.CloseMetrics(); + w.CloseDocument(); + + //Cout << ss.Str() << "\n"; + } +} diff --git a/library/cpp/monlib/deprecated/json/ya.make b/library/cpp/monlib/deprecated/json/ya.make new file mode 100644 index 0000000000..0ca903ee62 --- /dev/null +++ b/library/cpp/monlib/deprecated/json/ya.make @@ -0,0 +1,26 @@ +LIBRARY() + +# 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. + +OWNER( + jamel + g:solomon +) + +SRCS( + writer.h + writer.cpp +) + +PEERDIR( + library/cpp/json +) + +END() + +RECURSE_FOR_TESTS(ut) diff --git a/library/cpp/monlib/deprecated/ya.make b/library/cpp/monlib/deprecated/ya.make new file mode 100644 index 0000000000..9345139aee --- /dev/null +++ b/library/cpp/monlib/deprecated/ya.make @@ -0,0 +1,8 @@ +OWNER( + g:solomon + jamel +) + +RECURSE( + json +) |