Make addAttribute exception safe

This commit is contained in:
Frank Chen 2023-01-19 14:02:06 +08:00
parent b80ee8df50
commit 2fb49503da
2 changed files with 39 additions and 51 deletions

View File

@ -14,76 +14,65 @@ namespace OpenTelemetry
thread_local TracingContextOnThread current_thread_trace_context; thread_local TracingContextOnThread current_thread_trace_context;
void Span::addAttribute(std::string_view name, UInt64 value) bool Span::addAttribute(std::string_view name, UInt64 value) noexcept
{ {
if (!this->isTraceEnabled() || name.empty()) if (!this->isTraceEnabled() || name.empty())
return; return false;
this->attributes.push_back(Tuple{name, toString(value)}); return addAttribute(name, toString(value));
} }
void Span::addAttributeIfNotZero(std::string_view name, UInt64 value) bool Span::addAttributeIfNotZero(std::string_view name, UInt64 value) noexcept
{ {
if (value != 0) if (!this->isTraceEnabled() || name.empty() || value == 0)
addAttribute(name, value); return false;
return addAttribute(name, toString(value));
} }
void Span::addAttribute(std::string_view name, std::string_view value) bool Span::addAttribute(std::string_view name, std::string_view value) noexcept
{ {
if (!this->isTraceEnabled() || name.empty()) if (!this->isTraceEnabled() || name.empty())
return; return false;
this->attributes.push_back(Tuple{name, value}); try
{
this->attributes.push_back(Tuple{name, value});
}
catch (...)
{
return false;
}
return true;
} }
void Span::addAttributeIfNotEmpty(std::string_view name, std::string_view value) bool Span::addAttributeIfNotEmpty(std::string_view name, std::string_view value) noexcept
{ {
if (!this->isTraceEnabled() || name.empty() || value.empty()) return value.empty() ? false : addAttribute(name, value);
return;
this->attributes.push_back(Tuple{name, value});
} }
void Span::addAttribute(std::string_view name, std::function<String()> value_supplier) bool Span::addAttribute(std::string_view name, std::function<String()> value_supplier) noexcept
{ {
if (!this->isTraceEnabled() || !value_supplier) if (!this->isTraceEnabled() || !value_supplier)
return; return false;
String value = value_supplier(); return addAttributeIfNotEmpty(name, value_supplier());
if (value.empty())
return;
this->attributes.push_back(Tuple{name, value});
} }
void Span::addAttribute(const Exception & e) noexcept bool Span::addAttribute(const Exception & e) noexcept
{ {
if (!this->isTraceEnabled()) if (!this->isTraceEnabled())
return; return false;
try return addAttribute("clickhouse.exception", getExceptionMessage(e, false));
{
this->attributes.push_back(Tuple{"clickhouse.exception", getExceptionMessage(e, false)});
}
catch (...)
{
/// Ignore exceptions
}
} }
void Span::addAttribute(std::exception_ptr e) noexcept bool Span::addAttribute(std::exception_ptr e) noexcept
{ {
if (!this->isTraceEnabled() || e == nullptr) if (!this->isTraceEnabled() || e == nullptr)
return; return false;
try return addAttribute("clickhouse.exception", getExceptionMessage(e, false));
{
this->attributes.push_back(Tuple{"clickhouse.exception", getExceptionMessage(e, false)});
}
catch (...)
{
/// Ignore exceptions
}
} }
SpanHolder::SpanHolder(std::string_view _operation_name) SpanHolder::SpanHolder(std::string_view _operation_name)

View File

@ -23,16 +23,15 @@ struct Span
UInt64 finish_time_us = 0; UInt64 finish_time_us = 0;
Map attributes; Map attributes;
void addAttribute(std::string_view name, UInt64 value); /// Following methods are declared as noexcept to make sure they're exception safe
void addAttributeIfNotZero(std::string_view name, UInt64 value); /// This is because sometimes they will be called in exception handlers/dtor
void addAttribute(std::string_view name, std::string_view value); bool addAttribute(std::string_view name, UInt64 value) noexcept;
void addAttributeIfNotEmpty(std::string_view name, std::string_view value); bool addAttributeIfNotZero(std::string_view name, UInt64 value) noexcept;
void addAttribute(std::string_view name, std::function<String()> value_supplier); bool addAttribute(std::string_view name, std::string_view value) noexcept;
bool addAttributeIfNotEmpty(std::string_view name, std::string_view value) noexcept;
/// Following two methods are declared as noexcept to make sure they're exception safe bool addAttribute(std::string_view name, std::function<String()> value_supplier) noexcept;
/// This is because they're usually called in exception handler bool addAttribute(const Exception & e) noexcept;
void addAttribute(const Exception & e) noexcept; bool addAttribute(std::exception_ptr e) noexcept;
void addAttribute(std::exception_ptr e) noexcept;
bool isTraceEnabled() const bool isTraceEnabled() const
{ {