Skip to content

Commit

Permalink
OTLP gRPC Metrics Exporter (#1454)
Browse files Browse the repository at this point in the history
  • Loading branch information
lalitb committed Jun 23, 2022
1 parent 5ccca49 commit b285874
Show file tree
Hide file tree
Showing 20 changed files with 982 additions and 268 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ class OStreamMetricExporter final : public opentelemetry::sdk::metrics::MetricEx

/**
* Shut down the exporter.
* @param timeout an optional timeout, the default timeout of 0 means that no
* timeout is applied.
* @param timeout an optional timeout.
* @return return the status of this operation
*/
bool Shutdown(std::chrono::microseconds timeout = std::chrono::microseconds(0)) noexcept override;
bool Shutdown(
std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept override;

private:
std::ostream &sout_;
Expand Down
12 changes: 8 additions & 4 deletions exporters/ostream/test/ostream_metric_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ TEST(OStreamMetricsExporter, ExportSumPointData)
metric_sdk::InstrumentDescriptor{"library_name", "description", "unit",
metric_sdk::InstrumentType::kCounter,
metric_sdk::InstrumentValueType::kDouble},
opentelemetry::common::SystemTimestamp{}, opentelemetry::common::SystemTimestamp{},
metric_sdk::AggregationTemporality::kDelta, opentelemetry::common::SystemTimestamp{},
opentelemetry::common::SystemTimestamp{},
std::vector<metric_sdk::PointDataAttributes>{
{metric_sdk::PointAttributes{{"a1", "b1"}}, sum_point_data},
{metric_sdk::PointAttributes{{"a1", "b1"}}, sum_point_data2}}};
Expand Down Expand Up @@ -111,7 +112,8 @@ TEST(OStreamMetricsExporter, ExportHistogramPointData)
metric_sdk::InstrumentDescriptor{"library_name", "description", "unit",
metric_sdk::InstrumentType::kCounter,
metric_sdk::InstrumentValueType::kDouble},
opentelemetry::common::SystemTimestamp{}, opentelemetry::common::SystemTimestamp{},
metric_sdk::AggregationTemporality::kDelta, opentelemetry::common::SystemTimestamp{},
opentelemetry::common::SystemTimestamp{},
std::vector<metric_sdk::PointDataAttributes>{
{metric_sdk::PointAttributes{{"a1", "b1"}, {"a2", "b2"}}, histogram_point_data},
{metric_sdk::PointAttributes{{"a1", "b1"}}, histogram_point_data2}}};
Expand Down Expand Up @@ -179,7 +181,8 @@ TEST(OStreamMetricsExporter, ExportLastValuePointData)
metric_sdk::InstrumentDescriptor{"library_name", "description", "unit",
metric_sdk::InstrumentType::kCounter,
metric_sdk::InstrumentValueType::kDouble},
opentelemetry::common::SystemTimestamp{}, opentelemetry::common::SystemTimestamp{},
metric_sdk::AggregationTemporality::kDelta, opentelemetry::common::SystemTimestamp{},
opentelemetry::common::SystemTimestamp{},
std::vector<metric_sdk::PointDataAttributes>{
{metric_sdk::PointAttributes{}, last_value_point_data},
{metric_sdk::PointAttributes{}, last_value_point_data2}}};
Expand Down Expand Up @@ -236,7 +239,8 @@ TEST(OStreamMetricsExporter, ExportDropPointData)
metric_sdk::InstrumentDescriptor{"library_name", "description", "unit",
metric_sdk::InstrumentType::kCounter,
metric_sdk::InstrumentValueType::kDouble},
opentelemetry::common::SystemTimestamp{}, opentelemetry::common::SystemTimestamp{},
metric_sdk::AggregationTemporality::kDelta, opentelemetry::common::SystemTimestamp{},
opentelemetry::common::SystemTimestamp{},
std::vector<metric_sdk::PointDataAttributes>{
{metric_sdk::PointAttributes{}, drop_point_data},
{metric_sdk::PointAttributes{}, drop_point_data2}}};
Expand Down
2 changes: 2 additions & 0 deletions exporters/otlp/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ cc_library(
name = "otlp_recordable",
srcs = [
"src/otlp_log_recordable.cc",
"src/otlp_populate_attribute_utils.cc",
"src/otlp_recordable.cc",
"src/otlp_recordable_utils.cc",
],
hdrs = [
"include/opentelemetry/exporters/otlp/otlp_log_recordable.h",
"include/opentelemetry/exporters/otlp/otlp_populate_attribute_utils.h",
"include/opentelemetry/exporters/otlp/otlp_recordable.h",
"include/opentelemetry/exporters/otlp/otlp_recordable_utils.h",
"include/opentelemetry/exporters/otlp/protobuf_include_prefix.h",
Expand Down
29 changes: 28 additions & 1 deletion exporters/otlp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
add_library(
opentelemetry_otlp_recordable
src/otlp_log_recordable.cc src/otlp_recordable.cc
src/otlp_recordable_utils.cc)
src/otlp_populate_attribute_utils.cc src/otlp_recordable_utils.cc
src/otlp_metrics_utils.cc)
set_target_properties(opentelemetry_otlp_recordable PROPERTIES EXPORT_NAME
otlp_recordable)

Expand Down Expand Up @@ -37,6 +38,20 @@ if(WITH_OTLP_GRPC)
PUBLIC opentelemetry_otlp_recordable gRPC::grpc++)

list(APPEND OPENTELEMETRY_OTLP_TARGETS opentelemetry_exporter_otlp_grpc_log)

if(NOT WITH_METRICS_PREVIEW)
add_library(opentelemetry_exporter_otlp_grpc_metrics
src/otlp_grpc_metric_exporter.cc)

set_target_properties(opentelemetry_exporter_otlp_grpc_metrics
PROPERTIES EXPORT_NAME otlp_grpc_metrics_exporter)

target_link_libraries(opentelemetry_exporter_otlp_grpc_metrics
PUBLIC opentelemetry_otlp_recordable gRPC::grpc++)

list(APPEND OPENTELEMETRY_OTLP_TARGETS
opentelemetry_exporter_otlp_grpc_metrics)
endif()
endif()

if(WITH_OTLP_HTTP)
Expand Down Expand Up @@ -122,6 +137,18 @@ if(BUILD_TESTING)
TEST_LIST otlp_log_recordable_test)
endif()

if(NOT WITH_METRICS_PREVIEW)
add_executable(otlp_metrics_serialization_test
test/otlp_metrics_serialization_test.cc)
target_link_libraries(
otlp_metrics_serialization_test ${GTEST_BOTH_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT} opentelemetry_otlp_recordable)
gtest_add_tests(
TARGET otlp_metrics_serialization_test
TEST_PREFIX exporter.otlp.
TEST_LIST otlp_metrics_serialization_test)
endif()

if(MSVC)
# Explicitly specify that we consume GTest from shared library. The rest of
# code logic below determines whether we link Release or Debug flavor of the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,6 @@ inline OtlpHeaders GetOtlpDefaultLogHeaders()

return result;
}

} // namespace otlp
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once
#ifndef ENABLE_METRICS_PREVIEW

// clang-format off

# include "opentelemetry/exporters/otlp/protobuf_include_prefix.h"
# include "opentelemetry/proto/collector/metrics/v1/metrics_service.grpc.pb.h"
# include "opentelemetry/common/spin_lock_mutex.h"
# include "opentelemetry/exporters/otlp/protobuf_include_suffix.h"

// clang-format on

# include "opentelemetry/exporters/otlp/otlp_environment.h"
# include "opentelemetry/exporters/otlp/otlp_grpc_metric_exporter_options.h"
# include "opentelemetry/sdk/metrics/metric_exporter.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace exporter
{
namespace otlp
{

/**
* The OTLP exporter exports metrics data in OpenTelemetry Protocol (OTLP) format in gRPC.
*/
class OtlpGrpcMetricsExporter : public opentelemetry::sdk::metrics::MetricExporter
{
public:
/**
* Create an OtlpGrpcMetricsExporter using all default options.
*/
OtlpGrpcMetricsExporter();

/**
* Create an OtlpGrpcMetricsExporter using the given options.
*/
explicit OtlpGrpcMetricsExporter(const OtlpGrpcMetricsExporterOptions &options);

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

bool ForceFlush(
std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept override;

bool Shutdown(
std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept override;

private:
// The configuration options associated with this exporter.
const OtlpGrpcMetricsExporterOptions options_;

// For testing
friend class OtlpGrpcExporterTestPeer;

// Store service stub internally. Useful for testing.
std::unique_ptr<proto::collector::metrics::v1::MetricsService::StubInterface>
metrics_service_stub_;

/**
* Create an OtlpGrpcMetricsExporter using the specified service stub.
* Only tests can call this constructor directly.
* @param stub the service stub to be used for exporting
*/
OtlpGrpcMetricsExporter(
std::unique_ptr<proto::collector::metrics::v1::MetricsService::StubInterface> stub);
bool is_shutdown_ = false;
mutable opentelemetry::common::SpinLockMutex lock_;
bool isShutdown() const noexcept;
};
} // namespace otlp
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include "opentelemetry/exporters/otlp/otlp_grpc_exporter_options.h"
#include "opentelemetry/sdk/metrics/instruments.h"

#include <memory>

OPENTELEMETRY_BEGIN_NAMESPACE
namespace exporter
{
namespace otlp
{

/**
* Struct to hold OTLP metrics exporter options.
*/
struct OtlpGrpcMetricsExporterOptions : public OtlpGrpcExporterOptions
{
opentelemetry::sdk::metrics::AggregationTemporality aggregation_temporality =
opentelemetry::sdk::metrics::AggregationTemporality::kDelta;
};

} // namespace otlp
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include "opentelemetry/exporters/otlp/protobuf_include_prefix.h"

#include "opentelemetry/proto/collector/metrics/v1/metrics_service.pb.h"
#include "opentelemetry/proto/metrics/v1/metrics.pb.h"
#include "opentelemetry/proto/resource/v1/resource.pb.h"

#include "opentelemetry/exporters/otlp/protobuf_include_suffix.h"

#include "opentelemetry/sdk/metrics/export/metric_producer.h"

#ifndef ENABLE_METRICS_PREVIEW

OPENTELEMETRY_BEGIN_NAMESPACE
namespace exporter
{
namespace otlp
{
/**
* The OtlpMetricsUtils contains utility functions for OTLP metrics
*/
class OtlpMetricsUtils
{
public:
static opentelemetry::sdk::metrics::AggregationType GetAggregationType(
const opentelemetry::sdk::metrics::InstrumentType &instrument_type) noexcept;

static proto::metrics::v1::AggregationTemporality GetProtoAggregationTemporality(
const opentelemetry::sdk::metrics::AggregationTemporality &aggregation_temporality) noexcept;

static void ConvertSumMetric(const opentelemetry::sdk::metrics::MetricData &metric_data,
proto::metrics::v1::Sum *const sum) noexcept;

static void ConvertHistogramMetric(const opentelemetry::sdk::metrics::MetricData &metric_data,
proto::metrics::v1::Histogram *const histogram) noexcept;

static void PopulateInstrumentationInfoMetric(
const opentelemetry::sdk::metrics::MetricData &metric_data,
proto::metrics::v1::Metric *metric) noexcept;

static void PopulateResourceMetrics(
const opentelemetry::sdk::metrics::ResourceMetrics &data,
proto::metrics::v1::ResourceMetrics *proto_resource_metrics) noexcept;

static void PopulateRequest(
const opentelemetry::sdk::metrics::ResourceMetrics &data,
proto::collector::metrics::v1::ExportMetricsServiceRequest *request) noexcept;
};

} // namespace otlp
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include "opentelemetry/exporters/otlp/protobuf_include_prefix.h"
#include "opentelemetry/proto/resource/v1/resource.pb.h"

#include "opentelemetry/common/attribute_value.h"
#include "opentelemetry/nostd/string_view.h"
#include "opentelemetry/sdk/common/attribute_utils.h"
#include "opentelemetry/sdk/resource/resource.h"
#include "opentelemetry/version.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace exporter
{
namespace otlp
{
/**
* The OtlpCommoneUtils contains utility functions to populate attributes
*/
class OtlpPopulateAttributeUtils
{

public:
static void PopulateAttribute(opentelemetry::proto::resource::v1::Resource *proto,
const opentelemetry::sdk::resource::Resource &resource) noexcept;

static void PopulateAttribute(opentelemetry::proto::common::v1::KeyValue *attribute,
nostd::string_view key,
const opentelemetry::common::AttributeValue &value) noexcept;

static void PopulateAttribute(
opentelemetry::proto::common::v1::KeyValue *attribute,
nostd::string_view key,
const opentelemetry::sdk::common::OwnedAttributeValue &value) noexcept;
};

} // namespace otlp
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,9 @@

#include "opentelemetry/proto/collector/logs/v1/logs_service.pb.h"
#include "opentelemetry/proto/collector/trace/v1/trace_service.pb.h"
#include "opentelemetry/proto/resource/v1/resource.pb.h"

#include "opentelemetry/exporters/otlp/protobuf_include_suffix.h"

#include "opentelemetry/common/attribute_value.h"
#include "opentelemetry/nostd/string_view.h"
#include "opentelemetry/version.h"

#include "opentelemetry/sdk/common/attribute_utils.h"
#include "opentelemetry/sdk/resource/resource.h"
#include "opentelemetry/sdk/trace/recordable.h"

#ifdef ENABLE_LOGS_PREVIEW
Expand All @@ -36,18 +29,6 @@ namespace otlp
class OtlpRecordableUtils
{
public:
static void PopulateAttribute(opentelemetry::proto::common::v1::KeyValue *attribute,
nostd::string_view key,
const opentelemetry::common::AttributeValue &value) noexcept;

static void PopulateAttribute(
opentelemetry::proto::common::v1::KeyValue *attribute,
nostd::string_view key,
const opentelemetry::sdk::common::OwnedAttributeValue &value) noexcept;

static void PopulateAttribute(opentelemetry::proto::resource::v1::Resource *proto,
const opentelemetry::sdk::resource::Resource &resource) noexcept;

static void PopulateRequest(
const nostd::span<std::unique_ptr<opentelemetry::sdk::trace::Recordable>> &spans,
proto::collector::trace::v1::ExportTraceServiceRequest *request) noexcept;
Expand Down
Loading

1 comment on commit b285874

@github-actions
Copy link

Choose a reason for hiding this comment

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

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'OpenTelemetry-cpp sdk Benchmark'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: b285874 Previous: 5ccca49 Ratio
BM_BaselineBuffer/4 12559914.588928223 ns/iter 5863099.098205566 ns/iter 2.14
BM_AlwaysOffSamplerConstruction 3.4375936091421644 ns/iter 1.627969129271147 ns/iter 2.11
BM_AlwaysOnSamplerConstruction 3.38044651624186 ns/iter 1.6460540320517012 ns/iter 2.05

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.