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

OTLP gRPC Metrics Exporter #1454

Merged
merged 24 commits into from
Jun 23, 2022
Merged
Show file tree
Hide file tree
Changes from 15 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
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
17 changes: 16 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
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,75 @@
// 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 OtlpGrpcExporter using all default options.
lalitb marked this conversation as resolved.
Show resolved Hide resolved
*/
OtlpGrpcMetricsExporter();

/**
* Create an OtlpGrpcExporter 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(0)) noexcept override;
lalitb marked this conversation as resolved.
Show resolved Hide resolved

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 OtlpGrpcExporter 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,50 @@
// 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 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
lalitb marked this conversation as resolved.
Show resolved Hide resolved
*/
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