diff options
author | Daniil Cherednik <dan.cherednik@gmail.com> | 2023-03-31 10:54:08 +0300 |
---|---|---|
committer | Daniil Cherednik <dan.cherednik@gmail.com> | 2023-03-31 12:28:07 +0300 |
commit | fc1cffcfa7f0497a1f97b384a24bcbf23362f3be (patch) | |
tree | c15f7ab5b9e9b20fd0ef8fc07d598d28e8b32004 /library/cpp/unified_agent_client/backend.cpp | |
parent | 8a749596d40e91c896a1907afcd108d9221fbde1 (diff) | |
download | ydb-e9cbe5c5cf67db853d223fd365c9f05b695f7b96.tar.gz |
Ydb stable 23-1-1923.1.19
x-stable-origin-commit: c5d5a396e89d0a72e0267a55e93d8404d4fb54fe
Diffstat (limited to 'library/cpp/unified_agent_client/backend.cpp')
-rw-r--r-- | library/cpp/unified_agent_client/backend.cpp | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/library/cpp/unified_agent_client/backend.cpp b/library/cpp/unified_agent_client/backend.cpp new file mode 100644 index 0000000000..b3c4b4ebcf --- /dev/null +++ b/library/cpp/unified_agent_client/backend.cpp @@ -0,0 +1,112 @@ +#include "backend.h" + +#include <library/cpp/unified_agent_client/enum.h> + +#include <library/cpp/logger/record.h> + +#include <util/datetime/base.h> +#include <util/generic/guid.h> +#include <util/generic/serialized_enum.h> + +namespace NUnifiedAgent { + namespace { + class TDefaultRecordConverter : public IRecordConverter { + public: + TDefaultRecordConverter(bool stripTrailingNewLine) + : StripTrailingNewLine(stripTrailingNewLine) + , PriorityKey("_priority") + { + } + + TClientMessage Convert(const TLogRecord& rec) const override { + const auto stripTrailingNewLine = StripTrailingNewLine && + rec.Len > 0 && rec.Data[rec.Len - 1] == '\n'; + + THashMap<TString, TString> metaFlags{{PriorityKey, NameOf(rec.Priority)}}; + metaFlags.insert(rec.MetaFlags.begin(), rec.MetaFlags.end()); + + return { + TString(rec.Data, stripTrailingNewLine ? rec.Len - 1 : rec.Len), + std::move(metaFlags) + }; + } + + private: + const bool StripTrailingNewLine; + const TString PriorityKey; + }; + + class TClientSessionAdapter: public TLogBackend { + public: + explicit TClientSessionAdapter(const TClientSessionPtr& session, THolder<IRecordConverter> recordConverter) + : Session(session) + , RecordConverter(std::move(recordConverter)) + { + } + + void WriteData(const TLogRecord& rec) override { + Session->Send(RecordConverter->Convert(rec)); + } + + void ReopenLog() override { + } + + private: + TClientSessionPtr Session; + THolder<IRecordConverter> RecordConverter; + }; + + class TSessionHolder { + protected: + TSessionHolder(const TClientParameters& parameters, const TSessionParameters& sessionParameters) + : Client(MakeClient(parameters)) + , Session(Client->CreateSession(sessionParameters)) + { + } + + protected: + TClientPtr Client; + TClientSessionPtr Session; + }; + + class TAgentLogBackend: private TSessionHolder, public TClientSessionAdapter { + public: + TAgentLogBackend(const TClientParameters& parameters, + const TSessionParameters& sessionParameters, + THolder<IRecordConverter> recordConverter) + : TSessionHolder(parameters, sessionParameters) + , TClientSessionAdapter(TSessionHolder::Session, std::move(recordConverter)) + { + } + + ~TAgentLogBackend() override { + TSessionHolder::Session->Close(); + } + }; + } + + THolder<IRecordConverter> MakeDefaultRecordConverter(bool stripTrailingNewLine) { + return MakeHolder<TDefaultRecordConverter>(stripTrailingNewLine); + } + + THolder<TLogBackend> AsLogBackend(const TClientSessionPtr& session, bool stripTrailingNewLine) { + return MakeHolder<TClientSessionAdapter>(session, MakeDefaultRecordConverter(stripTrailingNewLine)); + } + + THolder<TLogBackend> MakeLogBackend(const TClientParameters& parameters, + const TSessionParameters& sessionParameters, + THolder<IRecordConverter> recordConverter) + { + if (!recordConverter) { + recordConverter = MakeDefaultRecordConverter(); + } + return MakeHolder<TAgentLogBackend>(parameters, sessionParameters, std::move(recordConverter)); + } + + THolder<::TLog> MakeLog(const TClientParameters& parameters, + const TSessionParameters& sessionParameters, + THolder<IRecordConverter> recordConverter) + { + return MakeHolder<::TLog>(MakeLogBackend(parameters, sessionParameters, std::move(recordConverter))); + } +} |