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

Add attributes for InstrumentationScope #2004

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -15,6 +15,8 @@ Increment the:

## [Unreleased]

* [SDK]Add attributes for InstrumentationScope
[#2004](https://github.com/open-telemetry/opentelemetry-cpp/pull/2004)
* [ETW Exporter]Support serialize span/log attributes into JSON
[#1991](https://github.com/open-telemetry/opentelemetry-cpp/pull/1991)
* ETW Exporter]Do not overwrite ParentId when setting attribute on Span
Expand Down
56 changes: 56 additions & 0 deletions api/include/opentelemetry/common/key_value_iterable_view.h
Expand Up @@ -8,6 +8,8 @@
#include <utility>

#include "opentelemetry/common/key_value_iterable.h"
#include "opentelemetry/nostd/span.h"
#include "opentelemetry/nostd/string_view.h"
#include "opentelemetry/nostd/type_traits.h"
#include "opentelemetry/nostd/utility.h"
#include "opentelemetry/version.h"
Expand Down Expand Up @@ -81,5 +83,59 @@ KeyValueIterableView<T> MakeKeyValueIterableView(const T &container) noexcept
return KeyValueIterableView<T>(container);
}

/**
* Utility function to help to make a attribute view from initializer_list
*
* @param attributes
* @return nostd::span<const std::pair<nostd::string_view, common::AttributeValue>>
*/
inline static nostd::span<const std::pair<nostd::string_view, common::AttributeValue>>
MakeAttributes(std::initializer_list<std::pair<nostd::string_view, common::AttributeValue>>
attributes) noexcept
{
return nostd::span<const std::pair<nostd::string_view, common::AttributeValue>>{
attributes.begin(), attributes.end()};
}

/**
* Utility function to help to make a attribute view from a span
*
* @param attributes
* @return nostd::span<const std::pair<nostd::string_view, common::AttributeValue>>
*/
inline static nostd::span<const std::pair<nostd::string_view, common::AttributeValue>>
MakeAttributes(
nostd::span<const std::pair<nostd::string_view, common::AttributeValue>> attributes) noexcept
{
return attributes;
}

/**
* Utility function to help to make a attribute view from a KeyValueIterable
*
* @param attributes
* @return common::KeyValueIterable
*/
inline static const common::KeyValueIterable &MakeAttributes(
const common::KeyValueIterable &attributes) noexcept
{
return attributes;
}

/**
* Utility function to help to make a attribute view from a key-value iterable object
*
* @param attributes
* @return nostd::span<const std::pair<nostd::string_view, common::AttributeValue>>
*/
template <
class ArgumentType,
nostd::enable_if_t<common::detail::is_key_value_iterable<ArgumentType>::value> * = nullptr>
inline static common::KeyValueIterableView<ArgumentType> MakeAttributes(
const ArgumentType &arg) noexcept
{
return common::KeyValueIterableView<ArgumentType>(arg);
}

} // namespace common
OPENTELEMETRY_END_NAMESPACE
54 changes: 0 additions & 54 deletions api/include/opentelemetry/logs/logger.h
Expand Up @@ -36,60 +36,6 @@ namespace logs
class Logger
{
public:
/**
* Utility function to help to make a attribute view from initializer_list
*
* @param attributes
* @return nostd::span<const std::pair<nostd::string_view, common::AttributeValue>>
*/
inline static nostd::span<const std::pair<nostd::string_view, common::AttributeValue>>
Copy link
Member

Choose a reason for hiding this comment

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

nit - with this cleanup, we can also remove

# include "opentelemetry/nostd/span.h"
# include "opentelemetry/nostd/type_traits.h"

from header includes, need to check if there are others too.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, and the unused headers are removed now.
And #2008 is also included in this PR.

MakeAttributes(std::initializer_list<std::pair<nostd::string_view, common::AttributeValue>>
attributes) noexcept
{
return nostd::span<const std::pair<nostd::string_view, common::AttributeValue>>{
attributes.begin(), attributes.end()};
}

/**
* Utility function to help to make a attribute view from a span
*
* @param attributes
* @return nostd::span<const std::pair<nostd::string_view, common::AttributeValue>>
*/
inline static nostd::span<const std::pair<nostd::string_view, common::AttributeValue>>
MakeAttributes(
nostd::span<const std::pair<nostd::string_view, common::AttributeValue>> attributes) noexcept
{
return attributes;
}

/**
* Utility function to help to make a attribute view from a KeyValueIterable
*
* @param attributes
* @return common::KeyValueIterable
*/
inline static const common::KeyValueIterable &MakeAttributes(
const common::KeyValueIterable &attributes) noexcept
{
return attributes;
}

/**
* Utility function to help to make a attribute view from a key-value iterable object
*
* @param attributes
* @return nostd::span<const std::pair<nostd::string_view, common::AttributeValue>>
*/
template <
class ArgumentType,
nostd::enable_if_t<common::detail::is_key_value_iterable<ArgumentType>::value> * = nullptr>
inline static common::KeyValueIterableView<ArgumentType> MakeAttributes(
const ArgumentType &arg) noexcept
{
return common::KeyValueIterableView<ArgumentType>(arg);
}

virtual ~Logger() = default;

/* Returns the name of the logger */
Expand Down
58 changes: 34 additions & 24 deletions api/test/logs/logger_test.cc
Expand Up @@ -61,48 +61,54 @@ TEST(Logger, LogMethodOverloads)
logger->EmitLogRecord(Severity::kDebug, m);
logger->EmitLogRecord(Severity::kWarn, "Logging a map", m);
logger->EmitLogRecord(Severity::kError,
Logger::MakeAttributes({{"key1", "value 1"}, {"key2", 2}}));
opentelemetry::common::MakeAttributes({{"key1", "value 1"}, {"key2", 2}}));
logger->EmitLogRecord(Severity::kFatal, "Logging an initializer list",
Logger::MakeAttributes({{"key1", "value 1"}, {"key2", 2}}));
logger->EmitLogRecord(Severity::kDebug, Logger::MakeAttributes(m));
opentelemetry::common::MakeAttributes({{"key1", "value 1"}, {"key2", 2}}));
logger->EmitLogRecord(Severity::kDebug, opentelemetry::common::MakeAttributes(m));
logger->EmitLogRecord(Severity::kDebug,
common::KeyValueIterableView<std::map<std::string, std::string>>(m));
std::pair<nostd::string_view, common::AttributeValue> array[] = {{"key1", "value1"}};
logger->EmitLogRecord(Severity::kDebug, Logger::MakeAttributes(array));
logger->EmitLogRecord(Severity::kDebug, opentelemetry::common::MakeAttributes(array));
std::vector<std::pair<std::string, std::string>> vec = {{"key1", "value1"}};
logger->EmitLogRecord(Severity::kDebug, Logger::MakeAttributes(vec));
logger->EmitLogRecord(Severity::kDebug, opentelemetry::common::MakeAttributes(vec));

// Severity methods
logger->Trace("Test log message");
logger->Trace("Test log message", m);
logger->Trace("Test log message", Logger::MakeAttributes({{"key1", "value 1"}, {"key2", 2}}));
logger->Trace("Test log message",
opentelemetry::common::MakeAttributes({{"key1", "value 1"}, {"key2", 2}}));
logger->Trace(m);
logger->Trace(Logger::MakeAttributes({{"key1", "value 1"}, {"key2", 2}}));
logger->Trace(opentelemetry::common::MakeAttributes({{"key1", "value 1"}, {"key2", 2}}));
logger->Debug("Test log message");
logger->Debug("Test log message", m);
logger->Debug("Test log message", Logger::MakeAttributes({{"key1", "value 1"}, {"key2", 2}}));
logger->Debug("Test log message",
opentelemetry::common::MakeAttributes({{"key1", "value 1"}, {"key2", 2}}));
logger->Debug(m);
logger->Debug(Logger::MakeAttributes({{"key1", "value 1"}, {"key2", 2}}));
logger->Debug(opentelemetry::common::MakeAttributes({{"key1", "value 1"}, {"key2", 2}}));
logger->Info("Test log message");
logger->Info("Test log message", m);
logger->Info("Test log message", Logger::MakeAttributes({{"key1", "value 1"}, {"key2", 2}}));
logger->Info("Test log message",
opentelemetry::common::MakeAttributes({{"key1", "value 1"}, {"key2", 2}}));
logger->Info(m);
logger->Info(Logger::MakeAttributes({{"key1", "value 1"}, {"key2", 2}}));
logger->Info(opentelemetry::common::MakeAttributes({{"key1", "value 1"}, {"key2", 2}}));
logger->Warn("Test log message");
logger->Warn("Test log message", m);
logger->Warn("Test log message", Logger::MakeAttributes({{"key1", "value 1"}, {"key2", 2}}));
logger->Warn("Test log message",
opentelemetry::common::MakeAttributes({{"key1", "value 1"}, {"key2", 2}}));
logger->Warn(m);
logger->Warn(Logger::MakeAttributes({{"key1", "value 1"}, {"key2", 2}}));
logger->Warn(opentelemetry::common::MakeAttributes({{"key1", "value 1"}, {"key2", 2}}));
logger->Error("Test log message");
logger->Error("Test log message", m);
logger->Error("Test log message", Logger::MakeAttributes({{"key1", "value 1"}, {"key2", 2}}));
logger->Error("Test log message",
opentelemetry::common::MakeAttributes({{"key1", "value 1"}, {"key2", 2}}));
logger->Error(m);
logger->Error(Logger::MakeAttributes({{"key1", "value 1"}, {"key2", 2}}));
logger->Error(opentelemetry::common::MakeAttributes({{"key1", "value 1"}, {"key2", 2}}));
logger->Fatal("Test log message");
logger->Fatal("Test log message", m);
logger->Fatal("Test log message", Logger::MakeAttributes({{"key1", "value 1"}, {"key2", 2}}));
logger->Fatal("Test log message",
opentelemetry::common::MakeAttributes({{"key1", "value 1"}, {"key2", 2}}));
logger->Fatal(m);
logger->Fatal(Logger::MakeAttributes({{"key1", "value 1"}, {"key2", 2}}));
logger->Fatal(opentelemetry::common::MakeAttributes({{"key1", "value 1"}, {"key2", 2}}));
}

TEST(Logger, EventLogMethodOverloads)
Expand All @@ -120,17 +126,21 @@ TEST(Logger, EventLogMethodOverloads)
event_logger->EmitEvent("event name", Severity::kInfo, "Test log message");
event_logger->EmitEvent("event name", Severity::kDebug, m);
event_logger->EmitEvent("event name", Severity::kWarn, "Logging a map", m);
event_logger->EmitEvent("event name", Severity::kError,
Logger::MakeAttributes({{"key1", "value 1"}, {"key2", 2}}));
event_logger->EmitEvent("event name", Severity::kFatal, "Logging an initializer list",
Logger::MakeAttributes({{"key1", "value 1"}, {"key2", 2}}));
event_logger->EmitEvent("event name", Severity::kDebug, Logger::MakeAttributes(m));
event_logger->EmitEvent(
"event name", Severity::kError,
opentelemetry::common::MakeAttributes({{"key1", "value 1"}, {"key2", 2}}));
event_logger->EmitEvent(
"event name", Severity::kFatal, "Logging an initializer list",
opentelemetry::common::MakeAttributes({{"key1", "value 1"}, {"key2", 2}}));
event_logger->EmitEvent("event name", Severity::kDebug, opentelemetry::common::MakeAttributes(m));
event_logger->EmitEvent("event name", Severity::kDebug,
common::KeyValueIterableView<std::map<std::string, std::string>>(m));
std::pair<nostd::string_view, common::AttributeValue> array[] = {{"key1", "value1"}};
event_logger->EmitEvent("event name", Severity::kDebug, Logger::MakeAttributes(array));
event_logger->EmitEvent("event name", Severity::kDebug,
opentelemetry::common::MakeAttributes(array));
std::vector<std::pair<std::string, std::string>> vec = {{"key1", "value1"}};
event_logger->EmitEvent("event name", Severity::kDebug, Logger::MakeAttributes(vec));
event_logger->EmitEvent("event name", Severity::kDebug,
opentelemetry::common::MakeAttributes(vec));
}

// Define a basic Logger class
Expand Down
2 changes: 1 addition & 1 deletion exporters/etw/test/etw_logger_test.cc
Expand Up @@ -96,7 +96,7 @@ TEST(ETWLogger, LoggerCheckWithAttributes)
// Log attributes
Properties attribs = {{"attrib1", 1}, {"attrib2", 2}};
EXPECT_NO_THROW(logger->EmitLogRecord(opentelemetry::logs::Severity::kDebug,
Logger::MakeAttributes(attribs)));
opentelemetry::common::MakeAttributes(attribs)));
}

# endif // _WIN32
Expand Down
21 changes: 13 additions & 8 deletions exporters/ostream/src/log_record_exporter.cc
Expand Up @@ -88,21 +88,26 @@ sdk::common::ExportResult OStreamLogRecordExporter::Export(

sout_ << " body : ";
opentelemetry::exporter::ostream_common::print_value(log_record->GetBody(), sout_);
sout_ << "\n";
sout_ << "\n resource : ";
printAttributes(log_record->GetResource().GetAttributes(), "\n ");

sout_ << " resource : ";
printAttributes(log_record->GetResource().GetAttributes());
sout_ << "\n attributes : ";

sout_ << "\n"
<< " attributes : ";

printAttributes(log_record->GetAttributes());
printAttributes(log_record->GetAttributes(), "\n ");

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"
<< "}\n";
<< " scope : \n"
<< " name : " << log_record->GetInstrumentationScope().GetName() << "\n"
<< " version : " << log_record->GetInstrumentationScope().GetVersion() << "\n"
<< " schema_url : " << log_record->GetInstrumentationScope().GetSchemaURL()
<< "\n"
<< " attributes : ";

printAttributes(log_record->GetInstrumentationScope().GetAttributes(), "\n ");
sout_ << "\n}\n";
}

return sdk::common::ExportResult::kSuccess;
Expand Down