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 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
69 changes: 0 additions & 69 deletions api/include/opentelemetry/logs/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,12 @@
#pragma once
#ifdef ENABLE_LOGS_PREVIEW

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

# include "opentelemetry/common/attribute_value.h"
# include "opentelemetry/common/key_value_iterable.h"
# include "opentelemetry/common/key_value_iterable_view.h"
# include "opentelemetry/common/macros.h"
# include "opentelemetry/common/timestamp.h"
# include "opentelemetry/logs/log_record.h"
# include "opentelemetry/logs/logger_type_traits.h"
# include "opentelemetry/logs/severity.h"
# include "opentelemetry/nostd/shared_ptr.h"
# include "opentelemetry/nostd/span.h"
# include "opentelemetry/nostd/string_view.h"
# include "opentelemetry/nostd/type_traits.h"
# include "opentelemetry/nostd/unique_ptr.h"
# include "opentelemetry/trace/span_context.h"
# include "opentelemetry/trace/span_id.h"
# include "opentelemetry/trace/trace_flags.h"
# include "opentelemetry/trace/trace_id.h"
# include "opentelemetry/version.h"

OPENTELEMETRY_BEGIN_NAMESPACE
Expand All @@ -36,60 +21,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
4 changes: 0 additions & 4 deletions api/include/opentelemetry/logs/logger_type_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@
#ifdef ENABLE_LOGS_PREVIEW

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

# include "opentelemetry/common/attribute_value.h"
# include "opentelemetry/common/key_value_iterable.h"
# include "opentelemetry/common/key_value_iterable_view.h"
# include "opentelemetry/common/macros.h"
# include "opentelemetry/common/timestamp.h"
# include "opentelemetry/logs/log_record.h"
# include "opentelemetry/logs/severity.h"
Expand Down
58 changes: 34 additions & 24 deletions api/test/logs/logger_test.cc
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading