Skip to content

Commit

Permalink
Add attributes/dimensions to metrics ostream exporter (#1400)
Browse files Browse the repository at this point in the history
  • Loading branch information
lalitb authored Jun 7, 2022
1 parent 7e90dae commit 18991a6
Show file tree
Hide file tree
Showing 10 changed files with 171 additions and 183 deletions.
3 changes: 3 additions & 0 deletions exporters/ostream/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ cc_library(
"src/log_exporter.cc",
],
hdrs = [
"include/opentelemetry/exporters/ostream/common_utils.h",
"include/opentelemetry/exporters/ostream/log_exporter.h",
],
strip_include_prefix = "include",
Expand Down Expand Up @@ -49,6 +50,7 @@ cc_library(
"src/metric_exporter.cc",
],
hdrs = [
"include/opentelemetry/exporters/ostream/common_utils.h",
"include/opentelemetry/exporters/ostream/metric_exporter.h",
],
strip_include_prefix = "include",
Expand Down Expand Up @@ -93,6 +95,7 @@ cc_library(
"src/span_exporter.cc",
],
hdrs = [
"include/opentelemetry/exporters/ostream/common_utils.h",
"include/opentelemetry/exporters/ostream/span_exporter.h",
],
strip_include_prefix = "include",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#include <sstream>
#include <string>
#include <vector>
#include "opentelemetry/nostd/variant.h"
#include "opentelemetry/sdk/common/attribute_utils.h"

#pragma once
OPENTELEMETRY_BEGIN_NAMESPACE
namespace exporter
{
namespace ostream_common
{
/*
print_value is used to print out the value of an attribute within a vector.
These values are held in a variant which makes the process of printing them much more
complicated.
*/

template <typename T>
void print_value(const T &item, std::ostream &sout)
{
sout << item;
}

template <typename T>
void print_value(const std::vector<T> &vec, std::ostream &sout)
{
sout << '[';
size_t i = 1;
size_t sz = vec.size();
for (auto v : vec)
{
sout << v;
if (i != sz)
sout << ',';
i++;
};
sout << ']';
}

// Prior to C++14, generic lambda is not available so fallback to functor.
#if __cplusplus < 201402L

class OwnedAttributeValueVisitor
{
public:
OwnedAttributeValueVisitor(std::ostream &sout) : sout_(sout) {}

template <typename T>
void operator()(T &&arg)
{
print_value(arg, sout_);
}

private:
std::ostream &sout_;
};

#endif

void print_value(const opentelemetry::sdk::common::OwnedAttributeValue &value, std::ostream &sout)
{
#if __cplusplus < 201402L
opentelemetry::nostd::visit(OwnedAttributeValueVisitor(sout), value);
#else
opentelemetry::nostd::visit(
[&sout](auto &&arg) {
/* explicit this is needed by some gcc versions (observed with v5.4.0)*/
print_value(arg, sout);
},
value);
#endif
}

} // namespace ostream_common
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ class OStreamLogExporter final : public opentelemetry::sdk::logs::LogExporter
bool is_shutdown_ = false;
mutable opentelemetry::common::SpinLockMutex lock_;
bool isShutdown() const noexcept;
void printAttributes(
const std::unordered_map<std::string, opentelemetry::sdk::common::OwnedAttributeValue> &map,
const std::string prefix = "\n\t");
};
} // namespace logs
} // namespace exporter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class OStreamMetricExporter final : public opentelemetry::sdk::metrics::MetricEx
void printInstrumentationInfoMetricData(
const sdk::metrics::InstrumentationInfoMetrics &info_metrics);
void printPointData(const opentelemetry::sdk::metrics::PointType &point_data);
void printPointAttributes(const opentelemetry::sdk::metrics::PointAttributes &point_attributes);
};
} // namespace metrics
} // namespace exporter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,68 +50,6 @@ class OStreamSpanExporter final : public opentelemetry::sdk::trace::SpanExporter
// Mapping status number to the string from api/include/opentelemetry/trace/canonical_code.h
std::map<int, std::string> statusMap{{0, "Unset"}, {1, "Ok"}, {2, "Error"}};

/*
print_value is used to print out the value of an attribute within a vector.
These values are held in a variant which makes the process of printing them much more
complicated.
*/

template <typename T>
void print_value(const T &item)
{
sout_ << item;
}

template <typename T>
void print_value(const std::vector<T> &vec)
{
sout_ << '[';
size_t i = 1;
size_t sz = vec.size();
for (auto v : vec)
{
sout_ << v;
if (i != sz)
sout_ << ',';
i++;
};
sout_ << ']';
}

// Prior to C++14, generic lambda is not available so fallback to functor.
#if __cplusplus < 201402L

class OwnedAttributeValueVisitor
{
public:
OwnedAttributeValueVisitor(OStreamSpanExporter &exporter) : exporter_(exporter) {}

template <typename T>
void operator()(T &&arg)
{
exporter_.print_value(arg);
}

private:
OStreamSpanExporter &exporter_;
};

#endif

void print_value(const opentelemetry::sdk::common::OwnedAttributeValue &value)
{
#if __cplusplus < 201402L
opentelemetry::nostd::visit(OwnedAttributeValueVisitor(*this), value);
#else
opentelemetry::nostd::visit(
[this](auto &&arg) {
/* explicit this is needed by some gcc versions (observed with v5.4.0)*/
this->print_value(arg);
},
value);
#endif
}

// various print helpers
void printAttributes(
const std::unordered_map<std::string, opentelemetry::sdk::common::OwnedAttributeValue> &map,
Expand Down
100 changes: 17 additions & 83 deletions exporters/ostream/src/log_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,97 +4,20 @@
#ifdef ENABLE_LOGS_PREVIEW
# include "opentelemetry/exporters/ostream/log_exporter.h"
# include <mutex>
# include "opentelemetry/exporters/ostream/common_utils.h"
# include "opentelemetry/sdk_config.h"

# include <iostream>
# include <type_traits>

namespace nostd = opentelemetry::nostd;
namespace sdklogs = opentelemetry::sdk::logs;

namespace nostd = opentelemetry::nostd;
namespace sdklogs = opentelemetry::sdk::logs;
namespace sdkcommon = opentelemetry::sdk::common;
OPENTELEMETRY_BEGIN_NAMESPACE
namespace exporter
{
namespace logs
{
/*********************** Helper functions ************************/

/*
print_value is used to print out the value of an attribute within a vector.
These values are held in a variant which makes the process of printing them much more
complicated.
*/

template <typename T>
void print_value(const T &item, std::ostream &sout)
{
sout << item;
}

template <typename T>
void print_value(const std::vector<T> &vec, std::ostream &sout)
{
sout << '[';
size_t i = 1;
size_t sz = vec.size();
for (auto v : vec)
{
sout << v;
if (i != sz)
sout << ',' << ' ';
i++;
};
sout << ']';
}

// Prior to C++14, generic lambda is not available so fallback to functor.
# if __cplusplus < 201402L

class OwnedAttributeValueVisitor
{
public:
OwnedAttributeValueVisitor(std::ostream &sout) : sout_(sout) {}

template <typename T>
void operator()(T &&arg)
{
print_value(arg, sout_);
}

private:
// The OStream to send the logs to
std::ostream &sout_;
};

# endif

void print_value(sdk::common::OwnedAttributeValue &value, std::ostream &sout)
{
# if __cplusplus < 201402L
nostd::visit(OwnedAttributeValueVisitor(sout), value);
# else
nostd::visit([&sout](auto &&arg) { print_value(arg, sout); }, value);
# endif
}

void printMap(std::unordered_map<std::string, sdk::common::OwnedAttributeValue> map,
std::ostream &sout)
{
sout << "{";
size_t size = map.size();
size_t i = 1;
for (auto kv : map)
{
sout << "{" << kv.first << ": ";
print_value(kv.second, sout);
sout << "}";

if (i != size)
sout << ", ";
i++;
}
sout << "}";
}

/*********************** Constructor ***********************/

Expand Down Expand Up @@ -162,12 +85,12 @@ sdk::common::ExportResult OStreamLogExporter::Export(
sout_ << " body : " << log_record->GetBody() << "\n"
<< " resource : ";

printMap(log_record->GetResource().GetAttributes(), sout_);
printAttributes(log_record->GetResource().GetAttributes());

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

printMap(log_record->GetAttributes(), sout_);
printAttributes(log_record->GetAttributes());

sout_ << "\n"
<< " trace_id : " << std::string(trace_id, trace_id_len) << "\n"
Expand All @@ -192,6 +115,17 @@ bool OStreamLogExporter::isShutdown() const noexcept
return is_shutdown_;
}

void OStreamLogExporter::printAttributes(
const std::unordered_map<std::string, sdkcommon::OwnedAttributeValue> &map,
const std::string prefix)
{
for (const auto &kv : map)
{
sout_ << prefix << kv.first << ": ";
opentelemetry::exporter::ostream_common::print_value(kv.second, sout_);
}
}

} // namespace logs
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE
Expand Down
20 changes: 17 additions & 3 deletions exporters/ostream/src/metric_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <chrono>
#ifndef ENABLE_METRICS_PREVIEW
# include <algorithm>
# include "opentelemetry/exporters/ostream/common_utils.h"
# include "opentelemetry/exporters/ostream/metric_exporter.h"
# include "opentelemetry/sdk/metrics/aggregation/default_aggregation.h"
# include "opentelemetry/sdk/metrics/aggregation/histogram_aggregation.h"
Expand Down Expand Up @@ -102,7 +103,11 @@ void OStreamMetricExporter::printInstrumentationInfoMetricData(

for (const auto &pd : record.point_data_attr_)
{
printPointData(pd.point_data);
if (!nostd::holds_alternative<sdk::metrics::DropPointData>(pd.point_data))
{
printPointData(pd.point_data);
printPointAttributes(pd.attributes);
}
}
}
sout_ << "\n}\n";
Expand Down Expand Up @@ -171,8 +176,17 @@ void OStreamMetricExporter::printPointData(const opentelemetry::sdk::metrics::Po
sout_ << nostd::get<long>(last_point_data.value_);
}
}
else if (nostd::holds_alternative<sdk::metrics::DropPointData>(point_data))
{}
}

void OStreamMetricExporter::printPointAttributes(
const opentelemetry::sdk::metrics::PointAttributes &point_attributes)
{
sout_ << "\n attributes\t\t: ";
for (const auto &kv : point_attributes)
{
sout_ << "\n\t" << kv.first << ": ";
opentelemetry::exporter::ostream_common::print_value(kv.second, sout_);
}
}

bool OStreamMetricExporter::ForceFlush(std::chrono::microseconds timeout) noexcept
Expand Down
4 changes: 3 additions & 1 deletion exporters/ostream/src/span_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// SPDX-License-Identifier: Apache-2.0

#include "opentelemetry/exporters/ostream/span_exporter.h"
#include "opentelemetry/exporters/ostream/common_utils.h"

#include <iostream>
#include <mutex>
#include "opentelemetry/sdk_config.h"
Expand Down Expand Up @@ -115,7 +117,7 @@ void OStreamSpanExporter::printAttributes(
for (const auto &kv : map)
{
sout_ << prefix << kv.first << ": ";
print_value(kv.second);
opentelemetry::exporter::ostream_common::print_value(kv.second, sout_);
}
}

Expand Down
Loading

0 comments on commit 18991a6

Please sign in to comment.