Skip to content

Commit

Permalink
Restore Logger APIs with name
Browse files Browse the repository at this point in the history
Signed-off-by: owent <admin@owent.net>
  • Loading branch information
owent committed May 10, 2022
1 parent 44982f2 commit 3619762
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 3 deletions.
32 changes: 32 additions & 0 deletions api/include/opentelemetry/common/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,35 @@
# endif
# endif
#endif

#if defined(__cplusplus) && __cplusplus >= 201402L
# define OPENTELEMETRY_DEPRECATED [[deprecated]]
#elif defined(__clang__)
# define OPENTELEMETRY_DEPRECATED __attribute__((deprecated))
#elif defined(__GNUC__)
# define OPENTELEMETRY_DEPRECATED __attribute__((deprecated))
#elif defined(_MSC_VER)
# if _MSC_VER >= 1910 && defined(_MSVC_LANG) && _MSVC_LANG >= 201703L
# define OPENTELEMETRY_DEPRECATED [[deprecated]]
# else
# define OPENTELEMETRY_DEPRECATED __declspec(deprecated)
# endif
#else
# define OPENTELEMETRY_DEPRECATED
#endif

#if defined(__cplusplus) && __cplusplus >= 201402L
# define OPENTELEMETRY_DEPRECATED_MESSAGE(msg) [[deprecated(msg)]]
#elif defined(__clang__)
# define OPENTELEMETRY_DEPRECATED_MESSAGE(msg) __attribute__((deprecated(msg)))
#elif defined(__GNUC__)
# define OPENTELEMETRY_DEPRECATED_MESSAGE(msg) __attribute__((deprecated(msg)))
#elif defined(_MSC_VER)
# if _MSC_VER >= 1910 && defined(_MSVC_LANG) && _MSVC_LANG >= 201703L
# define OPENTELEMETRY_DEPRECATED_MESSAGE(msg) [[deprecated(msg)]]
# else
# define OPENTELEMETRY_DEPRECATED_MESSAGE(msg) __declspec(deprecated(msg))
# endif
#else
# define OPENTELEMETRY_DEPRECATED_MESSAGE(msg)
#endif
133 changes: 133 additions & 0 deletions api/include/opentelemetry/logs/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# include "opentelemetry/common/attribute_value.h"
# include "opentelemetry/common/key_value_iterable.h"
# include "opentelemetry/common/key_value_iterable_view.h"
# include "opentelemetry/common/macros.h"
# include "opentelemetry/common/timestamp.h"
# include "opentelemetry/logs/severity.h"
# include "opentelemetry/nostd/shared_ptr.h"
Expand Down Expand Up @@ -63,6 +64,34 @@ class Logger
trace::TraceFlags trace_flags,
common::SystemTimestamp timestamp) noexcept = 0;

/**
* Each of the following overloaded Log(...) methods
* creates a log message with the specific parameters passed.
*
* @param severity the severity level of the log event.
* @param name the name of the log event.
* @param message the string message of the log (perhaps support std::fmt or fmt-lib format).
* @param attributes the attributes, stored as a 2D list of key/value pairs, that are associated
* with the log event.
* @param trace_id the trace id associated with the log event.
* @param span_id the span id associate with the log event.
* @param trace_flags the trace flags associated with the log event.
* @param timestamp the timestamp the log record was created.
* @throws No exceptions under any circumstances.
*/
OPENTELEMETRY_DEPRECATED_MESSAGE("name will be removed in the future")
virtual void Log(Severity severity,
OPENTELEMETRY_MAYBE_UNUSED nostd::string_view name,
nostd::string_view body,
const common::KeyValueIterable &attributes,
trace::TraceId trace_id,
trace::SpanId span_id,
trace::TraceFlags trace_flags,
common::SystemTimestamp timestamp) noexcept
{
Log(severity, body, attributes, trace_id, span_id, trace_flags, timestamp);
}

/*** Overloaded methods for KeyValueIterables ***/
/**
* The secondary base Log(...) method that all other Log(...) overloaded methods except the one
Expand All @@ -82,6 +111,22 @@ class Logger
timestamp);
}

template <class T,
nostd::enable_if_t<common::detail::is_key_value_iterable<T>::value> * = nullptr>
OPENTELEMETRY_DEPRECATED_MESSAGE("name will be removed in the future")
void Log(Severity severity,
nostd::string_view name,
nostd::string_view body,
const T &attributes,
trace::TraceId trace_id,
trace::SpanId span_id,
trace::TraceFlags trace_flags,
common::SystemTimestamp timestamp) noexcept
{
Log(severity, name, body, common::KeyValueIterableView<T>(attributes), trace_id, span_id,
trace_flags, timestamp);
}

void Log(Severity severity,
nostd::string_view body,
std::initializer_list<std::pair<nostd::string_view, common::AttributeValue>> attributes,
Expand All @@ -96,6 +141,22 @@ class Logger
trace_id, span_id, trace_flags, timestamp);
}

OPENTELEMETRY_DEPRECATED_MESSAGE("name will be removed in the future")
void Log(Severity severity,
nostd::string_view name,
nostd::string_view body,
std::initializer_list<std::pair<nostd::string_view, common::AttributeValue>> attributes,
trace::TraceId trace_id,
trace::SpanId span_id,
trace::TraceFlags trace_flags,
common::SystemTimestamp timestamp) noexcept
{
return this->Log(severity, name, body,
nostd::span<const std::pair<nostd::string_view, common::AttributeValue>>{
attributes.begin(), attributes.end()},
trace_id, span_id, trace_flags, timestamp);
}

/** Wrapper methods that the user could call for convenience when logging **/

/**
Expand All @@ -108,6 +169,12 @@ class Logger
this->Log(severity, message, {}, {}, {}, {}, std::chrono::system_clock::now());
}

OPENTELEMETRY_DEPRECATED_MESSAGE("name will be removed in the future")
void Log(Severity severity, nostd::string_view name, nostd::string_view message) noexcept
{
this->Log(severity, name, message, {}, {}, {}, {}, std::chrono::system_clock::now());
}

/**
* Writes a log.
* @param severity The severity of the log
Expand Down Expand Up @@ -192,6 +259,17 @@ class Logger
*/
void Trace(nostd::string_view message) noexcept { this->Log(Severity::kTrace, message); }

/**
* Writes a log with a severity of trace.
* @param name The name of the log
* @param message The message to log
*/
OPENTELEMETRY_DEPRECATED_MESSAGE("name will be removed in the future")
void Trace(nostd::string_view name, nostd::string_view message) noexcept
{
this->Log(Severity::kTrace, name, message);
}

/**
* Writes a log with a severity of trace.
* @param attributes The attributes of the log as a key/value object
Expand Down Expand Up @@ -245,6 +323,17 @@ class Logger
*/
void Debug(nostd::string_view message) noexcept { this->Log(Severity::kDebug, message); }

/**
* Writes a log with a severity of debug.
* @param name The name of the log
* @param message The message to log
*/
OPENTELEMETRY_DEPRECATED_MESSAGE("name will be removed in the future")
void Debug(nostd::string_view name, nostd::string_view message) noexcept
{
this->Log(Severity::kDebug, name, message);
}

/**
* Writes a log with a severity of debug.
* @param attributes The attributes of the log as a key/value object
Expand Down Expand Up @@ -298,6 +387,17 @@ class Logger
*/
void Info(nostd::string_view message) noexcept { this->Log(Severity::kInfo, message); }

/**
* Writes a log with a severity of info.
* @param name The name of the log
* @param message The message to log
*/
OPENTELEMETRY_DEPRECATED_MESSAGE("name will be removed in the future")
void Info(nostd::string_view name, nostd::string_view message) noexcept
{
this->Log(Severity::kInfo, name, message);
}

/**
* Writes a log with a severity of info.
* @param attributes The attributes of the log as a key/value object
Expand Down Expand Up @@ -351,6 +451,17 @@ class Logger
*/
void Warn(nostd::string_view message) noexcept { this->Log(Severity::kWarn, message); }

/**
* Writes a log with a severity of warn.
* @param name The name of the log
* @param message The message to log
*/
OPENTELEMETRY_DEPRECATED_MESSAGE("name will be removed in the future")
void Warn(nostd::string_view name, nostd::string_view message) noexcept
{
this->Log(Severity::kWarn, name, message);
}

/**
* Writes a log with a severity of warn.
* @param attributes The attributes of the log as a key/value object
Expand Down Expand Up @@ -404,6 +515,17 @@ class Logger
*/
void Error(nostd::string_view message) noexcept { this->Log(Severity::kError, message); }

/**
* Writes a log with a severity of error.
* @param name The name of the log
* @param message The message to log
*/
OPENTELEMETRY_DEPRECATED_MESSAGE("name will be removed in the future")
void Error(nostd::string_view name, nostd::string_view message) noexcept
{
this->Log(Severity::kError, name, message);
}

/**
* Writes a log with a severity of error.
* @param attributes The attributes of the log as a key/value object
Expand Down Expand Up @@ -457,6 +579,17 @@ class Logger
*/
void Fatal(nostd::string_view message) noexcept { this->Log(Severity::kFatal, message); }

/**
* Writes a log with a severity of fatal.
* @param name The name of the log
* @param message The message to log
*/
OPENTELEMETRY_DEPRECATED_MESSAGE("name will be removed in the future")
void Fatal(nostd::string_view name, nostd::string_view message) noexcept
{
this->Log(Severity::kFatal, name, message);
}

/**
* Writes a log with a severity of fatal.
* @param attributes The attributes of the log as a key/value object
Expand Down
13 changes: 13 additions & 0 deletions api/test/logs/logger_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,19 @@ TEST(Logger, LogMethodOverloads)
logger->Log(Severity::kError, {{"key1", "value 1"}, {"key2", 2}});
logger->Log(Severity::kFatal, "Logging an initializer list", {{"key1", "value 1"}, {"key2", 2}});

// Deprecated Log overloads
logger->Log(Severity::kTrace, "Log name", "Test log message");
logger->Log(Severity::kWarn, "Log name", "Logging a map", m, {}, {}, {},
std::chrono::system_clock::now());
logger->Log(Severity::kError, "Log name", "Logging a map", {{"key1", "value 1"}, {"key2", 2}}, {},
{}, {}, std::chrono::system_clock::now());
logger->Trace("Log name", "Test log message");
logger->Debug("Log name", "Test log message");
logger->Info("Log name", "Test log message");
logger->Warn("Log name", "Test log message");
logger->Error("Log name", "Test log message");
logger->Fatal("Log name", "Test log message");

// Severity methods
logger->Trace("Test log message");
logger->Trace("Test log message", m);
Expand Down
32 changes: 29 additions & 3 deletions exporters/etw/include/opentelemetry/exporters/etw/etw_logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,40 @@ class Logger : public opentelemetry::logs::Logger
if (evt != nullptr)
{
// Pass as a reference to original modifyable collection without creating a copy
return Log(severity, body, *evt, trace_id, span_id, trace_flags, timestamp);
return Log(severity, provId, body, *evt, trace_id, span_id, trace_flags, timestamp);
}
# endif
Properties evtCopy = attributes;
return Log(severity, body, evtCopy, trace_id, span_id, trace_flags, timestamp);
return Log(severity, provId, body, evtCopy, trace_id, span_id, trace_flags, timestamp);
}

void Log(opentelemetry::logs::Severity severity,
nostd::string_view name,
nostd::string_view body,
const common::KeyValueIterable &attributes,
opentelemetry::trace::TraceId trace_id,
opentelemetry::trace::SpanId span_id,
opentelemetry::trace::TraceFlags trace_flags,
common::SystemTimestamp timestamp) noexcept override
{

# ifdef OPENTELEMETRY_RTTI_ENABLED
common::KeyValueIterable &attribs = const_cast<common::KeyValueIterable &>(attributes);
Properties *evt = dynamic_cast<Properties *>(&attribs);
// Properties *res = dynamic_cast<Properties *>(&resr);

if (evt != nullptr)
{
// Pass as a reference to original modifyable collection without creating a copy
return Log(severity, name, body, *evt, trace_id, span_id, trace_flags, timestamp);
}
# endif
Properties evtCopy = attributes;
return Log(severity, name, body, evtCopy, trace_id, span_id, trace_flags, timestamp);
}

virtual void Log(opentelemetry::logs::Severity severity,
nostd::string_view name,
nostd::string_view body,
Properties &evt,
opentelemetry::trace::TraceId trace_id,
Expand Down Expand Up @@ -161,7 +187,7 @@ class Logger : public opentelemetry::logs::Logger
ActivityIdPtr = &ActivityId;
}
}
evt[ETW_FIELD_PAYLOAD_NAME] = std::string(provId.data(), provId.length());
evt[ETW_FIELD_PAYLOAD_NAME] = std::string(name.data(), name.size());
std::chrono::system_clock::time_point ts = timestamp;
int64_t tsMs =
std::chrono::duration_cast<std::chrono::milliseconds>(ts.time_since_epoch()).count();
Expand Down

0 comments on commit 3619762

Please sign in to comment.