Skip to content

Commit

Permalink
Merge branch 'otel-grpc-conf' of github.com:lalitb/opentelemetry-cpp …
Browse files Browse the repository at this point in the history
…into otel-grpc-conf
  • Loading branch information
lalitb committed Aug 8, 2022
2 parents 981d1a4 + 93a087a commit aa07289
Show file tree
Hide file tree
Showing 35 changed files with 398 additions and 66 deletions.
6 changes: 3 additions & 3 deletions bazel/repository.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ def opentelemetry_cpp_deps():
http_archive,
name = "com_github_opentelemetry_proto",
build_file = "@io_opentelemetry_cpp//bazel:opentelemetry_proto.BUILD",
sha256 = "f269fbcb30e17b03caa1decd231ce826e59d7651c0f71c3b28eb5140b4bb5412",
strip_prefix = "opentelemetry-proto-0.17.0",
sha256 = "134ce87f0a623daac19b9507b92da0d9b82929e3db796bba631e422f6ea8d3b3",
strip_prefix = "opentelemetry-proto-0.18.0",
urls = [
"https://github.com/open-telemetry/opentelemetry-proto/archive/v0.17.0.tar.gz",
"https://github.com/open-telemetry/opentelemetry-proto/archive/v0.18.0.tar.gz",
],
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,24 @@ class OStreamMetricExporter final : public opentelemetry::sdk::metrics::MetricEx
* export() function will send metrics data into.
* The default ostream is set to stdout
*/
explicit OStreamMetricExporter(std::ostream &sout = std::cout) noexcept;
explicit OStreamMetricExporter(std::ostream &sout = std::cout,
sdk::metrics::AggregationTemporality aggregation_temporality =
sdk::metrics::AggregationTemporality::kCumulative) noexcept;

/**
* Export
* @param data metrics data
*/
sdk::common::ExportResult Export(const sdk::metrics::ResourceMetrics &data) noexcept override;

/**
* Get the AggregationTemporality for ostream exporter
*
* @return AggregationTemporality
*/
sdk::metrics::AggregationTemporality GetAggregationTemporality(
sdk::metrics::InstrumentType instrument_type) const noexcept override;

/**
* Force flush the exporter.
*/
Expand All @@ -55,10 +65,15 @@ class OStreamMetricExporter final : public opentelemetry::sdk::metrics::MetricEx
std::ostream &sout_;
bool is_shutdown_ = false;
mutable opentelemetry::common::SpinLockMutex lock_;
sdk::metrics::AggregationTemporality aggregation_temporality_;
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
66 changes: 63 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 <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 @@ -64,7 +65,17 @@ inline void printVec(std::ostream &os, Container &vec)
os << ']';
}

OStreamMetricExporter::OStreamMetricExporter(std::ostream &sout) noexcept : sout_(sout) {}
OStreamMetricExporter::OStreamMetricExporter(
std::ostream &sout,
sdk::metrics::AggregationTemporality aggregation_temporality) noexcept
: sout_(sout), aggregation_temporality_(aggregation_temporality)
{}

sdk::metrics::AggregationTemporality OStreamMetricExporter::GetAggregationTemporality(
sdk::metrics::InstrumentType instrument_type) const noexcept
{
return aggregation_temporality_;
}

sdk::common::ExportResult OStreamMetricExporter::Export(
const sdk::metrics::ResourceMetrics &data) noexcept
Expand All @@ -79,13 +90,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 +146,9 @@ void OStreamMetricExporter::printInstrumentationInfoMetricData(
printPointAttributes(pd.attributes);
}
}

sout_ << "\n resources\t:";
printResources(*data.resource_);
}
sout_ << "\n}\n";
}
Expand Down Expand Up @@ -144,6 +184,26 @@ void OStreamMetricExporter::printPointData(const opentelemetry::sdk::metrics::Po
sout_ << nostd::get<long>(histogram_point_data.sum_);
}

if (histogram_point_data.record_min_max_)
{
if (nostd::holds_alternative<long>(histogram_point_data.min_))
{
sout_ << "\n min : " << nostd::get<long>(histogram_point_data.min_);
}
else if (nostd::holds_alternative<double>(histogram_point_data.min_))
{
sout_ << "\n min : " << nostd::get<double>(histogram_point_data.min_);
}
if (nostd::holds_alternative<long>(histogram_point_data.max_))
{
sout_ << "\n max : " << nostd::get<long>(histogram_point_data.max_);
}
if (nostd::holds_alternative<double>(histogram_point_data.max_))
{
sout_ << "\n max : " << nostd::get<double>(histogram_point_data.max_);
}
}

sout_ << "\n buckets : ";
if (nostd::holds_alternative<std::list<double>>(histogram_point_data.boundaries_))
{
Expand Down
39 changes: 35 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 All @@ -95,6 +102,8 @@ TEST(OStreamMetricsExporter, ExportHistogramPointData)
histogram_point_data.count_ = 3;
histogram_point_data.counts_ = {200, 300, 400, 500};
histogram_point_data.sum_ = 900.5;
histogram_point_data.min_ = 1.8;
histogram_point_data.max_ = 12.0;
metric_sdk::HistogramPointData histogram_point_data2{};
histogram_point_data2.boundaries_ = std::list<long>{10, 20, 30};
histogram_point_data2.count_ = 3;
Expand Down Expand Up @@ -139,6 +148,8 @@ TEST(OStreamMetricsExporter, ExportHistogramPointData)
"\n type : HistogramPointData"
"\n count : 3"
"\n sum : 900.5"
"\n min : 1.8"
"\n max : 12"
"\n buckets : [10.1, 20.2, 30.2, ]"
"\n counts : [200, 300, 400, 500, ]"
"\n attributes\t\t: "
Expand All @@ -147,11 +158,19 @@ TEST(OStreamMetricsExporter, ExportHistogramPointData)
"\n type : HistogramPointData"
"\n count : 3"
"\n sum : 900"
"\n min : 0"
"\n max : 0"
"\n buckets : [10, 20, 30, ]"
"\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 @@ -214,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 @@ -261,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
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ class OtlpGrpcMetricExporter : public opentelemetry::sdk::metrics::MetricExporte
*/
explicit OtlpGrpcMetricExporter(const OtlpGrpcMetricExporterOptions &options);

/**
* Get the AggregationTemporality for exporter
*
* @return AggregationTemporality
*/
sdk::metrics::AggregationTemporality GetAggregationTemporality(
sdk::metrics::InstrumentType instrument_type) const noexcept override;

opentelemetry::sdk::common::ExportResult Export(
const opentelemetry::sdk::metrics::ResourceMetrics &data) noexcept override;

Expand All @@ -75,6 +83,9 @@ class OtlpGrpcMetricExporter : public opentelemetry::sdk::metrics::MetricExporte
// The configuration options associated with this exporter.
const OtlpGrpcMetricExporterOptions options_;

// Aggregation Temporality selector
const sdk::metrics::AggregationTemporalitySelector aggregation_temporality_selector_;

// For testing
friend class OtlpGrpcExporterTestPeer;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ namespace otlp
*/
struct OtlpGrpcMetricExporterOptions : public OtlpGrpcExporterOptions
{
opentelemetry::sdk::metrics::AggregationTemporality aggregation_temporality =
opentelemetry::sdk::metrics::AggregationTemporality::kDelta;

// Preferred Aggregation Temporality
sdk::metrics::AggregationTemporality aggregation_temporality =
sdk::metrics::AggregationTemporality::kCumulative;
};

} // namespace otlp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# include "opentelemetry/exporters/otlp/otlp_http_client.h"

# include "opentelemetry/exporters/otlp/otlp_environment.h"
# include "opentelemetry/exporters/otlp/otlp_metric_utils.h"

# include <chrono>
# include <cstddef>
Expand Down Expand Up @@ -52,6 +53,10 @@ struct OtlpHttpMetricExporterOptions
// Additional HTTP headers
OtlpHeaders http_headers = GetOtlpDefaultMetricsHeaders();

// Preferred Aggregation Temporality
sdk::metrics::AggregationTemporality aggregation_temporality =
sdk::metrics::AggregationTemporality::kCumulative;

# ifdef ENABLE_ASYNC_EXPORT
// Concurrent requests
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/otlp.md#otlpgrpc-concurrent-requests
Expand Down Expand Up @@ -79,6 +84,14 @@ class OtlpHttpMetricExporter final : public opentelemetry::sdk::metrics::MetricE
*/
OtlpHttpMetricExporter(const OtlpHttpMetricExporterOptions &options);

/**
* Get the AggregationTemporality for exporter
*
* @return AggregationTemporality
*/
sdk::metrics::AggregationTemporality GetAggregationTemporality(
sdk::metrics::InstrumentType instrument_type) const noexcept override;

opentelemetry::sdk::common::ExportResult Export(
const opentelemetry::sdk::metrics::ResourceMetrics &data) noexcept override;

Expand All @@ -95,6 +108,9 @@ class OtlpHttpMetricExporter final : public opentelemetry::sdk::metrics::MetricE
// Configuration options for the exporter
const OtlpHttpMetricExporterOptions options_;

// Aggregation Temporality Selector
const sdk::metrics::AggregationTemporalitySelector aggregation_temporality_selector_;

// Object that stores the HTTP sessions that have been created
std::unique_ptr<OtlpHttpClient> http_client_;
// For testing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ class OtlpMetricUtils
static void PopulateRequest(
const opentelemetry::sdk::metrics::ResourceMetrics &data,
proto::collector::metrics::v1::ExportMetricsServiceRequest *request) noexcept;

static sdk::metrics::AggregationTemporalitySelector ChooseTemporalitySelector(
sdk::metrics::AggregationTemporality preferred_aggregation_temporality) noexcept;
static sdk::metrics::AggregationTemporality DeltaTemporalitySelector(
sdk::metrics::InstrumentType instrument_type) noexcept;
static sdk::metrics::AggregationTemporality CumulativeTemporalitySelector(
sdk::metrics::InstrumentType instrument_type) noexcept;
};

} // namespace otlp
Expand Down
16 changes: 14 additions & 2 deletions exporters/otlp/src/otlp_grpc_metric_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,28 @@ OtlpGrpcMetricExporter::OtlpGrpcMetricExporter()
{}

OtlpGrpcMetricExporter::OtlpGrpcMetricExporter(const OtlpGrpcMetricExporterOptions &options)
: options_(options), metrics_service_stub_(MakeMetricsServiceStub(options))
: options_(options),
aggregation_temporality_selector_{
OtlpMetricUtils::ChooseTemporalitySelector(options_.aggregation_temporality)},
metrics_service_stub_(MakeMetricsServiceStub(options))
{}

OtlpGrpcMetricExporter::OtlpGrpcMetricExporter(
std::unique_ptr<proto::collector::metrics::v1::MetricsService::StubInterface> stub)
: options_(OtlpGrpcMetricExporterOptions()), metrics_service_stub_(std::move(stub))
: options_(OtlpGrpcMetricExporterOptions()),
aggregation_temporality_selector_{
OtlpMetricUtils::ChooseTemporalitySelector(options_.aggregation_temporality)},
metrics_service_stub_(std::move(stub))
{}

// ----------------------------- Exporter methods ------------------------------

sdk::metrics::AggregationTemporality OtlpGrpcMetricExporter::GetAggregationTemporality(
sdk::metrics::InstrumentType instrument_type) const noexcept
{
return aggregation_temporality_selector_(instrument_type);
}

opentelemetry::sdk::common::ExportResult OtlpGrpcMetricExporter::Export(
const opentelemetry::sdk::metrics::ResourceMetrics &data) noexcept
{
Expand Down
Loading

0 comments on commit aa07289

Please sign in to comment.