CVB++ 15.0
Loading...
Searching...
No Matches
span.hpp
1#pragma once
2
3#include "module.hpp"
4#include "attribute_map.hpp"
5
6namespace Cvb
7{
8 CVB_BEGIN_INLINE_NS
9
10 namespace Telemetry
11 {
13
18 class Span : public AttributeMap
19 {
20
21 public:
23
27 static Span Create(const std::string &name)
28 {
29 return Span{name};
30 }
31
33
40 static Span Create()
41 {
42 return {};
43 }
44
45 Span(const Span &) = delete;
46 Span &operator=(const Span &) = delete;
47 Span(Span &&other) noexcept
48 : AttributeMap(std::move(other))
49 , statusMessage_(std::move(other.statusMessage_))
50 , status_(std::move(other.status_))
51
52 {
53 }
54
55 Span &operator=(Span &&other) noexcept
56 {
57 if (this != &other)
58 {
59 AttributeMap::operator=(std::move(other));
60 statusMessage_ = std::move(other.statusMessage_);
61 status_ = other.status_;
62 }
63 return *this;
64 }
65
66 ~Span()
67 {
68 if (!handle_)
69 return;
70
71 if (status_ == CExports::CVTSS_Unset && std::uncaught_exceptions() > 0)
72 status_ = CExports::CVTSS_Error;
73
74 Flush();
75 CExports::CVTSpanEnd(handle_, status_, (statusMessage_.size()) ? statusMessage_.c_str() : nullptr);
76 handle_ = nullptr;
77 }
78
80
86 explicit operator bool() const noexcept
87 {
88 return handle_ != nullptr;
89 }
90
92
95 void Succeed() noexcept
96 {
97 status_ = CExports::CVTSS_Ok;
98 }
99
101
106 template <class T>
107 T Succeed(T &&result)
108 {
109 status_ = CExports::CVTSS_Ok;
110 return std::forward(result);
111 }
112
114
117 void Fail() noexcept
118 {
119 status_ = CExports::CVTSS_Error;
120 }
121
123
128 void Fail(const std::string &message) noexcept
129 {
130 statusMessage_ = message;
131 status_ = CExports::CVTSS_Error;
132 }
133
135
140 template <class T>
141 [[noreturn]] void Raise(const T &error)
142 {
143 statusMessage_ = error.what();
144 status_ = CExports::CVTSS_Error;
145 throw error;
146 }
147
149
154 template <class T>
155 T Return(T code) noexcept
156 {
157 status_ = (code) ? CExports::CVTSS_Error : CExports::CVTSS_Ok;
158 return code;
159 }
160
162
167 template <class T>
168 T ReturnBool(T res) noexcept
169 {
170 status_ = (res) ? CExports::CVTSS_Ok : CExports::CVTSS_Error;
171 return res;
172 }
173
175
180 void SetStatusMessage(const std::string &statusMessage) noexcept
181 {
182 statusMessage_ = statusMessage;
183 }
184
185 private:
186 explicit Span(const std::string &name) noexcept
187 : AttributeMap()
188 {
189 const auto &module = Module::Get();
190 if (!module.WouldTrace())
191 return;
192 CExports::CVTSPAN handle = nullptr;
193 CExports::CVTModuleStartSpan(module.Handle(), name.c_str(), handle);
194 handle_ = handle;
195 }
196
197 Span() noexcept = default;
198
199 CExports::CVTSpanStatus status_ = CExports::CVTSS_Unset;
200 std::string statusMessage_;
201 };
202 } // namespace Telemetry
203
204 CVB_END_INLINE_NS
205
206} // namespace Cvb
A attribute map for telemetry data.
Definition attribute_map.hpp:32
A span object to report traces.
Definition span.hpp:19
static Span Create(const std::string &name)
Creates a span with the given name.
Definition span.hpp:27
T Return(T code) noexcept
Set the span status based on return code evaluation.
Definition span.hpp:155
static Span Create()
Creates a dummy span.
Definition span.hpp:40
void SetStatusMessage(const std::string &statusMessage) noexcept
Set the status message.
Definition span.hpp:180
T ReturnBool(T res) noexcept
Set the span status based on boolean interpretation of a result.
Definition span.hpp:168
T Succeed(T &&result)
Return a result and the span status to success.
Definition span.hpp:107
void Fail(const std::string &message) noexcept
Set the span status to fail with a message.
Definition span.hpp:128
void Fail() noexcept
Set the span status to fail.
Definition span.hpp:117
void Succeed() noexcept
Set the span status to success.
Definition span.hpp:95
void Raise(const T &error)
Set the span status to fail and raises an exception.
Definition span.hpp:141
T forward(T... args)
T move(T... args)
Namespace for telemetry.
Definition attribute_map.hpp:16
Root namespace for the Image Manager interface.
Definition version.hpp:11