aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/monlib/encode/format.cpp
diff options
context:
space:
mode:
authorDevtools Arcadia <arcadia-devtools@yandex-team.ru>2022-02-07 18:08:42 +0300
committerDevtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net>2022-02-07 18:08:42 +0300
commit1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch)
treee26c9fed0de5d9873cce7e00bc214573dc2195b7 /library/cpp/monlib/encode/format.cpp
downloadydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/monlib/encode/format.cpp')
-rw-r--r--library/cpp/monlib/encode/format.cpp202
1 files changed, 202 insertions, 0 deletions
diff --git a/library/cpp/monlib/encode/format.cpp b/library/cpp/monlib/encode/format.cpp
new file mode 100644
index 0000000000..400ce5a643
--- /dev/null
+++ b/library/cpp/monlib/encode/format.cpp
@@ -0,0 +1,202 @@
+#include "format.h"
+
+#include <util/string/ascii.h>
+#include <util/string/split.h>
+#include <util/string/strip.h>
+#include <util/stream/output.h>
+#include <util/string/cast.h>
+
+namespace NMonitoring {
+ static ECompression CompressionFromHeader(TStringBuf value) {
+ if (value.empty()) {
+ return ECompression::UNKNOWN;
+ }
+
+ for (const auto& it : StringSplitter(value).Split(',').SkipEmpty()) {
+ TStringBuf token = StripString(it.Token());
+
+ if (AsciiEqualsIgnoreCase(token, NFormatContentEncoding::IDENTITY)) {
+ return ECompression::IDENTITY;
+ } else if (AsciiEqualsIgnoreCase(token, NFormatContentEncoding::ZLIB)) {
+ return ECompression::ZLIB;
+ } else if (AsciiEqualsIgnoreCase(token, NFormatContentEncoding::LZ4)) {
+ return ECompression::LZ4;
+ } else if (AsciiEqualsIgnoreCase(token, NFormatContentEncoding::ZSTD)) {
+ return ECompression::ZSTD;
+ }
+ }
+
+ return ECompression::UNKNOWN;
+ }
+
+ static EFormat FormatFromHttpMedia(TStringBuf value) {
+ if (AsciiEqualsIgnoreCase(value, NFormatContenType::SPACK)) {
+ return EFormat::SPACK;
+ } else if (AsciiEqualsIgnoreCase(value, NFormatContenType::JSON)) {
+ return EFormat::JSON;
+ } else if (AsciiEqualsIgnoreCase(value, NFormatContenType::PROTOBUF)) {
+ return EFormat::PROTOBUF;
+ } else if (AsciiEqualsIgnoreCase(value, NFormatContenType::TEXT)) {
+ return EFormat::TEXT;
+ } else if (AsciiEqualsIgnoreCase(value, NFormatContenType::PROMETHEUS)) {
+ return EFormat::PROMETHEUS;
+ }
+
+ return EFormat::UNKNOWN;
+ }
+
+ EFormat FormatFromAcceptHeader(TStringBuf value) {
+ EFormat result{EFormat::UNKNOWN};
+
+ for (const auto& it : StringSplitter(value).Split(',').SkipEmpty()) {
+ TStringBuf token = StripString(it.Token());
+
+ result = FormatFromHttpMedia(token);
+ if (result != EFormat::UNKNOWN) {
+ break;
+ }
+ }
+
+ return result;
+ }
+
+ EFormat FormatFromContentType(TStringBuf value) {
+ value = value.NextTok(';');
+
+ return FormatFromHttpMedia(value);
+ }
+
+ TStringBuf ContentTypeByFormat(EFormat format) {
+ switch (format) {
+ case EFormat::SPACK:
+ return NFormatContenType::SPACK;
+ case EFormat::JSON:
+ return NFormatContenType::JSON;
+ case EFormat::PROTOBUF:
+ return NFormatContenType::PROTOBUF;
+ case EFormat::TEXT:
+ return NFormatContenType::TEXT;
+ case EFormat::PROMETHEUS:
+ return NFormatContenType::PROMETHEUS;
+ case EFormat::UNKNOWN:
+ return TStringBuf();
+ }
+
+ Y_FAIL(); // for GCC
+ }
+
+ ECompression CompressionFromAcceptEncodingHeader(TStringBuf value) {
+ return CompressionFromHeader(value);
+ }
+
+ ECompression CompressionFromContentEncodingHeader(TStringBuf value) {
+ return CompressionFromHeader(value);
+ }
+
+ TStringBuf ContentEncodingByCompression(ECompression compression) {
+ switch (compression) {
+ case ECompression::IDENTITY:
+ return NFormatContentEncoding::IDENTITY;
+ case ECompression::ZLIB:
+ return NFormatContentEncoding::ZLIB;
+ case ECompression::LZ4:
+ return NFormatContentEncoding::LZ4;
+ case ECompression::ZSTD:
+ return NFormatContentEncoding::ZSTD;
+ case ECompression::UNKNOWN:
+ return TStringBuf();
+ }
+
+ Y_FAIL(); // for GCC
+ }
+
+}
+
+template <>
+NMonitoring::EFormat FromStringImpl<NMonitoring::EFormat>(const char* str, size_t len) {
+ using NMonitoring::EFormat;
+ TStringBuf value(str, len);
+ if (value == TStringBuf("SPACK")) {
+ return EFormat::SPACK;
+ } else if (value == TStringBuf("JSON")) {
+ return EFormat::JSON;
+ } else if (value == TStringBuf("PROTOBUF")) {
+ return EFormat::PROTOBUF;
+ } else if (value == TStringBuf("TEXT")) {
+ return EFormat::TEXT;
+ } else if (value == TStringBuf("PROMETHEUS")) {
+ return EFormat::PROMETHEUS;
+ } else if (value == TStringBuf("UNKNOWN")) {
+ return EFormat::UNKNOWN;
+ }
+ ythrow yexception() << "unknown format: " << value;
+}
+
+template <>
+void Out<NMonitoring::EFormat>(IOutputStream& o, NMonitoring::EFormat f) {
+ using NMonitoring::EFormat;
+ switch (f) {
+ case EFormat::SPACK:
+ o << TStringBuf("SPACK");
+ return;
+ case EFormat::JSON:
+ o << TStringBuf("JSON");
+ return;
+ case EFormat::PROTOBUF:
+ o << TStringBuf("PROTOBUF");
+ return;
+ case EFormat::TEXT:
+ o << TStringBuf("TEXT");
+ return;
+ case EFormat::PROMETHEUS:
+ o << TStringBuf("PROMETHEUS");
+ return;
+ case EFormat::UNKNOWN:
+ o << TStringBuf("UNKNOWN");
+ return;
+ }
+
+ Y_FAIL(); // for GCC
+}
+
+template <>
+NMonitoring::ECompression FromStringImpl<NMonitoring::ECompression>(const char* str, size_t len) {
+ using NMonitoring::ECompression;
+ TStringBuf value(str, len);
+ if (value == TStringBuf("IDENTITY")) {
+ return ECompression::IDENTITY;
+ } else if (value == TStringBuf("ZLIB")) {
+ return ECompression::ZLIB;
+ } else if (value == TStringBuf("LZ4")) {
+ return ECompression::LZ4;
+ } else if (value == TStringBuf("ZSTD")) {
+ return ECompression::ZSTD;
+ } else if (value == TStringBuf("UNKNOWN")) {
+ return ECompression::UNKNOWN;
+ }
+ ythrow yexception() << "unknown compression: " << value;
+}
+
+template <>
+void Out<NMonitoring::ECompression>(IOutputStream& o, NMonitoring::ECompression c) {
+ using NMonitoring::ECompression;
+ switch (c) {
+ case ECompression::IDENTITY:
+ o << TStringBuf("IDENTITY");
+ return;
+ case ECompression::ZLIB:
+ o << TStringBuf("ZLIB");
+ return;
+ case ECompression::LZ4:
+ o << TStringBuf("LZ4");
+ return;
+ case ECompression::ZSTD:
+ o << TStringBuf("ZSTD");
+ return;
+ case ECompression::UNKNOWN:
+ o << TStringBuf("UNKNOWN");
+ return;
+ }
+
+ Y_FAIL(); // for GCC
+}