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 #1128

Closed
Show file tree
Hide file tree
Changes from 15 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))
* [EXPORTER] Bugfix: Jaeger exporter: extend supported attributes types ([#1106](https://github.com/open-telemetry/opentelemetry-cpp/pull/1106))
* [EXPORTER] Fix otlp generates null span ids ([#1106](https://github.com/open-telemetry/opentelemetry-cpp/pull/1106))

Expand Down
8 changes: 3 additions & 5 deletions api/include/opentelemetry/logs/logger_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,9 @@ class LoggerProvider
*
*/

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

virtual nostd::shared_ptr<Logger> GetLogger(nostd::string_view logger_name,
nostd::span<nostd::string_view> args) = 0;
virtual nostd::shared_ptr<Logger> GetLogger(nostd::string_view library_name,
Copy link
Member

@lalitb lalitb Dec 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for not clarifying this in the issue, but I think logger_name and library_name are two separate entities and would be good to support both of them. While LoggerName is still not in the log data model ( in specs ) the PR is open - open-telemetry/opentelemetry-specification#1236 - and hopefully it would get added soon.

Also, do you think we can keep options and args even though they are not used as of now?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So logger_name, library_name, options and args. library_name goes to the instrumentation , and options and args will stay unused correct?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes correct.

nostd::string_view library_version = "",
nostd::string_view schema_url = "") = 0;
};
} // namespace logs
OPENTELEMETRY_END_NAMESPACE
Expand Down
11 changes: 3 additions & 8 deletions api/include/opentelemetry/logs/noop.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,9 @@ class NoopLoggerProvider final : public opentelemetry::logs::LoggerProvider
nostd::shared_ptr<opentelemetry::logs::NoopLogger>(new opentelemetry::logs::NoopLogger)}
{}

nostd::shared_ptr<Logger> GetLogger(nostd::string_view logger_name,
nostd::string_view options) override
{
return logger_;
}

nostd::shared_ptr<Logger> GetLogger(nostd::string_view logger_name,
nostd::span<nostd::string_view> args) override
nostd::shared_ptr<Logger> GetLogger(nostd::string_view library_name,
nostd::string_view library_version = "",
nostd::string_view schema_url = "") override
{
return logger_;
}
Expand Down
31 changes: 8 additions & 23 deletions api/test/logs/logger_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,14 @@ 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", "", schema_url);
auto name = logger->GetName();
EXPECT_NE(nullptr, logger);
EXPECT_EQ(name, "noop logger");
}

// Test the two additional overloads for GetLogger()
TEST(Logger, GetNoopLoggerNameWithArgs)
{
auto lp = Provider::GetLoggerProvider();

// GetLogger(name, list(args))
std::array<string_view, 1> sv{"string"};
span<string_view> args{sv};
lp->GetLogger("NoopLoggerWithArgs", args);

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

// Test the Log() overloads
TEST(Logger, LogMethodOverloads)
{
Expand Down Expand Up @@ -92,12 +79,9 @@ 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
{
return shared_ptr<Logger>(new TestLogger());
}

shared_ptr<Logger> GetLogger(string_view library_name, span<string_view> args) override
shared_ptr<Logger> GetLogger(string_view library_name,
string_view library_version = "",
nostd::string_view schema_url = "") override
{
return shared_ptr<Logger>(new TestLogger());
}
Expand All @@ -112,7 +96,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", "", schema_url);
ASSERT_EQ("test logger", logger->GetName());
}
#endif
21 changes: 6 additions & 15 deletions api/test/logs/provider_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,9 @@ using opentelemetry::nostd::string_view;

class TestProvider : public LoggerProvider
{
shared_ptr<Logger> GetLogger(string_view library_name, string_view options) override
{
return shared_ptr<Logger>(nullptr);
}

shared_ptr<Logger> GetLogger(string_view library_name, span<string_view> args) override
shared_ptr<Logger> GetLogger(string_view library_name,
string_view library_version = "",
string_view schema_url = "") override
{
return shared_ptr<Logger>(nullptr);
}
Expand Down Expand Up @@ -57,15 +54,9 @@ 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", "", 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);
EXPECT_EQ(nullptr, logger2);
}
#endif
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 @@ -157,7 +157,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
22 changes: 7 additions & 15 deletions exporters/etw/include/opentelemetry/exporters/etw/etw_logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,28 +215,20 @@ class LoggerProvider : public opentelemetry::logs::LoggerProvider
* @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 version Library version
* @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 name,
nostd::string_view version = "",
nostd::string_view schema_url = "") override
{
UNREFERENCED_PARAMETER(args);
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]);
}
};

} // namespace etw
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
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 @@ -94,14 +95,25 @@ class OtlpLogRecordable final : public opentelemetry::sdk::logs::Recordable
*/
virtual 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_;
opentelemetry::sdk::common::AttributeMap resource_attributes_;
// 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 @@ -199,6 +199,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
9 changes: 9 additions & 0 deletions exporters/otlp/test/otlp_log_recordable_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,15 @@ TEST(OtlpLogRecordable, SetArrayAttribute)
}
}

TEST(OtlpLogRecordable, SetInstrumentationLibrary)
{
OtlpLogRecordable rec;
auto inst_lib =
opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary::Create("test", "v1");
rec.SetInstrumentationLibrary(*inst_lib);
EXPECT_EQ(rec.GetInstrumentationLibrary(), *inst_lib);
}

/**
* AttributeValue can contain different int types, such as int, int64_t,
* unsigned int, and uint64_t. To avoid writing test cases for each, we can
Expand Down
15 changes: 15 additions & 0 deletions sdk/include/opentelemetry/sdk/logs/log_record.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,21 @@ class LogRecord final : public Recordable
* @return the timestamp for this log
*/
opentelemetry::common::SystemTimestamp GetTimestamp() const noexcept { return timestamp_; }

/**
* 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;
}

private:
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
*instrumentation_library_ = nullptr;
};
} // namespace logs
} // namespace sdk
Expand Down
13 changes: 11 additions & 2 deletions sdk/include/opentelemetry/sdk/logs/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#ifdef ENABLE_LOGS_PREVIEW

# include "opentelemetry/logs/logger.h"
# include "opentelemetry/sdk/instrumentationlibrary/instrumentation_library.h"
# include "opentelemetry/sdk/logs/logger_provider.h"
# include "opentelemetry/sdk/logs/processor.h"

Expand All @@ -25,8 +26,11 @@ class Logger final : public opentelemetry::logs::Logger
* @param name The name of this logger instance
* @param logger_provider The logger provider that owns this logger.
*/
explicit Logger(opentelemetry::nostd::string_view name,
std::shared_ptr<LoggerProvider> logger_provider) noexcept;
explicit Logger(
opentelemetry::nostd::string_view name,
std::shared_ptr<LoggerProvider> logger_provider,
std::unique_ptr<instrumentationlibrary::InstrumentationLibrary> instrumentation_library =
instrumentationlibrary::InstrumentationLibrary::Create("")) noexcept;

/**
* Returns the name of this logger.
Expand Down Expand Up @@ -57,13 +61,18 @@ class Logger final : public opentelemetry::logs::Logger
opentelemetry::trace::TraceFlags trace_flags,
opentelemetry::common::SystemTimestamp timestamp) noexcept override;

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

private:
// The name of this logger
std::string logger_name_;

// The logger provider of this Logger. Uses a weak_ptr to avoid cyclic dependency issues the with
// logger provider
std::weak_ptr<LoggerProvider> logger_provider_;
std::shared_ptr<instrumentationlibrary::InstrumentationLibrary> instrumentation_library_;
};

} // namespace logs
Expand Down
17 changes: 3 additions & 14 deletions sdk/include/opentelemetry/sdk/logs/logger_provider.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,12 @@ class LoggerProvider final : public opentelemetry::logs::LoggerProvider,
* Creates a logger with the given name, and returns a shared pointer to it.
* If a logger with that name already exists, return a shared pointer to it
* @param name The name of the logger to be created.
* @param options (OPTIONAL) The options for the logger. TODO: Once the logging spec defines it,
* give a list of options that the logger supports.
* @param version The version of the library.
*/
opentelemetry::nostd::shared_ptr<opentelemetry::logs::Logger> GetLogger(
opentelemetry::nostd::string_view name,
opentelemetry::nostd::string_view options = "") noexcept override;

/**
* Creates a logger with the given name, and returns a shared pointer to it.
* If a logger with that name already exists, return a shared pointer to it
* @param name The name of the logger to be created.
* @param args (OPTIONAL) The arguments for the logger. TODO: Once the logging spec defines it,
* give a list of arguments that the logger supports.
*/
opentelemetry::nostd::shared_ptr<opentelemetry::logs::Logger> GetLogger(
opentelemetry::nostd::string_view name,
nostd::span<nostd::string_view> args) noexcept override;
opentelemetry::nostd::string_view version = "",
nostd::string_view schema_url = "") noexcept override;

/**
* Returns a shared pointer to the processor currently stored in the
Expand Down
8 changes: 8 additions & 0 deletions sdk/include/opentelemetry/sdk/logs/recordable.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ class Recordable
* @param trace_flags the trace flags to set
*/
virtual void SetTraceFlags(opentelemetry::trace::TraceFlags trace_flags) noexcept = 0;

/**
* Set instrumentation_library for this log.
* @param instrumentation_library the instrumentation library to set
*/
virtual void SetInstrumentationLibrary(
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
&instrumentation_library) noexcept = 0;
};
} // namespace logs
} // namespace sdk
Expand Down
Loading