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/lwtrace/custom_action.h | |
download | ydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/lwtrace/custom_action.h')
-rw-r--r-- | library/cpp/lwtrace/custom_action.h | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/library/cpp/lwtrace/custom_action.h b/library/cpp/lwtrace/custom_action.h new file mode 100644 index 0000000000..92a3c66b84 --- /dev/null +++ b/library/cpp/lwtrace/custom_action.h @@ -0,0 +1,75 @@ +#pragma once + +#include "probe.h" + +#include <library/cpp/lwtrace/protos/lwtrace.pb.h> + +#include <util/generic/hash.h> + +#include <functional> + +namespace NLWTrace { + class TSession; + + // Custom action can save any stuff (derived from IResource) in TSession object + // IMPORTANT: Derived class will be used from multiple threads! (see example3) + class IResource: public TAtomicRefCount<IResource> { + public: + virtual ~IResource() { + } + }; + using TResourcePtr = TIntrusivePtr<IResource>; + + // Trace resources that is used to hold/get/create any stuff + class TTraceResources: public THashMap<TString, TResourcePtr> { + public: + template <class T> + T& Get(const TString& name) { + auto iter = find(name); + if (iter == end()) { + iter = insert(value_type(name, TResourcePtr(new T()))).first; + } + return *static_cast<T*>(iter->second.Get()); + } + + template <class T> + const T* GetOrNull(const TString& name) const { + auto iter = find(name); + if (iter == end()) { + return nullptr; + } + return *iter->second; + } + }; + + // Base class of all custom actions + class TCustomActionExecutor: public IExecutor { + protected: + TProbe* const Probe; + bool Destructive; + + public: + TCustomActionExecutor(TProbe* probe, bool destructive) + : IExecutor() + , Probe(probe) + , Destructive(destructive) + { + } + + bool IsDestructive() { + return Destructive; + } + }; + + // Factory to produce custom action executors + class TCustomActionFactory { + public: + using TCallback = std::function<TCustomActionExecutor*(TProbe* probe, const TCustomAction& action, TSession* trace)>; + TCustomActionExecutor* Create(TProbe* probe, const TCustomAction& action, TSession* trace) const; + void Register(const TString& name, const TCallback& callback); + + private: + THashMap<TString, TCallback> Callbacks; + }; + +} |