From f5a22e758b2947a351d11dcb2a37d8df1a8243af Mon Sep 17 00:00:00 2001 From: owent Date: Tue, 29 Nov 2022 14:19:03 +0800 Subject: [PATCH] Remove `EmitEvent` to follow https://github.com/open-telemetry/opentelemetry-specification/pull/2941 Signed-off-by: owent Fix `-Werror=suggest-override` and style Signed-off-by: owent Fix ostream_log_test Signed-off-by: owent Fix ostream print_value for `AttributeValue` Signed-off-by: owent New Recordable of logs Signed-off-by: owent Restore .vscode/launch.json Signed-off-by: owent Fix warning. Signed-off-by: owent Fix warnings in maintainer mode and ETW exporter Signed-off-by: owent Add CHANGELOG Signed-off-by: owent Allow to move 'nostd::unique_ptr' into `nostd::shared_ptr` Signed-off-by: owent Do not use `std/type_traits.h` any more. Maybe we should remove this file later. Signed-off-by: owent Allow to add rvalue into `CircularBuffer` Signed-off-by: owent Finish new `LogRecord` for exporters. Signed-off-by: owent Finish unit tests in API and SDK. Exporters are still work in progress. Signed-off-by: owent New `LogRecord` and `Recordable` implementations. Signed-off-by: WenTao Ou --- .vscode/launch.json | 44 +-- CHANGELOG.md | 2 + .../common/key_value_iterable_view.h | 8 + api/include/opentelemetry/logs/log_record.h | 81 +++++ api/include/opentelemetry/logs/logger.h | 217 ++++++------- api/include/opentelemetry/logs/noop.h | 21 +- api/include/opentelemetry/nostd/shared_ptr.h | 7 + api/test/logs/logger_test.cc | 39 +-- examples/otlp/http_log_main.cc | 2 +- exporters/elasticsearch/BUILD | 1 + exporters/elasticsearch/CMakeLists.txt | 2 +- .../elasticsearch/es_log_record_exporter.h | 7 +- .../elasticsearch/es_log_recordable.h | 194 +++--------- .../src/es_log_record_exporter.cc | 8 +- .../elasticsearch/src/es_log_recordable.cc | 296 ++++++++++++++++++ .../test/es_log_record_exporter_test.cc | 1 - .../opentelemetry/exporters/etw/etw_logger.h | 148 ++++++--- .../exporters/ostream/common_utils.h | 47 +++ .../exporters/ostream/log_record_exporter.h | 9 +- exporters/ostream/src/log_record_exporter.cc | 49 ++- exporters/ostream/test/ostream_log_test.cc | 128 ++++---- .../otlp/otlp_grpc_log_record_exporter.h | 8 +- .../otlp_grpc_log_record_exporter_factory.h | 4 +- .../otlp/otlp_http_log_record_exporter.h | 8 +- .../otlp_http_log_record_exporter_factory.h | 4 +- .../exporters/otlp/otlp_log_recordable.h | 73 ++--- .../otlp/otlp_populate_attribute_utils.h | 10 + .../exporters/otlp/otlp_recordable_utils.h | 8 +- .../otlp/src/otlp_grpc_log_record_exporter.cc | 9 +- .../otlp_grpc_log_record_exporter_factory.cc | 6 +- .../otlp/src/otlp_http_log_record_exporter.cc | 9 +- .../otlp_http_log_record_exporter_factory.cc | 6 +- exporters/otlp/src/otlp_log_recordable.cc | 195 ++++++------ .../otlp/src/otlp_populate_attribute_utils.cc | 123 +++++--- exporters/otlp/src/otlp_recordable_utils.cc | 6 +- .../otlp_grpc_log_record_exporter_test.cc | 9 +- .../otlp_http_log_record_exporter_test.cc | 3 +- .../otlp/test/otlp_log_recordable_test.cc | 149 ++++----- .../sdk/common/circular_buffer.h | 8 + .../sdk/logs/batch_log_record_processor.h | 10 +- .../logs/batch_log_record_processor_factory.h | 4 +- sdk/include/opentelemetry/sdk/logs/exporter.h | 13 +- .../opentelemetry/sdk/logs/log_record.h | 200 ------------ sdk/include/opentelemetry/sdk/logs/logger.h | 41 +-- .../opentelemetry/sdk/logs/logger_context.h | 6 +- .../sdk/logs/logger_context_factory.h | 8 +- .../opentelemetry/sdk/logs/logger_provider.h | 6 +- .../sdk/logs/logger_provider_factory.h | 18 +- .../sdk/logs/multi_log_record_processor.h | 10 +- .../logs/multi_log_record_processor_factory.h | 4 +- .../opentelemetry/sdk/logs/multi_recordable.h | 63 ++-- .../opentelemetry/sdk/logs/processor.h | 13 +- .../sdk/logs/read_write_log_record.h | 190 +++++++++++ .../sdk/logs/readable_log_record.h | 121 +++++++ .../opentelemetry/sdk/logs/recordable.h | 63 +--- .../sdk/logs/simple_log_record_processor.h | 8 +- .../simple_log_record_processor_factory.h | 3 +- sdk/src/logs/CMakeLists.txt | 4 +- sdk/src/logs/batch_log_record_processor.cc | 16 +- .../batch_log_record_processor_factory.cc | 6 +- sdk/src/logs/logger.cc | 87 +---- sdk/src/logs/logger_context.cc | 6 +- sdk/src/logs/logger_context_factory.cc | 10 +- sdk/src/logs/logger_provider.cc | 10 +- sdk/src/logs/logger_provider_factory.cc | 24 +- sdk/src/logs/multi_log_record_processor.cc | 10 +- .../multi_log_record_processor_factory.cc | 7 +- sdk/src/logs/multi_recordable.cc | 91 ++++-- sdk/src/logs/read_write_log_record.cc | 177 +++++++++++ sdk/src/logs/readable_log_record.cc | 51 +++ sdk/src/logs/simple_log_record_processor.cc | 8 +- .../simple_log_record_processor_factory.cc | 7 +- .../logs/batch_log_record_processor_test.cc | 106 +++++-- sdk/test/logs/log_record_test.cc | 28 +- sdk/test/logs/logger_provider_sdk_test.cc | 44 ++- sdk/test/logs/logger_sdk_test.cc | 79 ++++- .../logs/simple_log_record_processor_test.cc | 79 ++++- 77 files changed, 2206 insertions(+), 1374 deletions(-) create mode 100644 api/include/opentelemetry/logs/log_record.h create mode 100644 exporters/elasticsearch/src/es_log_recordable.cc delete mode 100644 sdk/include/opentelemetry/sdk/logs/log_record.h create mode 100644 sdk/include/opentelemetry/sdk/logs/read_write_log_record.h create mode 100644 sdk/include/opentelemetry/sdk/logs/readable_log_record.h create mode 100644 sdk/src/logs/read_write_log_record.cc create mode 100644 sdk/src/logs/readable_log_record.cc diff --git a/.vscode/launch.json b/.vscode/launch.json index e9a256a518..3532ca4d2a 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -1,24 +1,24 @@ { - "version": "0.2.0", - "configurations": [ - { - "name": "Debug on Windows", - "type": "cppvsdbg", - "request": "launch", - "program": "${workspaceFolder}/build/", - "args": [], - "stopAtEntry": false, - "cwd": "${workspaceFolder}", - "environment": [], - "externalConsole": false - }, - { - "name": "Debug on Linux", - "type": "gdb", - "request": "launch", - "target": "${workspaceFolder}/bazel-bin/", - "cwd": "${workspaceRoot}", - "valuesFormatting": "parseText" - } - ] + "version": "0.2.0", + "configurations": [ + { + "name": "Debug on Windows", + "type": "cppvsdbg", + "request": "launch", + "program": "${workspaceFolder}/build/", + "args": [], + "stopAtEntry": false, + "cwd": "${workspaceFolder}", + "environment": [], + "externalConsole": false + }, + { + "name": "Debug on Linux", + "type": "gdb", + "request": "launch", + "target": "${workspaceFolder}/bazel-bin/", + "cwd": "${workspaceRoot}", + "valuesFormatting": "parseText" + } + ] } diff --git a/CHANGELOG.md b/CHANGELOG.md index 9531149ef5..4034723c5b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,6 +49,8 @@ Increment the: * [BUILD] Add CMake OTELCPP_PROTO_PATH [#1730](https://github.com/open-telemetry/opentelemetry-cpp/pull/1730) * [SEMANTIC CONVENTIONS] Upgrade to version 1.15.0 [#1761](https://github.com/open-telemetry/opentelemetry-cpp/pull/1761) +* [LOGS SDK] New LogRecord and logs::Recordable implementations. + [#1766](https://github.com/open-telemetry/opentelemetry-cpp/pull/1766) Deprecation notes: diff --git a/api/include/opentelemetry/common/key_value_iterable_view.h b/api/include/opentelemetry/common/key_value_iterable_view.h index 2a0cbbc445..e688ec253f 100644 --- a/api/include/opentelemetry/common/key_value_iterable_view.h +++ b/api/include/opentelemetry/common/key_value_iterable_view.h @@ -8,6 +8,7 @@ #include #include "opentelemetry/common/key_value_iterable.h" +#include "opentelemetry/nostd/type_traits.h" #include "opentelemetry/nostd/utility.h" #include "opentelemetry/version.h" @@ -73,5 +74,12 @@ class KeyValueIterableView final : public KeyValueIterable private: const T *container_; }; + +template ::value> * = nullptr> +KeyValueIterableView MakeKeyValueIterableView(const T &container) noexcept +{ + return KeyValueIterableView(container); +} + } // namespace common OPENTELEMETRY_END_NAMESPACE diff --git a/api/include/opentelemetry/logs/log_record.h b/api/include/opentelemetry/logs/log_record.h new file mode 100644 index 0000000000..7fb51c2d99 --- /dev/null +++ b/api/include/opentelemetry/logs/log_record.h @@ -0,0 +1,81 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once +#ifdef ENABLE_LOGS_PREVIEW + +# include "opentelemetry/common/attribute_value.h" +# include "opentelemetry/common/key_value_iterable.h" +# include "opentelemetry/common/timestamp.h" +# include "opentelemetry/logs/severity.h" +# include "opentelemetry/trace/span_id.h" +# include "opentelemetry/trace/trace_flags.h" +# include "opentelemetry/trace/trace_id.h" +# include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace logs +{ +/** + * Maintains a representation of a log in a format that can be processed by a recorder. + * + * This class is thread-compatible. + */ +class LogRecord +{ +public: + virtual ~LogRecord() = default; + + /** + * Set the timestamp for this log. + * @param timestamp the timestamp to set + */ + virtual void SetTimestamp(opentelemetry::common::SystemTimestamp timestamp) noexcept = 0; + + /** + * Set the observed timestamp for this log. + * @param timestamp the timestamp to set + */ + virtual void SetObservedTimestamp(opentelemetry::common::SystemTimestamp timestamp) noexcept = 0; + + /** + * Set the severity for this log. + * @param severity the severity of the event + */ + virtual void SetSeverity(opentelemetry::logs::Severity severity) noexcept = 0; + + /** + * Set body field for this log. + * @param message the body to set + */ + virtual void SetBody(const opentelemetry::common::AttributeValue &message) noexcept = 0; + + /** + * Set an attribute of a log. + * @param key the name of the attribute + * @param value the attribute value + */ + virtual void SetAttribute(nostd::string_view key, + const opentelemetry::common::AttributeValue &value) noexcept = 0; + + /** + * Set the trace id for this log. + * @param trace_id the trace id to set + */ + virtual void SetTraceId(const opentelemetry::trace::TraceId &trace_id) noexcept = 0; + + /** + * Set the span id for this log. + * @param span_id the span id to set + */ + virtual void SetSpanId(const opentelemetry::trace::SpanId &span_id) noexcept = 0; + + /** + * Inject trace_flags for this log. + * @param trace_flags the trace flags to set + */ + virtual void SetTraceFlags(const opentelemetry::trace::TraceFlags &trace_flags) noexcept = 0; +}; +} // namespace logs +OPENTELEMETRY_END_NAMESPACE +#endif diff --git a/api/include/opentelemetry/logs/logger.h b/api/include/opentelemetry/logs/logger.h index 32c3974a1f..a8342e3f71 100644 --- a/api/include/opentelemetry/logs/logger.h +++ b/api/include/opentelemetry/logs/logger.h @@ -13,11 +13,14 @@ # include "opentelemetry/common/key_value_iterable_view.h" # include "opentelemetry/common/macros.h" # include "opentelemetry/common/timestamp.h" +# include "opentelemetry/logs/log_record.h" # include "opentelemetry/logs/severity.h" # include "opentelemetry/nostd/shared_ptr.h" # include "opentelemetry/nostd/span.h" # include "opentelemetry/nostd/string_view.h" # include "opentelemetry/nostd/type_traits.h" +# include "opentelemetry/nostd/unique_ptr.h" +# include "opentelemetry/trace/span_context.h" # include "opentelemetry/trace/span_id.h" # include "opentelemetry/trace/trace_flags.h" # include "opentelemetry/trace/trace_id.h" @@ -38,8 +41,40 @@ class Logger virtual const nostd::string_view GetName() noexcept = 0; /** - * Each of the following overloaded Log(...) methods - * creates a log message with the specific parameters passed. + * Create a Log Record object + * + * @return nostd::unique_ptr + */ + virtual nostd::unique_ptr CreateLogRecord() noexcept = 0; + + /** + * Emit a Log Record object + * + * @param log_record + */ + virtual void EmitLogRecord(nostd::unique_ptr &&log_record) noexcept = 0; + + /** + * Emit a Log Record object with custom span context + * + * @param log_record + */ + virtual void EmitLogRecord(nostd::unique_ptr &&log_record, + const trace::SpanContext &trace_context) + { + if (!log_record) + { + return; + } + + log_record->SetSpanId(trace_context.span_id()); + log_record->SetTraceId(trace_context.trace_id()); + log_record->SetTraceFlags(trace_context.trace_flags()); + + EmitLogRecord(std::move(log_record)); + } + + /** * * @param severity the severity level of the log event. * @param message the string message of the log (perhaps support std::fmt or fmt-lib format). @@ -51,45 +86,75 @@ class Logger * @param timestamp the timestamp the log record was created. * @throws No exceptions under any circumstances. */ - - /** - * The base Log(...) method that all other Log(...) overloaded methods will eventually call, - * in order to create a log record. - */ virtual void Log(Severity severity, nostd::string_view body, const common::KeyValueIterable &attributes, trace::TraceId trace_id, trace::SpanId span_id, trace::TraceFlags trace_flags, - common::SystemTimestamp timestamp) noexcept = 0; + common::SystemTimestamp timestamp) noexcept + { + nostd::unique_ptr log_record = CreateLogRecord(); + if (!log_record) + { + return; + } + + log_record->SetSeverity(severity); + log_record->SetBody(body); + log_record->SetTimestamp(timestamp); + + if (trace_id.IsValid()) + { + log_record->SetTraceId(trace_id); + log_record->SetTraceFlags(trace_flags); + } + + if (span_id.IsValid()) + { + log_record->SetSpanId(span_id); + } + + attributes.ForEachKeyValue( + [&log_record](nostd::string_view key, common::AttributeValue value) noexcept { + log_record->SetAttribute(key, value); + return true; + }); + + EmitLogRecord(std::move(log_record)); + } /** - * 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); + nostd::unique_ptr log_record = CreateLogRecord(); + if (!log_record) + { + return; + } + + log_record->SetSeverity(severity); + log_record->SetBody(body); + log_record->SetTimestamp(timestamp); + + attributes.ForEachKeyValue( + [&log_record](nostd::string_view key, common::AttributeValue value) noexcept { + log_record->SetAttribute(key, value); + return true; + }); + + EmitLogRecord(std::move(log_record)); } /*** Overloaded methods for KeyValueIterables ***/ @@ -113,18 +178,12 @@ class Logger template ::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(attributes), trace_id, span_id, - trace_flags, timestamp); + Log(severity, body, common::KeyValueIterableView(attributes), timestamp); } void Log(Severity severity, @@ -141,22 +200,6 @@ 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> 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>{ - attributes.begin(), attributes.end()}, - trace_id, span_id, trace_flags, timestamp); - } - /** Wrapper methods that the user could call for convenience when logging **/ /** @@ -166,13 +209,9 @@ class Logger */ void Log(Severity severity, nostd::string_view message) noexcept { - 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()); + this->Log(severity, message, + nostd::span>{}, + std::chrono::system_clock::now()); } /** @@ -184,7 +223,7 @@ class Logger nostd::enable_if_t::value> * = nullptr> void Log(Severity severity, const T &attributes) noexcept { - this->Log(severity, "", attributes, {}, {}, {}, std::chrono::system_clock::now()); + this->Log(severity, "", attributes, std::chrono::system_clock::now()); } /** @@ -197,7 +236,7 @@ class Logger nostd::enable_if_t::value> * = nullptr> void Log(Severity severity, nostd::string_view message, const T &attributes) noexcept { - this->Log(severity, message, attributes, {}, {}, {}, std::chrono::system_clock::now()); + this->Log(severity, message, attributes, std::chrono::system_clock::now()); } /** @@ -209,7 +248,7 @@ class Logger std::initializer_list> attributes) noexcept { - this->Log(severity, "", attributes, {}, {}, {}, std::chrono::system_clock::now()); + this->Log(severity, "", attributes, std::chrono::system_clock::now()); } /** @@ -223,7 +262,7 @@ class Logger std::initializer_list> attributes) noexcept { - this->Log(severity, message, attributes, {}, {}, {}, std::chrono::system_clock::now()); + this->Log(severity, message, attributes, std::chrono::system_clock::now()); } /** @@ -234,7 +273,7 @@ class Logger */ void Log(Severity severity, const common::KeyValueIterable &attributes) noexcept { - this->Log(severity, "", attributes, {}, {}, {}, std::chrono::system_clock::now()); + this->Log(severity, "", attributes, std::chrono::system_clock::now()); } /** @@ -248,7 +287,7 @@ class Logger nostd::string_view message, const common::KeyValueIterable &attributes) noexcept { - this->Log(severity, message, attributes, {}, {}, {}, std::chrono::system_clock::now()); + this->Log(severity, message, attributes, std::chrono::system_clock::now()); } /** Trace severity overloads **/ @@ -259,17 +298,6 @@ 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 @@ -323,17 +351,6 @@ 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 @@ -387,17 +404,6 @@ 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 @@ -451,17 +457,6 @@ 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 @@ -515,17 +510,6 @@ 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 @@ -579,17 +563,6 @@ 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 diff --git a/api/include/opentelemetry/logs/noop.h b/api/include/opentelemetry/logs/noop.h index 4663c86c18..d8941510da 100644 --- a/api/include/opentelemetry/logs/noop.h +++ b/api/include/opentelemetry/logs/noop.h @@ -41,24 +41,11 @@ class NoopLogger final : public Logger public: const nostd::string_view GetName() noexcept override { return "noop logger"; } - void Log(Severity /* severity */, - nostd::string_view /* body */, - const common::KeyValueIterable & /* attributes */, - trace::TraceId /* trace_id */, - trace::SpanId /* span_id */, - trace::TraceFlags /* trace_flags */, - common::SystemTimestamp /* timestamp */) noexcept override - {} + nostd::unique_ptr CreateLogRecord() noexcept override { return nullptr; } - void Log(Severity /* severity */, - 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 override - {} + using Logger::EmitLogRecord; + + void EmitLogRecord(nostd::unique_ptr &&) noexcept override {} }; /** diff --git a/api/include/opentelemetry/nostd/shared_ptr.h b/api/include/opentelemetry/nostd/shared_ptr.h index e1eac61561..76e5ed615a 100644 --- a/api/include/opentelemetry/nostd/shared_ptr.h +++ b/api/include/opentelemetry/nostd/shared_ptr.h @@ -9,6 +9,7 @@ # include # include +# include "opentelemetry/nostd/unique_ptr.h" # include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE @@ -95,6 +96,12 @@ class shared_ptr shared_ptr(const shared_ptr &other) noexcept { other.wrapper().CopyTo(buffer_); } + shared_ptr(unique_ptr &&other) noexcept + { + std::shared_ptr ptr_(other.release()); + new (buffer_.data) shared_ptr_wrapper{std::move(ptr_)}; + } + ~shared_ptr() { wrapper().~shared_ptr_wrapper(); } shared_ptr &operator=(shared_ptr &&other) noexcept diff --git a/api/test/logs/logger_test.cc b/api/test/logs/logger_test.cc index 0ce3a61037..48e9c375d3 100644 --- a/api/test/logs/logger_test.cc +++ b/api/test/logs/logger_test.cc @@ -66,19 +66,6 @@ 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); @@ -117,24 +104,14 @@ class TestLogger : public Logger { const nostd::string_view GetName() noexcept override { return "test logger"; } - void Log(Severity /* severity */, - string_view /* body */, - const common::KeyValueIterable & /* attributes */, - trace::TraceId /* trace_id */, - trace::SpanId /* span_id */, - trace::TraceFlags /* trace_flags */, - common::SystemTimestamp /* timestamp */) noexcept override - {} - - void Log(Severity /* severity */, - 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 override - {} + nostd::unique_ptr CreateLogRecord() noexcept override + { + return nullptr; + } + + using Logger::EmitLogRecord; + + void EmitLogRecord(nostd::unique_ptr &&) noexcept override {} }; // Define a basic LoggerProvider class that returns an instance of the logger class defined above diff --git a/examples/otlp/http_log_main.cc b/examples/otlp/http_log_main.cc index ed52389f6e..19d8d80e76 100644 --- a/examples/otlp/http_log_main.cc +++ b/examples/otlp/http_log_main.cc @@ -49,7 +49,7 @@ void InitLogger() // Create OTLP exporter instance auto exporter = otlp::OtlpHttpLogRecordExporterFactory::Create(logger_opts); auto processor = logs_sdk::SimpleLogRecordProcessorFactory::Create(std::move(exporter)); - std::shared_ptr provider = + nostd::shared_ptr provider = logs_sdk::LoggerProviderFactory::Create(std::move(processor)); opentelemetry::logs::Provider::SetLoggerProvider(provider); diff --git a/exporters/elasticsearch/BUILD b/exporters/elasticsearch/BUILD index a13c881b0b..16fae2edab 100644 --- a/exporters/elasticsearch/BUILD +++ b/exporters/elasticsearch/BUILD @@ -4,6 +4,7 @@ cc_library( name = "es_log_record_exporter", srcs = [ "src/es_log_record_exporter.cc", + "src/es_log_recordable.cc", ], hdrs = [ "include/opentelemetry/exporters/elasticsearch/es_log_record_exporter.h", diff --git a/exporters/elasticsearch/CMakeLists.txt b/exporters/elasticsearch/CMakeLists.txt index ddae27cb71..90064e292d 100644 --- a/exporters/elasticsearch/CMakeLists.txt +++ b/exporters/elasticsearch/CMakeLists.txt @@ -1,5 +1,5 @@ add_library(opentelemetry_exporter_elasticsearch_logs - src/es_log_record_exporter.cc) + src/es_log_record_exporter.cc src/es_log_recordable.cc) set_target_properties(opentelemetry_exporter_elasticsearch_logs PROPERTIES EXPORT_NAME elasticsearch_log_record_exporter) diff --git a/exporters/elasticsearch/include/opentelemetry/exporters/elasticsearch/es_log_record_exporter.h b/exporters/elasticsearch/include/opentelemetry/exporters/elasticsearch/es_log_record_exporter.h index 2659851fd7..7e09ae8d1b 100644 --- a/exporters/elasticsearch/include/opentelemetry/exporters/elasticsearch/es_log_record_exporter.h +++ b/exporters/elasticsearch/include/opentelemetry/exporters/elasticsearch/es_log_record_exporter.h @@ -9,7 +9,7 @@ # include "opentelemetry/ext/http/client/http_client_factory.h" # include "opentelemetry/nostd/type_traits.h" # include "opentelemetry/sdk/logs/exporter.h" -# include "opentelemetry/sdk/logs/log_record.h" +# include "opentelemetry/sdk/logs/recordable.h" # include # include @@ -77,8 +77,9 @@ class ElasticsearchLogRecordExporter final : public opentelemetry::sdk::logs::Lo /** * Creates a recordable that stores the data in a JSON object + * @return a newly initialized Recordable object. */ - std::unique_ptr MakeRecordable() noexcept override; + nostd::unique_ptr MakeRecordable() noexcept override; /** * Exports a vector of log records to the Elasticsearch instance. Guaranteed to return after a @@ -86,7 +87,7 @@ class ElasticsearchLogRecordExporter final : public opentelemetry::sdk::logs::Lo * @param records A list of log records to send to Elasticsearch. */ sdk::common::ExportResult Export( - const opentelemetry::nostd::span> + const opentelemetry::nostd::span> &records) noexcept override; /** diff --git a/exporters/elasticsearch/include/opentelemetry/exporters/elasticsearch/es_log_recordable.h b/exporters/elasticsearch/include/opentelemetry/exporters/elasticsearch/es_log_recordable.h index 3e604ccf17..d6ed78d117 100644 --- a/exporters/elasticsearch/include/opentelemetry/exporters/elasticsearch/es_log_recordable.h +++ b/exporters/elasticsearch/include/opentelemetry/exporters/elasticsearch/es_log_recordable.h @@ -28,208 +28,92 @@ namespace logs class ElasticSearchRecordable final : public sdk::logs::Recordable { private: - // Define a JSON object that will be populated with the log data - nlohmann::json json_; - /** * A helper method that writes a key/value pair under a specified name, the two names used here * being "attributes" and "resources" */ void WriteKeyValue(nostd::string_view key, const opentelemetry::common::AttributeValue &value, - std::string name) - { - switch (value.index()) - { - case common::AttributeType::kTypeBool: - json_[name][key.data()] = opentelemetry::nostd::get(value) ? true : false; - return; - case common::AttributeType::kTypeInt: - json_[name][key.data()] = opentelemetry::nostd::get(value); - return; - case common::AttributeType::kTypeInt64: - json_[name][key.data()] = opentelemetry::nostd::get(value); - return; - case common::AttributeType::kTypeUInt: - json_[name][key.data()] = opentelemetry::nostd::get(value); - return; - case common::AttributeType::kTypeUInt64: - json_[name][key.data()] = opentelemetry::nostd::get(value); - return; - case common::AttributeType::kTypeDouble: - json_[name][key.data()] = opentelemetry::nostd::get(value); - return; - case common::AttributeType::kTypeCString: - json_[name][key.data()] = opentelemetry::nostd::get(value); - return; - case common::AttributeType::kTypeString: - json_[name][key.data()] = - opentelemetry::nostd::get(value).data(); - return; - default: - return; - } - } + std::string name); void WriteKeyValue(nostd::string_view key, const opentelemetry::sdk::common::OwnedAttributeValue &value, - std::string name) - { - namespace common = opentelemetry::sdk::common; - switch (value.index()) - { - case common::kTypeBool: - json_[name][key.data()] = opentelemetry::nostd::get(value) ? true : false; - return; - case common::kTypeInt: - json_[name][key.data()] = opentelemetry::nostd::get(value); - return; - case common::kTypeInt64: - json_[name][key.data()] = opentelemetry::nostd::get(value); - return; - case common::kTypeUInt: - json_[name][key.data()] = opentelemetry::nostd::get(value); - return; - case common::kTypeUInt64: - json_[name][key.data()] = opentelemetry::nostd::get(value); - return; - case common::kTypeDouble: - json_[name][key.data()] = opentelemetry::nostd::get(value); - return; - case common::kTypeString: - json_[name][key.data()] = opentelemetry::nostd::get(value).data(); - return; - default: - return; - } - } + std::string name); + + void WriteValue(const opentelemetry::common::AttributeValue &value, std::string name); public: /** - * Set the severity for this log. - * @param severity the severity of the event + * Returns a JSON object contain the log information */ - void SetSeverity(opentelemetry::logs::Severity severity) noexcept override - { - // Convert the severity enum to a string - std::uint32_t severity_index = static_cast(severity); - if (severity_index >= std::extent::value) - { - std::stringstream sout; - sout << "Invalid severity(" << severity_index << ")"; - json_["severity"] = sout.str(); - } - else - { - json_["severity"] = opentelemetry::logs::SeverityNumToText[severity_index]; - } - } + nlohmann::json GetJSON() noexcept; /** - * Set body field for this log. - * @param message the body to set + * Set the timestamp for this log. + * @param timestamp the timestamp to set */ - void SetBody(nostd::string_view message) noexcept override { json_["body"] = message.data(); } + void SetTimestamp(opentelemetry::common::SystemTimestamp timestamp) noexcept override; /** - * Set Resource of this log - * @param Resource the resource to set + * Set the observed timestamp for this log. + * @param timestamp the timestamp to set */ - void SetResource(const opentelemetry::sdk::resource::Resource &resource) noexcept override - { - for (auto &kv : resource.GetAttributes()) - { - WriteKeyValue(kv.first, kv.second, "resource"); - } - } + void SetObservedTimestamp(opentelemetry::common::SystemTimestamp timestamp) noexcept override; /** - * Set an attribute of a log. - * @param key the key of the attribute - * @param value the attribute value + * Set the severity for this log. + * @param severity the severity of the event */ - void SetAttribute(nostd::string_view key, - const opentelemetry::common::AttributeValue &value) noexcept override - { - WriteKeyValue(key, value, "attributes"); - } + void SetSeverity(opentelemetry::logs::Severity severity) noexcept override; + + /** + * Set body field for this log. + * @param message the body to set + */ + void SetBody(const opentelemetry::common::AttributeValue &message) noexcept override; /** - * Set trace id for this log. + * Set the trace id for this log. * @param trace_id the trace id to set */ - void SetTraceId(opentelemetry::trace::TraceId trace_id) noexcept override - { - char trace_buf[32]; - trace_id.ToLowerBase16(trace_buf); - json_["traceid"] = std::string(trace_buf, sizeof(trace_buf)); - } + void SetTraceId(const opentelemetry::trace::TraceId &trace_id) noexcept override; /** - * Set span id for this log. + * Set the span id for this log. * @param span_id the span id to set */ - virtual void SetSpanId(opentelemetry::trace::SpanId span_id) noexcept override - { - char span_buf[16]; - span_id.ToLowerBase16(span_buf); - json_["spanid"] = std::string(span_buf, sizeof(span_buf)); - } + void SetSpanId(const opentelemetry::trace::SpanId &span_id) noexcept override; /** - * Inject a trace_flags for this log. - * @param trace_flags the span id to set + * Inject trace_flags for this log. + * @param trace_flags the trace flags to set */ - void SetTraceFlags(opentelemetry::trace::TraceFlags trace_flags) noexcept override - { - char flag_buf[2]; - trace_flags.ToLowerBase16(flag_buf); - json_["traceflags"] = std::string(flag_buf, sizeof(flag_buf)); - } + void SetTraceFlags(const opentelemetry::trace::TraceFlags &trace_flags) noexcept override; /** - * Set the timestamp for this log. - * @param timestamp the timestamp of the event + * Set an attribute of a log. + * @param key the name of the attribute + * @param value the attribute value */ - void SetTimestamp(common::SystemTimestamp timestamp) noexcept override - { - json_["timestamp"] = timestamp.time_since_epoch().count(); - } + void SetAttribute(nostd::string_view key, + const opentelemetry::common::AttributeValue &value) noexcept override; /** - * Returns a JSON object contain the log information + * Set Resource of this log + * @param Resource the resource to set */ - nlohmann::json GetJSON() noexcept { return json_; } + void SetResource(const opentelemetry::sdk::resource::Resource &resource) noexcept override; /** * Set instrumentation_scope for this log. * @param instrumentation_scope the instrumentation scope to set */ void SetInstrumentationScope(const opentelemetry::sdk::instrumentationscope::InstrumentationScope - &instrumentation_scope) noexcept override - { - json_["name"] = instrumentation_scope.GetName(); - instrumentation_scope_ = &instrumentation_scope; - } - - /** Returns the associated instruementation library */ - OPENTELEMETRY_DEPRECATED_MESSAGE("Please use GetInstrumentationScope instead") - const opentelemetry::sdk::instrumentationscope::InstrumentationScope &GetInstrumentationLibrary() - const noexcept - { - return GetInstrumentationScope(); - } - - /** Returns the associated instruementation library */ - const opentelemetry::sdk::instrumentationscope::InstrumentationScope &GetInstrumentationScope() - const noexcept - { - return *instrumentation_scope_; - } + &instrumentation_scope) noexcept override; private: - const opentelemetry::sdk::instrumentationscope::InstrumentationScope *instrumentation_scope_ = - nullptr; + // Define a JSON object that will be populated with the log data + nlohmann::json json_; }; } // namespace logs } // namespace exporter diff --git a/exporters/elasticsearch/src/es_log_record_exporter.cc b/exporters/elasticsearch/src/es_log_record_exporter.cc index 72e3b2cad2..dc38e4bafd 100644 --- a/exporters/elasticsearch/src/es_log_record_exporter.cc +++ b/exporters/elasticsearch/src/es_log_record_exporter.cc @@ -299,13 +299,13 @@ ElasticsearchLogRecordExporter::ElasticsearchLogRecordExporter( : options_{options}, http_client_{ext::http::client::HttpClientFactory::Create()} {} -std::unique_ptr ElasticsearchLogRecordExporter::MakeRecordable() noexcept +nostd::unique_ptr ElasticsearchLogRecordExporter::MakeRecordable() noexcept { - return std::unique_ptr(new ElasticSearchRecordable); + return nostd::unique_ptr(new ElasticSearchRecordable()); } sdk::common::ExportResult ElasticsearchLogRecordExporter::Export( - const nostd::span> &records) noexcept + const nostd::span> &records) noexcept { // Return failure if this exporter has been shutdown if (isShutdown()) @@ -334,7 +334,7 @@ sdk::common::ExportResult ElasticsearchLogRecordExporter::Export( body += "{\"index\" : {}}\n"; // Add the context of the Recordable - auto json_record = std::unique_ptr( + auto json_record = nostd::unique_ptr( static_cast(record.release())); body += json_record->GetJSON().dump() + "\n"; } diff --git a/exporters/elasticsearch/src/es_log_recordable.cc b/exporters/elasticsearch/src/es_log_recordable.cc new file mode 100644 index 0000000000..03c6c640e5 --- /dev/null +++ b/exporters/elasticsearch/src/es_log_recordable.cc @@ -0,0 +1,296 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#ifdef ENABLE_LOGS_PREVIEW + +# include "opentelemetry/exporters/elasticsearch/es_log_recordable.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace exporter +{ +namespace logs +{ +void ElasticSearchRecordable::WriteKeyValue(nostd::string_view key, + const opentelemetry::common::AttributeValue &value, + std::string name) +{ + switch (value.index()) + { + case common::AttributeType::kTypeBool: + json_[name][key.data()] = opentelemetry::nostd::get(value) ? true : false; + return; + case common::AttributeType::kTypeInt: + json_[name][key.data()] = opentelemetry::nostd::get(value); + return; + case common::AttributeType::kTypeInt64: + json_[name][key.data()] = opentelemetry::nostd::get(value); + return; + case common::AttributeType::kTypeUInt: + json_[name][key.data()] = opentelemetry::nostd::get(value); + return; + case common::AttributeType::kTypeUInt64: + json_[name][key.data()] = opentelemetry::nostd::get(value); + return; + case common::AttributeType::kTypeDouble: + json_[name][key.data()] = opentelemetry::nostd::get(value); + return; + case common::AttributeType::kTypeCString: + json_[name][key.data()] = opentelemetry::nostd::get(value); + return; + case common::AttributeType::kTypeString: + json_[name][key.data()] = + opentelemetry::nostd::get(value).data(); + return; + default: + return; + } +} + +void ElasticSearchRecordable::WriteKeyValue( + nostd::string_view key, + const opentelemetry::sdk::common::OwnedAttributeValue &value, + std::string name) +{ + namespace common = opentelemetry::sdk::common; + switch (value.index()) + { + case common::kTypeBool: + json_[name][key.data()] = opentelemetry::nostd::get(value) ? true : false; + return; + case common::kTypeInt: + json_[name][key.data()] = opentelemetry::nostd::get(value); + return; + case common::kTypeInt64: + json_[name][key.data()] = opentelemetry::nostd::get(value); + return; + case common::kTypeUInt: + json_[name][key.data()] = opentelemetry::nostd::get(value); + return; + case common::kTypeUInt64: + json_[name][key.data()] = opentelemetry::nostd::get(value); + return; + case common::kTypeDouble: + json_[name][key.data()] = opentelemetry::nostd::get(value); + return; + case common::kTypeString: + json_[name][key.data()] = opentelemetry::nostd::get(value).data(); + return; + default: + return; + } +} + +void ElasticSearchRecordable::WriteValue(const opentelemetry::common::AttributeValue &value, + std::string name) +{ + + // Assert size of variant to ensure that this method gets updated if the variant + // definition changes + + if (nostd::holds_alternative(value)) + { + json_[name] = opentelemetry::nostd::get(value) ? true : false; + } + else if (nostd::holds_alternative(value)) + { + json_[name] = opentelemetry::nostd::get(value); + } + else if (nostd::holds_alternative(value)) + { + json_[name] = opentelemetry::nostd::get(value); + } + else if (nostd::holds_alternative(value)) + { + json_[name] = opentelemetry::nostd::get(value); + } + else if (nostd::holds_alternative(value)) + { + json_[name] = opentelemetry::nostd::get(value); + } + else if (nostd::holds_alternative(value)) + { + json_[name] = opentelemetry::nostd::get(value); + } + else if (nostd::holds_alternative(value)) + { + json_[name] = std::string(nostd::get(value)); + } + else if (nostd::holds_alternative(value)) + { + json_[name] = static_cast(opentelemetry::nostd::get(value)); + } + else if (nostd::holds_alternative>(value)) + { + nlohmann::json array_value = nlohmann::json::array(); + for (const auto &val : nostd::get>(value)) + { + array_value.push_back(val); + } + json_[name] = array_value; + } + else if (nostd::holds_alternative>(value)) + { + nlohmann::json array_value = nlohmann::json::array(); + for (const auto &val : nostd::get>(value)) + { + array_value.push_back(val); + } + json_[name] = array_value; + } + else if (nostd::holds_alternative>(value)) + { + nlohmann::json array_value = nlohmann::json::array(); + for (const auto &val : nostd::get>(value)) + { + array_value.push_back(val); + } + json_[name] = array_value; + } + else if (nostd::holds_alternative>(value)) + { + nlohmann::json array_value = nlohmann::json::array(); + for (const auto &val : nostd::get>(value)) + { + array_value.push_back(val); + } + json_[name] = array_value; + } + else if (nostd::holds_alternative>(value)) + { + nlohmann::json array_value = nlohmann::json::array(); + for (const auto &val : nostd::get>(value)) + { + array_value.push_back(val); + } + json_[name] = array_value; + } + else if (nostd::holds_alternative>(value)) + { + nlohmann::json array_value = nlohmann::json::array(); + for (const auto &val : nostd::get>(value)) + { + array_value.push_back(val); + } + json_[name] = array_value; + } + else if (nostd::holds_alternative>(value)) + { + nlohmann::json array_value = nlohmann::json::array(); + for (const auto &val : nostd::get>(value)) + { + array_value.push_back(val); + } + json_[name] = array_value; + } + else if (nostd::holds_alternative>(value)) + { + nlohmann::json array_value = nlohmann::json::array(); + for (const auto &val : nostd::get>(value)) + { + array_value.push_back(static_cast(val)); + } + json_[name] = array_value; + } +} + +nlohmann::json ElasticSearchRecordable::GetJSON() noexcept +{ + return json_; +} + +void ElasticSearchRecordable::SetTimestamp( + opentelemetry::common::SystemTimestamp timestamp) noexcept +{ + json_["timestamp"] = timestamp.time_since_epoch().count(); +} + +void ElasticSearchRecordable::SetObservedTimestamp( + opentelemetry::common::SystemTimestamp timestamp) noexcept +{ + json_["observedtimestamp"] = timestamp.time_since_epoch().count(); +} + +void ElasticSearchRecordable::SetSeverity(opentelemetry::logs::Severity severity) noexcept +{ + // Convert the severity enum to a string + std::uint32_t severity_index = static_cast(severity); + if (severity_index >= std::extent::value) + { + std::stringstream sout; + sout << "Invalid severity(" << severity_index << ")"; + json_["severity"] = sout.str(); + } + else + { + json_["severity"] = opentelemetry::logs::SeverityNumToText[severity_index]; + } +} + +void ElasticSearchRecordable::SetBody(const opentelemetry::common::AttributeValue &message) noexcept +{ + WriteValue(message, "body"); +} + +void ElasticSearchRecordable::SetTraceId(const opentelemetry::trace::TraceId &trace_id) noexcept +{ + if (trace_id.IsValid()) + { + char trace_buf[32]; + trace_id.ToLowerBase16(trace_buf); + json_["traceid"] = std::string(trace_buf, sizeof(trace_buf)); + } + else + { + json_.erase("traceid"); + } +} + +void ElasticSearchRecordable::SetSpanId(const opentelemetry::trace::SpanId &span_id) noexcept +{ + if (span_id.IsValid()) + { + char span_buf[16]; + span_id.ToLowerBase16(span_buf); + json_["spanid"] = std::string(span_buf, sizeof(span_buf)); + } + else + { + json_.erase("spanid"); + } +} + +void ElasticSearchRecordable::SetTraceFlags( + const opentelemetry::trace::TraceFlags &trace_flags) noexcept +{ + char flag_buf[2]; + trace_flags.ToLowerBase16(flag_buf); + json_["traceflags"] = std::string(flag_buf, sizeof(flag_buf)); +} + +void ElasticSearchRecordable::SetAttribute( + nostd::string_view key, + const opentelemetry::common::AttributeValue &value) noexcept +{ + WriteKeyValue(key, value, "attributes"); +} + +void ElasticSearchRecordable::SetResource( + const opentelemetry::sdk::resource::Resource &resource) noexcept +{ + for (auto &attribute : resource.GetAttributes()) + { + WriteKeyValue(attribute.first, attribute.second, "resource"); + } +} + +void ElasticSearchRecordable::SetInstrumentationScope( + const opentelemetry::sdk::instrumentationscope::InstrumentationScope + &instrumentation_scope) noexcept +{ + json_["name"] = instrumentation_scope.GetName(); +} + +} // namespace logs +} // namespace exporter +OPENTELEMETRY_END_NAMESPACE +#endif diff --git a/exporters/elasticsearch/test/es_log_record_exporter_test.cc b/exporters/elasticsearch/test/es_log_record_exporter_test.cc index 4cd3b5064f..cc9af2ee2d 100644 --- a/exporters/elasticsearch/test/es_log_record_exporter_test.cc +++ b/exporters/elasticsearch/test/es_log_record_exporter_test.cc @@ -7,7 +7,6 @@ # include "opentelemetry/ext/http/server/http_server.h" # include "opentelemetry/logs/provider.h" # include "opentelemetry/sdk/logs/exporter.h" -# include "opentelemetry/sdk/logs/log_record.h" # include "opentelemetry/sdk/logs/logger_provider.h" # include "opentelemetry/sdk/logs/simple_log_record_processor.h" diff --git a/exporters/etw/include/opentelemetry/exporters/etw/etw_logger.h b/exporters/etw/include/opentelemetry/exporters/etw/etw_logger.h index 5897d53808..d72fe64edb 100644 --- a/exporters/etw/include/opentelemetry/exporters/etw/etw_logger.h +++ b/exporters/etw/include/opentelemetry/exporters/etw/etw_logger.h @@ -6,6 +6,7 @@ # include +# include # include # include # include @@ -15,6 +16,7 @@ # include # include +# include # include "opentelemetry/nostd/shared_ptr.h" # include "opentelemetry/nostd/string_view.h" @@ -23,6 +25,7 @@ # include "opentelemetry/common/key_value_iterable_view.h" +# include "opentelemetry/logs/log_record.h" # include "opentelemetry/logs/logger_provider.h" # include "opentelemetry/trace/span_id.h" # include "opentelemetry/trace/trace_id.h" @@ -41,6 +44,88 @@ namespace etw class LoggerProvider; +class LogRecord : public opentelemetry::logs::LogRecord +{ +public: + ~LogRecord() override = default; + + void SetTimestamp(opentelemetry::common::SystemTimestamp timestamp) noexcept override + { + timestamp_ = timestamp; + } + + opentelemetry::common::SystemTimestamp GetTimestamp() const noexcept { return timestamp_; } + + void SetObservedTimestamp(opentelemetry::common::SystemTimestamp timestamp) noexcept override + { + observed_timestamp_ = timestamp; + } + + opentelemetry::common::SystemTimestamp GetObservedTimestamp() const noexcept + { + return observed_timestamp_; + } + + void SetSeverity(opentelemetry::logs::Severity severity) noexcept override + { + severity_ = severity; + } + + opentelemetry::logs::Severity GetSeverity() const noexcept { return severity_; } + + void SetBody(const opentelemetry::common::AttributeValue &message) noexcept override + { + body_ = message; + } + + const opentelemetry::common::AttributeValue &GetBody() const noexcept { return body_; } + + void SetAttribute(nostd::string_view key, + const opentelemetry::common::AttributeValue &value) noexcept override + { + attributes_map_[static_cast(key)] = value; + } + + const std::unordered_map &GetAttributes() + const noexcept + { + return attributes_map_; + } + + void SetTraceId(const opentelemetry::trace::TraceId &trace_id) noexcept override + { + trace_id_ = trace_id; + } + + const opentelemetry::trace::TraceId &GetTraceId() const noexcept { return trace_id_; } + + void SetSpanId(const opentelemetry::trace::SpanId &span_id) noexcept override + { + span_id_ = span_id; + } + + const opentelemetry::trace::SpanId &GetSpanId() const noexcept { return span_id_; } + + void SetTraceFlags(const opentelemetry::trace::TraceFlags &trace_flags) noexcept override + { + trace_flags_ = trace_flags; + } + + const opentelemetry::trace::TraceFlags &GetTraceFlags() const noexcept { return trace_flags_; } + +private: + opentelemetry::logs::Severity severity_ = opentelemetry::logs::Severity::kInvalid; + + std::unordered_map attributes_map_; + opentelemetry::common::AttributeValue body_ = opentelemetry::nostd::string_view(); + opentelemetry::common::SystemTimestamp timestamp_; + opentelemetry::common::SystemTimestamp observed_timestamp_ = std::chrono::system_clock::now(); + + opentelemetry::trace::TraceId trace_id_; + opentelemetry::trace::SpanId span_id_; + opentelemetry::trace::TraceFlags trace_flags_; +}; + /** * @brief Logger class that allows to send logs to ETW Provider. */ @@ -100,53 +185,38 @@ class Logger : public opentelemetry::logs::Logger provHandle(initProvHandle()) {} - void Log(opentelemetry::logs::Severity severity, - 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 + nostd::unique_ptr CreateLogRecord() noexcept { + return nostd::unique_ptr(new LogRecord()); + } -# ifdef OPENTELEMETRY_RTTI_ENABLED - common::KeyValueIterable &attribs = const_cast(attributes); - Properties *evt = dynamic_cast(&attribs); - // Properties *res = dynamic_cast(&resr); + using opentelemetry::logs::Logger::EmitLogRecord; - if (evt != nullptr) + void EmitLogRecord( + nostd::unique_ptr &&log_record) noexcept override + { + if (!log_record) { - // Pass as a reference to original modifyable collection without creating a copy - return Log(severity, provId, body, *evt, trace_id, span_id, trace_flags, timestamp); + return; } -# endif - Properties evtCopy = attributes; - return Log(severity, provId, body, evtCopy, trace_id, span_id, trace_flags, timestamp); - } + LogRecord *readable_record = static_cast(log_record.get()); - 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(attributes); - Properties *evt = dynamic_cast(&attribs); - // Properties *res = dynamic_cast(&resr); - - if (evt != nullptr) + nostd::string_view body; + if (nostd::holds_alternative(readable_record->GetBody())) { - // 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); + body = nostd::get(readable_record->GetBody()); } -# endif - Properties evtCopy = attributes; - return Log(severity, name, body, evtCopy, trace_id, span_id, trace_flags, timestamp); + else if (nostd::holds_alternative(readable_record->GetBody())) + { + body = nostd::get(readable_record->GetBody()); + } + // TODO: More body types? + + Properties evtCopy{ + opentelemetry::common::MakeKeyValueIterableView(readable_record->GetAttributes())}; + return Log(readable_record->GetSeverity(), provId, body, evtCopy, readable_record->GetTraceId(), + readable_record->GetSpanId(), readable_record->GetTraceFlags(), + readable_record->GetTimestamp()); } virtual void Log(opentelemetry::logs::Severity severity, diff --git a/exporters/ostream/include/opentelemetry/exporters/ostream/common_utils.h b/exporters/ostream/include/opentelemetry/exporters/ostream/common_utils.h index 1e51815b23..11e098c5c0 100644 --- a/exporters/ostream/include/opentelemetry/exporters/ostream/common_utils.h +++ b/exporters/ostream/include/opentelemetry/exporters/ostream/common_utils.h @@ -41,6 +41,22 @@ void print_value(const std::vector &vec, std::ostream &sout) sout << ']'; } +template +void print_value(const nostd::span &vec, std::ostream &sout) +{ + sout << '['; + size_t i = 1; + size_t sz = vec.size(); + for (auto v : vec) + { + sout << v; + if (i != sz) + sout << ','; + i++; + }; + sout << ']'; +} + // Prior to C++14, generic lambda is not available so fallback to functor. #if __cplusplus < 201402L @@ -59,6 +75,23 @@ class OwnedAttributeValueVisitor std::ostream &sout_; }; +class AttributeValueVisitor +{ +public: + AttributeValueVisitor(std::ostream &sout) : sout_(sout) {} + + template + void operator()(T &&arg) + { + print_value(arg, sout_); + } + + void operator()(const nostd::string_view &&arg) { sout_.write(arg.data(), arg.size()); } + +private: + std::ostream &sout_; +}; + #endif inline void print_value(const opentelemetry::sdk::common::OwnedAttributeValue &value, @@ -76,6 +109,20 @@ inline void print_value(const opentelemetry::sdk::common::OwnedAttributeValue &v #endif } +inline void print_value(const opentelemetry::common::AttributeValue &value, std::ostream &sout) +{ +#if __cplusplus < 201402L + opentelemetry::nostd::visit(AttributeValueVisitor(sout), value); +#else + opentelemetry::nostd::visit( + [&sout](auto &&arg) { + /* explicit this is needed by some gcc versions (observed with v5.4.0)*/ + print_value(arg, sout); + }, + value); +#endif +} + } // namespace ostream_common } // namespace exporter OPENTELEMETRY_END_NAMESPACE diff --git a/exporters/ostream/include/opentelemetry/exporters/ostream/log_record_exporter.h b/exporters/ostream/include/opentelemetry/exporters/ostream/log_record_exporter.h index 7aa60a8409..113b0bf21b 100644 --- a/exporters/ostream/include/opentelemetry/exporters/ostream/log_record_exporter.h +++ b/exporters/ostream/include/opentelemetry/exporters/ostream/log_record_exporter.h @@ -7,7 +7,7 @@ # include "opentelemetry/common/spin_lock_mutex.h" # include "opentelemetry/nostd/type_traits.h" # include "opentelemetry/sdk/logs/exporter.h" -# include "opentelemetry/sdk/logs/log_record.h" + # include "opentelemetry/version.h" # include @@ -30,13 +30,13 @@ class OStreamLogRecordExporter final : public opentelemetry::sdk::logs::LogRecor */ explicit OStreamLogRecordExporter(std::ostream &sout = std::cout) noexcept; - std::unique_ptr MakeRecordable() noexcept override; + nostd::unique_ptr MakeRecordable() noexcept override; /** * Exports a span of logs sent from the processor. */ opentelemetry::sdk::common::ExportResult Export( - const opentelemetry::nostd::span> &records) noexcept + const opentelemetry::nostd::span> &records) noexcept override; /** @@ -55,6 +55,9 @@ class OStreamLogRecordExporter final : public opentelemetry::sdk::logs::LogRecor void printAttributes( const std::unordered_map &map, const std::string prefix = "\n\t"); + void printAttributes( + const std::unordered_map &map, + const std::string prefix = "\n\t"); }; } // namespace logs } // namespace exporter diff --git a/exporters/ostream/src/log_record_exporter.cc b/exporters/ostream/src/log_record_exporter.cc index 66cff9c41b..73b9bb1ac6 100644 --- a/exporters/ostream/src/log_record_exporter.cc +++ b/exporters/ostream/src/log_record_exporter.cc @@ -3,11 +3,12 @@ #ifdef ENABLE_LOGS_PREVIEW # include "opentelemetry/exporters/ostream/log_record_exporter.h" -# include # include "opentelemetry/exporters/ostream/common_utils.h" +# include "opentelemetry/sdk/logs/read_write_log_record.h" # include "opentelemetry/sdk_config.h" # include +# include # include namespace nostd = opentelemetry::nostd; @@ -25,13 +26,13 @@ OStreamLogRecordExporter::OStreamLogRecordExporter(std::ostream &sout) noexcept /*********************** Exporter methods ***********************/ -std::unique_ptr OStreamLogRecordExporter::MakeRecordable() noexcept +nostd::unique_ptr OStreamLogRecordExporter::MakeRecordable() noexcept { - return std::unique_ptr(new sdklogs::LogRecord()); + return nostd::unique_ptr(new sdklogs::ReadWriteLogRecord()); } sdk::common::ExportResult OStreamLogRecordExporter::Export( - const nostd::span> &records) noexcept + const nostd::span> &records) noexcept { if (isShutdown()) { @@ -42,9 +43,8 @@ sdk::common::ExportResult OStreamLogRecordExporter::Export( for (auto &record : records) { - // Convert recordable to a LogRecord so that the getters of the LogRecord can be used - auto log_record = - std::unique_ptr(static_cast(record.release())); + auto log_record = nostd::unique_ptr( + static_cast(record.release())); if (log_record == nullptr) { @@ -68,9 +68,13 @@ sdk::common::ExportResult OStreamLogRecordExporter::Export( // Print out each field of the log record, noting that severity is separated // into severity_num and severity_text sout_ << "{\n" - << " timestamp : " << log_record->GetTimestamp().time_since_epoch().count() << "\n" - << " severity_num : " << static_cast(log_record->GetSeverity()) << "\n" - << " severity_text : "; + << " timestamp : " << log_record->GetTimestamp().time_since_epoch().count() + << "\n" + << " observed_timestamp : " + << log_record->GetObservedTimestamp().time_since_epoch().count() << "\n" + << " severity_num : " << static_cast(log_record->GetSeverity()) + << "\n" + << " severity_text : "; std::uint32_t severity_index = static_cast(log_record->GetSeverity()); if (severity_index >= std::extent::value) @@ -82,20 +86,22 @@ sdk::common::ExportResult OStreamLogRecordExporter::Export( sout_ << opentelemetry::logs::SeverityNumToText[severity_index] << "\n"; } - sout_ << " body : " << log_record->GetBody() << "\n" - << " resource : "; + sout_ << " body : "; + opentelemetry::exporter::ostream_common::print_value(log_record->GetBody(), sout_); + sout_ << "\n"; + sout_ << " resource : "; printAttributes(log_record->GetResource().GetAttributes()); sout_ << "\n" - << " attributes : "; + << " attributes : "; printAttributes(log_record->GetAttributes()); sout_ << "\n" - << " trace_id : " << std::string(trace_id, trace_id_len) << "\n" - << " span_id : " << std::string(span_id, span_id__len) << "\n" - << " trace_flags : " << std::string(trace_flags, trace_flags_len) << "\n" + << " trace_id : " << std::string(trace_id, trace_id_len) << "\n" + << " span_id : " << std::string(span_id, span_id__len) << "\n" + << " trace_flags : " << std::string(trace_flags, trace_flags_len) << "\n" << "}\n"; } @@ -126,6 +132,17 @@ void OStreamLogRecordExporter::printAttributes( } } +void OStreamLogRecordExporter::printAttributes( + const std::unordered_map &map, + const std::string prefix) +{ + for (const auto &kv : map) + { + sout_ << prefix << kv.first << ": "; + opentelemetry::exporter::ostream_common::print_value(kv.second, sout_); + } +} + } // namespace logs } // namespace exporter OPENTELEMETRY_END_NAMESPACE diff --git a/exporters/ostream/test/ostream_log_test.cc b/exporters/ostream/test/ostream_log_test.cc index 12551caf83..b3e057bebd 100644 --- a/exporters/ostream/test/ostream_log_test.cc +++ b/exporters/ostream/test/ostream_log_test.cc @@ -8,6 +8,7 @@ # include "opentelemetry/logs/provider.h" # include "opentelemetry/nostd/span.h" # include "opentelemetry/sdk/logs/logger_provider.h" +# include "opentelemetry/sdk/logs/read_write_log_record.h" # include "opentelemetry/sdk/logs/simple_log_record_processor.h" # include @@ -42,8 +43,8 @@ TEST(OStreamLogRecordExporter, Shutdown) // After processor/exporter is shutdown, no logs should be sent to stream auto record = exporter->MakeRecordable(); - record->SetBody("Log record not empty"); - exporter->Export(nostd::span>(&record, 1)); + static_cast(record.get())->SetBody("Log record not empty"); + exporter->Export(nostd::span>(&record, 1)); // Restore original stringstream buffer std::cout.rdbuf(original); @@ -70,25 +71,25 @@ TEST(OstreamLogExporter, DefaultLogRecordToCout) // Pass a default recordable created by the exporter to be exported auto log_record = exporter->MakeRecordable(); - exporter->Export(nostd::span>(&log_record, 1)); + exporter->Export(nostd::span>(&log_record, 1)); // Restore cout's original stringstream std::cout.rdbuf(original); std::vector expected_output{ "{\n" - " timestamp : 0\n" - " severity_num : 0\n" - " severity_text : INVALID\n" - " body : \n", - " resource : \n", + " timestamp : 0\n", + " severity_num : 0\n" + " severity_text : INVALID\n" + " body : \n", + " resource : \n", "telemetry.sdk.version: " OPENTELEMETRY_VERSION "\n", "telemetry.sdk.name: opentelemetry\n", "telemetry.sdk.language: cpp\n", - " attributes : \n" - " trace_id : 00000000000000000000000000000000\n" - " span_id : 0000000000000000\n" - " trace_flags : 00\n" + " attributes : \n" + " trace_id : 00000000000000000000000000000000\n" + " span_id : 0000000000000000\n" + " trace_flags : 00\n" "}\n"}; for (auto &expected : expected_output) @@ -114,33 +115,38 @@ TEST(OStreamLogRecordExporter, SimpleLogToCout) // Create a log record and manually timestamp, severity, name, message common::SystemTimestamp now(std::chrono::system_clock::now()); - auto record = std::unique_ptr(new sdklogs::LogRecord()); - record->SetTimestamp(now); - record->SetSeverity(logs_api::Severity::kTrace); // kTrace has enum value of 1 - record->SetBody("Message"); + auto record = nostd::unique_ptr(new sdklogs::ReadWriteLogRecord()); + static_cast(record.get())->SetTimestamp(now); + static_cast(record.get())->SetObservedTimestamp(now); + static_cast(record.get()) + ->SetSeverity(logs_api::Severity::kTrace); // kTrace has enum value of 1 + static_cast(record.get())->SetBody("Message"); // Log a record to cout - exporter->Export(nostd::span>(&record, 1)); + exporter->Export(nostd::span>(&record, 1)); // Reset cout's original stringstream buffer std::cout.rdbuf(original); std::vector expected_output{ "{\n" - " timestamp : " + + " timestamp : " + std::to_string(now.time_since_epoch().count()) + "\n" - " severity_num : 1\n" - " severity_text : TRACE\n" - " body : Message\n", - " resource : \n", + " observed_timestamp : " + + std::to_string(now.time_since_epoch().count()) + + "\n" + " severity_num : 1\n" + " severity_text : TRACE\n" + " body : Message\n", + " resource : \n", "telemetry.sdk.version: " OPENTELEMETRY_VERSION "\n", "telemetry.sdk.name: opentelemetry\n", "telemetry.sdk.language: cpp\n", - " attributes : \n" - " trace_id : 00000000000000000000000000000000\n" - " span_id : 0000000000000000\n" - " trace_flags : 00\n" + " attributes : \n" + " trace_id : 00000000000000000000000000000000\n" + " span_id : 0000000000000000\n" + " trace_flags : 00\n" "}\n"}; for (auto &expected : expected_output) @@ -169,34 +175,34 @@ TEST(OStreamLogRecordExporter, LogWithStringAttributesToCerr) // Set resources for this log record only of type auto resource = opentelemetry::sdk::resource::Resource::Create({{"key1", "val1"}}); - record->SetResource(resource); + static_cast(record.get())->SetResource(resource); // Set attributes to this log record of type - record->SetAttribute("a", true); + static_cast(record.get())->SetAttribute("a", true); // Log record to cerr - exporter->Export(nostd::span>(&record, 1)); + exporter->Export(nostd::span>(&record, 1)); // Reset cerr's original stringstream buffer std::cerr.rdbuf(original); std::vector expected_output{ "{\n" - " timestamp : 0\n" - " severity_num : 0\n" - " severity_text : INVALID\n" - " body : \n", - " resource : \n", + " timestamp : 0\n", + " severity_num : 0\n" + " severity_text : INVALID\n" + " body : \n", + " resource : \n", "telemetry.sdk.version: " OPENTELEMETRY_VERSION "\n", "telemetry.sdk.name: opentelemetry\n", "telemetry.sdk.language: cpp\n", "service.name: unknown_service\n", "key1: val1\n", - " attributes : \n", + " attributes : \n", "\ta: 1\n", - " trace_id : 00000000000000000000000000000000\n" - " span_id : 0000000000000000\n" - " trace_flags : 00\n" + " trace_id : 00000000000000000000000000000000\n" + " span_id : 0000000000000000\n" + " trace_flags : 00\n" "}\n"}; for (auto &expected : expected_output) @@ -229,36 +235,37 @@ TEST(OStreamLogRecordExporter, LogWithVariantTypesToClog) nostd::span data1{array1.data(), array1.size()}; auto resource = opentelemetry::sdk::resource::Resource::Create({{"res1", data1}}); - record->SetResource(resource); + static_cast(record.get())->SetResource(resource); // Set resources for this log record of bool types as the value // e.g. key/value is a par of type std::array array = {false, true, false}; - record->SetAttribute("attr1", nostd::span{array.data(), array.size()}); + static_cast(record.get()) + ->SetAttribute("attr1", nostd::span{array.data(), array.size()}); // Log a record to clog - exporter->Export(nostd::span>(&record, 1)); + exporter->Export(nostd::span>(&record, 1)); // Reset clog's original stringstream buffer std::clog.rdbuf(original); std::vector expected_output{ "{\n" - " timestamp : 0\n" - " severity_num : 0\n" - " severity_text : INVALID\n" - " body : \n", - " resource : \n", + " timestamp : 0\n", + " severity_num : 0\n" + " severity_text : INVALID\n" + " body : \n", + " resource : \n", "service.name: unknown_service\n", "telemetry.sdk.version: " OPENTELEMETRY_VERSION "\n", "telemetry.sdk.name: opentelemetry\n", "telemetry.sdk.language: cpp\n", "res1: [1,2,3]\n", - "attributes : \n", + "attributes : \n", "\tattr1: [0,1,0]\n" - " trace_id : 00000000000000000000000000000000\n" - " span_id : 0000000000000000\n" - " trace_flags : 00\n" + " trace_id : 00000000000000000000000000000000\n" + " span_id : 0000000000000000\n" + " trace_flags : 00\n" "}\n"}; for (auto &expected : expected_output) @@ -303,21 +310,20 @@ TEST(OStreamLogRecordExporter, IntegrationTest) // Compare actual vs expected outputs std::vector expected_output{ "{\n" - " timestamp : " + - std::to_string(now.time_since_epoch().count()) + - "\n" - " severity_num : 5\n" - " severity_text : DEBUG\n" - " body : Hello\n", - " resource : \n", + " timestamp : " + + std::to_string(now.time_since_epoch().count()) + "\n", + " severity_num : 5\n" + " severity_text : DEBUG\n" + " body : Hello\n", + " resource : \n", "telemetry.sdk.version: " OPENTELEMETRY_VERSION "\n", "service.name: unknown_service\n", "telemetry.sdk.name: opentelemetry\n", "telemetry.sdk.language: cpp\n", - " attributes : \n" - " trace_id : 00000000000000000000000000000000\n" - " span_id : 0000000000000000\n" - " trace_flags : 00\n" + " attributes : \n" + " trace_id : 00000000000000000000000000000000\n" + " span_id : 0000000000000000\n" + " trace_flags : 00\n" "}\n"}; for (auto &expected : expected_output) diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_log_record_exporter.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_log_record_exporter.h index 05224550a3..c3367da1cb 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_log_record_exporter.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_log_record_exporter.h @@ -44,7 +44,7 @@ class OtlpGrpcLogRecordExporter : public opentelemetry::sdk::logs::LogRecordExpo * Creates a recordable that stores the data in protobuf. * @return a newly initialized Recordable object. */ - std::unique_ptr MakeRecordable() noexcept override; + nostd::unique_ptr MakeRecordable() noexcept override; /** * Exports a vector of log records to the configured gRPC endpoint. Guaranteed to return after a @@ -52,7 +52,7 @@ class OtlpGrpcLogRecordExporter : public opentelemetry::sdk::logs::LogRecordExpo * @param records A list of log records. */ opentelemetry::sdk::common::ExportResult Export( - const nostd::span> &records) noexcept + const nostd::span> &records) noexcept override; /** @@ -70,7 +70,7 @@ class OtlpGrpcLogRecordExporter : public opentelemetry::sdk::logs::LogRecordExpo friend class OtlpGrpcLogRecordExporterTestPeer; // Store service stub internally. Useful for testing. - std::unique_ptr log_service_stub_; + nostd::unique_ptr log_service_stub_; /** * Create an OtlpGrpcLogRecordExporter using the specified service stub. @@ -78,7 +78,7 @@ class OtlpGrpcLogRecordExporter : public opentelemetry::sdk::logs::LogRecordExpo * @param stub the service stub to be used for exporting */ OtlpGrpcLogRecordExporter( - std::unique_ptr stub); + nostd::unique_ptr stub); bool is_shutdown_ = false; mutable opentelemetry::common::SpinLockMutex lock_; bool isShutdown() const noexcept; diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_log_record_exporter_factory.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_log_record_exporter_factory.h index 84e5aa1e96..90390c8945 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_log_record_exporter_factory.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_log_record_exporter_factory.h @@ -23,12 +23,12 @@ class OtlpGrpcLogRecordExporterFactory /** * Create a OtlpGrpcLogRecordExporter. */ - static std::unique_ptr Create(); + static nostd::unique_ptr Create(); /** * Create a OtlpGrpcLogRecordExporter. */ - static std::unique_ptr Create( + static nostd::unique_ptr Create( const OtlpGrpcExporterOptions &options); }; diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_record_exporter.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_record_exporter.h index 2250cfe831..af0e78c8ea 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_record_exporter.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_record_exporter.h @@ -42,7 +42,7 @@ class OtlpHttpLogRecordExporter final : public opentelemetry::sdk::logs::LogReco /** * Creates a recordable that stores the data in a JSON object */ - std::unique_ptr MakeRecordable() noexcept override; + nostd::unique_ptr MakeRecordable() noexcept override; /** * Exports a vector of log records to the Elasticsearch instance. Guaranteed to return after a @@ -50,7 +50,7 @@ class OtlpHttpLogRecordExporter final : public opentelemetry::sdk::logs::LogReco * @param records A list of log records to send to Elasticsearch. */ opentelemetry::sdk::common::ExportResult Export( - const nostd::span> &records) noexcept + const nostd::span> &records) noexcept override; /** @@ -65,7 +65,7 @@ class OtlpHttpLogRecordExporter final : public opentelemetry::sdk::logs::LogReco const OtlpHttpLogRecordExporterOptions options_; // Object that stores the HTTP sessions that have been created - std::unique_ptr http_client_; + nostd::unique_ptr http_client_; // For testing friend class OtlpHttpLogRecordExporterTestPeer; /** @@ -73,7 +73,7 @@ class OtlpHttpLogRecordExporter final : public opentelemetry::sdk::logs::LogReco * Only tests can call this constructor directly. * @param http_client the http client to be used for exporting */ - OtlpHttpLogRecordExporter(std::unique_ptr http_client); + OtlpHttpLogRecordExporter(nostd::unique_ptr http_client); }; } // namespace otlp } // namespace exporter diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_record_exporter_factory.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_record_exporter_factory.h index d2ea095245..c634ca076c 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_record_exporter_factory.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_record_exporter_factory.h @@ -24,12 +24,12 @@ class OtlpHttpLogRecordExporterFactory /** * Create a OtlpHttpLogRecordExporter. */ - static std::unique_ptr Create(); + static nostd::unique_ptr Create(); /** * Create a OtlpHttpLogRecordExporter. */ - static std::unique_ptr Create( + static nostd::unique_ptr Create( const OtlpHttpLogRecordExporterOptions &options); }; diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_log_recordable.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_log_recordable.h index b10898ac3c..cef1b3ef8f 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_log_recordable.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_log_recordable.h @@ -16,6 +16,7 @@ # include "opentelemetry/common/macros.h" # include "opentelemetry/sdk/common/attribute_utils.h" +# include "opentelemetry/sdk/logs/read_write_log_record.h" # include "opentelemetry/sdk/logs/recordable.h" OPENTELEMETRY_BEGIN_NAMESPACE @@ -32,11 +33,15 @@ class OtlpLogRecordable final : public opentelemetry::sdk::logs::Recordable public: ~OtlpLogRecordable() override = default; - proto::logs::v1::LogRecord &log_record() noexcept { return log_record_; } - const proto::logs::v1::LogRecord &log_record() const noexcept { return log_record_; } + proto::logs::v1::LogRecord &log_record() noexcept { return proto_record_; } + const proto::logs::v1::LogRecord &log_record() const noexcept { return proto_record_; } - /** Dynamically converts the resource of this log into a proto. */ - proto::resource::v1::Resource ProtoResource() const noexcept; + /** Returns the associated resource */ + const opentelemetry::sdk::resource::Resource &GetResource() const noexcept; + + /** Returns the associated instruementation scope */ + const opentelemetry::sdk::instrumentationscope::InstrumentationScope &GetInstrumentationScope() + const noexcept; /** * Set the timestamp for this log. @@ -44,6 +49,12 @@ class OtlpLogRecordable final : public opentelemetry::sdk::logs::Recordable */ void SetTimestamp(opentelemetry::common::SystemTimestamp timestamp) noexcept override; + /** + * Set the observed timestamp for this log. + * @param timestamp the timestamp to set + */ + void SetObservedTimestamp(opentelemetry::common::SystemTimestamp timestamp) noexcept override; + /** * Set the severity for this log. * @param severity the severity of the event @@ -54,42 +65,39 @@ class OtlpLogRecordable final : public opentelemetry::sdk::logs::Recordable * Set body field for this log. * @param message the body to set */ - void SetBody(nostd::string_view message) noexcept override; - - /** - * Set Resource of this log - * @param Resource the resource to set - */ - void SetResource(const opentelemetry::sdk::resource::Resource &resource) noexcept override; - - /** Returns the associated resource */ - const opentelemetry::sdk::resource::Resource &GetResource() const noexcept; - - /** - * Set an attribute of a log. - * @param key the name of the attribute - * @param value the attribute value - */ - void SetAttribute(nostd::string_view key, - const opentelemetry::common::AttributeValue &value) noexcept override; + void SetBody(const opentelemetry::common::AttributeValue &message) noexcept override; /** * Set the trace id for this log. * @param trace_id the trace id to set */ - void SetTraceId(opentelemetry::trace::TraceId trace_id) noexcept override; + void SetTraceId(const opentelemetry::trace::TraceId &trace_id) noexcept override; /** * Set the span id for this log. * @param span_id the span id to set */ - void SetSpanId(opentelemetry::trace::SpanId span_id) noexcept override; + void SetSpanId(const opentelemetry::trace::SpanId &span_id) noexcept override; /** * Inject trace_flags for this log. * @param trace_flags the trace flags to set */ - void SetTraceFlags(opentelemetry::trace::TraceFlags trace_flags) noexcept override; + void SetTraceFlags(const opentelemetry::trace::TraceFlags &trace_flags) noexcept override; + + /** + * Set an attribute of a log. + * @param key the name of the attribute + * @param value the attribute value + */ + void SetAttribute(nostd::string_view key, + const opentelemetry::common::AttributeValue &value) noexcept override; + + /** + * Set Resource of this log + * @param Resource the resource to set + */ + void SetResource(const opentelemetry::sdk::resource::Resource &resource) noexcept override; /** * Set instrumentation_scope for this log. @@ -98,22 +106,9 @@ class OtlpLogRecordable final : public opentelemetry::sdk::logs::Recordable void SetInstrumentationScope(const opentelemetry::sdk::instrumentationscope::InstrumentationScope &instrumentation_scope) noexcept override; - OPENTELEMETRY_DEPRECATED_MESSAGE("Please use GetInstrumentationScope instead") - const opentelemetry::sdk::instrumentationscope::InstrumentationScope &GetInstrumentationLibrary() - const noexcept - { - return GetInstrumentationScope(); - } - - /** Returns the associated instruementation scope */ - const opentelemetry::sdk::instrumentationscope::InstrumentationScope &GetInstrumentationScope() - const noexcept; - private: - proto::logs::v1::LogRecord log_record_; + proto::logs::v1::LogRecord proto_record_; const opentelemetry::sdk::resource::Resource *resource_ = nullptr; - // TODO shared resource - // const opentelemetry::sdk::resource::Resource *resource_ = nullptr; const opentelemetry::sdk::instrumentationscope::InstrumentationScope *instrumentation_scope_ = nullptr; }; diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_populate_attribute_utils.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_populate_attribute_utils.h index c64ca90b43..dc43827641 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_populate_attribute_utils.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_populate_attribute_utils.h @@ -3,8 +3,11 @@ #pragma once +// clang-format off #include "opentelemetry/exporters/otlp/protobuf_include_prefix.h" #include "opentelemetry/proto/resource/v1/resource.pb.h" +#include "opentelemetry/exporters/otlp/protobuf_include_suffix.h" +// clang-format on #include "opentelemetry/common/attribute_value.h" #include "opentelemetry/nostd/string_view.h" @@ -27,6 +30,13 @@ class OtlpPopulateAttributeUtils static void PopulateAttribute(opentelemetry::proto::resource::v1::Resource *proto, const opentelemetry::sdk::resource::Resource &resource) noexcept; + static void PopulateAnyValue(opentelemetry::proto::common::v1::AnyValue *proto_value, + const opentelemetry::common::AttributeValue &value) noexcept; + + static void PopulateAnyValue( + opentelemetry::proto::common::v1::AnyValue *proto_value, + const opentelemetry::sdk::common::OwnedAttributeValue &value) noexcept; + static void PopulateAttribute(opentelemetry::proto::common::v1::KeyValue *attribute, nostd::string_view key, const opentelemetry::common::AttributeValue &value) noexcept; diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_recordable_utils.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_recordable_utils.h index 57dbd3b8b0..051145e0b2 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_recordable_utils.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_recordable_utils.h @@ -3,6 +3,10 @@ #pragma once +#include + +#include "opentelemetry/nostd/unique_ptr.h" + #include "opentelemetry/exporters/otlp/protobuf_include_prefix.h" #include "opentelemetry/proto/collector/logs/v1/logs_service.pb.h" @@ -16,8 +20,6 @@ # include "opentelemetry/sdk/logs/recordable.h" #endif -#include - OPENTELEMETRY_BEGIN_NAMESPACE namespace exporter { @@ -35,7 +37,7 @@ class OtlpRecordableUtils #ifdef ENABLE_LOGS_PREVIEW static void PopulateRequest( - const nostd::span> &logs, + const nostd::span> &logs, proto::collector::logs::v1::ExportLogsServiceRequest *request) noexcept; #endif }; diff --git a/exporters/otlp/src/otlp_grpc_log_record_exporter.cc b/exporters/otlp/src/otlp_grpc_log_record_exporter.cc index 75e6b36597..db81037a39 100644 --- a/exporters/otlp/src/otlp_grpc_log_record_exporter.cc +++ b/exporters/otlp/src/otlp_grpc_log_record_exporter.cc @@ -42,21 +42,20 @@ OtlpGrpcLogRecordExporter::OtlpGrpcLogRecordExporter(const OtlpGrpcExporterOptio {} OtlpGrpcLogRecordExporter::OtlpGrpcLogRecordExporter( - std::unique_ptr stub) + nostd::unique_ptr stub) : options_(OtlpGrpcExporterOptions()), log_service_stub_(std::move(stub)) {} // ----------------------------- Exporter methods ------------------------------ -std::unique_ptr +nostd::unique_ptr OtlpGrpcLogRecordExporter::MakeRecordable() noexcept { - return std::unique_ptr( - new exporter::otlp::OtlpLogRecordable()); + return nostd::unique_ptr(new OtlpLogRecordable()); } opentelemetry::sdk::common::ExportResult OtlpGrpcLogRecordExporter::Export( - const nostd::span> &logs) noexcept + const nostd::span> &logs) noexcept { if (isShutdown()) { diff --git a/exporters/otlp/src/otlp_grpc_log_record_exporter_factory.cc b/exporters/otlp/src/otlp_grpc_log_record_exporter_factory.cc index d52a6269ba..de44a870e1 100644 --- a/exporters/otlp/src/otlp_grpc_log_record_exporter_factory.cc +++ b/exporters/otlp/src/otlp_grpc_log_record_exporter_factory.cc @@ -15,17 +15,17 @@ namespace exporter namespace otlp { -std::unique_ptr +nostd::unique_ptr OtlpGrpcLogRecordExporterFactory::Create() { OtlpGrpcExporterOptions options; return Create(options); } -std::unique_ptr +nostd::unique_ptr OtlpGrpcLogRecordExporterFactory::Create(const OtlpGrpcExporterOptions &options) { - std::unique_ptr exporter( + nostd::unique_ptr exporter( new OtlpGrpcLogRecordExporter(options)); return exporter; } diff --git a/exporters/otlp/src/otlp_http_log_record_exporter.cc b/exporters/otlp/src/otlp_http_log_record_exporter.cc index 77c70b628c..6d1603dd1e 100644 --- a/exporters/otlp/src/otlp_http_log_record_exporter.cc +++ b/exporters/otlp/src/otlp_http_log_record_exporter.cc @@ -45,7 +45,7 @@ OtlpHttpLogRecordExporter::OtlpHttpLogRecordExporter( ))) {} -OtlpHttpLogRecordExporter::OtlpHttpLogRecordExporter(std::unique_ptr http_client) +OtlpHttpLogRecordExporter::OtlpHttpLogRecordExporter(nostd::unique_ptr http_client) : options_(OtlpHttpLogRecordExporterOptions()), http_client_(std::move(http_client)) { OtlpHttpLogRecordExporterOptions &options = @@ -64,15 +64,14 @@ OtlpHttpLogRecordExporter::OtlpHttpLogRecordExporter(std::unique_ptr +nostd::unique_ptr OtlpHttpLogRecordExporter::MakeRecordable() noexcept { - return std::unique_ptr( - new exporter::otlp::OtlpLogRecordable()); + return nostd::unique_ptr(new OtlpLogRecordable()); } opentelemetry::sdk::common::ExportResult OtlpHttpLogRecordExporter::Export( - const nostd::span> &logs) noexcept + const nostd::span> &logs) noexcept { if (http_client_->IsShutdown()) { diff --git a/exporters/otlp/src/otlp_http_log_record_exporter_factory.cc b/exporters/otlp/src/otlp_http_log_record_exporter_factory.cc index d05d68ce26..0cb1184200 100644 --- a/exporters/otlp/src/otlp_http_log_record_exporter_factory.cc +++ b/exporters/otlp/src/otlp_http_log_record_exporter_factory.cc @@ -13,17 +13,17 @@ namespace exporter namespace otlp { -std::unique_ptr +nostd::unique_ptr OtlpHttpLogRecordExporterFactory::Create() { OtlpHttpLogRecordExporterOptions options; return Create(options); } -std::unique_ptr +nostd::unique_ptr OtlpHttpLogRecordExporterFactory::Create(const OtlpHttpLogRecordExporterOptions &options) { - std::unique_ptr exporter( + nostd::unique_ptr exporter( new OtlpHttpLogRecordExporter(options)); return exporter; } diff --git a/exporters/otlp/src/otlp_log_recordable.cc b/exporters/otlp/src/otlp_log_recordable.cc index 568bfa1a0a..cc4d712795 100644 --- a/exporters/otlp/src/otlp_log_recordable.cc +++ b/exporters/otlp/src/otlp_log_recordable.cc @@ -6,9 +6,9 @@ # include "opentelemetry/common/macros.h" # include "opentelemetry/exporters/otlp/otlp_log_recordable.h" - # include "opentelemetry/exporters/otlp/otlp_populate_attribute_utils.h" # include "opentelemetry/exporters/otlp/otlp_recordable_utils.h" +# include "opentelemetry/sdk/logs/readable_log_record.h" namespace nostd = opentelemetry::nostd; @@ -18,24 +18,30 @@ namespace exporter namespace otlp { -proto::resource::v1::Resource OtlpLogRecordable::ProtoResource() const noexcept +const opentelemetry::sdk::resource::Resource &OtlpLogRecordable::GetResource() const noexcept { - proto::resource::v1::Resource proto; - if (nullptr == resource_) - { - OtlpPopulateAttributeUtils::PopulateAttribute(&proto, sdk::resource::Resource::GetDefault()); - } - else - { - OtlpPopulateAttributeUtils::PopulateAttribute(&proto, *resource_); - } + OPENTELEMETRY_LIKELY_IF(nullptr != resource_) { return *resource_; } + + return opentelemetry::sdk::logs::ReadableLogRecord::GetDefaultResource(); +} + +const opentelemetry::sdk::instrumentationscope::InstrumentationScope & +OtlpLogRecordable::GetInstrumentationScope() const noexcept +{ + OPENTELEMETRY_LIKELY_IF(nullptr != instrumentation_scope_) { return *instrumentation_scope_; } - return proto; + return opentelemetry::sdk::logs::ReadableLogRecord::GetDefaultInstrumentationScope(); } void OtlpLogRecordable::SetTimestamp(opentelemetry::common::SystemTimestamp timestamp) noexcept { - log_record_.set_time_unix_nano(timestamp.time_since_epoch().count()); + proto_record_.set_time_unix_nano(timestamp.time_since_epoch().count()); +} + +void OtlpLogRecordable::SetObservedTimestamp( + opentelemetry::common::SystemTimestamp timestamp) noexcept +{ + proto_record_.set_observed_time_unix_nano(timestamp.time_since_epoch().count()); } void OtlpLogRecordable::SetSeverity(opentelemetry::logs::Severity severity) noexcept @@ -43,171 +49,178 @@ void OtlpLogRecordable::SetSeverity(opentelemetry::logs::Severity severity) noex switch (severity) { case opentelemetry::logs::Severity::kTrace: { - log_record_.set_severity_text("TRACE"); - log_record_.set_severity_number(proto::logs::v1::SEVERITY_NUMBER_TRACE); + proto_record_.set_severity_text("TRACE"); + proto_record_.set_severity_number(proto::logs::v1::SEVERITY_NUMBER_TRACE); break; } case opentelemetry::logs::Severity::kTrace2: { - log_record_.set_severity_text("TRACE2"); - log_record_.set_severity_number(proto::logs::v1::SEVERITY_NUMBER_TRACE2); + proto_record_.set_severity_text("TRACE2"); + proto_record_.set_severity_number(proto::logs::v1::SEVERITY_NUMBER_TRACE2); break; } case opentelemetry::logs::Severity::kTrace3: { - log_record_.set_severity_text("TRACE3"); - log_record_.set_severity_number(proto::logs::v1::SEVERITY_NUMBER_TRACE3); + proto_record_.set_severity_text("TRACE3"); + proto_record_.set_severity_number(proto::logs::v1::SEVERITY_NUMBER_TRACE3); break; } case opentelemetry::logs::Severity::kTrace4: { - log_record_.set_severity_text("TRACE4"); - log_record_.set_severity_number(proto::logs::v1::SEVERITY_NUMBER_TRACE4); + proto_record_.set_severity_text("TRACE4"); + proto_record_.set_severity_number(proto::logs::v1::SEVERITY_NUMBER_TRACE4); break; } case opentelemetry::logs::Severity::kDebug: { - log_record_.set_severity_text("DEBUG"); - log_record_.set_severity_number(proto::logs::v1::SEVERITY_NUMBER_DEBUG); + proto_record_.set_severity_text("DEBUG"); + proto_record_.set_severity_number(proto::logs::v1::SEVERITY_NUMBER_DEBUG); break; } case opentelemetry::logs::Severity::kDebug2: { - log_record_.set_severity_text("DEBUG2"); - log_record_.set_severity_number(proto::logs::v1::SEVERITY_NUMBER_DEBUG2); + proto_record_.set_severity_text("DEBUG2"); + proto_record_.set_severity_number(proto::logs::v1::SEVERITY_NUMBER_DEBUG2); break; } case opentelemetry::logs::Severity::kDebug3: { - log_record_.set_severity_text("DEBUG3"); - log_record_.set_severity_number(proto::logs::v1::SEVERITY_NUMBER_DEBUG3); + proto_record_.set_severity_text("DEBUG3"); + proto_record_.set_severity_number(proto::logs::v1::SEVERITY_NUMBER_DEBUG3); break; } case opentelemetry::logs::Severity::kDebug4: { - log_record_.set_severity_text("DEBUG4"); - log_record_.set_severity_number(proto::logs::v1::SEVERITY_NUMBER_DEBUG4); + proto_record_.set_severity_text("DEBUG4"); + proto_record_.set_severity_number(proto::logs::v1::SEVERITY_NUMBER_DEBUG4); break; } case opentelemetry::logs::Severity::kInfo: { - log_record_.set_severity_text("INFO"); - log_record_.set_severity_number(proto::logs::v1::SEVERITY_NUMBER_INFO); + proto_record_.set_severity_text("INFO"); + proto_record_.set_severity_number(proto::logs::v1::SEVERITY_NUMBER_INFO); break; } case opentelemetry::logs::Severity::kInfo2: { - log_record_.set_severity_text("INFO2"); - log_record_.set_severity_number(proto::logs::v1::SEVERITY_NUMBER_INFO2); + proto_record_.set_severity_text("INFO2"); + proto_record_.set_severity_number(proto::logs::v1::SEVERITY_NUMBER_INFO2); break; } case opentelemetry::logs::Severity::kInfo3: { - log_record_.set_severity_text("INFO3"); - log_record_.set_severity_number(proto::logs::v1::SEVERITY_NUMBER_INFO3); + proto_record_.set_severity_text("INFO3"); + proto_record_.set_severity_number(proto::logs::v1::SEVERITY_NUMBER_INFO3); break; } case opentelemetry::logs::Severity::kInfo4: { - log_record_.set_severity_text("INFO4"); - log_record_.set_severity_number(proto::logs::v1::SEVERITY_NUMBER_INFO4); + proto_record_.set_severity_text("INFO4"); + proto_record_.set_severity_number(proto::logs::v1::SEVERITY_NUMBER_INFO4); break; } case opentelemetry::logs::Severity::kWarn: { - log_record_.set_severity_text("WARN"); - log_record_.set_severity_number(proto::logs::v1::SEVERITY_NUMBER_WARN); + proto_record_.set_severity_text("WARN"); + proto_record_.set_severity_number(proto::logs::v1::SEVERITY_NUMBER_WARN); break; } case opentelemetry::logs::Severity::kWarn2: { - log_record_.set_severity_text("WARN2"); - log_record_.set_severity_number(proto::logs::v1::SEVERITY_NUMBER_WARN2); + proto_record_.set_severity_text("WARN2"); + proto_record_.set_severity_number(proto::logs::v1::SEVERITY_NUMBER_WARN2); break; } case opentelemetry::logs::Severity::kWarn3: { - log_record_.set_severity_text("WARN3"); - log_record_.set_severity_number(proto::logs::v1::SEVERITY_NUMBER_WARN3); + proto_record_.set_severity_text("WARN3"); + proto_record_.set_severity_number(proto::logs::v1::SEVERITY_NUMBER_WARN3); break; } case opentelemetry::logs::Severity::kWarn4: { - log_record_.set_severity_text("WARN4"); - log_record_.set_severity_number(proto::logs::v1::SEVERITY_NUMBER_WARN4); + proto_record_.set_severity_text("WARN4"); + proto_record_.set_severity_number(proto::logs::v1::SEVERITY_NUMBER_WARN4); break; } case opentelemetry::logs::Severity::kError: { - log_record_.set_severity_text("ERROR"); - log_record_.set_severity_number(proto::logs::v1::SEVERITY_NUMBER_ERROR); + proto_record_.set_severity_text("ERROR"); + proto_record_.set_severity_number(proto::logs::v1::SEVERITY_NUMBER_ERROR); break; } case opentelemetry::logs::Severity::kError2: { - log_record_.set_severity_text("ERROR2"); - log_record_.set_severity_number(proto::logs::v1::SEVERITY_NUMBER_ERROR2); + proto_record_.set_severity_text("ERROR2"); + proto_record_.set_severity_number(proto::logs::v1::SEVERITY_NUMBER_ERROR2); break; } case opentelemetry::logs::Severity::kError3: { - log_record_.set_severity_text("ERROR3"); - log_record_.set_severity_number(proto::logs::v1::SEVERITY_NUMBER_ERROR3); + proto_record_.set_severity_text("ERROR3"); + proto_record_.set_severity_number(proto::logs::v1::SEVERITY_NUMBER_ERROR3); break; } case opentelemetry::logs::Severity::kError4: { - log_record_.set_severity_text("ERROR4"); - log_record_.set_severity_number(proto::logs::v1::SEVERITY_NUMBER_ERROR4); + proto_record_.set_severity_text("ERROR4"); + proto_record_.set_severity_number(proto::logs::v1::SEVERITY_NUMBER_ERROR4); break; } case opentelemetry::logs::Severity::kFatal: { - log_record_.set_severity_text("FATAL"); - log_record_.set_severity_number(proto::logs::v1::SEVERITY_NUMBER_FATAL); + proto_record_.set_severity_text("FATAL"); + proto_record_.set_severity_number(proto::logs::v1::SEVERITY_NUMBER_FATAL); break; } case opentelemetry::logs::Severity::kFatal2: { - log_record_.set_severity_text("FATAL2"); - log_record_.set_severity_number(proto::logs::v1::SEVERITY_NUMBER_FATAL2); + proto_record_.set_severity_text("FATAL2"); + proto_record_.set_severity_number(proto::logs::v1::SEVERITY_NUMBER_FATAL2); break; } case opentelemetry::logs::Severity::kFatal3: { - log_record_.set_severity_text("FATAL3"); - log_record_.set_severity_number(proto::logs::v1::SEVERITY_NUMBER_FATAL3); + proto_record_.set_severity_text("FATAL3"); + proto_record_.set_severity_number(proto::logs::v1::SEVERITY_NUMBER_FATAL3); break; } case opentelemetry::logs::Severity::kFatal4: { - log_record_.set_severity_text("FATAL4"); - log_record_.set_severity_number(proto::logs::v1::SEVERITY_NUMBER_FATAL4); + proto_record_.set_severity_text("FATAL4"); + proto_record_.set_severity_number(proto::logs::v1::SEVERITY_NUMBER_FATAL4); break; } default: { - log_record_.set_severity_text("INVALID"); - log_record_.set_severity_number(proto::logs::v1::SEVERITY_NUMBER_UNSPECIFIED); + proto_record_.set_severity_text("INVALID"); + proto_record_.set_severity_number(proto::logs::v1::SEVERITY_NUMBER_UNSPECIFIED); break; } } } -void OtlpLogRecordable::SetBody(nostd::string_view message) noexcept +void OtlpLogRecordable::SetBody(const opentelemetry::common::AttributeValue &message) noexcept { - log_record_.mutable_body()->set_string_value(message.data(), message.size()); + OtlpPopulateAttributeUtils::PopulateAnyValue(proto_record_.mutable_body(), message); } -void OtlpLogRecordable::SetResource(const opentelemetry::sdk::resource::Resource &resource) noexcept -{ - resource_ = &resource; -} - -const opentelemetry::sdk::resource::Resource &OtlpLogRecordable::GetResource() const noexcept +void OtlpLogRecordable::SetTraceId(const opentelemetry::trace::TraceId &trace_id) noexcept { - OPENTELEMETRY_LIKELY_IF(nullptr != resource_) { return *resource_; } - - return opentelemetry::sdk::resource::Resource::GetDefault(); + if (trace_id.IsValid()) + { + proto_record_.set_trace_id(reinterpret_cast(trace_id.Id().data()), + trace_id.Id().size()); + } + else + { + proto_record_.clear_trace_id(); + } } -void OtlpLogRecordable::SetAttribute(nostd::string_view key, - const opentelemetry::common::AttributeValue &value) noexcept +void OtlpLogRecordable::SetSpanId(const opentelemetry::trace::SpanId &span_id) noexcept { - OtlpPopulateAttributeUtils::PopulateAttribute(log_record_.add_attributes(), key, value); + if (span_id.IsValid()) + { + proto_record_.set_span_id(reinterpret_cast(span_id.Id().data()), + span_id.Id().size()); + } + else + { + proto_record_.clear_span_id(); + } } -void OtlpLogRecordable::SetTraceId(opentelemetry::trace::TraceId trace_id) noexcept +void OtlpLogRecordable::SetTraceFlags(const opentelemetry::trace::TraceFlags &trace_flags) noexcept { - log_record_.set_trace_id(reinterpret_cast(trace_id.Id().data()), - trace::TraceId::kSize); + proto_record_.set_flags(trace_flags.flags()); } -void OtlpLogRecordable::SetSpanId(opentelemetry::trace::SpanId span_id) noexcept +void OtlpLogRecordable::SetAttribute(nostd::string_view key, + const opentelemetry::common::AttributeValue &value) noexcept { - log_record_.set_span_id(reinterpret_cast(span_id.Id().data()), - trace::SpanId::kSize); + OtlpPopulateAttributeUtils::PopulateAttribute(proto_record_.add_attributes(), key, value); } -void OtlpLogRecordable::SetTraceFlags(opentelemetry::trace::TraceFlags trace_flags) noexcept +void OtlpLogRecordable::SetResource(const opentelemetry::sdk::resource::Resource &resource) noexcept { - log_record_.set_flags(trace_flags.flags()); + resource_ = &resource; } void OtlpLogRecordable::SetInstrumentationScope( @@ -217,18 +230,6 @@ void OtlpLogRecordable::SetInstrumentationScope( instrumentation_scope_ = &instrumentation_scope; } -const opentelemetry::sdk::instrumentationscope::InstrumentationScope & -OtlpLogRecordable::GetInstrumentationScope() const noexcept -{ - OPENTELEMETRY_LIKELY_IF(nullptr != instrumentation_scope_) { return *instrumentation_scope_; } - - static opentelemetry::nostd::unique_ptr< - opentelemetry::sdk::instrumentationscope::InstrumentationScope> - default_instrumentation = - opentelemetry::sdk::instrumentationscope::InstrumentationScope::Create( - "default", "1.0.0", "https://opentelemetry.io/schemas/1.11.0"); - return *default_instrumentation; -} } // namespace otlp } // namespace exporter OPENTELEMETRY_END_NAMESPACE diff --git a/exporters/otlp/src/otlp_populate_attribute_utils.cc b/exporters/otlp/src/otlp_populate_attribute_utils.cc index 6dff7c4d01..d86953506c 100644 --- a/exporters/otlp/src/otlp_populate_attribute_utils.cc +++ b/exporters/otlp/src/otlp_populate_attribute_utils.cc @@ -17,12 +17,11 @@ namespace otlp const int kAttributeValueSize = 16; const int kOwnedAttributeValueSize = 15; -void OtlpPopulateAttributeUtils::PopulateAttribute( - opentelemetry::proto::common::v1::KeyValue *attribute, - nostd::string_view key, +void OtlpPopulateAttributeUtils::PopulateAnyValue( + opentelemetry::proto::common::v1::AnyValue *proto_value, const opentelemetry::common::AttributeValue &value) noexcept { - if (nullptr == attribute) + if (nullptr == proto_value) { return; } @@ -33,107 +32,102 @@ void OtlpPopulateAttributeUtils::PopulateAttribute( nostd::variant_size::value == kAttributeValueSize, "AttributeValue contains unknown type"); - attribute->set_key(key.data(), key.size()); - if (nostd::holds_alternative(value)) { - attribute->mutable_value()->set_bool_value(nostd::get(value)); + proto_value->set_bool_value(nostd::get(value)); } else if (nostd::holds_alternative(value)) { - attribute->mutable_value()->set_int_value(nostd::get(value)); + proto_value->set_int_value(nostd::get(value)); } else if (nostd::holds_alternative(value)) { - attribute->mutable_value()->set_int_value(nostd::get(value)); + proto_value->set_int_value(nostd::get(value)); } else if (nostd::holds_alternative(value)) { - attribute->mutable_value()->set_int_value(nostd::get(value)); + proto_value->set_int_value(nostd::get(value)); } else if (nostd::holds_alternative(value)) { - attribute->mutable_value()->set_int_value(nostd::get(value)); + proto_value->set_int_value(nostd::get(value)); } else if (nostd::holds_alternative(value)) { - attribute->mutable_value()->set_double_value(nostd::get(value)); + proto_value->set_double_value(nostd::get(value)); } else if (nostd::holds_alternative(value)) { - attribute->mutable_value()->set_string_value(nostd::get(value)); + proto_value->set_string_value(nostd::get(value)); } else if (nostd::holds_alternative(value)) { - attribute->mutable_value()->set_string_value(nostd::get(value).data(), - nostd::get(value).size()); + proto_value->set_string_value(nostd::get(value).data(), + nostd::get(value).size()); } else if (nostd::holds_alternative>(value)) { for (const auto &val : nostd::get>(value)) { - attribute->mutable_value()->mutable_array_value()->add_values()->set_int_value(val); + proto_value->mutable_array_value()->add_values()->set_int_value(val); } } else if (nostd::holds_alternative>(value)) { for (const auto &val : nostd::get>(value)) { - attribute->mutable_value()->mutable_array_value()->add_values()->set_bool_value(val); + proto_value->mutable_array_value()->add_values()->set_bool_value(val); } } else if (nostd::holds_alternative>(value)) { for (const auto &val : nostd::get>(value)) { - attribute->mutable_value()->mutable_array_value()->add_values()->set_int_value(val); + proto_value->mutable_array_value()->add_values()->set_int_value(val); } } else if (nostd::holds_alternative>(value)) { for (const auto &val : nostd::get>(value)) { - attribute->mutable_value()->mutable_array_value()->add_values()->set_int_value(val); + proto_value->mutable_array_value()->add_values()->set_int_value(val); } } else if (nostd::holds_alternative>(value)) { for (const auto &val : nostd::get>(value)) { - attribute->mutable_value()->mutable_array_value()->add_values()->set_int_value(val); + proto_value->mutable_array_value()->add_values()->set_int_value(val); } } else if (nostd::holds_alternative>(value)) { for (const auto &val : nostd::get>(value)) { - attribute->mutable_value()->mutable_array_value()->add_values()->set_int_value(val); + proto_value->mutable_array_value()->add_values()->set_int_value(val); } } else if (nostd::holds_alternative>(value)) { for (const auto &val : nostd::get>(value)) { - attribute->mutable_value()->mutable_array_value()->add_values()->set_double_value(val); + proto_value->mutable_array_value()->add_values()->set_double_value(val); } } else if (nostd::holds_alternative>(value)) { for (const auto &val : nostd::get>(value)) { - attribute->mutable_value()->mutable_array_value()->add_values()->set_string_value(val.data(), - val.size()); + proto_value->mutable_array_value()->add_values()->set_string_value(val.data(), val.size()); } } } -/** Maps from C++ attribute into OTLP proto attribute. */ -void OtlpPopulateAttributeUtils::PopulateAttribute( - opentelemetry::proto::common::v1::KeyValue *attribute, - nostd::string_view key, +void OtlpPopulateAttributeUtils::PopulateAnyValue( + opentelemetry::proto::common::v1::AnyValue *proto_value, const opentelemetry::sdk::common::OwnedAttributeValue &value) noexcept { - if (nullptr == attribute) + if (nullptr == proto_value) { return; } @@ -144,87 +138,126 @@ void OtlpPopulateAttributeUtils::PopulateAttribute( kOwnedAttributeValueSize, "OwnedAttributeValue contains unknown type"); - attribute->set_key(key.data(), key.size()); - if (nostd::holds_alternative(value)) { - attribute->mutable_value()->set_bool_value(nostd::get(value)); + proto_value->set_bool_value(nostd::get(value)); } else if (nostd::holds_alternative(value)) { - attribute->mutable_value()->set_int_value(nostd::get(value)); + proto_value->set_int_value(nostd::get(value)); } else if (nostd::holds_alternative(value)) { - attribute->mutable_value()->set_int_value(nostd::get(value)); + proto_value->set_int_value(nostd::get(value)); } else if (nostd::holds_alternative(value)) { - attribute->mutable_value()->set_int_value(nostd::get(value)); + proto_value->set_int_value(nostd::get(value)); } else if (nostd::holds_alternative(value)) { - attribute->mutable_value()->set_int_value(nostd::get(value)); + proto_value->set_int_value(nostd::get(value)); } else if (nostd::holds_alternative(value)) { - attribute->mutable_value()->set_double_value(nostd::get(value)); + proto_value->set_double_value(nostd::get(value)); } else if (nostd::holds_alternative(value)) { - attribute->mutable_value()->set_string_value(nostd::get(value)); + proto_value->set_string_value(nostd::get(value)); } else if (nostd::holds_alternative>(value)) { for (const auto val : nostd::get>(value)) { - attribute->mutable_value()->mutable_array_value()->add_values()->set_bool_value(val); + proto_value->mutable_array_value()->add_values()->set_bool_value(val); } } else if (nostd::holds_alternative>(value)) { for (const auto &val : nostd::get>(value)) { - attribute->mutable_value()->mutable_array_value()->add_values()->set_int_value(val); + proto_value->mutable_array_value()->add_values()->set_int_value(val); } } else if (nostd::holds_alternative>(value)) { for (const auto &val : nostd::get>(value)) { - attribute->mutable_value()->mutable_array_value()->add_values()->set_int_value(val); + proto_value->mutable_array_value()->add_values()->set_int_value(val); } } else if (nostd::holds_alternative>(value)) { for (const auto &val : nostd::get>(value)) { - attribute->mutable_value()->mutable_array_value()->add_values()->set_int_value(val); + proto_value->mutable_array_value()->add_values()->set_int_value(val); } } else if (nostd::holds_alternative>(value)) { for (const auto &val : nostd::get>(value)) { - attribute->mutable_value()->mutable_array_value()->add_values()->set_int_value(val); + proto_value->mutable_array_value()->add_values()->set_int_value(val); } } else if (nostd::holds_alternative>(value)) { for (const auto &val : nostd::get>(value)) { - attribute->mutable_value()->mutable_array_value()->add_values()->set_double_value(val); + proto_value->mutable_array_value()->add_values()->set_double_value(val); } } else if (nostd::holds_alternative>(value)) { for (const auto &val : nostd::get>(value)) { - attribute->mutable_value()->mutable_array_value()->add_values()->set_string_value(val); + proto_value->mutable_array_value()->add_values()->set_string_value(val); } } } +void OtlpPopulateAttributeUtils::PopulateAttribute( + opentelemetry::proto::common::v1::KeyValue *attribute, + nostd::string_view key, + const opentelemetry::common::AttributeValue &value) noexcept +{ + if (nullptr == attribute) + { + return; + } + + // Assert size of variant to ensure that this method gets updated if the variant + // definition changes + static_assert( + nostd::variant_size::value == kAttributeValueSize, + "AttributeValue contains unknown type"); + + attribute->set_key(key.data(), key.size()); + PopulateAnyValue(attribute->mutable_value(), value); +} + +/** Maps from C++ attribute into OTLP proto attribute. */ +void OtlpPopulateAttributeUtils::PopulateAttribute( + opentelemetry::proto::common::v1::KeyValue *attribute, + nostd::string_view key, + const opentelemetry::sdk::common::OwnedAttributeValue &value) noexcept +{ + if (nullptr == attribute) + { + return; + } + + // Assert size of variant to ensure that this method gets updated if the variant + // definition changes + static_assert(nostd::variant_size::value == + kOwnedAttributeValueSize, + "OwnedAttributeValue contains unknown type"); + + attribute->set_key(key.data(), key.size()); + PopulateAnyValue(attribute->mutable_value(), value); +} + void OtlpPopulateAttributeUtils::PopulateAttribute( opentelemetry::proto::resource::v1::Resource *proto, const opentelemetry::sdk::resource::Resource &resource) noexcept diff --git a/exporters/otlp/src/otlp_recordable_utils.cc b/exporters/otlp/src/otlp_recordable_utils.cc index 3bfe606bab..2e2996883d 100644 --- a/exporters/otlp/src/otlp_recordable_utils.cc +++ b/exporters/otlp/src/otlp_recordable_utils.cc @@ -11,6 +11,7 @@ #include "opentelemetry/exporters/otlp/protobuf_include_suffix.h" #include "opentelemetry/exporters/otlp/otlp_log_recordable.h" +#include "opentelemetry/exporters/otlp/otlp_populate_attribute_utils.h" #include "opentelemetry/exporters/otlp/otlp_recordable.h" #include @@ -73,7 +74,7 @@ void OtlpRecordableUtils::PopulateRequest( #ifdef ENABLE_LOGS_PREVIEW void OtlpRecordableUtils::PopulateRequest( - const nostd::span> &logs, + const nostd::span> &logs, proto::collector::logs::v1::ExportLogsServiceRequest *request) noexcept { if (nullptr == request) @@ -109,7 +110,8 @@ void OtlpRecordableUtils::PopulateRequest( { if (!output_resource_log->has_resource()) { - *output_resource_log->mutable_resource() = input_log_record->ProtoResource(); + OtlpPopulateAttributeUtils::PopulateAttribute(output_resource_log->mutable_resource(), + *input_resource_log.first); output_resource_log->set_schema_url(input_resource_log.first->GetSchemaURL()); } diff --git a/exporters/otlp/test/otlp_grpc_log_record_exporter_test.cc b/exporters/otlp/test/otlp_grpc_log_record_exporter_test.cc index 34aee61a0c..6bf92daf87 100644 --- a/exporters/otlp/test/otlp_grpc_log_record_exporter_test.cc +++ b/exporters/otlp/test/otlp_grpc_log_record_exporter_test.cc @@ -16,7 +16,6 @@ # include "opentelemetry/logs/provider.h" # include "opentelemetry/sdk/logs/batch_log_record_processor.h" # include "opentelemetry/sdk/logs/exporter.h" -# include "opentelemetry/sdk/logs/log_record.h" # include "opentelemetry/sdk/logs/logger_provider.h" # include "opentelemetry/sdk/resource/resource.h" @@ -63,7 +62,7 @@ TEST_F(OtlpGrpcLogRecordExporterTestPeer, ShutdownTest) auto recordable_2 = exporter->MakeRecordable(); // exporter shuold not be shutdown by default - nostd::span> batch_1(&recordable_1, 1); + nostd::span> batch_1(&recordable_1, 1); EXPECT_CALL(*mock_stub, Export(_, _, _)) .Times(Exactly(1)) .WillOnce(Return(grpc::Status::OK)) @@ -74,7 +73,7 @@ TEST_F(OtlpGrpcLogRecordExporterTestPeer, ShutdownTest) exporter->Shutdown(); - nostd::span> batch_2(&recordable_2, 1); + nostd::span> batch_2(&recordable_2, 1); result = exporter->Export(batch_2); EXPECT_EQ(sdk::common::ExportResult::kFailure, result); } @@ -90,13 +89,13 @@ TEST_F(OtlpGrpcLogRecordExporterTestPeer, ExportUnitTest) auto recordable_2 = exporter->MakeRecordable(); // Test successful RPC - nostd::span> batch_1(&recordable_1, 1); + nostd::span> batch_1(&recordable_1, 1); EXPECT_CALL(*mock_stub, Export(_, _, _)).Times(Exactly(1)).WillOnce(Return(grpc::Status::OK)); auto result = exporter->Export(batch_1); EXPECT_EQ(sdk::common::ExportResult::kSuccess, result); // Test failed RPC - nostd::span> batch_2(&recordable_2, 1); + nostd::span> batch_2(&recordable_2, 1); EXPECT_CALL(*mock_stub, Export(_, _, _)) .Times(Exactly(1)) .WillOnce(Return(grpc::Status::CANCELLED)); diff --git a/exporters/otlp/test/otlp_http_log_record_exporter_test.cc b/exporters/otlp/test/otlp_http_log_record_exporter_test.cc index 6dc8ef8960..240de8b40d 100644 --- a/exporters/otlp/test/otlp_http_log_record_exporter_test.cc +++ b/exporters/otlp/test/otlp_http_log_record_exporter_test.cc @@ -21,7 +21,6 @@ # include "opentelemetry/logs/provider.h" # include "opentelemetry/sdk/logs/batch_log_record_processor.h" # include "opentelemetry/sdk/logs/exporter.h" -# include "opentelemetry/sdk/logs/log_record.h" # include "opentelemetry/sdk/logs/logger_provider.h" # include "opentelemetry/sdk/resource/resource.h" # include "opentelemetry/test_common/ext/http/client/nosend/http_client_nosend.h" @@ -492,7 +491,7 @@ TEST(OtlpHttpLogRecordExporterTest, Shutdown) std::unique_ptr(new OtlpHttpLogRecordExporter()); ASSERT_TRUE(exporter->Shutdown()); - nostd::span> logs = {}; + nostd::span> logs = {}; auto result = exporter->Export(logs); EXPECT_EQ(result, opentelemetry::sdk::common::ExportResult::kFailure); diff --git a/exporters/otlp/test/otlp_log_recordable_test.cc b/exporters/otlp/test/otlp_log_recordable_test.cc index a20d80b36a..96c5c66b09 100644 --- a/exporters/otlp/test/otlp_log_recordable_test.cc +++ b/exporters/otlp/test/otlp_log_recordable_test.cc @@ -5,7 +5,10 @@ # include +# include + # include "opentelemetry/exporters/otlp/otlp_log_recordable.h" +# include "opentelemetry/sdk/logs/read_write_log_record.h" # include "opentelemetry/sdk/resource/resource.h" # include "opentelemetry/sdk/resource/semantic_conventions.h" @@ -17,7 +20,7 @@ namespace otlp namespace resource = opentelemetry::sdk::resource; namespace proto = opentelemetry::proto; -TEST(OtlpLogRecordable, SetTimestamp) +TEST(OtlpLogRecordable, Basic) { OtlpLogRecordable rec; std::chrono::system_clock::time_point now = std::chrono::system_clock::now(); @@ -27,53 +30,34 @@ TEST(OtlpLogRecordable, SetTimestamp) std::chrono::duration_cast(now.time_since_epoch()).count(); rec.SetTimestamp(start_timestamp); - EXPECT_EQ(rec.log_record().time_unix_nano(), unix_start); -} - -TEST(OtlpLogRecordable, SetSeverity) -{ - OtlpLogRecordable rec; rec.SetSeverity(opentelemetry::logs::Severity::kError); - - EXPECT_EQ(rec.log_record().severity_number(), proto::logs::v1::SEVERITY_NUMBER_ERROR); - EXPECT_EQ(rec.log_record().severity_text(), "ERROR"); -} - -TEST(OtlpLogRecordable, SetBody) -{ - OtlpLogRecordable rec; nostd::string_view name = "Test Log Message"; rec.SetBody(name); - EXPECT_EQ(rec.log_record().body().string_value(), name); -} -TEST(OtlpLogRecordable, SetTraceId) -{ - OtlpLogRecordable rec; uint8_t trace_id_bin[opentelemetry::trace::TraceId::kSize] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; - std::string expected_bytes; - expected_bytes.assign( + std::string expected_trace_id_bytes; + expected_trace_id_bytes.assign( reinterpret_cast(trace_id_bin), reinterpret_cast(trace_id_bin) + opentelemetry::trace::TraceId::kSize); rec.SetTraceId(opentelemetry::trace::TraceId{trace_id_bin}); - EXPECT_EQ(rec.log_record().trace_id(), expected_bytes); -} - -TEST(OtlpLogRecordable, SetSpanId) -{ - OtlpLogRecordable rec; uint8_t span_id_bin[opentelemetry::trace::SpanId::kSize] = {'7', '6', '5', '4', '3', '2', '1', '0'}; - std::string expected_bytes; - expected_bytes.assign( + std::string expected_span_id_bytes; + expected_span_id_bytes.assign( reinterpret_cast(span_id_bin), reinterpret_cast(span_id_bin) + opentelemetry::trace::SpanId::kSize); rec.SetSpanId(opentelemetry::trace::SpanId{span_id_bin}); - EXPECT_EQ(rec.log_record().span_id(), expected_bytes); + + EXPECT_EQ(rec.log_record().time_unix_nano(), unix_start); + EXPECT_EQ(rec.log_record().severity_number(), proto::logs::v1::SEVERITY_NUMBER_ERROR); + EXPECT_EQ(rec.log_record().severity_text(), "ERROR"); + EXPECT_EQ(rec.log_record().body().string_value(), name); + EXPECT_EQ(rec.log_record().trace_id(), expected_trace_id_bytes); + EXPECT_EQ(rec.log_record().span_id(), expected_span_id_bytes); } -TEST(OtlpLogRecordable, SetResource) +TEST(OtlpLogRecordable, GetResource) { OtlpLogRecordable rec; const std::string service_name_key = "service.name"; @@ -82,51 +66,21 @@ TEST(OtlpLogRecordable, SetResource) opentelemetry::sdk::resource::Resource::Create({{service_name_key, service_name}}); rec.SetResource(resource); - auto proto_resource = rec.ProtoResource(); - bool found_service_name = false; - for (int i = 0; i < proto_resource.attributes_size(); i++) - { - auto attr = proto_resource.attributes(static_cast(i)); - if (attr.key() == service_name_key && attr.value().string_value() == service_name) - { - found_service_name = true; - } - } - EXPECT_TRUE(found_service_name); + EXPECT_EQ(&rec.GetResource(), &resource); } TEST(OtlpLogRecordable, DefaultResource) { OtlpLogRecordable rec; - auto proto_resource = rec.ProtoResource(); - int found_resource_count = 0; - for (int i = 0; i < proto_resource.attributes_size(); i++) - { - auto attr = proto_resource.attributes(static_cast(i)); - if (attr.key() == resource::SemanticConventions::kTelemetrySdkLanguage) - { - EXPECT_EQ(attr.value().string_value(), "cpp"); - ++found_resource_count; - } - else if (attr.key() == resource::SemanticConventions::kTelemetrySdkName) - { - EXPECT_EQ(attr.value().string_value(), "opentelemetry"); - ++found_resource_count; - } - else if (attr.key() == resource::SemanticConventions::kTelemetrySdkVersion) - { - EXPECT_EQ(attr.value().string_value(), OPENTELEMETRY_SDK_VERSION); - ++found_resource_count; - } - } - EXPECT_EQ(3, found_resource_count); + EXPECT_EQ(&rec.GetResource(), &opentelemetry::sdk::logs::ReadableLogRecord::GetDefaultResource()); } // Test non-int single types. Int single types are tested using templates (see IntAttributeTest) TEST(OtlpLogRecordable, SetSingleAttribute) { OtlpLogRecordable rec; + nostd::string_view bool_key = "bool_attr"; common::AttributeValue bool_val(true); rec.SetAttribute(bool_key, bool_val); @@ -139,21 +93,33 @@ TEST(OtlpLogRecordable, SetSingleAttribute) common::AttributeValue str_val(nostd::string_view("Test")); rec.SetAttribute(str_key, str_val); - EXPECT_EQ(rec.log_record().attributes(0).key(), bool_key); - EXPECT_EQ(rec.log_record().attributes(0).value().bool_value(), nostd::get(bool_val)); - - EXPECT_EQ(rec.log_record().attributes(1).key(), double_key); - EXPECT_EQ(rec.log_record().attributes(1).value().double_value(), nostd::get(double_val)); - - EXPECT_EQ(rec.log_record().attributes(2).key(), str_key); - EXPECT_EQ(rec.log_record().attributes(2).value().string_value(), - nostd::get(str_val).data()); + int checked_attributes = 0; + for (auto &attribute : rec.log_record().attributes()) + { + if (attribute.key() == bool_key) + { + ++checked_attributes; + EXPECT_EQ(attribute.value().bool_value(), nostd::get(bool_val)); + } + else if (attribute.key() == double_key) + { + ++checked_attributes; + EXPECT_EQ(attribute.value().double_value(), nostd::get(double_val)); + } + else if (attribute.key() == str_key) + { + ++checked_attributes; + EXPECT_EQ(attribute.value().string_value(), nostd::get(str_val).data()); + } + } + EXPECT_EQ(3, checked_attributes); } // Test non-int array types. Int array types are tested using templates (see IntAttributeTest) TEST(OtlpLogRecordable, SetArrayAttribute) { OtlpLogRecordable rec; + const int kArraySize = 3; bool bool_arr[kArraySize] = {true, false, true}; @@ -168,24 +134,42 @@ TEST(OtlpLogRecordable, SetArrayAttribute) nostd::span str_span(str_arr); rec.SetAttribute("str_arr_attr", str_span); + const opentelemetry::proto::common::v1::ArrayValue *bool_values = nullptr; + const opentelemetry::proto::common::v1::ArrayValue *double_values = nullptr; + const opentelemetry::proto::common::v1::ArrayValue *string_values = nullptr; + for (int i = 0; i < rec.log_record().attributes_size(); i++) + { + if (rec.log_record().attributes(i).value().array_value().values(0).has_bool_value()) + { + bool_values = &rec.log_record().attributes(i).value().array_value(); + } + else if (rec.log_record().attributes(i).value().array_value().values(0).has_double_value()) + { + double_values = &rec.log_record().attributes(i).value().array_value(); + } + else if (rec.log_record().attributes(i).value().array_value().values(0).has_string_value()) + { + string_values = &rec.log_record().attributes(i).value().array_value(); + } + } + for (int i = 0; i < kArraySize; i++) { - EXPECT_EQ(rec.log_record().attributes(0).value().array_value().values(i).bool_value(), - bool_span[i]); - EXPECT_EQ(rec.log_record().attributes(1).value().array_value().values(i).double_value(), - double_span[i]); - EXPECT_EQ(rec.log_record().attributes(2).value().array_value().values(i).string_value(), - str_span[i]); + EXPECT_EQ(bool_values->values(i).bool_value(), bool_span[i]); + EXPECT_EQ(double_values->values(i).double_value(), double_span[i]); + EXPECT_EQ(string_values->values(i).string_value(), str_span[i]); } } TEST(OtlpLogRecordable, SetInstrumentationScope) { OtlpLogRecordable rec; + auto inst_lib = opentelemetry::sdk::instrumentationscope::InstrumentationScope::Create("test", "v1"); rec.SetInstrumentationScope(*inst_lib); - EXPECT_EQ(rec.GetInstrumentationScope(), *inst_lib); + + EXPECT_EQ(&rec.GetInstrumentationScope(), inst_lib.get()); } /** @@ -209,7 +193,9 @@ TYPED_TEST(OtlpLogRecordableIntAttributeTest, SetIntSingleAttribute) common::AttributeValue int_val(i); OtlpLogRecordable rec; + rec.SetAttribute("int_attr", int_val); + EXPECT_EQ(rec.log_record().attributes(0).value().int_value(), nostd::get(int_val)); } @@ -222,6 +208,7 @@ TYPED_TEST(OtlpLogRecordableIntAttributeTest, SetIntArrayAttribute) nostd::span int_span(int_arr); OtlpLogRecordable rec; + rec.SetAttribute("int_arr_attr", int_span); for (int i = 0; i < kArraySize; i++) diff --git a/sdk/include/opentelemetry/sdk/common/circular_buffer.h b/sdk/include/opentelemetry/sdk/common/circular_buffer.h index 6af9001833..9f34659352 100644 --- a/sdk/include/opentelemetry/sdk/common/circular_buffer.h +++ b/sdk/include/opentelemetry/sdk/common/circular_buffer.h @@ -115,6 +115,14 @@ class CircularBuffer return true; } + bool Add(std::unique_ptr &&ptr) noexcept + { + // rvalue to lvalue reference + bool result = Add(std::ref(ptr)); + ptr.reset(); + return result; + } + /** * Clear the circular buffer. * diff --git a/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor.h b/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor.h index 0621316dcd..f9b30d9195 100644 --- a/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor.h +++ b/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor.h @@ -41,7 +41,7 @@ class BatchLogRecordProcessor : public LogRecordProcessor * equal to max_queue_size */ explicit BatchLogRecordProcessor( - std::unique_ptr &&exporter, + nostd::unique_ptr &&exporter, const size_t max_queue_size = 2048, const std::chrono::milliseconds scheduled_delay_millis = std::chrono::milliseconds(5000), const size_t max_export_batch_size = 512); @@ -53,18 +53,18 @@ class BatchLogRecordProcessor : public LogRecordProcessor * @param exporter - The backend exporter to pass the logs to * @param options - The batch SpanProcessor options. */ - explicit BatchLogRecordProcessor(std::unique_ptr &&exporter, + explicit BatchLogRecordProcessor(nostd::unique_ptr &&exporter, const BatchLogRecordProcessorOptions &options); /** Makes a new recordable **/ - std::unique_ptr MakeRecordable() noexcept override; + nostd::unique_ptr MakeRecordable() noexcept override; /** * Called when the Logger's log method creates a log record * @param record the log record */ - void OnEmit(std::unique_ptr &&record) noexcept override; + void OnEmit(nostd::unique_ptr &&record) noexcept override; /** * Export all log records that have not been exported yet. @@ -134,7 +134,7 @@ class BatchLogRecordProcessor : public LogRecordProcessor std::chrono::time_point &start_time); /* The configured backend log exporter */ - std::unique_ptr exporter_; + nostd::unique_ptr exporter_; /* Configurable parameters as per the official *trace* specs */ const size_t max_queue_size_; diff --git a/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor_factory.h b/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor_factory.h index a3ebb0e877..e670a66d69 100644 --- a/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor_factory.h +++ b/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor_factory.h @@ -25,8 +25,8 @@ class BatchLogRecordProcessorFactory /** * Create a BatchLogRecordProcessor. */ - std::unique_ptr Create(std::unique_ptr &&exporter, - const BatchLogRecordProcessorOptions &options); + nostd::unique_ptr Create(nostd::unique_ptr &&exporter, + const BatchLogRecordProcessorOptions &options); }; } // namespace logs diff --git a/sdk/include/opentelemetry/sdk/logs/exporter.h b/sdk/include/opentelemetry/sdk/logs/exporter.h index 02865dc204..76ec8cb320 100644 --- a/sdk/include/opentelemetry/sdk/logs/exporter.h +++ b/sdk/include/opentelemetry/sdk/logs/exporter.h @@ -6,9 +6,12 @@ # include # include + # include "opentelemetry/nostd/span.h" +# include "opentelemetry/nostd/unique_ptr.h" # include "opentelemetry/sdk/common/exporter_utils.h" # include "opentelemetry/sdk/logs/processor.h" +# include "opentelemetry/sdk/logs/readable_log_record.h" # include "opentelemetry/sdk/logs/recordable.h" OPENTELEMETRY_BEGIN_NAMESPACE @@ -25,15 +28,13 @@ class LogRecordExporter virtual ~LogRecordExporter() = default; /** - * Create a log recordable. This object will be used to record log data and - * will subsequently be passed to LogRecordExporter::Export. Vendors can implement - * custom recordables or use the default LogRecord recordable provided by the - * SDK. + * Create a log recordable from readable log record object. This object will be used to record log + * data and will subsequently be passed to LogRecordExporter::Export. * @return a newly initialized Recordable object * * Note: This method must be callable from multiple threads. */ - virtual std::unique_ptr MakeRecordable() noexcept = 0; + virtual nostd::unique_ptr MakeRecordable() noexcept = 0; /** * Exports the batch of log records to their export destination. @@ -44,7 +45,7 @@ class LogRecordExporter * @returns an ExportResult code (whether export was success or failure) */ virtual sdk::common::ExportResult Export( - const nostd::span> &records) noexcept = 0; + const nostd::span> &records) noexcept = 0; /** * Marks the exporter as ShutDown and cleans up any resources as required. diff --git a/sdk/include/opentelemetry/sdk/logs/log_record.h b/sdk/include/opentelemetry/sdk/logs/log_record.h deleted file mode 100644 index 8b7ac19570..0000000000 --- a/sdk/include/opentelemetry/sdk/logs/log_record.h +++ /dev/null @@ -1,200 +0,0 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 - -#pragma once -#ifdef ENABLE_LOGS_PREVIEW - -# include -# include -# include "opentelemetry/sdk/common/attribute_utils.h" -# include "opentelemetry/sdk/logs/recordable.h" -# include "opentelemetry/sdk/resource/resource.h" -# include "opentelemetry/version.h" - -OPENTELEMETRY_BEGIN_NAMESPACE -namespace sdk -{ -namespace logs -{ - -/** - * A default Recordable implemenation to be passed in log statements, - * matching the 10 fields of the Log Data Model. - * (https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/logs/data-model.md#log-and-event-record-definition) - * - */ -class LogRecord final : public Recordable -{ -private: - // Default values are set by the respective data structures' constructors for all fields, - // except the severity field, which must be set manually (an enum with no default value). - opentelemetry::logs::Severity severity_ = opentelemetry::logs::Severity::kInvalid; - const opentelemetry::sdk::resource::Resource *resource_ = nullptr; - common::AttributeMap attributes_map_; - std::string body_; // Currently a simple string, but should be changed to "Any" type - opentelemetry::trace::TraceId trace_id_; - opentelemetry::trace::SpanId span_id_; - opentelemetry::trace::TraceFlags trace_flags_; - opentelemetry::common::SystemTimestamp timestamp_; // uint64 nanoseconds since Unix epoch - -public: - /********** Setters for each field (overrides methods from the Recordable interface) ************/ - - /** - * Set the severity for this log. - * @param severity the severity of the event - */ - void SetSeverity(opentelemetry::logs::Severity severity) noexcept override - { - severity_ = severity; - } - - /** - * Set body field for this log. - * @param message the body to set - */ - void SetBody(nostd::string_view message) noexcept override { body_ = std::string(message); } - - /** - * Set Resource of this log - * @param Resource the resource to set - */ - void SetResource(const opentelemetry::sdk::resource::Resource &resource) noexcept override - { - resource_ = &resource; - } - - /** - * Set an attribute of a log. - * @param name the name of the attribute - * @param value the attribute value - */ - - void SetAttribute(nostd::string_view key, - const opentelemetry::common::AttributeValue &value) noexcept override - { - attributes_map_.SetAttribute(key, value); - } - - /** - * Set trace id for this log. - * @param trace_id the trace id to set - */ - void SetTraceId(opentelemetry::trace::TraceId trace_id) noexcept override - { - trace_id_ = trace_id; - } - - /** - * Set span id for this log. - * @param span_id the span id to set - */ - virtual void SetSpanId(opentelemetry::trace::SpanId span_id) noexcept override - { - span_id_ = span_id; - } - - /** - * Inject a trace_flags for this log. - * @param trace_flags the span id to set - */ - void SetTraceFlags(opentelemetry::trace::TraceFlags trace_flags) noexcept override - { - trace_flags_ = trace_flags; - } - - /** - * Set the timestamp for this log. - * @param timestamp the timestamp of the event - */ - void SetTimestamp(opentelemetry::common::SystemTimestamp timestamp) noexcept override - { - timestamp_ = timestamp; - } - - /************************** Getters for each field ****************************/ - - /** - * Get the severity for this log - * @return the severity for this log - */ - opentelemetry::logs::Severity GetSeverity() const noexcept { return severity_; } - - /** - * Get the body of this log - * @return the body of this log - */ - std::string GetBody() const noexcept { return body_; } - - /** - * Get the resource for this log - * @return the resource for this log - */ - const opentelemetry::sdk::resource::Resource &GetResource() const noexcept - { - if (nullptr == resource_) - { - return sdk::resource::Resource::GetDefault(); - } - return *resource_; - } - - /** - * Get the attributes for this log - * @return the attributes for this log - */ - const std::unordered_map &GetAttributes() const noexcept - { - return attributes_map_.GetAttributes(); - } - - /** - * Get the trace id for this log - * @return the trace id for this log - */ - opentelemetry::trace::TraceId GetTraceId() const noexcept { return trace_id_; } - - /** - * Get the span id for this log - * @return the span id for this log - */ - opentelemetry::trace::SpanId GetSpanId() const noexcept { return span_id_; } - - /** - * Get the trace flags for this log - * @return the trace flags for this log - */ - opentelemetry::trace::TraceFlags GetTraceFlags() const noexcept { return trace_flags_; } - - /** - * Get the timestamp for this log - * @return the timestamp for this log - */ - opentelemetry::common::SystemTimestamp GetTimestamp() const noexcept { return timestamp_; } - - /** - * Set instrumentation_scope for this log. - * @param instrumentation_scope the instrumentation scope to set - */ - void SetInstrumentationScope(const opentelemetry::sdk::instrumentationscope::InstrumentationScope - &instrumentation_scope) noexcept override - { - instrumentation_scope_ = &instrumentation_scope; - } - - OPENTELEMETRY_DEPRECATED_MESSAGE("Please use SetInstrumentationScope instead") - void SetInstrumentationLibrary( - const opentelemetry::sdk::instrumentationscope::InstrumentationScope - &instrumentation_scope) noexcept - { - SetInstrumentationScope(instrumentation_scope); - } - -private: - const opentelemetry::sdk::instrumentationscope::InstrumentationScope *instrumentation_scope_ = - nullptr; -}; -} // namespace logs -} // namespace sdk -OPENTELEMETRY_END_NAMESPACE -#endif diff --git a/sdk/include/opentelemetry/sdk/logs/logger.h b/sdk/include/opentelemetry/sdk/logs/logger.h index 7e4429e0cf..e8f8cfadab 100644 --- a/sdk/include/opentelemetry/sdk/logs/logger.h +++ b/sdk/include/opentelemetry/sdk/logs/logger.h @@ -6,6 +6,7 @@ # include "opentelemetry/common/macros.h" # include "opentelemetry/logs/logger.h" +# include "opentelemetry/nostd/unique_ptr.h" # include "opentelemetry/sdk/instrumentationscope/instrumentation_scope.h" # include "opentelemetry/sdk/logs/logger_context.h" # include "opentelemetry/sdk/logs/logger_provider.h" @@ -30,7 +31,7 @@ class Logger final : public opentelemetry::logs::Logger explicit Logger( opentelemetry::nostd::string_view name, std::shared_ptr context, - std::unique_ptr instrumentation_scope = + nostd::unique_ptr instrumentation_scope = instrumentationscope::InstrumentationScope::Create("")) noexcept; /** @@ -38,38 +39,12 @@ class Logger final : public opentelemetry::logs::Logger */ const opentelemetry::nostd::string_view GetName() noexcept override; - /** - * Writes a log record into the processor. - * @param severity the severity level of the log event. - * @param message the string message of the log (perhaps support std::fmt or fmt-lib format). - * with the log event. - * @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. */ - void Log(opentelemetry::logs::Severity severity, - nostd::string_view body, - const opentelemetry::common::KeyValueIterable &attributes, - opentelemetry::trace::TraceId trace_id, - opentelemetry::trace::SpanId span_id, - opentelemetry::trace::TraceFlags trace_flags, - opentelemetry::common::SystemTimestamp timestamp) noexcept override; + nostd::unique_ptr CreateLogRecord() noexcept override; - // Deprecated - void Log(opentelemetry::logs::Severity severity, - nostd::string_view /* name */, - nostd::string_view body, - const opentelemetry::common::KeyValueIterable &attributes, - opentelemetry::trace::TraceId trace_id, - opentelemetry::trace::SpanId span_id, - opentelemetry::trace::TraceFlags trace_flags, - opentelemetry::common::SystemTimestamp timestamp) noexcept override - { - Log(severity, body, attributes, trace_id, span_id, trace_flags, timestamp); - } + using opentelemetry::logs::Logger::EmitLogRecord; + + void EmitLogRecord( + nostd::unique_ptr &&log_record) noexcept override; /** Returns the associated instrumentation scope */ const opentelemetry::sdk::instrumentationscope::InstrumentationScope &GetInstrumentationScope() @@ -88,7 +63,7 @@ class Logger final : public opentelemetry::logs::Logger // order of declaration is important here - instrumentation scope should destroy after // logger-context. - std::unique_ptr instrumentation_scope_; + nostd::unique_ptr instrumentation_scope_; std::shared_ptr context_; }; diff --git a/sdk/include/opentelemetry/sdk/logs/logger_context.h b/sdk/include/opentelemetry/sdk/logs/logger_context.h index da41c122b7..b416f67eaf 100644 --- a/sdk/include/opentelemetry/sdk/logs/logger_context.h +++ b/sdk/include/opentelemetry/sdk/logs/logger_context.h @@ -30,7 +30,7 @@ namespace logs class LoggerContext { public: - explicit LoggerContext(std::vector> &&processors, + explicit LoggerContext(std::vector> &&processors, opentelemetry::sdk::resource::Resource resource = opentelemetry::sdk::resource::Resource::Create({})) noexcept; @@ -42,7 +42,7 @@ class LoggerContext * * Note: This method is not thread safe. */ - void AddProcessor(std::unique_ptr processor) noexcept; + void AddProcessor(nostd::unique_ptr processor) noexcept; /** * Obtain the configured (composite) processor. @@ -72,7 +72,7 @@ class LoggerContext private: // order of declaration is important here - resource object should be destroyed after processor. opentelemetry::sdk::resource::Resource resource_; - std::unique_ptr processor_; + nostd::unique_ptr processor_; }; } // namespace logs } // namespace sdk diff --git a/sdk/include/opentelemetry/sdk/logs/logger_context_factory.h b/sdk/include/opentelemetry/sdk/logs/logger_context_factory.h index 28dc24d56c..86ae4f8f00 100644 --- a/sdk/include/opentelemetry/sdk/logs/logger_context_factory.h +++ b/sdk/include/opentelemetry/sdk/logs/logger_context_factory.h @@ -25,14 +25,14 @@ class LoggerContextFactory /** * Create a LoggerContext. */ - static std::unique_ptr Create( - std::vector> &&processors); + static nostd::unique_ptr Create( + std::vector> &&processors); /** * Create a LoggerContext. */ - static std::unique_ptr Create( - std::vector> &&processors, + static nostd::unique_ptr Create( + std::vector> &&processors, const opentelemetry::sdk::resource::Resource &resource); }; diff --git a/sdk/include/opentelemetry/sdk/logs/logger_provider.h b/sdk/include/opentelemetry/sdk/logs/logger_provider.h index fd38532752..ee99c5a393 100644 --- a/sdk/include/opentelemetry/sdk/logs/logger_provider.h +++ b/sdk/include/opentelemetry/sdk/logs/logger_provider.h @@ -41,11 +41,11 @@ class LoggerProvider final : public opentelemetry::logs::LoggerProvider * @param id_generator The custom id generator for this logger provider. This must * not be a nullptr */ - explicit LoggerProvider(std::unique_ptr &&processor, + explicit LoggerProvider(nostd::unique_ptr &&processor, opentelemetry::sdk::resource::Resource resource = opentelemetry::sdk::resource::Resource::Create({})) noexcept; - explicit LoggerProvider(std::vector> &&processors, + explicit LoggerProvider(std::vector> &&processors, opentelemetry::sdk::resource::Resource resource = opentelemetry::sdk::resource::Resource::Create({})) noexcept; @@ -101,7 +101,7 @@ class LoggerProvider final : public opentelemetry::logs::LoggerProvider * @param processor The processor to be stored inside the logger provider. * This must not be a nullptr. */ - void AddProcessor(std::unique_ptr processor) noexcept; + void AddProcessor(nostd::unique_ptr processor) noexcept; /** * Obtain the resource associated with this logger provider. diff --git a/sdk/include/opentelemetry/sdk/logs/logger_provider_factory.h b/sdk/include/opentelemetry/sdk/logs/logger_provider_factory.h index e103fce8be..e68efd33aa 100644 --- a/sdk/include/opentelemetry/sdk/logs/logger_provider_factory.h +++ b/sdk/include/opentelemetry/sdk/logs/logger_provider_factory.h @@ -30,33 +30,33 @@ class LoggerProviderFactory /** * Create a LoggerProvider. */ - static std::unique_ptr Create( - std::unique_ptr &&processor); + static nostd::unique_ptr Create( + nostd::unique_ptr &&processor); /** * Create a LoggerProvider. */ - static std::unique_ptr Create( - std::unique_ptr &&processor, + static nostd::unique_ptr Create( + nostd::unique_ptr &&processor, const opentelemetry::sdk::resource::Resource &resource); /** * Create a LoggerProvider. */ - static std::unique_ptr Create( - std::vector> &&processors); + static nostd::unique_ptr Create( + std::vector> &&processors); /** * Create a LoggerProvider. */ - static std::unique_ptr Create( - std::vector> &&processors, + static nostd::unique_ptr Create( + std::vector> &&processors, const opentelemetry::sdk::resource::Resource &resource); /** * Create a LoggerProvider. */ - static std::unique_ptr Create( + static nostd::unique_ptr Create( std::shared_ptr context); }; diff --git a/sdk/include/opentelemetry/sdk/logs/multi_log_record_processor.h b/sdk/include/opentelemetry/sdk/logs/multi_log_record_processor.h index 54a7e7959e..e4a6d59ec7 100644 --- a/sdk/include/opentelemetry/sdk/logs/multi_log_record_processor.h +++ b/sdk/include/opentelemetry/sdk/logs/multi_log_record_processor.h @@ -28,18 +28,18 @@ namespace logs class MultiLogRecordProcessor : public LogRecordProcessor { public: - MultiLogRecordProcessor(std::vector> &&processors); + MultiLogRecordProcessor(std::vector> &&processors); ~MultiLogRecordProcessor() override; - void AddProcessor(std::unique_ptr &&processor); + void AddProcessor(nostd::unique_ptr &&processor); - std::unique_ptr MakeRecordable() noexcept override; + nostd::unique_ptr MakeRecordable() noexcept override; /** * OnEmit is called by the SDK once a log record has been successfully created. * @param record the log record */ - void OnEmit(std::unique_ptr &&record) noexcept override; + void OnEmit(nostd::unique_ptr &&record) noexcept override; /** * Exports all log records that have not yet been exported to the configured Exporter. @@ -60,7 +60,7 @@ class MultiLogRecordProcessor : public LogRecordProcessor std::chrono::microseconds timeout = std::chrono::microseconds::max()) noexcept override; private: - std::vector> processors_; + std::vector> processors_; }; } // namespace logs } // namespace sdk diff --git a/sdk/include/opentelemetry/sdk/logs/multi_log_record_processor_factory.h b/sdk/include/opentelemetry/sdk/logs/multi_log_record_processor_factory.h index 0e8b9a5878..f6879d2fcc 100644 --- a/sdk/include/opentelemetry/sdk/logs/multi_log_record_processor_factory.h +++ b/sdk/include/opentelemetry/sdk/logs/multi_log_record_processor_factory.h @@ -22,8 +22,8 @@ namespace logs class MultiLogRecordProcessorFactory { public: - static std::unique_ptr Create( - std::vector> &&processors); + static nostd::unique_ptr Create( + std::vector> &&processors); }; } // namespace logs diff --git a/sdk/include/opentelemetry/sdk/logs/multi_recordable.h b/sdk/include/opentelemetry/sdk/logs/multi_recordable.h index 9b37806e2a..b369cdaa03 100644 --- a/sdk/include/opentelemetry/sdk/logs/multi_recordable.h +++ b/sdk/include/opentelemetry/sdk/logs/multi_recordable.h @@ -24,12 +24,12 @@ class MultiRecordable final : public Recordable { public: void AddRecordable(const LogRecordProcessor &processor, - std::unique_ptr recordable) noexcept; + nostd::unique_ptr recordable) noexcept; - const std::unique_ptr &GetRecordable( + const nostd::unique_ptr &GetRecordable( const LogRecordProcessor &processor) const noexcept; - std::unique_ptr ReleaseRecordable(const LogRecordProcessor &processor) noexcept; + nostd::unique_ptr ReleaseRecordable(const LogRecordProcessor &processor) noexcept; /** * Set the timestamp for this log. @@ -37,6 +37,12 @@ class MultiRecordable final : public Recordable */ void SetTimestamp(opentelemetry::common::SystemTimestamp timestamp) noexcept override; + /** + * Set the observed timestamp for this log. + * @param timestamp the timestamp to set + */ + void SetObservedTimestamp(opentelemetry::common::SystemTimestamp timestamp) noexcept override; + /** * Set the severity for this log. * @param severity the severity of the event @@ -47,39 +53,39 @@ class MultiRecordable final : public Recordable * Set body field for this log. * @param message the body to set */ - void SetBody(nostd::string_view message) noexcept override; - - /** - * Set Resource of this log - * @param Resource the resource to set - */ - void SetResource(const opentelemetry::sdk::resource::Resource &resource) noexcept override; - - /** - * Set an attribute of a log. - * @param key the name of the attribute - * @param value the attribute value - */ - void SetAttribute(nostd::string_view key, - const opentelemetry::common::AttributeValue &value) noexcept override; + void SetBody(const opentelemetry::common::AttributeValue &message) noexcept override; /** * Set the trace id for this log. * @param trace_id the trace id to set */ - void SetTraceId(opentelemetry::trace::TraceId trace_id) noexcept override; + void SetTraceId(const opentelemetry::trace::TraceId &trace_id) noexcept override; /** * Set the span id for this log. * @param span_id the span id to set */ - void SetSpanId(opentelemetry::trace::SpanId span_id) noexcept override; + void SetSpanId(const opentelemetry::trace::SpanId &span_id) noexcept override; /** * Inject trace_flags for this log. * @param trace_flags the trace flags to set */ - void SetTraceFlags(opentelemetry::trace::TraceFlags trace_flags) noexcept override; + void SetTraceFlags(const opentelemetry::trace::TraceFlags &trace_flags) noexcept override; + + /** + * Set an attribute of a log. + * @param key the name of the attribute + * @param value the attribute value + */ + void SetAttribute(nostd::string_view key, + const opentelemetry::common::AttributeValue &value) noexcept override; + + /** + * Set Resource of this log + * @param Resource the resource to set + */ + void SetResource(const opentelemetry::sdk::resource::Resource &resource) noexcept override; /** * Set instrumentation_scope for this log. @@ -88,21 +94,8 @@ class MultiRecordable final : public Recordable void SetInstrumentationScope(const opentelemetry::sdk::instrumentationscope::InstrumentationScope &instrumentation_scope) noexcept override; - /** Returns the associated instrumentation scope */ - const opentelemetry::sdk::instrumentationscope::InstrumentationScope &GetInstrumentationScope() - const noexcept; - - OPENTELEMETRY_DEPRECATED_MESSAGE("Please use GetInstrumentationScope instead") - const opentelemetry::sdk::instrumentationscope::InstrumentationScope &GetInstrumentationLibrary() - const noexcept - { - return GetInstrumentationScope(); - } - private: - std::unordered_map> recordables_; - const opentelemetry::sdk::instrumentationscope::InstrumentationScope *instrumentation_scope_ = - nullptr; + std::unordered_map> recordables_; }; } // namespace logs } // namespace sdk diff --git a/sdk/include/opentelemetry/sdk/logs/processor.h b/sdk/include/opentelemetry/sdk/logs/processor.h index 536140f7db..bef5fd4882 100644 --- a/sdk/include/opentelemetry/sdk/logs/processor.h +++ b/sdk/include/opentelemetry/sdk/logs/processor.h @@ -6,6 +6,9 @@ # include # include + +# include "opentelemetry/nostd/unique_ptr.h" +# include "opentelemetry/sdk/logs/readable_log_record.h" # include "opentelemetry/sdk/logs/recordable.h" OPENTELEMETRY_BEGIN_NAMESPACE @@ -23,19 +26,19 @@ class LogRecordProcessor virtual ~LogRecordProcessor() = default; /** - * Create a log recordable. This requests a new log recordable from the - * associated exporter. + * Create a log recordable from readable log record object + * * @return a newly initialized recordable * * Note: This method must be callable from multiple threads. */ - virtual std::unique_ptr MakeRecordable() noexcept = 0; + virtual nostd::unique_ptr MakeRecordable() noexcept = 0; /** * OnEmit is called by the SDK once a log record has been successfully created. - * @param record the log record + * @param record the log recordable object */ - virtual void OnEmit(std::unique_ptr &&record) noexcept = 0; + virtual void OnEmit(nostd::unique_ptr &&record) noexcept = 0; /** * Exports all log records that have not yet been exported to the configured Exporter. diff --git a/sdk/include/opentelemetry/sdk/logs/read_write_log_record.h b/sdk/include/opentelemetry/sdk/logs/read_write_log_record.h new file mode 100644 index 0000000000..a043f7b5fd --- /dev/null +++ b/sdk/include/opentelemetry/sdk/logs/read_write_log_record.h @@ -0,0 +1,190 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once +#ifdef ENABLE_LOGS_PREVIEW + +# include + +# include "opentelemetry/common/attribute_value.h" +# include "opentelemetry/common/key_value_iterable.h" +# include "opentelemetry/common/timestamp.h" +# include "opentelemetry/logs/log_record.h" +# include "opentelemetry/logs/severity.h" +# include "opentelemetry/nostd/unique_ptr.h" +# include "opentelemetry/sdk/common/attribute_utils.h" +# include "opentelemetry/sdk/common/empty_attributes.h" +# include "opentelemetry/sdk/instrumentationscope/instrumentation_scope.h" +# include "opentelemetry/sdk/logs/readable_log_record.h" +# include "opentelemetry/sdk/resource/resource.h" +# include "opentelemetry/trace/span_id.h" +# include "opentelemetry/trace/trace_flags.h" +# include "opentelemetry/trace/trace_id.h" +# include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace logs +{ +/** + * Maintains a representation of a log in a format that can be processed by a recorder. + * + * This class is thread-compatible. + */ +class ReadWriteLogRecord final : public ReadableLogRecord +{ +public: + ReadWriteLogRecord(); + ~ReadWriteLogRecord() override; + + /** + * Set the timestamp for this log. + * @param timestamp the timestamp to set + */ + void SetTimestamp(opentelemetry::common::SystemTimestamp timestamp) noexcept override; + + /** + * Get the timestamp of this log. + * @return the timestamp of this log + */ + opentelemetry::common::SystemTimestamp GetTimestamp() const noexcept override; + + /** + * Set the observed timestamp for this log. + * @param timestamp the timestamp to set + */ + void SetObservedTimestamp(opentelemetry::common::SystemTimestamp timestamp) noexcept override; + + /** + * Get the observed timestamp of this log. + * @return the observed timestamp of this log + */ + opentelemetry::common::SystemTimestamp GetObservedTimestamp() const noexcept override; + + /** + * Set the severity for this log. + * @param severity the severity of the event + */ + void SetSeverity(opentelemetry::logs::Severity severity) noexcept override; + + /** + * Get the severity of this log. + * @return the severity of this log + */ + opentelemetry::logs::Severity GetSeverity() const noexcept override; + + /** + * Set body field for this log. + * @param message the body to set + */ + void SetBody(const opentelemetry::common::AttributeValue &message) noexcept override; + + /** + * Get body field of this log. + * @return the body field for this log. + */ + const opentelemetry::common::AttributeValue &GetBody() const noexcept override; + + /** + * Set the trace id for this log. + * @param trace_id the trace id to set + */ + void SetTraceId(const opentelemetry::trace::TraceId &trace_id) noexcept override; + + /** + * Get the trace id of this log. + * @return the trace id of this log + */ + const opentelemetry::trace::TraceId &GetTraceId() const noexcept override; + + /** + * Set the span id for this log. + * @param span_id the span id to set + */ + void SetSpanId(const opentelemetry::trace::SpanId &span_id) noexcept override; + + /** + * Get the span id of this log. + * @return the span id of this log + */ + const opentelemetry::trace::SpanId &GetSpanId() const noexcept override; + + /** + * Inject trace_flags for this log. + * @param trace_flags the trace flags to set + */ + void SetTraceFlags(const opentelemetry::trace::TraceFlags &trace_flags) noexcept override; + + /** + * Inject trace_flags of this log. + * @return trace_flags of this log + */ + const opentelemetry::trace::TraceFlags &GetTraceFlags() const noexcept override; + + /** + * Set an attribute of a log. + * @param key the name of the attribute + * @param value the attribute value + */ + void SetAttribute(nostd::string_view key, + const opentelemetry::common::AttributeValue &value) noexcept override; + + /** + * Get attributes of this log. + * @return the body field of this log + */ + const std::unordered_map &GetAttributes() + const noexcept override; + + /** + * Get resource of this log + * @return the resource of this log + */ + const opentelemetry::sdk::resource::Resource &GetResource() const noexcept override; + + /** + * Set Resource of this log + * @param Resource the resource to set + */ + void SetResource(const opentelemetry::sdk::resource::Resource &resource) noexcept override; + + /** + * Get instrumentation_scope of this log. + * @return the instrumentation_scope of this log + */ + const opentelemetry::sdk::instrumentationscope::InstrumentationScope &GetInstrumentationScope() + const noexcept override; + + /** + * Set instrumentation_scope for this log. + * @param instrumentation_scope the instrumentation scope to set + */ + void SetInstrumentationScope(const opentelemetry::sdk::instrumentationscope::InstrumentationScope + &instrumentation_scope) noexcept override; + +private: + // Default values are set by the respective data structures' constructors for all fields, + // except the severity field, which must be set manually (an enum with no default value). + opentelemetry::logs::Severity severity_; + const opentelemetry::sdk::resource::Resource *resource_; + const opentelemetry::sdk::instrumentationscope::InstrumentationScope *instrumentation_scope_; + + std::unordered_map attributes_map_; + opentelemetry::common::AttributeValue body_; + opentelemetry::common::SystemTimestamp timestamp_; + opentelemetry::common::SystemTimestamp observed_timestamp_; + + // We do not pay for trace state when not necessary + struct TraceState + { + opentelemetry::trace::TraceId trace_id; + opentelemetry::trace::SpanId span_id; + opentelemetry::trace::TraceFlags trace_flags; + }; + nostd::unique_ptr trace_state_; +}; +} // namespace logs +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE +#endif diff --git a/sdk/include/opentelemetry/sdk/logs/readable_log_record.h b/sdk/include/opentelemetry/sdk/logs/readable_log_record.h new file mode 100644 index 0000000000..7b3d8ad502 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/logs/readable_log_record.h @@ -0,0 +1,121 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once +#ifdef ENABLE_LOGS_PREVIEW + +# include +# include + +# include "opentelemetry/common/attribute_value.h" +# include "opentelemetry/common/key_value_iterable.h" +# include "opentelemetry/common/timestamp.h" +# include "opentelemetry/logs/severity.h" +# include "opentelemetry/sdk/common/attribute_utils.h" +# include "opentelemetry/sdk/common/empty_attributes.h" +# include "opentelemetry/sdk/instrumentationscope/instrumentation_scope.h" +# include "opentelemetry/sdk/logs/recordable.h" +# include "opentelemetry/sdk/resource/resource.h" +# include "opentelemetry/trace/span_id.h" +# include "opentelemetry/trace/trace_flags.h" +# include "opentelemetry/trace/trace_id.h" +# include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace logs +{ +/** + * Maintains a representation of a log in a format that can be processed by a recorder. + * + * This class is thread-compatible. + */ +class ReadableLogRecord : public Recordable +{ +public: + /** + * Get the timestamp of this log. + * @return the timestamp of this log + */ + virtual opentelemetry::common::SystemTimestamp GetTimestamp() const noexcept = 0; + + /** + * Get the observed timestamp of this log. + * @return the observed timestamp of this log + */ + virtual opentelemetry::common::SystemTimestamp GetObservedTimestamp() const noexcept = 0; + + /** + * Get the severity of this log. + * @return the severity of this log + */ + virtual opentelemetry::logs::Severity GetSeverity() const noexcept = 0; + + /** + * Get the severity text of this log. + * @return the severity text for this log + */ + virtual nostd::string_view GetSeverityText() const noexcept; + + /** + * Get body field of this log. + * @return the body field for this log. + */ + virtual const opentelemetry::common::AttributeValue &GetBody() const noexcept = 0; + + /** + * Get the trace id of this log. + * @return the trace id of this log + */ + virtual const opentelemetry::trace::TraceId &GetTraceId() const noexcept = 0; + + /** + * Get the span id of this log. + * @return the span id of this log + */ + virtual const opentelemetry::trace::SpanId &GetSpanId() const noexcept = 0; + + /** + * Inject trace_flags of this log. + * @return trace_flags of this log + */ + virtual const opentelemetry::trace::TraceFlags &GetTraceFlags() const noexcept = 0; + + /** + * Get attributes of this log. + * @return the body field of this log + */ + virtual const std::unordered_map + &GetAttributes() const noexcept = 0; + + /** + * Get resource of this log + * @return the resource of this log + */ + virtual const opentelemetry::sdk::resource::Resource &GetResource() const noexcept = 0; + + /** + * Get instrumentation scope of this log. + * @return the instrumentation scope of this log + */ + virtual const opentelemetry::sdk::instrumentationscope::InstrumentationScope & + GetInstrumentationScope() const noexcept = 0; + + /** + * Get default instrumentation scope of logs. + * @return the default instrumentation scope of logs + */ + static const opentelemetry::sdk::instrumentationscope::InstrumentationScope & + GetDefaultInstrumentationScope() noexcept; + + /** + * Get default resource of logs. + * @return the default resource of logs + */ + static const opentelemetry::sdk::resource::Resource &GetDefaultResource() noexcept; +}; +} // namespace logs +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE +#endif diff --git a/sdk/include/opentelemetry/sdk/logs/recordable.h b/sdk/include/opentelemetry/sdk/logs/recordable.h index 13e834ee49..983a2e6b57 100644 --- a/sdk/include/opentelemetry/sdk/logs/recordable.h +++ b/sdk/include/opentelemetry/sdk/logs/recordable.h @@ -7,12 +7,13 @@ # include "opentelemetry/common/attribute_value.h" # include "opentelemetry/common/key_value_iterable.h" # include "opentelemetry/common/timestamp.h" +# include "opentelemetry/logs/log_record.h" # include "opentelemetry/logs/severity.h" +# include "opentelemetry/sdk/common/attribute_utils.h" # include "opentelemetry/sdk/common/empty_attributes.h" # include "opentelemetry/sdk/instrumentationscope/instrumentation_scope.h" +# include "opentelemetry/sdk/logs/recordable.h" # include "opentelemetry/sdk/resource/resource.h" -# include "opentelemetry/trace/span.h" -# include "opentelemetry/trace/span_context.h" # include "opentelemetry/trace/span_id.h" # include "opentelemetry/trace/trace_flags.h" # include "opentelemetry/trace/trace_id.h" @@ -28,61 +29,16 @@ namespace logs * * This class is thread-compatible. */ -class Recordable + +class Recordable : public opentelemetry::logs::LogRecord { public: - virtual ~Recordable() = default; - - /** - * Set the timestamp for this log. - * @param timestamp the timestamp to set - */ - virtual void SetTimestamp(opentelemetry::common::SystemTimestamp timestamp) noexcept = 0; - - /** - * Set the severity for this log. - * @param severity the severity of the event - */ - virtual void SetSeverity(opentelemetry::logs::Severity severity) noexcept = 0; - - /** - * Set body field for this log. - * @param message the body to set - */ - virtual void SetBody(nostd::string_view message) noexcept = 0; - /** * Set Resource of this log * @param Resource the resource to set */ virtual void SetResource(const opentelemetry::sdk::resource::Resource &resource) noexcept = 0; - /** - * Set an attribute of a log. - * @param key the name of the attribute - * @param value the attribute value - */ - virtual void SetAttribute(nostd::string_view key, - const opentelemetry::common::AttributeValue &value) noexcept = 0; - - /** - * Set the trace id for this log. - * @param trace_id the trace id to set - */ - virtual void SetTraceId(opentelemetry::trace::TraceId trace_id) noexcept = 0; - - /** - * Set the span id for this log. - * @param span_id the span id to set - */ - virtual void SetSpanId(opentelemetry::trace::SpanId span_id) noexcept = 0; - - /** - * Inject trace_flags for this log. - * @param trace_flags the trace flags to set - */ - virtual void SetTraceFlags(opentelemetry::trace::TraceFlags trace_flags) noexcept = 0; - /** * Set instrumentation_scope for this log. * @param instrumentation_scope the instrumentation scope to set @@ -90,15 +46,8 @@ class Recordable virtual void SetInstrumentationScope( const opentelemetry::sdk::instrumentationscope::InstrumentationScope &instrumentation_scope) noexcept = 0; - - OPENTELEMETRY_DEPRECATED_MESSAGE("Please use SetInstrumentationScope instead") - void SetInstrumentationLibrary( - const opentelemetry::sdk::instrumentationscope::InstrumentationScope - &instrumentation_scope) noexcept - { - SetInstrumentationScope(instrumentation_scope); - } }; + } // namespace logs } // namespace sdk OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/include/opentelemetry/sdk/logs/simple_log_record_processor.h b/sdk/include/opentelemetry/sdk/logs/simple_log_record_processor.h index 0e4ce3f2ab..5c8d790696 100644 --- a/sdk/include/opentelemetry/sdk/logs/simple_log_record_processor.h +++ b/sdk/include/opentelemetry/sdk/logs/simple_log_record_processor.h @@ -28,12 +28,12 @@ class SimpleLogRecordProcessor : public LogRecordProcessor { public: - explicit SimpleLogRecordProcessor(std::unique_ptr &&exporter); + explicit SimpleLogRecordProcessor(nostd::unique_ptr &&exporter); ~SimpleLogRecordProcessor() override = default; - std::unique_ptr MakeRecordable() noexcept override; + nostd::unique_ptr MakeRecordable() noexcept override; - void OnEmit(std::unique_ptr &&record) noexcept override; + void OnEmit(nostd::unique_ptr &&record) noexcept override; bool ForceFlush( std::chrono::microseconds timeout = std::chrono::microseconds::max()) noexcept override; @@ -45,7 +45,7 @@ class SimpleLogRecordProcessor : public LogRecordProcessor private: // The configured exporter - std::unique_ptr exporter_; + nostd::unique_ptr exporter_; // The lock used to ensure the exporter is not called concurrently opentelemetry::common::SpinLockMutex lock_; // The atomic boolean to ensure the ShutDown() function is only called once diff --git a/sdk/include/opentelemetry/sdk/logs/simple_log_record_processor_factory.h b/sdk/include/opentelemetry/sdk/logs/simple_log_record_processor_factory.h index 7ba2c3ac0f..ce299263c9 100644 --- a/sdk/include/opentelemetry/sdk/logs/simple_log_record_processor_factory.h +++ b/sdk/include/opentelemetry/sdk/logs/simple_log_record_processor_factory.h @@ -23,7 +23,8 @@ class SimpleLogRecordProcessorFactory /** * Create a SimpleLogRecordProcessor. */ - static std::unique_ptr Create(std::unique_ptr &&exporter); + static nostd::unique_ptr Create( + nostd::unique_ptr &&exporter); }; } // namespace logs diff --git a/sdk/src/logs/CMakeLists.txt b/sdk/src/logs/CMakeLists.txt index 546e66614b..ff43c4d92b 100644 --- a/sdk/src/logs/CMakeLists.txt +++ b/sdk/src/logs/CMakeLists.txt @@ -11,7 +11,9 @@ add_library( logger_context_factory.cc multi_log_record_processor.cc multi_log_record_processor_factory.cc - multi_recordable.cc) + multi_recordable.cc + read_write_log_record.cc + readable_log_record.cc) set_target_properties(opentelemetry_logs PROPERTIES EXPORT_NAME logs) diff --git a/sdk/src/logs/batch_log_record_processor.cc b/sdk/src/logs/batch_log_record_processor.cc index 87992e7822..40bce9474e 100644 --- a/sdk/src/logs/batch_log_record_processor.cc +++ b/sdk/src/logs/batch_log_record_processor.cc @@ -15,7 +15,7 @@ namespace sdk namespace logs { BatchLogRecordProcessor::BatchLogRecordProcessor( - std::unique_ptr &&exporter, + nostd::unique_ptr &&exporter, const size_t max_queue_size, const std::chrono::milliseconds scheduled_delay_millis, const size_t max_export_batch_size) @@ -33,7 +33,7 @@ BatchLogRecordProcessor::BatchLogRecordProcessor( synchronization_data_->is_shutdown.store(false); } -BatchLogRecordProcessor::BatchLogRecordProcessor(std::unique_ptr &&exporter, +BatchLogRecordProcessor::BatchLogRecordProcessor(nostd::unique_ptr &&exporter, const BatchLogRecordProcessorOptions &options) : exporter_(std::move(exporter)), max_queue_size_(options.max_queue_size), @@ -49,19 +49,19 @@ BatchLogRecordProcessor::BatchLogRecordProcessor(std::unique_ptris_shutdown.store(false); } -std::unique_ptr BatchLogRecordProcessor::MakeRecordable() noexcept +nostd::unique_ptr BatchLogRecordProcessor::MakeRecordable() noexcept { return exporter_->MakeRecordable(); } -void BatchLogRecordProcessor::OnEmit(std::unique_ptr &&record) noexcept +void BatchLogRecordProcessor::OnEmit(nostd::unique_ptr &&record) noexcept { if (synchronization_data_->is_shutdown.load() == true) { return; } - if (buffer_.Add(record) == false) + if (buffer_.Add(std::unique_ptr(record.release())) == false) { return; } @@ -186,7 +186,7 @@ void BatchLogRecordProcessor::Export() { do { - std::vector> records_arr; + std::vector> records_arr; size_t num_records_to_export; bool notify_force_flush = synchronization_data_->is_force_flush_pending.exchange(false, std::memory_order_acq_rel); @@ -211,13 +211,13 @@ void BatchLogRecordProcessor::Export() range.ForEach([&](AtomicUniquePtr &ptr) { std::unique_ptr swap_ptr = std::unique_ptr(nullptr); ptr.Swap(swap_ptr); - records_arr.push_back(std::unique_ptr(swap_ptr.release())); + records_arr.push_back(nostd::unique_ptr(swap_ptr.release())); return true; }); }); exporter_->Export( - nostd::span>(records_arr.data(), records_arr.size())); + nostd::span>(records_arr.data(), records_arr.size())); NotifyCompletion(notify_force_flush, synchronization_data_); } while (true); } diff --git a/sdk/src/logs/batch_log_record_processor_factory.cc b/sdk/src/logs/batch_log_record_processor_factory.cc index 67d0a677ab..65d78afd6e 100644 --- a/sdk/src/logs/batch_log_record_processor_factory.cc +++ b/sdk/src/logs/batch_log_record_processor_factory.cc @@ -12,11 +12,11 @@ namespace sdk namespace logs { -std::unique_ptr BatchLogRecordProcessorFactory::Create( - std::unique_ptr &&exporter, +nostd::unique_ptr BatchLogRecordProcessorFactory::Create( + nostd::unique_ptr &&exporter, const BatchLogRecordProcessorOptions &options) { - std::unique_ptr processor( + nostd::unique_ptr processor( new BatchLogRecordProcessor(std::move(exporter), options)); return processor; } diff --git a/sdk/src/logs/logger.cc b/sdk/src/logs/logger.cc index d7ab485dff..5e3e7594f3 100644 --- a/sdk/src/logs/logger.cc +++ b/sdk/src/logs/logger.cc @@ -3,7 +3,7 @@ #ifdef ENABLE_LOGS_PREVIEW # include "opentelemetry/sdk/logs/logger.h" -# include "opentelemetry/sdk/logs/log_record.h" +# include "opentelemetry/sdk/logs/read_write_log_record.h" # include "opentelemetry/sdk_config.h" # include "opentelemetry/trace/provider.h" @@ -19,7 +19,7 @@ namespace common = opentelemetry::common; Logger::Logger( nostd::string_view name, std::shared_ptr context, - std::unique_ptr instrumentation_scope) noexcept + nostd::unique_ptr instrumentation_scope) noexcept : logger_name_(std::string(name)), instrumentation_scope_(std::move(instrumentation_scope)), context_(context) @@ -30,88 +30,31 @@ const nostd::string_view Logger::GetName() noexcept return logger_name_; } -/** - * Create and populate recordable with the log event's fields passed in. - * The timestamp, severity, traceid, spanid, and traceflags, are injected - * if the user does not specify them. - */ -void Logger::Log(opentelemetry::logs::Severity severity, - nostd::string_view body, - const common::KeyValueIterable &attributes, - trace_api::TraceId trace_id, - trace_api::SpanId span_id, - trace_api::TraceFlags trace_flags, - common::SystemTimestamp timestamp) noexcept +nostd::unique_ptr Logger::CreateLogRecord() noexcept { - // If this logger does not have a processor, no need to create a log record + // If this logger does not have a processor, no need to create a log recordable if (!context_) { - return; + return nullptr; } - auto &processor = context_->GetProcessor(); - // TODO: Sampler (should include check for minSeverity) + return nostd::unique_ptr( + context_->GetProcessor().MakeRecordable().release()); +} - auto recordable = processor.MakeRecordable(); - if (recordable == nullptr) +void Logger::EmitLogRecord(nostd::unique_ptr &&log_record) noexcept +{ + if (!log_record) { - OTEL_INTERNAL_LOG_ERROR("[LOGGER] Recordable creation failed"); return; } - // Populate recordable fields - recordable->SetTimestamp(timestamp); - recordable->SetSeverity(severity); - recordable->SetBody(body); - recordable->SetInstrumentationScope(GetInstrumentationScope()); - - recordable->SetResource(context_->GetResource()); - - attributes.ForEachKeyValue([&](nostd::string_view key, common::AttributeValue value) noexcept { - recordable->SetAttribute(key, value); - return true; - }); - - // Inject trace_id/span_id/trace_flags if none is set by user - auto provider = trace_api::Provider::GetTracerProvider(); - auto tracer = provider->GetTracer(logger_name_); - auto span_context = tracer->GetCurrentSpan()->GetContext(); - - // Leave these fields in the recordable empty if neither the passed in values - // nor the context values are valid (e.g. the application is not using traces) - - // TraceId - if (trace_id.IsValid()) - { - recordable->SetTraceId(trace_id); - } - else if (span_context.trace_id().IsValid()) - { - recordable->SetTraceId(span_context.trace_id()); - } - - // SpanId - if (span_id.IsValid()) - { - recordable->SetSpanId(span_id); - } - else if (span_context.span_id().IsValid()) - { - recordable->SetSpanId(span_context.span_id()); - } + auto &processor = context_->GetProcessor(); - // TraceFlags - if (trace_flags.IsSampled()) - { - recordable->SetTraceFlags(trace_flags); - } - else if (span_context.trace_flags().IsSampled()) - { - recordable->SetTraceFlags(span_context.trace_flags()); - } + // TODO: Sampler (should include check for minSeverity) - // Send the log record to the processor - processor.OnEmit(std::move(recordable)); + // Send the log recordable to the processor + processor.OnEmit(nostd::unique_ptr(static_cast(log_record.release()))); } const opentelemetry::sdk::instrumentationscope::InstrumentationScope & diff --git a/sdk/src/logs/logger_context.cc b/sdk/src/logs/logger_context.cc index 3421ad9999..deadbb77fa 100644 --- a/sdk/src/logs/logger_context.cc +++ b/sdk/src/logs/logger_context.cc @@ -15,14 +15,14 @@ namespace sdk namespace logs { -LoggerContext::LoggerContext(std::vector> &&processors, +LoggerContext::LoggerContext(std::vector> &&processors, opentelemetry::sdk::resource::Resource resource) noexcept : resource_(resource), processor_( - std::unique_ptr(new MultiLogRecordProcessor(std::move(processors)))) + nostd::unique_ptr(new MultiLogRecordProcessor(std::move(processors)))) {} -void LoggerContext::AddProcessor(std::unique_ptr processor) noexcept +void LoggerContext::AddProcessor(nostd::unique_ptr processor) noexcept { auto multi_processor = static_cast(processor_.get()); multi_processor->AddProcessor(std::move(processor)); diff --git a/sdk/src/logs/logger_context_factory.cc b/sdk/src/logs/logger_context_factory.cc index 985743fd67..67e52ab670 100644 --- a/sdk/src/logs/logger_context_factory.cc +++ b/sdk/src/logs/logger_context_factory.cc @@ -14,18 +14,18 @@ namespace sdk namespace logs { -std::unique_ptr LoggerContextFactory::Create( - std::vector> &&processors) +nostd::unique_ptr LoggerContextFactory::Create( + std::vector> &&processors) { auto resource = opentelemetry::sdk::resource::Resource::Create({}); return Create(std::move(processors), resource); } -std::unique_ptr LoggerContextFactory::Create( - std::vector> &&processors, +nostd::unique_ptr LoggerContextFactory::Create( + std::vector> &&processors, const opentelemetry::sdk::resource::Resource &resource) { - std::unique_ptr context(new LoggerContext(std::move(processors), resource)); + nostd::unique_ptr context(new LoggerContext(std::move(processors), resource)); return context; } diff --git a/sdk/src/logs/logger_provider.cc b/sdk/src/logs/logger_provider.cc index 6f806d5be9..2abc0f1cbf 100644 --- a/sdk/src/logs/logger_provider.cc +++ b/sdk/src/logs/logger_provider.cc @@ -21,16 +21,16 @@ namespace logs namespace nostd = opentelemetry::nostd; namespace logs_api = opentelemetry::logs; -LoggerProvider::LoggerProvider(std::unique_ptr &&processor, +LoggerProvider::LoggerProvider(nostd::unique_ptr &&processor, opentelemetry::sdk::resource::Resource resource) noexcept { - std::vector> processors; + std::vector> processors; processors.emplace_back(std::move(processor)); context_ = std::make_shared(std::move(processors), std::move(resource)); OTEL_INTERNAL_LOG_DEBUG("[LoggerProvider] LoggerProvider created."); } -LoggerProvider::LoggerProvider(std::vector> &&processors, +LoggerProvider::LoggerProvider(std::vector> &&processors, opentelemetry::sdk::resource::Resource resource) noexcept : context_{ std::make_shared(std::move(processors), std::move(resource))} @@ -38,7 +38,7 @@ LoggerProvider::LoggerProvider(std::vector> LoggerProvider::LoggerProvider() noexcept : context_{std::make_shared( - std::vector>{})} + std::vector>{})} {} LoggerProvider::LoggerProvider(std::shared_ptr context) noexcept @@ -118,7 +118,7 @@ nostd::shared_ptr LoggerProvider::GetLogger( return GetLogger(logger_name, "", library_name, library_version, schema_url); } -void LoggerProvider::AddProcessor(std::unique_ptr processor) noexcept +void LoggerProvider::AddProcessor(nostd::unique_ptr processor) noexcept { context_->AddProcessor(std::move(processor)); } diff --git a/sdk/src/logs/logger_provider_factory.cc b/sdk/src/logs/logger_provider_factory.cc index 3177b9180e..654b3f2d61 100644 --- a/sdk/src/logs/logger_provider_factory.cc +++ b/sdk/src/logs/logger_provider_factory.cc @@ -13,42 +13,42 @@ namespace sdk namespace logs { -std::unique_ptr LoggerProviderFactory::Create( - std::unique_ptr &&processor) +nostd::unique_ptr LoggerProviderFactory::Create( + nostd::unique_ptr &&processor) { auto resource = opentelemetry::sdk::resource::Resource::Create({}); return Create(std::move(processor), resource); } -std::unique_ptr LoggerProviderFactory::Create( - std::unique_ptr &&processor, +nostd::unique_ptr LoggerProviderFactory::Create( + nostd::unique_ptr &&processor, const opentelemetry::sdk::resource::Resource &resource) { - std::unique_ptr provider( + nostd::unique_ptr provider( new LoggerProvider(std::move(processor), resource)); return provider; } -std::unique_ptr LoggerProviderFactory::Create( - std::vector> &&processors) +nostd::unique_ptr LoggerProviderFactory::Create( + std::vector> &&processors) { auto resource = opentelemetry::sdk::resource::Resource::Create({}); return Create(std::move(processors), resource); } -std::unique_ptr LoggerProviderFactory::Create( - std::vector> &&processors, +nostd::unique_ptr LoggerProviderFactory::Create( + std::vector> &&processors, const opentelemetry::sdk::resource::Resource &resource) { - std::unique_ptr provider( + nostd::unique_ptr provider( new LoggerProvider(std::move(processors), resource)); return provider; } -std::unique_ptr LoggerProviderFactory::Create( +nostd::unique_ptr LoggerProviderFactory::Create( std::shared_ptr context) { - std::unique_ptr provider(new LoggerProvider(context)); + nostd::unique_ptr provider(new LoggerProvider(context)); return provider; } diff --git a/sdk/src/logs/multi_log_record_processor.cc b/sdk/src/logs/multi_log_record_processor.cc index a6f20be7eb..1f47910b7e 100644 --- a/sdk/src/logs/multi_log_record_processor.cc +++ b/sdk/src/logs/multi_log_record_processor.cc @@ -16,7 +16,7 @@ namespace logs { MultiLogRecordProcessor::MultiLogRecordProcessor( - std::vector> &&processors) + std::vector> &&processors) { for (auto &processor : processors) { @@ -29,7 +29,7 @@ MultiLogRecordProcessor::~MultiLogRecordProcessor() Shutdown(); } -void MultiLogRecordProcessor::AddProcessor(std::unique_ptr &&processor) +void MultiLogRecordProcessor::AddProcessor(nostd::unique_ptr &&processor) { // Add preocessor to end of the list. if (processor) @@ -38,9 +38,9 @@ void MultiLogRecordProcessor::AddProcessor(std::unique_ptr & } } -std::unique_ptr MultiLogRecordProcessor::MakeRecordable() noexcept +nostd::unique_ptr MultiLogRecordProcessor::MakeRecordable() noexcept { - auto recordable = std::unique_ptr(new MultiRecordable); + auto recordable = nostd::unique_ptr(new MultiRecordable()); auto multi_recordable = static_cast(recordable.get()); for (auto &processor : processors_) { @@ -49,7 +49,7 @@ std::unique_ptr MultiLogRecordProcessor::MakeRecordable() noexcept return recordable; } -void MultiLogRecordProcessor::OnEmit(std::unique_ptr &&record) noexcept +void MultiLogRecordProcessor::OnEmit(nostd::unique_ptr &&record) noexcept { if (!record) { diff --git a/sdk/src/logs/multi_log_record_processor_factory.cc b/sdk/src/logs/multi_log_record_processor_factory.cc index 3e0de46c32..5f3aa4b2ab 100644 --- a/sdk/src/logs/multi_log_record_processor_factory.cc +++ b/sdk/src/logs/multi_log_record_processor_factory.cc @@ -16,10 +16,11 @@ namespace sdk namespace logs { -std::unique_ptr MultiLogRecordProcessorFactory::Create( - std::vector> &&processors) +nostd::unique_ptr MultiLogRecordProcessorFactory::Create( + std::vector> &&processors) { - std::unique_ptr processor(new MultiLogRecordProcessor(std::move(processors))); + nostd::unique_ptr processor( + new MultiLogRecordProcessor(std::move(processors))); return processor; } diff --git a/sdk/src/logs/multi_recordable.cc b/sdk/src/logs/multi_recordable.cc index c91ffc6b82..f98935abdd 100644 --- a/sdk/src/logs/multi_recordable.cc +++ b/sdk/src/logs/multi_recordable.cc @@ -25,12 +25,12 @@ std::size_t MakeKey(const opentelemetry::sdk::logs::LogRecordProcessor &processo } // namespace void MultiRecordable::AddRecordable(const LogRecordProcessor &processor, - std::unique_ptr recordable) noexcept + nostd::unique_ptr recordable) noexcept { recordables_[MakeKey(processor)] = std::move(recordable); } -const std::unique_ptr &MultiRecordable::GetRecordable( +const nostd::unique_ptr &MultiRecordable::GetRecordable( const LogRecordProcessor &processor) const noexcept { // TODO - return nullptr ref on failed lookup? @@ -39,28 +39,43 @@ const std::unique_ptr &MultiRecordable::GetRecordable( { return i->second; } - static std::unique_ptr empty(nullptr); + static nostd::unique_ptr empty(nullptr); return empty; } -std::unique_ptr MultiRecordable::ReleaseRecordable( +nostd::unique_ptr MultiRecordable::ReleaseRecordable( const LogRecordProcessor &processor) noexcept { auto i = recordables_.find(MakeKey(processor)); if (i != recordables_.end()) { - std::unique_ptr result(i->second.release()); + nostd::unique_ptr result(i->second.release()); recordables_.erase(MakeKey(processor)); return result; } - return std::unique_ptr(nullptr); + return nostd::unique_ptr(nullptr); } void MultiRecordable::SetTimestamp(opentelemetry::common::SystemTimestamp timestamp) noexcept { for (auto &recordable : recordables_) { - recordable.second->SetTimestamp(timestamp); + if (recordable.second) + { + recordable.second->SetTimestamp(timestamp); + } + } +} + +void MultiRecordable::SetObservedTimestamp( + opentelemetry::common::SystemTimestamp timestamp) noexcept +{ + for (auto &recordable : recordables_) + { + if (recordable.second) + { + recordable.second->SetObservedTimestamp(timestamp); + } } } @@ -68,56 +83,77 @@ void MultiRecordable::SetSeverity(opentelemetry::logs::Severity severity) noexce { for (auto &recordable : recordables_) { - recordable.second->SetSeverity(severity); + if (recordable.second) + { + recordable.second->SetSeverity(severity); + } } } -void MultiRecordable::SetBody(nostd::string_view message) noexcept +void MultiRecordable::SetBody(const opentelemetry::common::AttributeValue &message) noexcept { for (auto &recordable : recordables_) { - recordable.second->SetBody(message); + if (recordable.second) + { + recordable.second->SetBody(message); + } } } -void MultiRecordable::SetResource(const opentelemetry::sdk::resource::Resource &resource) noexcept +void MultiRecordable::SetTraceId(const opentelemetry::trace::TraceId &trace_id) noexcept { for (auto &recordable : recordables_) { - recordable.second->SetResource(resource); + if (recordable.second) + { + recordable.second->SetTraceId(trace_id); + } } } -void MultiRecordable::SetAttribute(nostd::string_view key, - const opentelemetry::common::AttributeValue &value) noexcept +void MultiRecordable::SetSpanId(const opentelemetry::trace::SpanId &span_id) noexcept { for (auto &recordable : recordables_) { - recordable.second->SetAttribute(key, value); + if (recordable.second) + { + recordable.second->SetSpanId(span_id); + } } } -void MultiRecordable::SetTraceId(opentelemetry::trace::TraceId trace_id) noexcept +void MultiRecordable::SetTraceFlags(const opentelemetry::trace::TraceFlags &trace_flags) noexcept { for (auto &recordable : recordables_) { - recordable.second->SetTraceId(trace_id); + if (recordable.second) + { + recordable.second->SetTraceFlags(trace_flags); + } } } -void MultiRecordable::SetSpanId(opentelemetry::trace::SpanId span_id) noexcept +void MultiRecordable::SetAttribute(nostd::string_view key, + const opentelemetry::common::AttributeValue &value) noexcept { for (auto &recordable : recordables_) { - recordable.second->SetSpanId(span_id); + if (recordable.second) + { + recordable.second->SetAttribute(key, value); + } } } -void MultiRecordable::SetTraceFlags(opentelemetry::trace::TraceFlags trace_flags) noexcept +void MultiRecordable::SetResource(const opentelemetry::sdk::resource::Resource &resource) noexcept { for (auto &recordable : recordables_) { - recordable.second->SetTraceFlags(trace_flags); + if (recordable.second) + { + recordable.second->SetResource(resource); + } } } @@ -125,14 +161,15 @@ void MultiRecordable::SetInstrumentationScope( const opentelemetry::sdk::instrumentationscope::InstrumentationScope &instrumentation_scope) noexcept { - instrumentation_scope_ = &instrumentation_scope; + for (auto &recordable : recordables_) + { + if (recordable.second) + { + recordable.second->SetInstrumentationScope(instrumentation_scope); + } + } } -const opentelemetry::sdk::instrumentationscope::InstrumentationScope & -MultiRecordable::GetInstrumentationScope() const noexcept -{ - return *instrumentation_scope_; -} } // namespace logs } // namespace sdk diff --git a/sdk/src/logs/read_write_log_record.cc b/sdk/src/logs/read_write_log_record.cc new file mode 100644 index 0000000000..ed87c55080 --- /dev/null +++ b/sdk/src/logs/read_write_log_record.cc @@ -0,0 +1,177 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#ifdef ENABLE_LOGS_PREVIEW + +# include +# include + +# include "opentelemetry/sdk/logs/read_write_log_record.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace logs +{ + +ReadWriteLogRecord::ReadWriteLogRecord() + : severity_(opentelemetry::logs::Severity::kInvalid), + resource_(nullptr), + instrumentation_scope_(nullptr), + body_(nostd::string_view()), + observed_timestamp_(std::chrono::system_clock::now()) +{} + +ReadWriteLogRecord::~ReadWriteLogRecord() {} + +void ReadWriteLogRecord::SetTimestamp(opentelemetry::common::SystemTimestamp timestamp) noexcept +{ + timestamp_ = timestamp; +} + +opentelemetry::common::SystemTimestamp ReadWriteLogRecord::GetTimestamp() const noexcept +{ + return timestamp_; +} + +void ReadWriteLogRecord::SetObservedTimestamp( + opentelemetry::common::SystemTimestamp timestamp) noexcept +{ + observed_timestamp_ = timestamp; +} + +opentelemetry::common::SystemTimestamp ReadWriteLogRecord::GetObservedTimestamp() const noexcept +{ + return observed_timestamp_; +} + +void ReadWriteLogRecord::SetSeverity(opentelemetry::logs::Severity severity) noexcept +{ + severity_ = severity; +} + +opentelemetry::logs::Severity ReadWriteLogRecord::GetSeverity() const noexcept +{ + return severity_; +} + +void ReadWriteLogRecord::SetBody(const opentelemetry::common::AttributeValue &message) noexcept +{ + body_ = message; +} + +const opentelemetry::common::AttributeValue &ReadWriteLogRecord::GetBody() const noexcept +{ + return body_; +} + +void ReadWriteLogRecord::SetTraceId(const opentelemetry::trace::TraceId &trace_id) noexcept +{ + if (!trace_state_) + { + trace_state_ = nostd::unique_ptr(new TraceState()); + } + + trace_state_->trace_id = trace_id; +} + +const opentelemetry::trace::TraceId &ReadWriteLogRecord::GetTraceId() const noexcept +{ + if (trace_state_) + { + return trace_state_->trace_id; + } + + static opentelemetry::trace::TraceId empty; + return empty; +} + +void ReadWriteLogRecord::SetSpanId(const opentelemetry::trace::SpanId &span_id) noexcept +{ + if (!trace_state_) + { + trace_state_ = nostd::unique_ptr(new TraceState()); + } + + trace_state_->span_id = span_id; +} + +const opentelemetry::trace::SpanId &ReadWriteLogRecord::GetSpanId() const noexcept +{ + if (trace_state_) + { + return trace_state_->span_id; + } + + static opentelemetry::trace::SpanId empty; + return empty; +} + +void ReadWriteLogRecord::SetTraceFlags(const opentelemetry::trace::TraceFlags &trace_flags) noexcept +{ + if (!trace_state_) + { + trace_state_ = nostd::unique_ptr(new TraceState()); + } + + trace_state_->trace_flags = trace_flags; +} + +const opentelemetry::trace::TraceFlags &ReadWriteLogRecord::GetTraceFlags() const noexcept +{ + if (trace_state_) + { + return trace_state_->trace_flags; + } + + static opentelemetry::trace::TraceFlags empty; + return empty; +} + +void ReadWriteLogRecord::SetAttribute(nostd::string_view key, + const opentelemetry::common::AttributeValue &value) noexcept +{ + attributes_map_[static_cast(key)] = value; +} + +const std::unordered_map + &ReadWriteLogRecord::GetAttributes() const noexcept +{ + return attributes_map_; +} + +const opentelemetry::sdk::resource::Resource &ReadWriteLogRecord::GetResource() const noexcept +{ + if (nullptr != resource_) + { + return *resource_; + } + + return GetDefaultResource(); +} + +void ReadWriteLogRecord::SetResource( + const opentelemetry::sdk::resource::Resource &resource) noexcept +{ + resource_ = &resource; +} + +const opentelemetry::sdk::instrumentationscope::InstrumentationScope & +ReadWriteLogRecord::GetInstrumentationScope() const noexcept +{ + OPENTELEMETRY_LIKELY_IF(nullptr != instrumentation_scope_) { return *instrumentation_scope_; } + + return GetDefaultInstrumentationScope(); +} + +void ReadWriteLogRecord::SetInstrumentationScope( + const opentelemetry::sdk::instrumentationscope::InstrumentationScope + &instrumentation_scope) noexcept +{ + instrumentation_scope_ = &instrumentation_scope; +} +} // namespace logs +} // namespace sdk + +OPENTELEMETRY_END_NAMESPACE +#endif diff --git a/sdk/src/logs/readable_log_record.cc b/sdk/src/logs/readable_log_record.cc new file mode 100644 index 0000000000..e962fb665d --- /dev/null +++ b/sdk/src/logs/readable_log_record.cc @@ -0,0 +1,51 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#ifdef ENABLE_LOGS_PREVIEW + +# include +# include + +# include "opentelemetry/sdk/logs/readable_log_record.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace logs +{ + +nostd::string_view ReadableLogRecord::GetSeverityText() const noexcept +{ + std::size_t severity_index = static_cast(GetSeverity()); + if (severity_index >= std::extent::value) + { + return opentelemetry::logs::SeverityNumToText[0]; + } + + return opentelemetry::logs::SeverityNumToText[severity_index]; +} + +const opentelemetry::sdk::instrumentationscope::InstrumentationScope & +ReadableLogRecord::GetDefaultInstrumentationScope() noexcept +{ + // FIXME: Use shared default instrumentation scope? + static nostd::unique_ptr + default_scope = opentelemetry::sdk::instrumentationscope::InstrumentationScope::Create( + "otel-cpp", OPENTELEMETRY_SDK_VERSION, "https://opentelemetry.io/schemas/1.15.0"); + return *default_scope; +} + +const opentelemetry::sdk::resource::Resource &ReadableLogRecord::GetDefaultResource() noexcept +{ + static opentelemetry::sdk::resource::Resource default_resource = + opentelemetry::sdk::resource::Resource::Create( + {}, GetDefaultInstrumentationScope().GetSchemaURL()); + + return default_resource; +} + +} // namespace logs +} // namespace sdk + +OPENTELEMETRY_END_NAMESPACE +#endif diff --git a/sdk/src/logs/simple_log_record_processor.cc b/sdk/src/logs/simple_log_record_processor.cc index ef923bc590..3a7f25ed13 100644 --- a/sdk/src/logs/simple_log_record_processor.cc +++ b/sdk/src/logs/simple_log_record_processor.cc @@ -16,11 +16,11 @@ namespace logs * Initialize a simple log processor. * @param exporter the configured exporter where log records are sent */ -SimpleLogRecordProcessor::SimpleLogRecordProcessor(std::unique_ptr &&exporter) +SimpleLogRecordProcessor::SimpleLogRecordProcessor(nostd::unique_ptr &&exporter) : exporter_(std::move(exporter)), is_shutdown_(false) {} -std::unique_ptr SimpleLogRecordProcessor::MakeRecordable() noexcept +nostd::unique_ptr SimpleLogRecordProcessor::MakeRecordable() noexcept { return exporter_->MakeRecordable(); } @@ -29,9 +29,9 @@ std::unique_ptr SimpleLogRecordProcessor::MakeRecordable() noexcept * Batches the log record it receives in a batch of 1 and immediately sends it * to the configured exporter */ -void SimpleLogRecordProcessor::OnEmit(std::unique_ptr &&record) noexcept +void SimpleLogRecordProcessor::OnEmit(nostd::unique_ptr &&record) noexcept { - nostd::span> batch(&record, 1); + nostd::span> batch(&record, 1); // Get lock to ensure Export() is never called concurrently const std::lock_guard locked(lock_); diff --git a/sdk/src/logs/simple_log_record_processor_factory.cc b/sdk/src/logs/simple_log_record_processor_factory.cc index 213f61e16c..e25dfd261b 100644 --- a/sdk/src/logs/simple_log_record_processor_factory.cc +++ b/sdk/src/logs/simple_log_record_processor_factory.cc @@ -12,10 +12,11 @@ namespace sdk namespace logs { -std::unique_ptr SimpleLogRecordProcessorFactory::Create( - std::unique_ptr &&exporter) +nostd::unique_ptr SimpleLogRecordProcessorFactory::Create( + nostd::unique_ptr &&exporter) { - std::unique_ptr processor(new SimpleLogRecordProcessor(std::move(exporter))); + nostd::unique_ptr processor( + new SimpleLogRecordProcessor(std::move(exporter))); return processor; } diff --git a/sdk/test/logs/batch_log_record_processor_test.cc b/sdk/test/logs/batch_log_record_processor_test.cc index bbf1fc3016..abd018e601 100644 --- a/sdk/test/logs/batch_log_record_processor_test.cc +++ b/sdk/test/logs/batch_log_record_processor_test.cc @@ -5,7 +5,8 @@ # include "opentelemetry/sdk/logs/batch_log_record_processor.h" # include "opentelemetry/sdk/logs/exporter.h" -# include "opentelemetry/sdk/logs/log_record.h" +# include "opentelemetry/sdk/logs/readable_log_record.h" +# include "opentelemetry/sdk/logs/recordable.h" # include # include @@ -15,6 +16,53 @@ using namespace opentelemetry::sdk::logs; using namespace opentelemetry::sdk::common; +namespace nostd = opentelemetry::nostd; + +class MockLogRecordable final : public opentelemetry::sdk::logs::Recordable +{ +public: + void SetTimestamp(opentelemetry::common::SystemTimestamp) noexcept override {} + + void SetObservedTimestamp(opentelemetry::common::SystemTimestamp) noexcept override {} + + void SetSeverity(opentelemetry::logs::Severity) noexcept override {} + + nostd::string_view GetBody() const noexcept { return body_; } + + void SetBody(const opentelemetry::common::AttributeValue &message) noexcept override + { + if (nostd::holds_alternative(message)) + { + body_ = nostd::get(message); + } + else if (nostd::holds_alternative(message)) + { + body_ = static_cast(nostd::get(message)); + } + } + + void SetBody(const std::string &message) noexcept { body_ = message; } + + void SetTraceId(const opentelemetry::trace::TraceId &) noexcept override {} + + void SetSpanId(const opentelemetry::trace::SpanId &) noexcept override {} + + void SetTraceFlags(const opentelemetry::trace::TraceFlags &) noexcept override {} + + void SetAttribute(nostd::string_view, + const opentelemetry::common::AttributeValue &) noexcept override + {} + + void SetResource(const opentelemetry::sdk::resource::Resource &) noexcept override {} + + void SetInstrumentationScope( + const opentelemetry::sdk::instrumentationscope::InstrumentationScope &) noexcept override + {} + +private: + std::string body_; +}; + /** * A sample log exporter * for testing the batch log processor @@ -22,7 +70,7 @@ using namespace opentelemetry::sdk::common; class MockLogExporter final : public LogRecordExporter { public: - MockLogExporter(std::shared_ptr>> logs_received, + MockLogExporter(std::shared_ptr>> logs_received, std::shared_ptr> is_shutdown, std::shared_ptr> is_export_completed, const std::chrono::milliseconds export_delay = std::chrono::milliseconds(0)) @@ -32,26 +80,32 @@ class MockLogExporter final : public LogRecordExporter export_delay_(export_delay) {} - std::unique_ptr MakeRecordable() noexcept override + nostd::unique_ptr MakeRecordable() noexcept override { - return std::unique_ptr(new LogRecord()); + return nostd::unique_ptr(new MockLogRecordable()); } // Export method stores the logs received into a shared list of record names ExportResult Export( - const opentelemetry::nostd::span> &records) noexcept override + const opentelemetry::nostd::span> &records) noexcept override { *is_export_completed_ = false; // Meant exclusively to test scheduled_delay_millis for (auto &record : records) { - auto log = std::unique_ptr(static_cast(record.release())); + auto log = + nostd::unique_ptr(static_cast(record.release())); if (log != nullptr) { logs_received_->push_back(std::move(log)); } } + if (export_delay_ > std::chrono::milliseconds::zero()) + { + std::this_thread::sleep_for(export_delay_); + } + *is_export_completed_ = true; return ExportResult::kSuccess; } @@ -64,7 +118,7 @@ class MockLogExporter final : public LogRecordExporter } private: - std::shared_ptr>> logs_received_; + std::shared_ptr>> logs_received_; std::shared_ptr> is_shutdown_; std::shared_ptr> is_export_completed_; const std::chrono::milliseconds export_delay_; @@ -80,7 +134,7 @@ class BatchLogRecordProcessorTest : public testing::Test // ::testing::Test // returns a batch log processor that received a batch of log records, a shared pointer to a // is_shutdown flag, and the processor configuration options (default if unspecified) std::shared_ptr GetMockProcessor( - std::shared_ptr>> logs_received, + std::shared_ptr>> logs_received, std::shared_ptr> is_shutdown, std::shared_ptr> is_export_completed = std::shared_ptr>(new std::atomic(false)), @@ -90,7 +144,7 @@ class BatchLogRecordProcessorTest : public testing::Test // ::testing::Test const size_t max_export_batch_size = 512) { return std::shared_ptr(new BatchLogRecordProcessor( - std::unique_ptr( + nostd::unique_ptr( new MockLogExporter(logs_received, is_shutdown, is_export_completed, export_delay)), max_queue_size, scheduled_delay_millis, max_export_batch_size)); } @@ -99,8 +153,8 @@ class BatchLogRecordProcessorTest : public testing::Test // ::testing::Test TEST_F(BatchLogRecordProcessorTest, TestShutdown) { // initialize a batch log processor with the test exporter - std::shared_ptr>> logs_received( - new std::vector>); + std::shared_ptr>> logs_received( + new std::vector>); std::shared_ptr> is_shutdown(new std::atomic(false)); auto batch_processor = GetMockProcessor(logs_received, is_shutdown); @@ -111,7 +165,7 @@ TEST_F(BatchLogRecordProcessorTest, TestShutdown) for (int i = 0; i < num_logs; ++i) { auto log = batch_processor->MakeRecordable(); - log->SetBody("Log" + std::to_string(i)); + static_cast(log.get())->SetBody("Log" + std::to_string(i)); batch_processor->OnEmit(std::move(log)); } @@ -137,8 +191,8 @@ TEST_F(BatchLogRecordProcessorTest, TestShutdown) TEST_F(BatchLogRecordProcessorTest, TestForceFlush) { std::shared_ptr> is_shutdown(new std::atomic(false)); - std::shared_ptr>> logs_received( - new std::vector>); + std::shared_ptr>> logs_received( + new std::vector>); auto batch_processor = GetMockProcessor(logs_received, is_shutdown); const int num_logs = 2048; @@ -146,7 +200,7 @@ TEST_F(BatchLogRecordProcessorTest, TestForceFlush) for (int i = 0; i < num_logs; ++i) { auto log = batch_processor->MakeRecordable(); - log->SetBody("Log" + std::to_string(i)); + static_cast(log.get())->SetBody("Log" + std::to_string(i)); batch_processor->OnEmit(std::move(log)); } @@ -162,7 +216,7 @@ TEST_F(BatchLogRecordProcessorTest, TestForceFlush) for (int i = 0; i < num_logs; ++i) { auto log = batch_processor->MakeRecordable(); - log->SetBody("Log" + std::to_string(i)); + static_cast(log.get())->SetBody("Log" + std::to_string(i)); batch_processor->OnEmit(std::move(log)); } @@ -180,8 +234,8 @@ TEST_F(BatchLogRecordProcessorTest, TestManyLogsLoss) /* Test that when exporting more than max_queue_size logs, some are most likely lost*/ std::shared_ptr> is_shutdown(new std::atomic(false)); - std::shared_ptr>> logs_received( - new std::vector>); + std::shared_ptr>> logs_received( + new std::vector>); const int max_queue_size = 4096; @@ -191,7 +245,7 @@ TEST_F(BatchLogRecordProcessorTest, TestManyLogsLoss) for (int i = 0; i < max_queue_size; ++i) { auto log = batch_processor->MakeRecordable(); - log->SetBody("Log" + std::to_string(i)); + static_cast(log.get())->SetBody("Log" + std::to_string(i)); batch_processor->OnEmit(std::move(log)); } @@ -206,8 +260,8 @@ TEST_F(BatchLogRecordProcessorTest, TestManyLogsLossLess) /* Test that no logs are lost when sending max_queue_size logs */ std::shared_ptr> is_shutdown(new std::atomic(false)); - std::shared_ptr>> logs_received( - new std::vector>); + std::shared_ptr>> logs_received( + new std::vector>); auto batch_processor = GetMockProcessor(logs_received, is_shutdown); const int num_logs = 2048; @@ -215,7 +269,7 @@ TEST_F(BatchLogRecordProcessorTest, TestManyLogsLossLess) for (int i = 0; i < num_logs; ++i) { auto log = batch_processor->MakeRecordable(); - log->SetBody("Log" + std::to_string(i)); + static_cast(log.get())->SetBody("Log" + std::to_string(i)); batch_processor->OnEmit(std::move(log)); } @@ -235,8 +289,8 @@ TEST_F(BatchLogRecordProcessorTest, TestScheduledDelayMillis) std::shared_ptr> is_shutdown(new std::atomic(false)); std::shared_ptr> is_export_completed(new std::atomic(false)); - std::shared_ptr>> logs_received( - new std::vector>); + std::shared_ptr>> logs_received( + new std::vector>); const std::chrono::milliseconds export_delay(0); const std::chrono::milliseconds scheduled_delay_millis(2000); @@ -248,7 +302,7 @@ TEST_F(BatchLogRecordProcessorTest, TestScheduledDelayMillis) for (std::size_t i = 0; i < max_export_batch_size; ++i) { auto log = batch_processor->MakeRecordable(); - log->SetBody("Log" + std::to_string(i)); + static_cast(log.get())->SetBody("Log" + std::to_string(i)); batch_processor->OnEmit(std::move(log)); } // Sleep for scheduled_delay_millis milliseconds @@ -257,7 +311,7 @@ TEST_F(BatchLogRecordProcessorTest, TestScheduledDelayMillis) // small delay to give time to export, which is being performed // asynchronously by the worker thread (this thread will not // forcibly join() the main thread unless processor's shutdown() is called). - std::this_thread::sleep_for(std::chrono::milliseconds(50)); + std::this_thread::sleep_for(std::chrono::milliseconds(200)); // Logs should be exported by now EXPECT_TRUE(is_export_completed->load()); diff --git a/sdk/test/logs/log_record_test.cc b/sdk/test/logs/log_record_test.cc index 89b07473a3..3ad3acaca5 100644 --- a/sdk/test/logs/log_record_test.cc +++ b/sdk/test/logs/log_record_test.cc @@ -3,28 +3,27 @@ #ifdef ENABLE_LOGS_PREVIEW -# include "opentelemetry/sdk/logs/log_record.h" # include "opentelemetry/nostd/variant.h" +# include "opentelemetry/sdk/logs/read_write_log_record.h" # include "opentelemetry/trace/span_id.h" # include "opentelemetry/trace/trace_id.h" # include -using opentelemetry::sdk::logs::LogRecord; +using opentelemetry::sdk::logs::ReadWriteLogRecord; namespace trace_api = opentelemetry::trace; namespace logs_api = opentelemetry::logs; namespace nostd = opentelemetry::nostd; -// Test what a default LogRecord with no fields set holds -TEST(LogRecord, GetDefaultValues) +// Test what a default ReadWriteLogRecord with no fields set holds +TEST(ReadWriteLogRecord, GetDefaultValues) { trace_api::TraceId zero_trace_id; trace_api::SpanId zero_span_id; trace_api::TraceFlags zero_trace_flags; - LogRecord record; + ReadWriteLogRecord record; ASSERT_EQ(record.GetSeverity(), logs_api::Severity::kInvalid); - ASSERT_EQ(record.GetBody(), ""); ASSERT_NE(record.GetResource().GetAttributes().size(), 0); ASSERT_EQ(record.GetAttributes().size(), 0); ASSERT_EQ(record.GetTraceId(), zero_trace_id); @@ -33,16 +32,16 @@ TEST(LogRecord, GetDefaultValues) ASSERT_EQ(record.GetTimestamp().time_since_epoch(), std::chrono::nanoseconds(0)); } -// Test LogRecord fields are properly set and get -TEST(LogRecord, SetAndGet) +// Test ReadWriteLogRecord fields are properly set and get +TEST(ReadWriteLogRecord, SetAndGet) { trace_api::TraceId trace_id; trace_api::SpanId span_id; trace_api::TraceFlags trace_flags; opentelemetry::common::SystemTimestamp now(std::chrono::system_clock::now()); - // Set all fields of the LogRecord - LogRecord record; + // Set most fields of the ReadWriteLogRecord + ReadWriteLogRecord record; auto resource = opentelemetry::sdk::resource::Resource::Create({{"res1", true}}); record.SetSeverity(logs_api::Severity::kInvalid); record.SetBody("Message"); @@ -55,7 +54,14 @@ TEST(LogRecord, SetAndGet) // Test that all fields match what was set ASSERT_EQ(record.GetSeverity(), logs_api::Severity::kInvalid); - ASSERT_EQ(record.GetBody(), "Message"); + if (nostd::holds_alternative(record.GetBody())) + { + ASSERT_EQ(std::string(nostd::get(record.GetBody())), "Message"); + } + else if (nostd::holds_alternative(record.GetBody())) + { + ASSERT_TRUE(nostd::get(record.GetBody()) == "Message"); + } ASSERT_TRUE(nostd::get(record.GetResource().GetAttributes().at("res1"))); ASSERT_EQ(nostd::get(record.GetAttributes().at("attr1")), 314159); ASSERT_EQ(record.GetTraceId(), trace_id); diff --git a/sdk/test/logs/logger_provider_sdk_test.cc b/sdk/test/logs/logger_provider_sdk_test.cc index 4f517f8fd4..04d7108194 100644 --- a/sdk/test/logs/logger_provider_sdk_test.cc +++ b/sdk/test/logs/logger_provider_sdk_test.cc @@ -7,9 +7,9 @@ # include "opentelemetry/logs/provider.h" # include "opentelemetry/nostd/shared_ptr.h" # include "opentelemetry/nostd/string_view.h" -# include "opentelemetry/sdk/logs/log_record.h" # include "opentelemetry/sdk/logs/logger.h" # include "opentelemetry/sdk/logs/logger_provider.h" +# include "opentelemetry/sdk/logs/recordable.h" # include "opentelemetry/sdk/logs/simple_log_record_processor.h" # include @@ -79,14 +79,42 @@ TEST(LoggerProviderSDK, LoggerProviderLoggerArguments) ASSERT_EQ(sdk_logger2->GetInstrumentationScope(), sdk_logger1->GetInstrumentationScope()); } +class DummyLogRecordable final : public opentelemetry::sdk::logs::Recordable +{ +public: + void SetTimestamp(opentelemetry::common::SystemTimestamp) noexcept override {} + + void SetObservedTimestamp(opentelemetry::common::SystemTimestamp) noexcept override {} + + void SetSeverity(opentelemetry::logs::Severity) noexcept override {} + + void SetBody(const opentelemetry::common::AttributeValue &) noexcept override {} + + void SetTraceId(const opentelemetry::trace::TraceId &) noexcept override {} + + void SetSpanId(const opentelemetry::trace::SpanId &) noexcept override {} + + void SetTraceFlags(const opentelemetry::trace::TraceFlags &) noexcept override {} + + void SetAttribute(nostd::string_view, + const opentelemetry::common::AttributeValue &) noexcept override + {} + + void SetResource(const opentelemetry::sdk::resource::Resource &) noexcept override {} + + void SetInstrumentationScope( + const opentelemetry::sdk::instrumentationscope::InstrumentationScope &) noexcept override + {} +}; + class DummyProcessor : public LogRecordProcessor { - std::unique_ptr MakeRecordable() noexcept override + nostd::unique_ptr MakeRecordable() noexcept override { - return std::unique_ptr(new LogRecord); + return nostd::unique_ptr(new DummyLogRecordable()); } - void OnEmit(std::unique_ptr && /* record */) noexcept override {} + void OnEmit(nostd::unique_ptr && /* record */) noexcept override {} bool ForceFlush(std::chrono::microseconds /* timeout */) noexcept override { return true; } bool Shutdown(std::chrono::microseconds /* timeout */) noexcept override { return true; } }; @@ -101,9 +129,9 @@ TEST(LoggerProviderSDK, GetResource) TEST(LoggerProviderSDK, Shutdown) { - std::unique_ptr processor(new SimpleLogRecordProcessor(nullptr)); + nostd::unique_ptr processor(new SimpleLogRecordProcessor(nullptr)); SimpleLogRecordProcessor *processor_ptr = processor.get(); - std::vector> processors; + std::vector> processors; processors.push_back(std::move(processor)); LoggerProvider lp(std::make_shared(std::move(processors))); @@ -117,8 +145,8 @@ TEST(LoggerProviderSDK, Shutdown) TEST(LoggerProviderSDK, ForceFlush) { - std::unique_ptr processor(new SimpleLogRecordProcessor(nullptr)); - std::vector> processors; + nostd::unique_ptr processor(new SimpleLogRecordProcessor(nullptr)); + std::vector> processors; processors.push_back(std::move(processor)); LoggerProvider lp(std::make_shared(std::move(processors))); diff --git a/sdk/test/logs/logger_sdk_test.cc b/sdk/test/logs/logger_sdk_test.cc index 478d874b8e..ede74cd8ef 100644 --- a/sdk/test/logs/logger_sdk_test.cc +++ b/sdk/test/logs/logger_sdk_test.cc @@ -3,13 +3,18 @@ #ifdef ENABLE_LOGS_PREVIEW -# include "opentelemetry/sdk/logs/log_record.h" +# include + +# include "opentelemetry/nostd/string_view.h" +# include "opentelemetry/nostd/variant.h" # include "opentelemetry/sdk/logs/logger.h" +# include "opentelemetry/sdk/logs/recordable.h" # include using namespace opentelemetry::sdk::logs; namespace logs_api = opentelemetry::logs; +namespace nostd = opentelemetry::nostd; TEST(LoggerSDK, LogToNullProcessor) { @@ -29,34 +34,87 @@ TEST(LoggerSDK, LogToNullProcessor) logger->Debug("Test log"); } +class MockLogRecordable final : public opentelemetry::sdk::logs::Recordable +{ +public: + void SetTimestamp(opentelemetry::common::SystemTimestamp) noexcept override {} + + void SetObservedTimestamp(opentelemetry::common::SystemTimestamp) noexcept override {} + + opentelemetry::logs::Severity GetSeverity() const noexcept { return severity_; } + + void SetSeverity(opentelemetry::logs::Severity severity) noexcept override + { + severity_ = severity; + } + + nostd::string_view GetBody() const noexcept { return body_; } + + void SetBody(const opentelemetry::common::AttributeValue &message) noexcept override + { + if (nostd::holds_alternative(message)) + { + body_ = nostd::get(message); + } + else if (nostd::holds_alternative(message)) + { + body_ = static_cast(nostd::get(message)); + } + } + + void SetBody(const std::string &message) noexcept { body_ = message; } + + void SetTraceId(const opentelemetry::trace::TraceId &) noexcept override {} + + void SetSpanId(const opentelemetry::trace::SpanId &) noexcept override {} + + void SetTraceFlags(const opentelemetry::trace::TraceFlags &) noexcept override {} + + void SetAttribute(nostd::string_view, + const opentelemetry::common::AttributeValue &) noexcept override + {} + + void SetResource(const opentelemetry::sdk::resource::Resource &) noexcept override {} + + void SetInstrumentationScope( + const opentelemetry::sdk::instrumentationscope::InstrumentationScope &) noexcept override + {} + +private: + opentelemetry::logs::Severity severity_ = opentelemetry::logs::Severity::kInvalid; + std::string body_; +}; + class MockProcessor final : public LogRecordProcessor { private: - std::shared_ptr record_received_; + std::shared_ptr record_received_; public: // A processor used for testing that keeps a track of the recordable it received - explicit MockProcessor(std::shared_ptr record_received) noexcept + explicit MockProcessor(std::shared_ptr record_received) noexcept : record_received_(record_received) {} - std::unique_ptr MakeRecordable() noexcept override + nostd::unique_ptr MakeRecordable() noexcept override { - return std::unique_ptr(new LogRecord); + return nostd::unique_ptr(new MockLogRecordable()); } // OnEmit stores the record it receives into the shared_ptr recordable passed into its // constructor - void OnEmit(std::unique_ptr &&record) noexcept override + void OnEmit(nostd::unique_ptr &&record) noexcept override { - // Cast the recordable received into a concrete LogRecord type - auto copy = std::shared_ptr(static_cast(record.release())); + // Cast the recordable received into a concrete MockLogRecordable type + auto copy = + std::shared_ptr(static_cast(record.release())); // Copy over the received log record's severity, name, and body fields over to the recordable // passed in the constructor record_received_->SetSeverity(copy->GetSeverity()); record_received_->SetBody(copy->GetBody()); } + bool ForceFlush(std::chrono::microseconds /* timeout */) noexcept override { return true; } bool Shutdown(std::chrono::microseconds /* timeout */) noexcept override { return true; } }; @@ -80,8 +138,9 @@ TEST(LoggerSDK, LogToAProcessor) ASSERT_EQ(sdk_logger->GetInstrumentationScope().GetVersion(), ""); ASSERT_EQ(sdk_logger->GetInstrumentationScope().GetSchemaURL(), schema_url); // Set a processor for the LoggerProvider - auto shared_recordable = std::shared_ptr(new LogRecord()); - lp->AddProcessor(std::unique_ptr(new MockProcessor(shared_recordable))); + auto shared_recordable = std::shared_ptr(new MockLogRecordable()); + lp->AddProcessor(nostd::unique_ptr( + new MockProcessor(shared_recordable))); // Check that the recordable created by the Log() statement is set properly logger->Log(logs_api::Severity::kWarn, "Log Message"); diff --git a/sdk/test/logs/simple_log_record_processor_test.cc b/sdk/test/logs/simple_log_record_processor_test.cc index 47b16eab7e..0e6c276aa4 100644 --- a/sdk/test/logs/simple_log_record_processor_test.cc +++ b/sdk/test/logs/simple_log_record_processor_test.cc @@ -6,7 +6,8 @@ # include "opentelemetry/sdk/logs/simple_log_record_processor.h" # include "opentelemetry/nostd/span.h" # include "opentelemetry/sdk/logs/exporter.h" -# include "opentelemetry/sdk/logs/log_record.h" +# include "opentelemetry/sdk/logs/readable_log_record.h" +# include "opentelemetry/sdk/logs/recordable.h" # include @@ -17,6 +18,51 @@ using namespace opentelemetry::sdk::logs; using namespace opentelemetry::sdk::common; namespace nostd = opentelemetry::nostd; +class TestLogRecordable final : public opentelemetry::sdk::logs::Recordable +{ +public: + void SetTimestamp(opentelemetry::common::SystemTimestamp) noexcept override {} + + void SetObservedTimestamp(opentelemetry::common::SystemTimestamp) noexcept override {} + + void SetSeverity(opentelemetry::logs::Severity) noexcept override {} + + nostd::string_view GetBody() const noexcept { return body_; } + + void SetBody(const opentelemetry::common::AttributeValue &message) noexcept override + { + if (nostd::holds_alternative(message)) + { + body_ = nostd::get(message); + } + else if (nostd::holds_alternative(message)) + { + body_ = static_cast(nostd::get(message)); + } + } + + void SetBody(const char *message) noexcept { body_ = message; } + + void SetTraceId(const opentelemetry::trace::TraceId &) noexcept override {} + + void SetSpanId(const opentelemetry::trace::SpanId &) noexcept override {} + + void SetTraceFlags(const opentelemetry::trace::TraceFlags &) noexcept override {} + + void SetAttribute(nostd::string_view, + const opentelemetry::common::AttributeValue &) noexcept override + {} + + void SetResource(const opentelemetry::sdk::resource::Resource &) noexcept override {} + + void SetInstrumentationScope( + const opentelemetry::sdk::instrumentationscope::InstrumentationScope &) noexcept override + {} + +private: + std::string body_; +}; + /* * A test exporter that can return a vector of all the records it has received, * and keep track of the number of times its Shutdown() function was called. @@ -25,25 +71,26 @@ class TestExporter final : public LogRecordExporter { public: TestExporter(int *shutdown_counter, - std::shared_ptr>> logs_received, + std::shared_ptr>> logs_received, size_t *batch_size_received) : shutdown_counter_(shutdown_counter), logs_received_(logs_received), batch_size_received(batch_size_received) {} - std::unique_ptr MakeRecordable() noexcept override + nostd::unique_ptr MakeRecordable() noexcept override { - return std::unique_ptr(new LogRecord()); + return nostd::unique_ptr(new TestLogRecordable()); } // Stores the names of the log records this exporter receives to an internal list - ExportResult Export(const nostd::span> &records) noexcept override + ExportResult Export(const nostd::span> &records) noexcept override { *batch_size_received = records.size(); for (auto &record : records) { - auto log_record = std::unique_ptr(static_cast(record.release())); + auto log_record = + nostd::unique_ptr(static_cast(record.release())); if (log_record != nullptr) { @@ -62,7 +109,7 @@ class TestExporter final : public LogRecordExporter private: int *shutdown_counter_; - std::shared_ptr>> logs_received_; + std::shared_ptr>> logs_received_; size_t *batch_size_received; }; @@ -71,11 +118,11 @@ class TestExporter final : public LogRecordExporter TEST(SimpleLogRecordProcessorTest, SendReceivedLogsToExporter) { // Create a simple processor with a TestExporter attached - std::shared_ptr>> logs_received( - new std::vector>); + std::shared_ptr>> logs_received( + new std::vector>); size_t batch_size_received = 0; - std::unique_ptr exporter( + nostd::unique_ptr exporter( new TestExporter(nullptr, logs_received, &batch_size_received)); SimpleLogRecordProcessor processor(std::move(exporter)); @@ -85,7 +132,7 @@ TEST(SimpleLogRecordProcessorTest, SendReceivedLogsToExporter) for (int i = 0; i < num_logs; i++) { auto recordable = processor.MakeRecordable(); - recordable->SetBody("Log Body"); + static_cast(recordable.get())->SetBody("Log Body"); processor.OnEmit(std::move(recordable)); // Verify that the batch of 1 log record sent by processor matches what exporter received @@ -106,7 +153,7 @@ TEST(SimpleLogRecordProcessorTest, ShutdownCalledOnce) // Create a TestExporter int num_shutdowns = 0; - std::unique_ptr exporter(new TestExporter(&num_shutdowns, nullptr, nullptr)); + nostd::unique_ptr exporter(new TestExporter(&num_shutdowns, nullptr, nullptr)); // Create a processor with the previous test exporter SimpleLogRecordProcessor processor(std::move(exporter)); @@ -127,13 +174,13 @@ class FailShutDownExporter final : public LogRecordExporter public: FailShutDownExporter() {} - std::unique_ptr MakeRecordable() noexcept override + nostd::unique_ptr MakeRecordable() noexcept override { - return std::unique_ptr(new LogRecord()); + return nostd::unique_ptr(new TestLogRecordable()); } ExportResult Export( - const nostd::span> & /* records */) noexcept override + const nostd::span> & /* records */) noexcept override { return ExportResult::kSuccess; } @@ -144,7 +191,7 @@ class FailShutDownExporter final : public LogRecordExporter // Tests for when when processor should fail to shutdown TEST(SimpleLogRecordProcessorTest, ShutDownFail) { - std::unique_ptr exporter(new FailShutDownExporter()); + nostd::unique_ptr exporter(new FailShutDownExporter()); SimpleLogRecordProcessor processor(std::move(exporter)); // Expect failure result when exporter fails to shutdown