Skip to content

Commit

Permalink
Merge branch 'main' into Histogram-min/max
Browse files Browse the repository at this point in the history
  • Loading branch information
lalitb authored Aug 6, 2022
2 parents d1b4585 + 22f07a0 commit 6753e6f
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,13 @@ class OStreamMetricExporter final : public opentelemetry::sdk::metrics::MetricEx
bool is_shutdown_ = false;
mutable opentelemetry::common::SpinLockMutex lock_;
bool isShutdown() const noexcept;
void printInstrumentationInfoMetricData(const sdk::metrics::ScopeMetrics &info_metrics);
void printInstrumentationInfoMetricData(const sdk::metrics::ScopeMetrics &info_metrics,
const sdk::metrics::ResourceMetrics &data);
void printPointData(const opentelemetry::sdk::metrics::PointType &point_data);
void printPointAttributes(const opentelemetry::sdk::metrics::PointAttributes &point_attributes);
void printAttributes(const std::map<std::string, sdk::common::OwnedAttributeValue> &map,
const std::string prefix);
void printResources(const opentelemetry::sdk::resource::Resource &resources);
};
} // namespace metrics
} // namespace exporter
Expand Down
34 changes: 32 additions & 2 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 <map>
# include "opentelemetry/exporters/ostream/common_utils.h"
# include "opentelemetry/exporters/ostream/metric_exporter.h"
# include "opentelemetry/sdk/metrics/aggregation/default_aggregation.h"
Expand Down Expand Up @@ -79,13 +80,39 @@ sdk::common::ExportResult OStreamMetricExporter::Export(

for (auto &record : data.scope_metric_data_)
{
printInstrumentationInfoMetricData(record);
printInstrumentationInfoMetricData(record, data);
}
return sdk::common::ExportResult::kSuccess;
}

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

void OStreamMetricExporter::printResources(const opentelemetry::sdk::resource::Resource &resources)
{
auto attributes = resources.GetAttributes();
if (attributes.size())
{
// Convert unordered_map to map for printing so that iteration
// order is guaranteed.
std::map<std::string, sdk::common::OwnedAttributeValue> attr_map;
for (auto &kv : attributes)
attr_map[kv.first] = std::move(kv.second);
printAttributes(attr_map, "\n\t");
}
}

void OStreamMetricExporter::printInstrumentationInfoMetricData(
const sdk::metrics::ScopeMetrics &info_metric)
const sdk::metrics::ScopeMetrics &info_metric,
const sdk::metrics::ResourceMetrics &data)
{
// sout_ is shared
const std::lock_guard<opentelemetry::common::SpinLockMutex> locked(lock_);
Expand All @@ -109,6 +136,9 @@ void OStreamMetricExporter::printInstrumentationInfoMetricData(
printPointAttributes(pd.attributes);
}
}

sout_ << "\n resources\t:";
printResources(*data.resource_);
}
sout_ << "\n}\n";
}
Expand Down
33 changes: 29 additions & 4 deletions exporters/ostream/test/ostream_metric_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# include <vector>
# include "opentelemetry/sdk/metrics/instruments.h"
# include "opentelemetry/sdk/resource/resource_detector.h"
# include "opentelemetry/sdk/version/version.h"

# include <iostream>
# include "opentelemetry/exporters/ostream/metric_exporter.h"
Expand Down Expand Up @@ -81,7 +82,13 @@ TEST(OStreamMetricsExporter, ExportSumPointData)
"\n value\t\t: 20"
"\n attributes\t\t: "
"\n\ta1: b1"
"\n}\n";
"\n resources\t:"
"\n\tservice.name: unknown_service"
"\n\ttelemetry.sdk.language: cpp"
"\n\ttelemetry.sdk.name: opentelemetry"
"\n\ttelemetry.sdk.version: ";
expected_output += OPENTELEMETRY_SDK_VERSION;
expected_output += "\n}\n";
ASSERT_EQ(stdoutOutput.str(), expected_output);
}

Expand Down Expand Up @@ -157,7 +164,13 @@ TEST(OStreamMetricsExporter, ExportHistogramPointData)
"\n counts : [200, 300, 400, 500, ]"
"\n attributes\t\t: "
"\n\ta1: b1"
"\n}\n";
"\n resources\t:"
"\n\tservice.name: unknown_service"
"\n\ttelemetry.sdk.language: cpp"
"\n\ttelemetry.sdk.name: opentelemetry"
"\n\ttelemetry.sdk.version: ";
expected_output += OPENTELEMETRY_SDK_VERSION;
expected_output += "\n}\n";
ASSERT_EQ(stdoutOutput.str(), expected_output);
}

Expand Down Expand Up @@ -220,7 +233,13 @@ TEST(OStreamMetricsExporter, ExportLastValuePointData)
"\n valid : true"
"\n value : 20"
"\n attributes\t\t: "
"\n}\n";
"\n resources\t:"
"\n\tservice.name: unknown_service"
"\n\ttelemetry.sdk.language: cpp"
"\n\ttelemetry.sdk.name: opentelemetry"
"\n\ttelemetry.sdk.version: ";
expected_output += OPENTELEMETRY_SDK_VERSION;
expected_output += "\n}\n";
ASSERT_EQ(stdoutOutput.str(), expected_output);
}

Expand Down Expand Up @@ -267,7 +286,13 @@ TEST(OStreamMetricsExporter, ExportDropPointData)
"\n name\t\t: library_name"
"\n description\t: description"
"\n unit\t\t: unit"
"\n}\n";
"\n resources\t:"
"\n\tservice.name: unknown_service"
"\n\ttelemetry.sdk.language: cpp"
"\n\ttelemetry.sdk.name: opentelemetry"
"\n\ttelemetry.sdk.version: ";
expected_output += OPENTELEMETRY_SDK_VERSION;
expected_output += "\n}\n";

ASSERT_EQ(stdoutOutput.str(), expected_output);
}
Expand Down

0 comments on commit 6753e6f

Please sign in to comment.