Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions api/include/opentelemetry/baggage/baggage_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ static const std::string kBaggageHeader = "baggage";
inline nostd::shared_ptr<Baggage> GetBaggage(const context::Context &context) noexcept
{
context::ContextValue context_value = context.GetValue(kBaggageHeader);
if (nostd::holds_alternative<nostd::shared_ptr<Baggage>>(context_value))

if (const nostd::shared_ptr<Baggage> *value =
nostd::get_if<nostd::shared_ptr<Baggage>>(&context_value))
{
return nostd::get<nostd::shared_ptr<Baggage>>(context_value);
return *value;
}
static nostd::shared_ptr<Baggage> empty_baggage{new Baggage()};
return empty_baggage;
Expand Down
38 changes: 38 additions & 0 deletions api/include/opentelemetry/common/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,44 @@ point.
# define OPENTELEMETRY_HAVE_EXCEPTIONS 0
#endif

// Exception handling macros
//
// When exceptions are enabled, expands to real try/catch.
// When exceptions are disabled, expands to if(true)/else-if(false)
//
// Usage:
// bool NoexceptMethod() noexcept
// {
// OPENTELEMETRY_TRY
// {
// return ThrowingMethod();
// }
// OPENTELEMETRY_CATCH_ALL
// {
// return false;
// }
// }
#if OPENTELEMETRY_HAVE_EXCEPTIONS
# define OPENTELEMETRY_TRY try
# define OPENTELEMETRY_CATCH_ALL catch (...)
#else
# define OPENTELEMETRY_TRY if (true)
# define OPENTELEMETRY_CATCH_ALL else
#endif

// Marks a path as unreachable
#if defined(__cpp_lib_unreachable) && (__cpp_lib_unreachable >= 202202L)
# include <utility>
# define OPENTELEMETRY_UNREACHABLE() std::unreachable()
#elif OPENTELEMETRY_HAVE_BUILTIN(__builtin_unreachable)
# define OPENTELEMETRY_UNREACHABLE() __builtin_unreachable()
#elif defined(_MSC_VER)
# define OPENTELEMETRY_UNREACHABLE() __assume(0)
#else
# include <cstdlib>
# define OPENTELEMETRY_UNREACHABLE() abort()
#endif

/*
OPENTELEMETRY_ATTRIBUTE_LIFETIME_BOUND indicates that a resource owned by a function
parameter or implicit object parameter is retained by the return value of the
Expand Down
11 changes: 10 additions & 1 deletion api/include/opentelemetry/common/string_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <ctype.h>

#include "opentelemetry/common/macros.h"
#include "opentelemetry/nostd/string_view.h"
#include "opentelemetry/version.h"

Expand All @@ -25,7 +26,15 @@ class StringUtil
{
right--;
}
return str.substr(left, 1 + right - left);
OPENTELEMETRY_TRY
{
return str.substr(left, 1 + right - left);
}
OPENTELEMETRY_CATCH_ALL
{
return nostd::string_view();
}
OPENTELEMETRY_UNREACHABLE();
}

static nostd::string_view Trim(nostd::string_view str) noexcept
Expand Down
2 changes: 1 addition & 1 deletion api/include/opentelemetry/nostd/function_ref.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ class function_ref<R(Args...)>
std::is_convertible<typename std::result_of<F &(Args...)>::type, R>::value,
#endif
int>::type = 0>
// NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward)
function_ref(F &&f)
{
// Binding by named variable here intentionally keeps function_ref non-owning.
// NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward)
BindTo(f); // not forward
}

Expand Down
2 changes: 1 addition & 1 deletion api/include/opentelemetry/nostd/string_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class string_view
{
if (pos > length_)
{
# if __EXCEPTIONS
# if OPENTELEMETRY_HAVE_EXCEPTIONS
throw std::out_of_range{"opentelemetry::nostd::string_view"};
# else
std::terminate();
Expand Down
2 changes: 1 addition & 1 deletion api/include/opentelemetry/nostd/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
// Header-only. Without compiling the actual Abseil binary. As Abseil moves on to new
// toolchains, it may drop support for Visual Studio 2015 in future versions.

# if defined(__EXCEPTIONS)
# if OPENTELEMETRY_HAVE_EXCEPTIONS
# include <exception>
OPENTELEMETRY_BEGIN_NAMESPACE
namespace nostd
Expand Down
26 changes: 10 additions & 16 deletions api/include/opentelemetry/plugin/detail/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,30 @@

#pragma once

#if __EXCEPTIONS
# include <new>
#endif // __EXCEPTIONS
#include "opentelemetry/version.h"

#include <string>

#include "opentelemetry/version.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace plugin
{
namespace detail
{
inline void CopyErrorMessage(const char *source, std::string &destination) noexcept
#if __EXCEPTIONS
try
#endif
{
if (source == nullptr)
OPENTELEMETRY_TRY
{
if (source == nullptr)
{
return;
}
destination.assign(source);
}
OPENTELEMETRY_CATCH_ALL
{
return;
}
destination.assign(source);
}
#if __EXCEPTIONS
catch (const std::bad_alloc &)
{
return;
}
#endif
} // namespace detail
} // namespace plugin
OPENTELEMETRY_END_NAMESPACE
4 changes: 2 additions & 2 deletions api/include/opentelemetry/std/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ using monostate = std::monostate;
// Apple Platforms provide std::bad_variant_access only in newer versions of OS.
// To keep API compatible with any version of OS - we are providing our own
// implementation of nostd::bad_variant_access exception.
# if __EXCEPTIONS
# if OPENTELEMETRY_HAVE_EXCEPTIONS

// nostd::bad_variant_access
class bad_variant_access : public std::exception
Expand All @@ -48,7 +48,7 @@ class bad_variant_access : public std::exception
}
# endif

# if __EXCEPTIONS
# if OPENTELEMETRY_HAVE_EXCEPTIONS
# define THROW_BAD_VARIANT_ACCESS throw_bad_variant_access()
# else
# define THROW_BAD_VARIANT_ACCESS std::terminate()
Expand Down
11 changes: 6 additions & 5 deletions api/include/opentelemetry/trace/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,21 @@ namespace trace
// Get Span from explicit context
inline nostd::shared_ptr<Span> GetSpan(const context::Context &context) noexcept
{
context::ContextValue span = context.GetValue(kSpanKey);
if (nostd::holds_alternative<nostd::shared_ptr<Span>>(span))
context::ContextValue span_value = context.GetValue(kSpanKey);
if (const nostd::shared_ptr<Span> *value = nostd::get_if<nostd::shared_ptr<Span>>(&span_value))
{
return nostd::get<nostd::shared_ptr<Span>>(span);
return *value;
}
return nostd::shared_ptr<Span>(new DefaultSpan(SpanContext::GetInvalid()));
}

// Check if the context is from a root span
inline bool IsRootSpan(const context::Context &context) noexcept
{
context::ContextValue is_root_span = context.GetValue(kIsRootSpanKey);
if (nostd::holds_alternative<bool>(is_root_span))
if (const bool *value = nostd::get_if<bool>(&is_root_span))
{
return nostd::get<bool>(is_root_span);
return *value;
}
return false;
}
Expand Down
146 changes: 89 additions & 57 deletions api/include/opentelemetry/trace/trace_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,36 +54,44 @@ class OPENTELEMETRY_EXPORT TraceState
*/
static nostd::shared_ptr<TraceState> FromHeader(nostd::string_view header) noexcept
{

common::KeyValueStringTokenizer kv_str_tokenizer(header);
size_t cnt = kv_str_tokenizer.NumTokens(); // upper bound on number of kv pairs
if (cnt > kMaxKeyValuePairs)
{
// trace state should be discarded if count exceeds
return GetDefault();
}

nostd::shared_ptr<TraceState> ts(new TraceState(cnt));
bool kv_valid{false};
nostd::string_view key, value;
while (kv_str_tokenizer.next(kv_valid, key, value) && ts->kv_properties_->Size() < cnt)
OPENTELEMETRY_TRY
{
if (kv_valid == false)
common::KeyValueStringTokenizer kv_str_tokenizer(header);
size_t cnt = kv_str_tokenizer.NumTokens(); // upper bound on number of kv pairs
if (cnt > kMaxKeyValuePairs)
{
// trace state should be discarded if count exceeds
return GetDefault();
}

if (!IsValidKey(key) || !IsValidValue(value))
nostd::shared_ptr<TraceState> ts(new TraceState(cnt));
bool kv_valid{false};
nostd::string_view key;
nostd::string_view value;
while (kv_str_tokenizer.next(kv_valid, key, value) && ts->kv_properties_->Size() < cnt)
{
// invalid header. return empty TraceState
ts->kv_properties_.reset(new common::KeyValueProperties());
break;
if (kv_valid == false)
{
return GetDefault();
}

if (!IsValidKey(key) || !IsValidValue(value))
{
// invalid header. return empty TraceState
ts->kv_properties_.reset(new common::KeyValueProperties());
break;
}

ts->kv_properties_->AddEntry(key, value);
}

ts->kv_properties_->AddEntry(key, value);
return ts;
}

return ts;
OPENTELEMETRY_CATCH_ALL
{
return GetDefault();
}
OPENTELEMETRY_UNREACHABLE();
}

/**
Expand Down Expand Up @@ -117,11 +125,17 @@ class OPENTELEMETRY_EXPORT TraceState
*/
bool Get(nostd::string_view key, std::string &value) const noexcept
{
if (!IsValidKey(key))
OPENTELEMETRY_TRY
{
if (!IsValidKey(key))
{
return false;
}
}
OPENTELEMETRY_CATCH_ALL
{
return false;
}

return kv_properties_->GetValue(key, value);
}

Expand All @@ -138,29 +152,37 @@ class OPENTELEMETRY_EXPORT TraceState
nostd::shared_ptr<TraceState> Set(const nostd::string_view &key,
const nostd::string_view &value) noexcept
{
auto curr_size = kv_properties_->Size();
if (!IsValidKey(key) || !IsValidValue(value))
{
// max size reached or invalid key/value. Returning empty TraceState
return TraceState::GetDefault();
}
auto allocate_size = curr_size;
if (curr_size < kMaxKeyValuePairs)
OPENTELEMETRY_TRY
{
allocate_size += 1;
if (!IsValidKey(key) || !IsValidValue(value))
{
// max size reached or invalid key/value. Returning empty TraceState
return TraceState::GetDefault();
}
const auto curr_size = kv_properties_->Size();
auto allocate_size = curr_size;
if (curr_size < kMaxKeyValuePairs)
{
allocate_size += 1;
}
nostd::shared_ptr<TraceState> ts(new TraceState(allocate_size));
if (curr_size < kMaxKeyValuePairs)
{
// add new field first
ts->kv_properties_->AddEntry(key, value);
}
// add rest of the fields.
kv_properties_->GetAllEntries([&ts](nostd::string_view key, nostd::string_view value) {
ts->kv_properties_->AddEntry(key, value);
return true;
});
return ts;
}
nostd::shared_ptr<TraceState> ts(new TraceState(allocate_size));
if (curr_size < kMaxKeyValuePairs)
OPENTELEMETRY_CATCH_ALL
{
// add new field first
ts->kv_properties_->AddEntry(key, value);
return TraceState::GetDefault();
}
// add rest of the fields.
kv_properties_->GetAllEntries([&ts](nostd::string_view key, nostd::string_view value) {
ts->kv_properties_->AddEntry(key, value);
return true;
});
return ts;
OPENTELEMETRY_UNREACHABLE();
}

/**
Expand All @@ -171,25 +193,35 @@ class OPENTELEMETRY_EXPORT TraceState
*/
nostd::shared_ptr<TraceState> Delete(const nostd::string_view &key) noexcept
{
if (!IsValidKey(key))
OPENTELEMETRY_TRY
{
return TraceState::GetDefault();
if (!IsValidKey(key))
{
return TraceState::GetDefault();
}
const auto curr_size = kv_properties_->Size();
auto allocate_size = curr_size;
std::string unused;
if (kv_properties_->GetValue(key, unused))
{
allocate_size -= 1;
}
nostd::shared_ptr<TraceState> ts(new TraceState(allocate_size));
kv_properties_->GetAllEntries(
[&ts, &key](nostd::string_view e_key, nostd::string_view e_value) {
if (key != e_key)
{
ts->kv_properties_->AddEntry(e_key, e_value);
}
return true;
});
return ts;
}
auto curr_size = kv_properties_->Size();
auto allocate_size = curr_size;
std::string unused;
if (kv_properties_->GetValue(key, unused))
OPENTELEMETRY_CATCH_ALL
{
allocate_size -= 1;
return TraceState::GetDefault();
}
nostd::shared_ptr<TraceState> ts(new TraceState(allocate_size));
kv_properties_->GetAllEntries(
[&ts, &key](nostd::string_view e_key, nostd::string_view e_value) {
if (key != e_key)
ts->kv_properties_->AddEntry(e_key, e_value);
return true;
});
return ts;
OPENTELEMETRY_UNREACHABLE();
}

// Returns true if there are no keys, false otherwise.
Expand Down
Loading
Loading