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
83 void Succeed() noexcept
84 {
85 status_ = CExports::CVTSS_Ok;
86 }
87
89
94 template <class T>
95 T Succeed(T &&result)
96 {
97 status_ = CExports::CVTSS_Ok;
98 return std::forward(result);
99 }
100
102
105 void Fail() noexcept
106 {
107 status_ = CExports::CVTSS_Error;
108 }
109
111
116 void Fail(const std::string &message) noexcept
117 {
118 statusMessage_ = message;
119 status_ = CExports::CVTSS_Error;
120 }
121
123
128 template <class T>
129 [[noreturn]] void Raise(const T &error)
130 {
131 statusMessage_ = error.what();
132 status_ = CExports::CVTSS_Error;
133 throw error;
134 }
135
137
142 template <class T>
143 T Return(T code) noexcept
144 {
145 status_ = (code) ? CExports::CVTSS_Error : CExports::CVTSS_Ok;
146 return code;
147 }
148
150
155 template <class T>
156 T ReturnBool(T res) noexcept
157 {
158 status_ = (res) ? CExports::CVTSS_Ok : CExports::CVTSS_Error;
159 return res;
160 }
161
163
168 void SetStatusMessage(const std::string &statusMessage) noexcept
169 {
170 statusMessage_ = statusMessage;
171 }
172
173 private:
174 explicit Span(const std::string &name) noexcept
175 : AttributeMap()
176 {
177 const auto &module = Module::Get();
178 if (!module.WouldTrace())
179 return;
180 CExports::CVTSPAN handle = nullptr;
181 CExports::CVTModuleStartSpan(module.Handle(), name.c_str(), handle);
182 handle_ = handle;
183 }
184
185 Span() noexcept = default;
186
187 CExports::CVTSpanStatus status_ = CExports::CVTSS_Unset;
188 std::string statusMessage_;
189 };
190 } // namespace Telemetry
191
192 CVB_END_INLINE_NS
193
194} // 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:143
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:168
T ReturnBool(T res) noexcept
Set the span status based on boolean interpretation of a result.
Definition span.hpp:156
T Succeed(T &&result)
Return a result and the span status to success.
Definition span.hpp:95
void Fail(const std::string &message) noexcept
Set the span status to fail with a message.
Definition span.hpp:116
void Fail() noexcept
Set the span status to fail.
Definition span.hpp:105
void Succeed() noexcept
Set the span status to success.
Definition span.hpp:83
void Raise(const T &error)
Set the span status to fail and raises an exception.
Definition span.hpp:129
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