Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logger-Support-for-Instrumentation-library #1149

Merged
merged 15 commits into from
Jan 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]

* [API/SDK] Logger: Support for Instrumentation library ([#1128](https://github.com/open-telemetry/opentelemetry-cpp/pull/1128))
* [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))
* [API]: Allow to use external abseil for bazel targets ([#1172](https://github.com/open-telemetry/opentelemetry-cpp/pull/1172))
Expand Down
10 changes: 8 additions & 2 deletions api/include/opentelemetry/logs/logger_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,16 @@ class LoggerProvider
*/

virtual nostd::shared_ptr<Logger> GetLogger(nostd::string_view logger_name,
nostd::string_view options = "") = 0;
nostd::string_view options,
nostd::string_view library_name,
nostd::string_view library_version = "",
nostd::string_view schema_url = "") = 0;

virtual nostd::shared_ptr<Logger> GetLogger(nostd::string_view logger_name,
nostd::span<nostd::string_view> args) = 0;
nostd::span<nostd::string_view> args,
nostd::string_view library_name,
nostd::string_view library_version = "",
nostd::string_view schema_url = "") = 0;
};
} // namespace logs
OPENTELEMETRY_END_NAMESPACE
Expand Down
10 changes: 8 additions & 2 deletions api/include/opentelemetry/logs/noop.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,19 @@ class NoopLoggerProvider final : public opentelemetry::logs::LoggerProvider
{}

nostd::shared_ptr<Logger> GetLogger(nostd::string_view logger_name,
nostd::string_view options) override
nostd::string_view options,
nostd::string_view library_name,
nostd::string_view library_version = "",
nostd::string_view schema_url = "") override
{
return logger_;
}

nostd::shared_ptr<Logger> GetLogger(nostd::string_view logger_name,
nostd::span<nostd::string_view> args) override
nostd::span<nostd::string_view> args,
nostd::string_view library_name,
nostd::string_view library_version = "",
nostd::string_view schema_url = "") override
{
return logger_;
}
Expand Down
30 changes: 21 additions & 9 deletions api/test/logs/logger_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ namespace trace = opentelemetry::trace;
// Check that the default logger is a noop logger instance
TEST(Logger, GetLoggerDefault)
{
auto lp = Provider::GetLoggerProvider();
auto logger = lp->GetLogger("TestLogger");
auto lp = Provider::GetLoggerProvider();
const std::string schema_url{"https://opentelemetry.io/schemas/1.2.0"};
auto logger = lp->GetLogger("TestLogger", "", "opentelelemtry_library", "", schema_url);
auto name = logger->GetName();
EXPECT_NE(nullptr, logger);
EXPECT_EQ(name, "noop logger");
Expand All @@ -40,17 +41,19 @@ TEST(Logger, GetNoopLoggerNameWithArgs)
// GetLogger(name, list(args))
std::array<string_view, 1> sv{"string"};
span<string_view> args{sv};
lp->GetLogger("NoopLoggerWithArgs", args);
const std::string schema_url{"https://opentelemetry.io/schemas/1.2.0"};
lp->GetLogger("NoopLoggerWithArgs", args, "opentelelemtry_library", "", schema_url);

// GetLogger(name, string options)
lp->GetLogger("NoopLoggerWithOptions", "options");
lp->GetLogger("NoopLoggerWithOptions", "options", "opentelelemtry_library", "", schema_url);
}

// Test the Log() overloads
TEST(Logger, LogMethodOverloads)
{
auto lp = Provider::GetLoggerProvider();
auto logger = lp->GetLogger("TestLogger");
auto lp = Provider::GetLoggerProvider();
const std::string schema_url{"https://opentelemetry.io/schemas/1.2.0"};
auto logger = lp->GetLogger("TestLogger", "", "opentelelemtry_library", "", schema_url);

// Create a map to test the logs with
std::map<std::string, std::string> m = {{"key1", "value1"}};
Expand Down Expand Up @@ -91,12 +94,20 @@ class TestLogger : public Logger
// Define a basic LoggerProvider class that returns an instance of the logger class defined above
class TestProvider : public LoggerProvider
{
shared_ptr<Logger> GetLogger(string_view library_name, string_view options = "") override
nostd::shared_ptr<Logger> GetLogger(nostd::string_view logger_name,
nostd::string_view options,
nostd::string_view library_name,
nostd::string_view library_version = "",
nostd::string_view schema_url = "") override
{
return shared_ptr<Logger>(new TestLogger());
}

shared_ptr<Logger> GetLogger(string_view library_name, span<string_view> args) override
nostd::shared_ptr<Logger> GetLogger(nostd::string_view logger_name,
nostd::span<nostd::string_view> args,
nostd::string_view library_name,
nostd::string_view library_version = "",
nostd::string_view schema_url = "") override
{
return shared_ptr<Logger>(new TestLogger());
}
Expand All @@ -111,7 +122,8 @@ TEST(Logger, PushLoggerImplementation)
auto lp = Provider::GetLoggerProvider();

// Check that the implementation was pushed by calling TestLogger's GetName()
auto logger = lp->GetLogger("TestLogger");
nostd::string_view schema_url{"https://opentelemetry.io/schemas/1.2.0"};
auto logger = lp->GetLogger("TestLogger", "", "opentelelemtry_library", "", schema_url);
ASSERT_EQ("test logger", logger->GetName());
}
#endif
28 changes: 17 additions & 11 deletions api/test/logs/provider_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,29 @@

# include "opentelemetry/logs/provider.h"
# include "opentelemetry/nostd/shared_ptr.h"
# include "opentelemetry/nostd/span.h"
# include "opentelemetry/nostd/string_view.h"

using opentelemetry::logs::Logger;
using opentelemetry::logs::LoggerProvider;
using opentelemetry::logs::Provider;
using opentelemetry::nostd::shared_ptr;
using opentelemetry::nostd::span;
using opentelemetry::nostd::string_view;
namespace nostd = opentelemetry::nostd;

class TestProvider : public LoggerProvider
{
shared_ptr<Logger> GetLogger(string_view library_name, string_view options) override
nostd::shared_ptr<Logger> GetLogger(nostd::string_view logger_name,
nostd::string_view options,
nostd::string_view library_name,
nostd::string_view library_version = "",
nostd::string_view schema_url = "") override
{
return shared_ptr<Logger>(nullptr);
}

shared_ptr<Logger> GetLogger(string_view library_name, span<string_view> args) override
nostd::shared_ptr<Logger> GetLogger(nostd::string_view logger_name,
nostd::span<nostd::string_view> args,
nostd::string_view library_name,
nostd::string_view library_version = "",
nostd::string_view schema_url = "") override
{
return shared_ptr<Logger>(nullptr);
}
Expand Down Expand Up @@ -57,15 +62,16 @@ TEST(Provider, MultipleLoggerProviders)
TEST(Provider, GetLogger)
{
auto tf = shared_ptr<LoggerProvider>(new TestProvider());
// tests GetLogger(name, options)
auto logger = tf->GetLogger("logger1");
// tests GetLogger(name, version, schema)
const std::string schema_url{"https://opentelemetry.io/schemas/1.2.0"};
auto logger = tf->GetLogger("logger1", "", "opentelelemtry_library", "", schema_url);
EXPECT_EQ(nullptr, logger);

// tests GetLogger(name, arguments)

std::array<string_view, 1> sv{"string"};
span<string_view> args{sv};
auto logger2 = tf->GetLogger("logger2", args);
std::array<nostd::string_view, 1> sv{"string"};
nostd::span<nostd::string_view> args{sv};
auto logger2 = tf->GetLogger("logger2", args, "opentelelemtry_library", "", schema_url);
EXPECT_EQ(nullptr, logger2);
}
#endif
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 @@ -22,7 +22,7 @@ nostd::shared_ptr<trace::Tracer> get_tracer()
nostd::shared_ptr<logs::Logger> get_logger()
{
auto provider = logs::Provider::GetLoggerProvider();
return provider->GetLogger("foo_library_logger");
return provider->GetLogger("foo_library_logger", "", "foo_library");
}
} // namespace

Expand Down
24 changes: 23 additions & 1 deletion exporters/elasticsearch/include/opentelemetry/exporters/elasticsearch/es_log_recordable.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,29 @@ class ElasticSearchRecordable final : public sdk::logs::Recordable
/**
* Returns a JSON object contain the log information
*/
nlohmann::json GetJSON() noexcept { return json_; };
nlohmann::json GetJSON() noexcept { return json_; }

/**
* Set instrumentation_library for this log.
* @param instrumentation_library the instrumentation library to set
*/
void SetInstrumentationLibrary(
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
&instrumentation_library) noexcept
{
instrumentation_library_ = &instrumentation_library;
}

/** Returns the associated instruementation library */
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary &
GetInstrumentationLibrary() const noexcept
{
return *instrumentation_library_;
}

private:
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
*instrumentation_library_ = nullptr;
};
} // namespace logs
} // namespace exporter
Expand Down
42 changes: 30 additions & 12 deletions exporters/etw/include/opentelemetry/exporters/etw/etw_logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -221,31 +221,49 @@ class LoggerProvider : public opentelemetry::logs::LoggerProvider
config_.encoding = ETWProvider::EventFormat::ETW_MANIFEST;
}

nostd::shared_ptr<opentelemetry::logs::Logger> GetLogger(
nostd::string_view logger_name,
nostd::string_view options,
nostd::string_view library_name,
nostd::string_view version = "",
nostd::string_view schema_url = "") override
{
UNREFERENCED_PARAMETER(options);
UNREFERENCED_PARAMETER(library_name);
UNREFERENCED_PARAMETER(version);
UNREFERENCED_PARAMETER(schema_url);
ETWProvider::EventFormat evtFmt = config_.encoding;
return nostd::shared_ptr<opentelemetry::logs::Logger>{
new (std::nothrow) etw::Logger(*this, logger_name, evtFmt)};
}

/**
* @brief Obtain ETW Tracer.
* @param name ProviderId (instrumentation name) - Name or GUID
*
* @param args Additional arguments that controls `codec` of the provider.
* Possible values are:
* - "ETW" - 'classic' Trace Logging Dynamic manifest ETW events.
* - "MSGPACK" - MessagePack-encoded binary payload ETW events.
* - "XML" - XML events (reserved for future use)
* @param library_name Library name
* @param version Library version
* @param schema_url schema URL
* @return
*/
nostd::shared_ptr<opentelemetry::logs::Logger> GetLogger(nostd::string_view name,
nostd::string_view args = "") override
nostd::shared_ptr<opentelemetry::logs::Logger> GetLogger(
nostd::string_view logger_name,
nostd::span<nostd::string_view> args,
nostd::string_view library_name,
nostd::string_view version = "",
nostd::string_view schema_url = "") override
{
UNREFERENCED_PARAMETER(args);
UNREFERENCED_PARAMETER(library_name);
UNREFERENCED_PARAMETER(version);
UNREFERENCED_PARAMETER(schema_url);
ETWProvider::EventFormat evtFmt = config_.encoding;
return nostd::shared_ptr<opentelemetry::logs::Logger>{new (std::nothrow)
etw::Logger(*this, name, evtFmt)};
}

nostd::shared_ptr<opentelemetry::logs::Logger> GetLogger(
nostd::string_view name,
nostd::span<nostd::string_view> args) override
{
return GetLogger(name, args[0]);
return nostd::shared_ptr<opentelemetry::logs::Logger>{
new (std::nothrow) etw::Logger(*this, logger_name, evtFmt)};
}
};

Expand Down
6 changes: 4 additions & 2 deletions exporters/etw/test/etw_logger_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ TEST(ETWLogger, LoggerCheckWithBody)
std::string providerName = kGlobalProviderName; // supply unique instrumentation name here
exporter::etw::LoggerProvider lp;

auto logger = lp.GetLogger(providerName);
const std::string schema_url{"https://opentelemetry.io/schemas/1.2.0"};
auto logger = lp.GetLogger(providerName, "", schema_url);
Properties attribs = {{"attrib1", 1}, {"attrib2", 2}};
EXPECT_NO_THROW(
logger->Log(opentelemetry::logs::Severity::kDebug, "My Log", "This is test log body"));
Expand Down Expand Up @@ -90,7 +91,8 @@ TEST(ETWLogger, LoggerCheckWithAttributes)
std::string providerName = kGlobalProviderName; // supply unique instrumentation name here
exporter::etw::LoggerProvider lp;

auto logger = lp.GetLogger(providerName);
const std::string schema_url{"https://opentelemetry.io/schemas/1.2.0"};
auto logger = lp.GetLogger(providerName, "", schema_url);
// Log attributes
Properties attribs = {{"attrib1", 1}, {"attrib2", 2}};
EXPECT_NO_THROW(logger->Log(opentelemetry::logs::Severity::kDebug, "My Log", attribs));
Expand Down
4 changes: 3 additions & 1 deletion exporters/ostream/test/ostream_log_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,9 @@ TEST(OStreamLogExporter, IntegrationTest)
auto apiProvider = nostd::shared_ptr<logs_api::LoggerProvider>(sdkProvider);
auto provider = nostd::shared_ptr<logs_api::LoggerProvider>(apiProvider);
logs_api::Provider::SetLoggerProvider(provider);
auto logger = logs_api::Provider::GetLoggerProvider()->GetLogger("Logger");
const std::string schema_url{"https://opentelemetry.io/schemas/1.2.0"};
auto logger = logs_api::Provider::GetLoggerProvider()->GetLogger(
"Logger", "", "opentelelemtry_library", "", schema_url);

// Back up cout's streambuf
std::streambuf *original = std::cout.rdbuf();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

# include "opentelemetry/proto/logs/v1/logs.pb.h"
# include "opentelemetry/proto/resource/v1/resource.pb.h"
# include "opentelemetry/sdk/instrumentationlibrary/instrumentation_library.h"

# include "opentelemetry/exporters/otlp/protobuf_include_suffix.h"
// clang-format on
Expand Down Expand Up @@ -92,14 +93,25 @@ class OtlpLogRecordable final : public opentelemetry::sdk::logs::Recordable
*/
void SetTraceFlags(opentelemetry::trace::TraceFlags trace_flags) noexcept override;

/**
* Set instrumentation_library for this log.
* @param instrumentation_library the instrumentation library to set
*/
void SetInstrumentationLibrary(
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
&instrumentation_library) noexcept override;

/** Returns the associated instruementation library */
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary &
GetInstrumentationLibrary() const noexcept;

private:
proto::logs::v1::LogRecord log_record_;
const opentelemetry::sdk::resource::Resource *resource_ = nullptr;
// TODO shared resource
// const opentelemetry::sdk::resource::Resource *resource_ = nullptr;
// TODO InstrumentationLibrary
// const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
// *instrumentation_library_ = nullptr;
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
*instrumentation_library_ = nullptr;
};

} // namespace otlp
Expand Down
12 changes: 12 additions & 0 deletions exporters/otlp/src/otlp_log_recordable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,18 @@ void OtlpLogRecordable::SetTraceFlags(opentelemetry::trace::TraceFlags trace_fla
log_record_.set_flags(trace_flags.flags());
}

void OtlpLogRecordable::SetInstrumentationLibrary(
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
&instrumentation_library) noexcept
{
instrumentation_library_ = &instrumentation_library;
}

const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary &
OtlpLogRecordable::GetInstrumentationLibrary() const noexcept
{
return *instrumentation_library_;
}
} // namespace otlp
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE
Expand Down
3 changes: 2 additions & 1 deletion exporters/otlp/test/otlp_grpc_log_exporter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ TEST_F(OtlpGrpcLogExporterTestPeer, ExportIntegrationTest)
'3', '2', '1', '0'};
opentelemetry::trace::SpanId span_id{span_id_bin};

auto logger = provider->GetLogger("test");
const std::string schema_url{"https://opentelemetry.io/schemas/1.2.0"};
auto logger = provider->GetLogger("test", "", "opentelelemtry_library", "", schema_url);
logger->Log(opentelemetry::logs::Severity::kInfo, "Log name", "Log message",
{{"service.name", "unit_test_service"},
{"tenant.id", "test_user"},
Expand Down
6 changes: 4 additions & 2 deletions exporters/otlp/test/otlp_http_log_exporter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,8 @@ TEST_F(OtlpHttpLogExporterTestPeer, ExportJsonIntegrationTest)
char span_id_hex[2 * opentelemetry::trace::SpanId::kSize] = {0};
opentelemetry::trace::SpanId span_id{span_id_bin};

auto logger = provider->GetLogger("test");
const std::string schema_url{"https://opentelemetry.io/schemas/1.2.0"};
auto logger = provider->GetLogger("test", "", "opentelelemtry_library", "", schema_url);
logger->Log(opentelemetry::logs::Severity::kInfo, "Log name", "Log message",
{{"service.name", "unit_test_service"},
{"tenant.id", "test_user"},
Expand Down Expand Up @@ -313,7 +314,8 @@ TEST_F(OtlpHttpLogExporterTestPeer, ExportBinaryIntegrationTest)
'3', '2', '1', '0'};
opentelemetry::trace::SpanId span_id{span_id_bin};

auto logger = provider->GetLogger("test");
const std::string schema_url{"https://opentelemetry.io/schemas/1.2.0"};
auto logger = provider->GetLogger("test", "", "opentelelemtry_library", "", schema_url);
logger->Log(opentelemetry::logs::Severity::kInfo, "Log name", "Log message",
{{"service.name", "unit_test_service"},
{"tenant.id", "test_user"},
Expand Down
Loading