Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add noexcept/const qualifier at missing places for Trace API. #1374

Merged
merged 5 commits into from
May 6, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
18 changes: 10 additions & 8 deletions api/include/opentelemetry/baggage/baggage.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ class Baggage
static constexpr char kMembersSeparator = ',';
static constexpr char kMetadataSeparator = ';';

Baggage() : kv_properties_(new opentelemetry::common::KeyValueProperties()) {}
Baggage(size_t size) : kv_properties_(new opentelemetry::common::KeyValueProperties(size)){};
Baggage() noexcept : kv_properties_(new opentelemetry::common::KeyValueProperties()) {}
Baggage(size_t size) noexcept
: kv_properties_(new opentelemetry::common::KeyValueProperties(size)){};

template <class T>
Baggage(const T &keys_and_values)
Baggage(const T &keys_and_values) noexcept
: kv_properties_(new opentelemetry::common::KeyValueProperties(keys_and_values))
{}

Expand All @@ -42,15 +43,16 @@ class Baggage
/* Get value for key in the baggage
@returns true if key is found, false otherwise
*/
bool GetValue(nostd::string_view key, std::string &value) const
bool GetValue(nostd::string_view key, std::string &value) const noexcept
{
return kv_properties_->GetValue(key, value);
}

/* Returns shared_ptr of new baggage object which contains new key-value pair. If key or value is
invalid, copy of current baggage is returned
*/
nostd::shared_ptr<Baggage> Set(const nostd::string_view &key, const nostd::string_view &value)
nostd::shared_ptr<Baggage> Set(const nostd::string_view &key,
const nostd::string_view &value) noexcept
{

nostd::shared_ptr<Baggage> baggage(new Baggage(kv_properties_->Size() + 1));
Expand Down Expand Up @@ -89,7 +91,7 @@ class Baggage
// if key does not exist, copy of current baggage is returned.
// Validity of key is not checked as invalid keys should never be populated in baggage in the
// first place.
nostd::shared_ptr<Baggage> Delete(nostd::string_view key)
nostd::shared_ptr<Baggage> Delete(nostd::string_view key) noexcept
{
// keeping size of baggage same as key might not be found in it
nostd::shared_ptr<Baggage> baggage(new Baggage(kv_properties_->Size()));
Expand All @@ -103,7 +105,7 @@ class Baggage
}

// Returns shared_ptr of baggage after extracting key-value pairs from header
static nostd::shared_ptr<Baggage> FromHeader(nostd::string_view header)
static nostd::shared_ptr<Baggage> FromHeader(nostd::string_view header) noexcept
{
if (header.size() > kMaxSize)
{
Expand Down Expand Up @@ -158,7 +160,7 @@ class Baggage
}

// Creates string from baggage object.
std::string ToHeader() const
std::string ToHeader() const noexcept
{
std::string header_s;
bool first = true;
Expand Down
7 changes: 4 additions & 3 deletions api/include/opentelemetry/baggage/baggage_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace baggage
static const std::string kBaggageHeader = "baggage";

inline nostd::shared_ptr<opentelemetry::baggage::Baggage> GetBaggage(
const opentelemetry::context::Context &context)
const opentelemetry::context::Context &context) noexcept
{
context::ContextValue context_value = context.GetValue(kBaggageHeader);
if (nostd::holds_alternative<nostd::shared_ptr<opentelemetry::baggage::Baggage>>(context_value))
Expand All @@ -28,8 +28,9 @@ inline nostd::shared_ptr<opentelemetry::baggage::Baggage> GetBaggage(
return empty_baggage;
}

inline context::Context SetBaggage(opentelemetry::context::Context &context,
nostd::shared_ptr<opentelemetry::baggage::Baggage> baggage)
inline context::Context SetBaggage(
opentelemetry::context::Context &context,
nostd::shared_ptr<opentelemetry::baggage::Baggage> baggage) noexcept
{
return context.SetValue(kBaggageHeader, baggage);
}
Expand Down
22 changes: 11 additions & 11 deletions api/include/opentelemetry/common/kv_properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class KeyValueStringTokenizer
public:
KeyValueStringTokenizer(
nostd::string_view str,
const KeyValueStringTokenizerOptions &opts = KeyValueStringTokenizerOptions())
const KeyValueStringTokenizerOptions &opts = KeyValueStringTokenizerOptions()) noexcept
: str_(str), opts_(opts), index_(0)
{}

Expand All @@ -48,7 +48,7 @@ class KeyValueStringTokenizer
// @param key : key in kv pair
// @param key : value in kv pair
// @returns true if next kv pair was found, false otherwise.
bool next(bool &valid_kv, nostd::string_view &key, nostd::string_view &value)
bool next(bool &valid_kv, nostd::string_view &key, nostd::string_view &value) noexcept
{
valid_kv = true;
while (index_ < str_.size())
Expand Down Expand Up @@ -170,13 +170,13 @@ class KeyValueProperties
}

// Gets the key associated with this entry.
nostd::string_view GetKey() const { return key_.get(); }
nostd::string_view GetKey() const noexcept { return key_.get(); }

// Gets the value associated with this entry.
nostd::string_view GetValue() const { return value_.get(); }
nostd::string_view GetValue() const noexcept { return value_.get(); }

// Sets the value for this entry. This overrides the previous value.
void SetValue(nostd::string_view value) { value_ = CopyStringToPointer(value); }
void SetValue(nostd::string_view value) noexcept { value_ = CopyStringToPointer(value); }

private:
// Store key and value as raw char pointers to avoid using std::string.
Expand Down Expand Up @@ -206,15 +206,15 @@ class KeyValueProperties
public:
// Create Key-value list of given size
// @param size : Size of list.
KeyValueProperties(size_t size)
KeyValueProperties(size_t size) noexcept
: num_entries_(0), max_num_entries_(size), entries_(new Entry[size])
{}

// Create Empty Key-Value list
KeyValueProperties() : num_entries_(0), max_num_entries_(0), entries_(nullptr) {}
KeyValueProperties() noexcept : num_entries_(0), max_num_entries_(0), entries_(nullptr) {}

template <class T, class = typename std::enable_if<detail::is_key_value_iterable<T>::value>::type>
KeyValueProperties(const T &keys_and_values)
KeyValueProperties(const T &keys_and_values) noexcept
: num_entries_(0),
max_num_entries_(keys_and_values.size()),
entries_(new Entry[max_num_entries_])
Expand All @@ -227,7 +227,7 @@ class KeyValueProperties
}

// Adds new kv pair into kv properties
void AddEntry(nostd::string_view key, nostd::string_view value)
void AddEntry(nostd::string_view key, nostd::string_view value) noexcept
{
if (num_entries_ < max_num_entries_)
{
Expand All @@ -238,7 +238,7 @@ class KeyValueProperties

// Returns all kv pair entries
bool GetAllEntries(
nostd::function_ref<bool(nostd::string_view, nostd::string_view)> callback) const
nostd::function_ref<bool(nostd::string_view, nostd::string_view)> callback) const noexcept
{
for (size_t i = 0; i < num_entries_; i++)
{
Expand All @@ -252,7 +252,7 @@ class KeyValueProperties
}

// Return value for key if exists, return false otherwise
bool GetValue(nostd::string_view key, std::string &value) const
bool GetValue(nostd::string_view key, std::string &value) const noexcept
{
for (size_t i = 0; i < num_entries_; i++)
{
Expand Down
4 changes: 2 additions & 2 deletions api/include/opentelemetry/common/string_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace common
class StringUtil
{
public:
static nostd::string_view Trim(nostd::string_view str, size_t left, size_t right)
static nostd::string_view Trim(nostd::string_view str, size_t left, size_t right) noexcept
{
while (str[static_cast<std::size_t>(left)] == ' ' && left <= right)
{
Expand All @@ -39,7 +39,7 @@ class StringUtil
return str.substr(left, 1 + right - left);
}

static nostd::string_view Trim(nostd::string_view str)
static nostd::string_view Trim(nostd::string_view str) noexcept
{
if (str.empty())
{
Expand Down
4 changes: 2 additions & 2 deletions api/include/opentelemetry/context/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ class Context
// Creates a context object from a map of keys and identifiers, this will
// hold a shared_ptr to the head of the DataList linked list
template <class T>
Context(const T &keys_and_values)
Context(const T &keys_and_values) noexcept
{
head_ = nostd::shared_ptr<DataList>{new DataList(keys_and_values)};
}

// Creates a context object from a key and value, this will
// hold a shared_ptr to the head of the DataList linked list
Context(nostd::string_view key, ContextValue value)
Context(nostd::string_view key, ContextValue value) noexcept
{
head_ = nostd::shared_ptr<DataList>{new DataList(key, value)};
}
Expand Down
2 changes: 1 addition & 1 deletion api/include/opentelemetry/context/runtime_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ class RuntimeContext
}
};

inline Token::~Token()
inline Token::~Token() noexcept
{
context::RuntimeContext::Detach(*this);
}
Expand Down
4 changes: 2 additions & 2 deletions api/include/opentelemetry/trace/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace trace
{

// Get Span from explicit context
inline nostd::shared_ptr<Span> GetSpan(const opentelemetry::context::Context &context)
inline nostd::shared_ptr<Span> GetSpan(const opentelemetry::context::Context &context) noexcept
{
context::ContextValue span = context.GetValue(kSpanKey);
if (nostd::holds_alternative<nostd::shared_ptr<Span>>(span))
Expand All @@ -24,7 +24,7 @@ inline nostd::shared_ptr<Span> GetSpan(const opentelemetry::context::Context &co

// Set Span into explicit context
inline context::Context SetSpan(opentelemetry::context::Context &context,
nostd::shared_ptr<Span> span)
nostd::shared_ptr<Span> span) noexcept
{
return context.SetValue(kSpanKey, span);
}
Expand Down
8 changes: 4 additions & 4 deletions api/include/opentelemetry/trace/default_span.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ class DefaultSpan : public Span

void End(const EndSpanOptions & /* options */ = {}) noexcept {}

nostd::string_view ToString() { return "DefaultSpan"; }
nostd::string_view ToString() const noexcept { return "DefaultSpan"; }

DefaultSpan(SpanContext span_context) : span_context_(span_context) {}
DefaultSpan(SpanContext span_context) noexcept : span_context_(span_context) {}

// movable and copiable
DefaultSpan(DefaultSpan &&spn) : span_context_(spn.GetContext()) {}
DefaultSpan(const DefaultSpan &spn) : span_context_(spn.GetContext()) {}
DefaultSpan(DefaultSpan &&spn) noexcept : span_context_(spn.GetContext()) {}
DefaultSpan(const DefaultSpan &spn) noexcept : span_context_(spn.GetContext()) {}

private:
SpanContext span_context_;
Expand Down
9 changes: 5 additions & 4 deletions api/include/opentelemetry/trace/noop.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,15 @@ class NoopTracer final : public Tracer, public std::enable_shared_from_this<Noop
class NoopTracerProvider final : public opentelemetry::trace::TracerProvider
{
public:
NoopTracerProvider()
NoopTracerProvider() noexcept
: tracer_{nostd::shared_ptr<opentelemetry::trace::NoopTracer>(
new opentelemetry::trace::NoopTracer)}
{}

nostd::shared_ptr<opentelemetry::trace::Tracer> GetTracer(nostd::string_view library_name,
nostd::string_view library_version,
nostd::string_view schema_url) override
nostd::shared_ptr<opentelemetry::trace::Tracer> GetTracer(
nostd::string_view library_name,
nostd::string_view library_version,
nostd::string_view schema_url) noexcept override
{
return tracer_;
}
Expand Down
2 changes: 1 addition & 1 deletion api/include/opentelemetry/trace/span_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class SpanContext final
* sampled
* @param is_remote true if this context was propagated from a remote parent.
*/
SpanContext(bool sampled_flag, bool is_remote)
SpanContext(bool sampled_flag, bool is_remote) noexcept
: trace_id_(),
span_id_(),
trace_flags_(opentelemetry::trace::TraceFlags((uint8_t)sampled_flag)),
Expand Down
9 changes: 5 additions & 4 deletions api/include/opentelemetry/trace/trace_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class TraceState
* the W3C Trace Context specification https://www.w3.org/TR/trace-context/
* @return TraceState A new TraceState instance or DEFAULT
*/
static nostd::shared_ptr<TraceState> FromHeader(nostd::string_view header)
static nostd::shared_ptr<TraceState> FromHeader(nostd::string_view header) noexcept
{

common::KeyValueStringTokenizer kv_str_tokenizer(header);
Expand Down Expand Up @@ -89,7 +89,7 @@ class TraceState
/**
* Creates a w3c tracestate header from TraceState object
*/
std::string ToHeader()
std::string ToHeader() const noexcept
{
std::string header_s;
bool first = true;
Expand Down Expand Up @@ -135,7 +135,8 @@ class TraceState
*
* If the existing object has maximum list members, it's copy is returned.
*/
nostd::shared_ptr<TraceState> Set(const nostd::string_view &key, const nostd::string_view &value)
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))
Expand Down Expand Up @@ -168,7 +169,7 @@ class TraceState
* @returns empty TraceState object if key is invalid
* @returns copy of original TraceState object if key is not present (??)
*/
nostd::shared_ptr<TraceState> Delete(const nostd::string_view &key)
nostd::shared_ptr<TraceState> Delete(const nostd::string_view &key) noexcept
{
if (!IsValidKey(key))
{
Expand Down
2 changes: 1 addition & 1 deletion api/include/opentelemetry/trace/tracer_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class TracerProvider
*/
virtual nostd::shared_ptr<Tracer> GetTracer(nostd::string_view library_name,
nostd::string_view library_version = "",
nostd::string_view schema_url = "") = 0;
nostd::string_view schema_url = "") noexcept = 0;
};
} // namespace trace
OPENTELEMETRY_END_NAMESPACE
2 changes: 1 addition & 1 deletion api/test/trace/provider_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class TestProvider : public TracerProvider
{
nostd::shared_ptr<Tracer> GetTracer(nostd::string_view library_name,
nostd::string_view library_version,
nostd::string_view schema_url) override
nostd::string_view schema_url) noexcept override
{
return nostd::shared_ptr<Tracer>(nullptr);
}
Expand Down