forked from open-telemetry/opentelemetry-collector
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a minimal Tracer and Span API (open-telemetry#25)
* Add a unique_ptr * Fill out more unique_ptr functionality * Add missing assignment * Fill out unique_ptr * Add unique_ptr tests * Add equality comparison * Add documentation * Add minimal Tracer interface * Update cmake * Add tests to cmake * Fix unused variable * Fix comments * Remove unused include * Fix build for gcc48 * Fix test prefix
- Loading branch information
Showing
12 changed files
with
440 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
#pragma once | ||
|
||
#include <cstddef> | ||
#include <memory> | ||
#include <type_traits> | ||
#include <utility> | ||
|
||
namespace opentelemetry | ||
{ | ||
namespace nostd | ||
{ | ||
/** | ||
* Provide a simplified port of std::unique_ptr that has ABI stability. | ||
* | ||
* Note: This implementation doesn't allow for a custom deleter or array specializations. | ||
*/ | ||
template <class T> | ||
class unique_ptr | ||
{ | ||
public: | ||
unique_ptr() noexcept : ptr_{nullptr} {} | ||
|
||
unique_ptr(std::nullptr_t) noexcept : ptr_{nullptr} {} | ||
|
||
explicit unique_ptr(T *ptr) noexcept : ptr_{ptr} {} | ||
|
||
unique_ptr(unique_ptr &&other) noexcept : ptr_{other.release()} {} | ||
|
||
template <class U, | ||
typename std::enable_if<std::is_convertible<U *, T *>::value>::type * = nullptr> | ||
unique_ptr(unique_ptr<U> &&other) noexcept : ptr_{other.release()} | ||
{} | ||
|
||
template <class U, | ||
typename std::enable_if<std::is_convertible<U *, T *>::value>::type * = nullptr> | ||
unique_ptr(std::unique_ptr<U> &&other) noexcept : ptr_{other.release()} | ||
{} | ||
|
||
~unique_ptr() { reset(); } | ||
|
||
unique_ptr &operator=(unique_ptr &&other) noexcept | ||
{ | ||
reset(other.release()); | ||
return *this; | ||
} | ||
|
||
unique_ptr &operator=(std::nullptr_t) noexcept | ||
{ | ||
reset(); | ||
return *this; | ||
} | ||
|
||
template <class U, | ||
typename std::enable_if<std::is_convertible<U *, T *>::value>::type * = nullptr> | ||
unique_ptr &operator=(unique_ptr<U> &&other) noexcept | ||
{ | ||
reset(other.release()); | ||
return *this; | ||
} | ||
|
||
template <class U, | ||
typename std::enable_if<std::is_convertible<U *, T *>::value>::type * = nullptr> | ||
unique_ptr &operator=(std::unique_ptr<U> &&other) noexcept | ||
{ | ||
reset(other.release()); | ||
return *this; | ||
} | ||
|
||
operator std::unique_ptr<T>() && noexcept { return std::unique_ptr<T>{release()}; } | ||
|
||
operator bool() const noexcept { return ptr_ != nullptr; } | ||
|
||
T &operator*() const noexcept { return *ptr_; } | ||
|
||
T *operator->() const noexcept { return get(); } | ||
|
||
T *get() const noexcept { return ptr_; } | ||
|
||
void reset(T *ptr = nullptr) noexcept | ||
{ | ||
if (ptr_ != nullptr) | ||
{ | ||
delete ptr_; | ||
} | ||
ptr_ = ptr; | ||
} | ||
|
||
T *release() noexcept | ||
{ | ||
auto result = ptr_; | ||
ptr_ = nullptr; | ||
return result; | ||
} | ||
|
||
void swap(unique_ptr &other) noexcept { std::swap(ptr_, other.ptr_); } | ||
|
||
private: | ||
T *ptr_; | ||
}; | ||
|
||
template <class T1, class T2> | ||
bool operator==(const unique_ptr<T1> &lhs, const unique_ptr<T2> &rhs) noexcept | ||
{ | ||
return lhs.get() == rhs.get(); | ||
} | ||
|
||
template <class T> | ||
bool operator==(const unique_ptr<T> &lhs, std::nullptr_t) noexcept | ||
{ | ||
return lhs.get() == nullptr; | ||
} | ||
|
||
template <class T> | ||
bool operator==(std::nullptr_t, const unique_ptr<T> &rhs) noexcept | ||
{ | ||
return nullptr == rhs.get(); | ||
} | ||
|
||
template <class T1, class T2> | ||
bool operator!=(const unique_ptr<T1> &lhs, const unique_ptr<T2> &rhs) noexcept | ||
{ | ||
return lhs.get() != rhs.get(); | ||
} | ||
|
||
template <class T> | ||
bool operator!=(const unique_ptr<T> &lhs, std::nullptr_t) noexcept | ||
{ | ||
return lhs.get() != nullptr; | ||
} | ||
|
||
template <class T> | ||
bool operator!=(std::nullptr_t, const unique_ptr<T> &rhs) noexcept | ||
{ | ||
return nullptr != rhs.get(); | ||
} | ||
} // namespace nostd | ||
} // namespace opentelemetry |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#pragma once | ||
|
||
#include "opentelemetry/trace/tracer.h" | ||
|
||
#include <memory> | ||
|
||
namespace opentelemetry | ||
{ | ||
namespace trace | ||
{ | ||
/** | ||
* Noop implementation of Span. | ||
*/ | ||
class NoopSpan final : public Span | ||
{ | ||
public: | ||
explicit NoopSpan(const std::shared_ptr<Tracer> &tracer) noexcept : tracer_{tracer} {} | ||
|
||
// Span | ||
Tracer &tracer() const noexcept override { return *tracer_; } | ||
|
||
private: | ||
std::shared_ptr<Tracer> tracer_; | ||
}; | ||
|
||
/** | ||
* Noop implementation of Tracer | ||
*/ | ||
class NoopTracer final : public Tracer, public std::enable_shared_from_this<NoopTracer> | ||
{ | ||
public: | ||
// Tracer | ||
nostd::unique_ptr<Span> StartSpan(nostd::string_view /*name*/, | ||
const StartSpanOptions & /*options*/) noexcept override | ||
{ | ||
return nostd::unique_ptr<Span>{new (std::nothrow) NoopSpan{this->shared_from_this()}}; | ||
} | ||
}; | ||
} // namespace trace | ||
} // namespace opentelemetry |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#pragma once | ||
|
||
namespace opentelemetry | ||
{ | ||
namespace trace | ||
{ | ||
/** | ||
* StartSpanOptions provides options to set properties of span at the time of starting a new span. | ||
*/ | ||
struct StartSpanOptions | ||
{}; | ||
|
||
class Tracer; | ||
|
||
/** | ||
* A span represents a single operation within a trace. | ||
*/ | ||
class Span | ||
{ | ||
public: | ||
virtual ~Span() = default; | ||
|
||
virtual Tracer &tracer() const noexcept = 0; | ||
}; | ||
} // namespace trace | ||
} // namespace opentelemetry |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#pragma once | ||
|
||
#include "opentelemetry/nostd/string_view.h" | ||
#include "opentelemetry/nostd/unique_ptr.h" | ||
#include "opentelemetry/trace/span.h" | ||
|
||
namespace opentelemetry | ||
{ | ||
namespace trace | ||
{ | ||
/** | ||
* Handles span creation and in-process context propagation. | ||
* | ||
* This class provides methods for manipulating the context, creating spans, and controlling spans' | ||
* lifecycles. | ||
*/ | ||
class Tracer | ||
{ | ||
public: | ||
virtual ~Tracer() = default; | ||
|
||
/** | ||
* Starts a span. | ||
*/ | ||
virtual nostd::unique_ptr<Span> StartSpan(nostd::string_view name, | ||
const StartSpanOptions &options = {}) noexcept = 0; | ||
}; | ||
} // namespace trace | ||
} // namespace opentelemetry |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
add_subdirectory(nostd) | ||
add_subdirectory(trace) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.