CVB++ 15.1
Loading...
Searching...
No Matches
span.hpp
1#pragma once
2
3#include "module.hpp"
4#include "attribute_map.hpp"
5
6#include <exception>
7
8namespace Cvb
9{
10 CVB_BEGIN_INLINE_NS
11
12 namespace Telemetry
13 {
15
20 class Span : public AttributeMap
21 {
22
23 public:
25
29 static Span Create(const std::string &name)
30 {
31 return Span{name};
32 }
33
35
42 static Span Create()
43 {
44 return {};
45 }
46
47 Span(const Span &) = delete;
48 Span &operator=(const Span &) = delete;
49 Span(Span &&other) noexcept
50 : AttributeMap(std::move(other))
51 , statusMessage_(std::move(other.statusMessage_))
52 , status_(std::move(other.status_))
53
54 {
55 }
56
57 Span &operator=(Span &&other) noexcept
58 {
59 if (this != &other)
60 {
61 AttributeMap::operator=(std::move(other));
62 statusMessage_ = std::move(other.statusMessage_);
63 status_ = other.status_;
64 }
65 return *this;
66 }
67
68 ~Span()
69 {
70 if (!handle_)
71 return;
72#ifdef __cpp_lib_uncaught_exceptions
73 const bool hasUncaughtException = std::uncaught_exceptions() > 0;
74#else
75 const bool hasUncaughtException = std::uncaught_exception;
76#endif
77 if (status_ == CExports::CVTSS_Unset && hasUncaughtException)
78 status_ = CExports::CVTSS_Error;
79
80 Flush();
81 CExports::CVTSpanEnd(handle_, status_, (statusMessage_.size()) ? statusMessage_.c_str() : nullptr);
82 handle_ = nullptr;
83 }
84
86
89 void Succeed() noexcept
90 {
91 status_ = CExports::CVTSS_Ok;
92 }
93
95
100 template <class T>
101 T Succeed(T &&result)
102 {
103 status_ = CExports::CVTSS_Ok;
104 return std::forward<T>(result);
105 }
106
108
111 void Fail() noexcept
112 {
113 status_ = CExports::CVTSS_Error;
114 }
115
117
122 void Fail(const std::string &message) noexcept
123 {
124 statusMessage_ = message;
125 status_ = CExports::CVTSS_Error;
126 }
127
129
134 template <class T>
135 [[noreturn]] void Raise(const T &error)
136 {
137 statusMessage_ = error.what();
138 status_ = CExports::CVTSS_Error;
139 throw error;
140 }
141
143
148 template <class T>
149 T Return(T code) noexcept
150 {
151 status_ = (code) ? CExports::CVTSS_Error : CExports::CVTSS_Ok;
152 return code;
153 }
154
156
161 template <class T>
162 T ReturnBool(T res) noexcept
163 {
164 status_ = (res) ? CExports::CVTSS_Ok : CExports::CVTSS_Error;
165 return res;
166 }
167
169
174 void SetStatusMessage(const std::string &statusMessage) noexcept
175 {
176 statusMessage_ = statusMessage;
177 }
178
179 private:
180 explicit Span(const std::string &name) noexcept
181 : AttributeMap()
182 {
183 const auto &module = Module::Get();
184 if (!module.WouldTrace())
185 return;
186 CExports::CVTSPAN handle = nullptr;
187 CExports::CVTModuleStartSpan(module.Handle(), name.c_str(), handle);
188 handle_ = handle;
189 }
190
191 Span() noexcept = default;
192
193 CExports::CVTSpanStatus status_ = CExports::CVTSS_Unset;
194 std::string statusMessage_;
195 };
196 } // namespace Telemetry
197
198 CVB_END_INLINE_NS
199
200} // namespace Cvb
A attribute map for telemetry data.
Definition attribute_map.hpp:32
A span object to report traces.
Definition span.hpp:21
static Span Create(const std::string &name)
Creates a span with the given name.
Definition span.hpp:29
T Return(T code) noexcept
Set the span status based on return code evaluation.
Definition span.hpp:149
static Span Create()
Creates a dummy span.
Definition span.hpp:42
void SetStatusMessage(const std::string &statusMessage) noexcept
Set the status message.
Definition span.hpp:174
T ReturnBool(T res) noexcept
Set the span status based on boolean interpretation of a result.
Definition span.hpp:162
T Succeed(T &&result)
Return a result and the span status to success.
Definition span.hpp:101
void Fail(const std::string &message) noexcept
Set the span status to fail with a message.
Definition span.hpp:122
void Fail() noexcept
Set the span status to fail.
Definition span.hpp:111
void Succeed() noexcept
Set the span status to success.
Definition span.hpp:89
void Raise(const T &error)
Set the span status to fail and raises an exception.
Definition span.hpp:135
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