Skip to content

Commit

Permalink
Logger: Propagating resources through LoggerProvider (#1154)
Browse files Browse the repository at this point in the history
  • Loading branch information
owent authored Jan 12, 2022
1 parent f20f72f commit d206b50
Show file tree
Hide file tree
Showing 40 changed files with 972 additions and 237 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ Increment the:
## [Unreleased]

* [SDK] Add LogLevel to internal_log ([#1147](https://github.com/open-telemetry/opentelemetry-cpp/pull/1147))
* [API/SDK] Logger: Propagating resources through LoggerProvider ([#1154](https://github.com/open-telemetry/opentelemetry-cpp/pull/1154))

## [1.1.1] 2021-12-
## [1.1.1] 2021-12-20

* [SDK] Rename OTEL_CPP_GET_ATTR macro, and define it using fully qualified attr function ([#1140](https://github.com/open-telemetry/opentelemetry-cpp/pull/1140))
* [SDK] Default resource attributes and attributes in OTEL_RESOURCE_ATTRIBUTES are missing when using Otlp*LogExporter ([#1082](https://github.com/open-telemetry/opentelemetry-cpp/pull/1082))
Expand Down
26 changes: 26 additions & 0 deletions api/include/opentelemetry/common/macros.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include <cstdint>

#include "opentelemetry/version.h"

/// \brief Declare variable as maybe unused
/// usage:
/// OPENTELEMETRY_MAYBE_UNUSED int a;
/// class OPENTELEMETRY_MAYBE_UNUSED a;
/// OPENTELEMETRY_MAYBE_UNUSED int a();
///
#if defined(__cplusplus) && __cplusplus >= 201703L
# define OPENTELEMETRY_MAYBE_UNUSED [[maybe_unused]]
#elif defined(__clang__)
# define OPENTELEMETRY_MAYBE_UNUSED __attribute__((unused))
#elif defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
# define OPENTELEMETRY_MAYBE_UNUSED __attribute__((unused))
#elif (defined(_MSC_VER) && _MSC_VER >= 1910) && (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)
# define OPENTELEMETRY_MAYBE_UNUSED [[maybe_unused]]
#else
# define OPENTELEMETRY_MAYBE_UNUSED
#endif
40 changes: 11 additions & 29 deletions api/include/opentelemetry/logs/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ class Logger
* @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 resource the resources, stored as a 2D list of key/value pairs, that are associated
* 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.
Expand All @@ -57,13 +55,10 @@ class Logger
/**
* The base Log(...) method that all other Log(...) overloaded methods will eventually call,
* in order to create a log record.
*
* Note this takes in a KeyValueIterable for the resource and attributes fields.
*/
virtual void Log(Severity severity,
nostd::string_view name,
nostd::string_view body,
const common::KeyValueIterable &resource,
const common::KeyValueIterable &attributes,
trace::TraceId trace_id,
trace::SpanId span_id,
Expand All @@ -74,40 +69,32 @@ class Logger
/**
* The secondary base Log(...) method that all other Log(...) overloaded methods except the one
* above will eventually call, in order to create a log record.
*
* Note this takes in template types for the resource and attributes fields.
*/
template <class T,
class U,
nostd::enable_if_t<common::detail::is_key_value_iterable<T>::value> * = nullptr,
nostd::enable_if_t<common::detail::is_key_value_iterable<U>::value> * = nullptr>
nostd::enable_if_t<common::detail::is_key_value_iterable<T>::value> * = nullptr>
void Log(Severity severity,
nostd::string_view name,
nostd::string_view body,
const T &resource,
const U &attributes,
const T &attributes,
trace::TraceId trace_id,
trace::SpanId span_id,
trace::TraceFlags trace_flags,
common::SystemTimestamp timestamp) noexcept
{
Log(severity, name, body, common::KeyValueIterableView<T>(resource),
common::KeyValueIterableView<U>(attributes), trace_id, span_id, trace_flags, timestamp);
Log(severity, name, body, common::KeyValueIterableView<T>(attributes), trace_id, span_id,
trace_flags, timestamp);
}

void Log(Severity severity,
nostd::string_view name,
nostd::string_view body,
std::initializer_list<std::pair<nostd::string_view, common::AttributeValue>> resource,
std::initializer_list<std::pair<nostd::string_view, common::AttributeValue>> attributes,
trace::TraceId trace_id,
trace::SpanId span_id,
trace::TraceFlags trace_flags,
common::SystemTimestamp timestamp) noexcept
{
return this->Log(severity, name, body,
nostd::span<const std::pair<nostd::string_view, common::AttributeValue>>{
resource.begin(), resource.end()},
nostd::span<const std::pair<nostd::string_view, common::AttributeValue>>{
attributes.begin(), attributes.end()},
trace_id, span_id, trace_flags, timestamp);
Expand All @@ -122,7 +109,7 @@ class Logger
*/
void Log(Severity severity, nostd::string_view message) noexcept
{
this->Log(severity, "", message, {}, {}, {}, {}, {}, std::chrono::system_clock::now());
this->Log(severity, "", message, {}, {}, {}, {}, std::chrono::system_clock::now());
}

/**
Expand All @@ -133,7 +120,7 @@ class Logger
*/
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, name, message, {}, {}, {}, {}, std::chrono::system_clock::now());
}

/**
Expand All @@ -145,8 +132,7 @@ class Logger
nostd::enable_if_t<common::detail::is_key_value_iterable<T>::value> * = nullptr>
void Log(Severity severity, const T &attributes) noexcept
{
this->Log(severity, "", "", std::map<std::string, std::string>{}, attributes, {}, {}, {},
std::chrono::system_clock::now());
this->Log(severity, "", "", attributes, {}, {}, {}, std::chrono::system_clock::now());
}

/**
Expand All @@ -159,8 +145,7 @@ class Logger
nostd::enable_if_t<common::detail::is_key_value_iterable<T>::value> * = nullptr>
void Log(Severity severity, nostd::string_view name, const T &attributes) noexcept
{
this->Log(severity, name, "", std::map<std::string, std::string>{}, attributes, {}, {}, {},
std::chrono::system_clock::now());
this->Log(severity, name, "", attributes, {}, {}, {}, std::chrono::system_clock::now());
}

/**
Expand All @@ -172,7 +157,7 @@ class Logger
std::initializer_list<std::pair<nostd::string_view, common::AttributeValue>>
attributes) noexcept
{
this->Log(severity, "", "", {}, attributes, {}, {}, {}, std::chrono::system_clock::now());
this->Log(severity, "", "", attributes, {}, {}, {}, std::chrono::system_clock::now());
}

/**
Expand All @@ -186,7 +171,7 @@ class Logger
std::initializer_list<std::pair<nostd::string_view, common::AttributeValue>>
attributes) noexcept
{
this->Log(severity, name, "", {}, attributes, {}, {}, {}, std::chrono::system_clock::now());
this->Log(severity, name, "", attributes, {}, {}, {}, std::chrono::system_clock::now());
}

/**
Expand All @@ -200,10 +185,7 @@ class Logger
nostd::string_view name,
common::KeyValueIterable &attributes) noexcept
{
this->Log(severity, name, {},
common::KeyValueIterableView<
std::initializer_list<std::pair<nostd::string_view, common::AttributeValue>>>({}),
attributes, {}, {}, {}, std::chrono::system_clock::now());
this->Log(severity, name, {}, attributes, {}, {}, {}, std::chrono::system_clock::now());
}

/** Trace severity overloads **/
Expand Down
1 change: 0 additions & 1 deletion api/include/opentelemetry/logs/noop.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ class NoopLogger final : public Logger
void Log(Severity severity,
nostd::string_view name,
nostd::string_view body,
const common::KeyValueIterable &resource,
const common::KeyValueIterable &attributes,
trace::TraceId trace_id,
trace::SpanId span_id,
Expand Down
1 change: 0 additions & 1 deletion api/test/logs/logger_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ class TestLogger : public Logger
void Log(Severity severity,
string_view name,
string_view body,
const common::KeyValueIterable &resource,
const common::KeyValueIterable &attributes,
trace::TraceId trace_id,
trace::SpanId span_id,
Expand Down
6 changes: 3 additions & 3 deletions bazel/repository.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ def opentelemetry_cpp_deps():
maybe(
http_archive,
name = "com_github_google_benchmark",
sha256 = "dccbdab796baa1043f04982147e67bb6e118fe610da2c65f88912d73987e700c",
strip_prefix = "benchmark-1.5.2",
sha256 = "1f71c72ce08d2c1310011ea6436b31e39ccab8c2db94186d26657d41747c85d6",
strip_prefix = "benchmark-1.6.0",
urls = [
"https://github.com/google/benchmark/archive/v1.5.2.tar.gz",
"https://github.com/google/benchmark/archive/v1.6.0.tar.gz",
],
)

Expand Down
2 changes: 1 addition & 1 deletion examples/common/logs_foo_library/foo_library.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void foo_library()
auto scoped_span = trace::Scope(get_tracer()->StartSpan("foo_library"));
auto ctx = span->GetContext();
auto logger = get_logger();
logger->Log(opentelemetry::logs::Severity::kDebug, "name", "body", {}, {}, ctx.trace_id(),
logger->Log(opentelemetry::logs::Severity::kDebug, "name", "body", {}, ctx.trace_id(),
ctx.span_id(), ctx.trace_flags(), opentelemetry::common::SystemTimestamp());
}
#endif
9 changes: 4 additions & 5 deletions examples/otlp/grpc_log_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,10 @@ void InitTracer()
void InitLogger()
{
// Create OTLP exporter instance
auto exporter = std::unique_ptr<logs_sdk::LogExporter>(new otlp::OtlpGrpcLogExporter(opts));
auto processor = std::shared_ptr<logs_sdk::LogProcessor>(
new logs_sdk::SimpleLogProcessor(std::move(exporter)));
auto sdkProvider = std::shared_ptr<logs_sdk::LoggerProvider>(new logs_sdk::LoggerProvider());
sdkProvider->SetProcessor(processor);
auto exporter = std::unique_ptr<logs_sdk::LogExporter>(new otlp::OtlpGrpcLogExporter(opts));
auto sdkProvider = std::shared_ptr<logs_sdk::LoggerProvider>(
new logs_sdk::LoggerProvider(std::unique_ptr<logs_sdk::LogProcessor>(
new logs_sdk::SimpleLogProcessor(std::move(exporter)))));
auto apiProvider = nostd::shared_ptr<logs::LoggerProvider>(sdkProvider);
auto provider = nostd::shared_ptr<logs::LoggerProvider>(apiProvider);
opentelemetry::logs::Provider::SetLoggerProvider(provider);
Expand Down
7 changes: 3 additions & 4 deletions examples/otlp/http_log_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,9 @@ void InitLogger()
// Create OTLP exporter instance
auto exporter =
std::unique_ptr<logs_sdk::LogExporter>(new otlp::OtlpHttpLogExporter(logger_opts));
auto processor = std::shared_ptr<logs_sdk::LogProcessor>(
new logs_sdk::SimpleLogProcessor(std::move(exporter)));
auto sdkProvider = std::shared_ptr<logs_sdk::LoggerProvider>(new logs_sdk::LoggerProvider());
sdkProvider->SetProcessor(processor);
auto sdkProvider = std::shared_ptr<logs_sdk::LoggerProvider>(
new logs_sdk::LoggerProvider(std::unique_ptr<logs_sdk::LogProcessor>(
new logs_sdk::SimpleLogProcessor(std::move(exporter)))));
auto apiProvider = nostd::shared_ptr<logs::LoggerProvider>(sdkProvider);
auto provider = nostd::shared_ptr<logs::LoggerProvider>(apiProvider);
opentelemetry::logs::Provider::SetLoggerProvider(provider);
Expand Down
5 changes: 3 additions & 2 deletions exporters/elasticsearch/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ target_include_directories(
PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>"
"$<INSTALL_INTERFACE:include>")

target_link_libraries(opentelemetry_exporter_elasticsearch_logs
PUBLIC opentelemetry_trace http_client_curl)
target_link_libraries(
opentelemetry_exporter_elasticsearch_logs
PUBLIC opentelemetry_trace opentelemetry_logs http_client_curl)

install(
TARGETS opentelemetry_exporter_elasticsearch_logs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
#ifdef ENABLE_LOGS_PREVIEW

# include <map>
# include <sstream>
# include <type_traits>
# include <unordered_map>

# include "nlohmann/json.hpp"
# include "opentelemetry/sdk/common/attribute_utils.h"
# include "opentelemetry/sdk/logs/recordable.h"
Expand Down Expand Up @@ -67,6 +70,39 @@ class ElasticSearchRecordable final : public sdk::logs::Recordable
}
}

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<bool>(value) ? true : false;
return;
case common::kTypeInt:
json_[name][key.data()] = opentelemetry::nostd::get<int>(value);
return;
case common::kTypeInt64:
json_[name][key.data()] = opentelemetry::nostd::get<int64_t>(value);
return;
case common::kTypeUInt:
json_[name][key.data()] = opentelemetry::nostd::get<unsigned int>(value);
return;
case common::kTypeUInt64:
json_[name][key.data()] = opentelemetry::nostd::get<uint64_t>(value);
return;
case common::kTypeDouble:
json_[name][key.data()] = opentelemetry::nostd::get<double>(value);
return;
case common::kTypeString:
json_[name][key.data()] = opentelemetry::nostd::get<std::string>(value).data();
return;
default:
return;
}
}

public:
/**
* Set the severity for this log.
Expand All @@ -75,7 +111,18 @@ class ElasticSearchRecordable final : public sdk::logs::Recordable
void SetSeverity(opentelemetry::logs::Severity severity) noexcept override
{
// Convert the severity enum to a string
json_["severity"] = opentelemetry::logs::SeverityNumToText[static_cast<int>(severity)];
int severity_index = static_cast<int>(severity);
if (severity_index < 0 ||
severity_index >= std::extent<decltype(opentelemetry::logs::SeverityNumToText)>::value)
{
std::stringstream sout;
sout << "Invalid severity(" << severity_index << ")";
json_["severity"] = sout.str();
}
else
{
json_["severity"] = opentelemetry::logs::SeverityNumToText[severity_index];
}
}

/**
Expand All @@ -91,14 +138,15 @@ class ElasticSearchRecordable final : public sdk::logs::Recordable
void SetBody(nostd::string_view message) noexcept override { json_["body"] = message.data(); }

/**
* Set a resource for this log.
* @param name the name of the resource
* @param value the resource value
* Set Resource of this log
* @param Resource the resource to set
*/
void SetResource(nostd::string_view key,
const opentelemetry::common::AttributeValue &value) noexcept override
void SetResource(const opentelemetry::sdk::resource::Resource &resource) noexcept override
{
WriteKeyValue(key, value, "resource");
for (auto &kv : resource.GetAttributes())
{
WriteKeyValue(kv.first, kv.second, "resource");
}
}

/**
Expand Down
5 changes: 3 additions & 2 deletions exporters/elasticsearch/test/es_log_exporter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ TEST(ElasticsearchLogsExporterTests, RecordableCreation)
// Attributes and resource support different types
record->SetAttribute("key0", false);
record->SetAttribute("key1", "1");
record->SetResource("key2", 2);
record->SetResource("key3", 3.142);

auto resource = opentelemetry::sdk::resource::Resource::Create({{"key2", 2}, {"key3", 3142}});
record->SetResource(resource);

exporter->Export(nostd::span<std::unique_ptr<sdklogs::Recordable>>(&record, 1));
}
Expand Down
Loading

0 comments on commit d206b50

Please sign in to comment.