Skip to content

Commit

Permalink
Update logs sdk and api to follow specification (#1884)
Browse files Browse the repository at this point in the history
  • Loading branch information
owent authored Jan 28, 2023
1 parent 7e7184d commit 65ebdb1
Show file tree
Hide file tree
Showing 32 changed files with 1,346 additions and 767 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Increment the:

## [Unreleased]

* [LOGS] Add `include_trace_context` and `EventLogger` [#1884](https://github.com/open-telemetry/opentelemetry-cpp/pull/1884)
* [METRICS] Change BatchLogRecordProcessorFactory::Create to static method
* [BUILD] Fix OTELCPP_MAINTAINER_MODE [#1844](https://github.com/open-telemetry/opentelemetry-cpp/pull/1844)
* [BUILD] Fix compatibility when using clang and libc++, upgrade GTest and
Expand Down
27 changes: 27 additions & 0 deletions api/include/opentelemetry/common/key_value_iterable.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,32 @@ class KeyValueIterable
*/
virtual size_t size() const noexcept = 0;
};

/**
* Supports internal iteration over a collection of key-value pairs.
*/
class NoopKeyValueIterable : public KeyValueIterable
{
public:
~NoopKeyValueIterable() override = default;

/**
* Iterate over key-value pairs
* @param callback a callback to invoke for each key-value. If the callback returns false,
* the iteration is aborted.
* @return true if every key-value pair was iterated over
*/
bool ForEachKeyValue(
nostd::function_ref<bool(nostd::string_view, common::AttributeValue)>) const noexcept override
{
return true;
}

/**
* @return the number of key-value pairs
*/
size_t size() const noexcept override { return 0; }
};

} // namespace common
OPENTELEMETRY_END_NAMESPACE
94 changes: 94 additions & 0 deletions api/include/opentelemetry/logs/event_logger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once
#ifdef ENABLE_LOGS_PREVIEW

# include <chrono>
# include <map>
# include <vector>

# include "opentelemetry/common/macros.h"
# include "opentelemetry/logs/log_record.h"
# include "opentelemetry/logs/logger.h"
# include "opentelemetry/logs/logger_type_traits.h"
# include "opentelemetry/logs/severity.h"
# include "opentelemetry/nostd/shared_ptr.h"
# include "opentelemetry/nostd/string_view.h"
# include "opentelemetry/nostd/unique_ptr.h"
# include "opentelemetry/version.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace logs
{
/**
* Handles event log record creation.
**/
class EventLogger
{
public:
virtual ~EventLogger() = default;

/* Returns the name of the logger */
virtual const nostd::string_view GetName() noexcept = 0;

/* Returns the delegate logger of this event logger */
virtual nostd::shared_ptr<Logger> GetDelegateLogger() noexcept = 0;

/**
* Emit a event Log Record object
*
* @param event_name Event name
* @param log_record Log record
*/
virtual void EmitEvent(nostd::string_view event_name,
nostd::unique_ptr<LogRecord> &&log_record) noexcept = 0;

/**
* Emit a event Log Record object with arguments
*
* @param event_name Event name
* @tparam args Arguments which can be used to set data of log record by type.
* Severity -> severity, severity_text
* string_view -> body
* AttributeValue -> body
* SpanContext -> span_id,tace_id and trace_flags
* SpanId -> span_id
* TraceId -> tace_id
* TraceFlags -> trace_flags
* SystemTimestamp -> timestamp
* system_clock::time_point -> timestamp
* KeyValueIterable -> attributes
* Key value iterable container -> attributes
* span<pair<string_view, AttributeValue>> -> attributes(return type of MakeAttributes)
*/
template <class... ArgumentType>
void EmitEvent(nostd::string_view event_name, ArgumentType &&... args)
{
nostd::shared_ptr<Logger> delegate_logger = GetDelegateLogger();
if (!delegate_logger)
{
return;
}
nostd::unique_ptr<LogRecord> log_record = delegate_logger->CreateLogRecord();
if (!log_record)
{
return;
}

IgnoreTraitResult(
detail::LogRecordSetterTrait<typename std::decay<ArgumentType>::type>::template Set(
log_record.get(), std::forward<ArgumentType>(args))...);

EmitEvent(event_name, std::move(log_record));
}

private:
template <class... ValueType>
void IgnoreTraitResult(ValueType &&...)
{}
};
} // namespace logs
OPENTELEMETRY_END_NAMESPACE

#endif
36 changes: 36 additions & 0 deletions api/include/opentelemetry/logs/event_logger_provider.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once
#ifdef ENABLE_LOGS_PREVIEW

# include "opentelemetry/common/key_value_iterable.h"
# include "opentelemetry/common/key_value_iterable_view.h"
# include "opentelemetry/logs/event_logger.h"
# include "opentelemetry/logs/logger.h"
# include "opentelemetry/nostd/shared_ptr.h"
# include "opentelemetry/nostd/string_view.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace logs
{
/**
* Creates new EventLogger instances.
*/
class EventLoggerProvider
{
public:
virtual ~EventLoggerProvider() = default;

/**
* Creates a named EventLogger instance.
*
*/

virtual nostd::shared_ptr<EventLogger> CreateEventLogger(
nostd::shared_ptr<Logger> delegate_logger,
nostd::string_view event_domain) noexcept = 0;
};
} // namespace logs
OPENTELEMETRY_END_NAMESPACE
#endif
Loading

0 comments on commit 65ebdb1

Please sign in to comment.