Skip to content

Commit

Permalink
[Metrics] Add OTLP http metric exporter (open-telemetry#1487)
Browse files Browse the repository at this point in the history
* Add OTLP http metric exporter

Signed-off-by: owentou <owentou@tencent.com>

* Add changelog for OTLP HTTP metric exporter

Signed-off-by: owentou <owentou@tencent.com>

* Add unit test of `SumPointData` for `OtlpHttpMetricExporter`

Signed-off-by: owentou <owentou@tencent.com>

* Fix dependency of otlp recordable, add `Gauge` support for `OtlpMetricUtils::PopulateRequest`

Signed-off-by: owentou <owentou@tencent.com>

* Add unit test for `OtlpHttpMetricExporter`

Signed-off-by: owentou <owentou@tencent.com>

* Fix format

Signed-off-by: owentou <owentou@tencent.com>

* Fix unit test `OtlpMetricSerializationTest.ObservableGauge`

Signed-off-by: owentou <owentou@tencent.com>
  • Loading branch information
owent authored and yxue committed Dec 5, 2022
1 parent 9b51749 commit 39d04e6
Show file tree
Hide file tree
Showing 12 changed files with 775 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,18 @@ update the semantic convention in instrumentation library is needed.
* [BUILD] Remove `--config Debug` when installing. [#1480](https://github.com/open-telemetry/opentelemetry-cpp/pull/1480)
* [EXPORTER] Fix endpoint in otlp grpc exporter [#1474](https://github.com/open-telemetry/opentelemetry-cpp/pull/1474)
* [EXAMPLE] Fix memory ownership of InMemorySpanExporter (#1473) [#1471](https://github.com/open-telemetry/opentelemetry-cpp/pull/1471)
<<<<<<< HEAD
* [EXPORTER TESTS] Prometheus unit test [#1461](https://github.com/open-telemetry/opentelemetry-cpp/pull/1461)
* [DOCS] Update docs to use relative code links. [#1447](https://github.com/open-telemetry/opentelemetry-cpp/pull/1447)
* [SDK] Remove reference to deprecated InstrumentationLibrary in OTLP [#1469](https://github.com/open-telemetry/opentelemetry-cpp/pull/1469)
* [SDK] Fix trace kIsSampled flag set incorrectly [#1465](https://github.com/open-telemetry/opentelemetry-cpp/pull/1465)
* [METRICS EXPORTER] OTLP gRPC Metrics Exporter [#1454](https://github.com/open-telemetry/opentelemetry-cpp/pull/1454)
* [EXPORTER] fix prometheus exporter failure type [#1460](https://github.com/open-telemetry/opentelemetry-cpp/pull/1460)
* [SDK] Fix build warnings about missing aggregates. [#1368](https://github.com/open-telemetry/opentelemetry-cpp/pull/1368)
=======
* [EXPORTER] Add metrics OTLP/HTTP exporter [#1487](https://github.com/open-telemetry/opentelemetry-cpp/pull/1487)
* [EXPORTER] OTLP http exporter allow concurrency session ([#1209](https://github.com/open-telemetry/opentelemetry-cpp/pull/1209))
>>>>>>> 8d1f241d ([Metrics] Add OTLP http metric exporter (#1487))
* [EXT] `curl::HttpClient` use `curl_multi_handle` instead of creating a thread
for every request and it's able to reuse connections now. ([#1317](https://github.com/open-telemetry/opentelemetry-cpp/pull/1317))
<<<<<<< HEAD
Expand Down
12 changes: 12 additions & 0 deletions cmake/opentelemetry-cpp-config.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@
# opentelemetry-cpp::otlp_recordable - Imported target of opentelemetry-cpp::otlp_recordable
# opentelemetry-cpp::otlp_grpc_exporter - Imported target of opentelemetry-cpp::otlp_grpc_exporter
# opentelemetry-cpp::otlp_grpc_log_exporter - Imported target of opentelemetry-cpp::otlp_grpc_log_exporter
# opentelemetry-cpp::otlp_grpc_metrics_exporter - Imported target of opentelemetry-cpp::otlp_grpc_metrics_exporter
# opentelemetry-cpp::otlp_http_client - Imported target of opentelemetry-cpp::otlp_http_client
# opentelemetry-cpp::otlp_http_exporter - Imported target of opentelemetry-cpp::otlp_http_exporter
# opentelemetry-cpp::otlp_http_log_exporter - Imported target of opentelemetry-cpp::otlp_http_log_exporter
# opentelemetry-cpp::otlp_http_metric_exporter - Imported target of opentelemetry-cpp::otlp_http_metric_exporter
# opentelemetry-cpp::ostream_log_exporter - Imported target of opentelemetry-cpp::ostream_log_exporter
# opentelemetry-cpp::ostream_metrics_exporter - Imported target of opentelemetry-cpp::ostream_metrics_exporter
# opentelemetry-cpp::ostream_span_exporter - Imported target of opentelemetry-cpp::ostream_span_exporter
Expand Down Expand Up @@ -116,13 +118,23 @@ set(_OPENTELEMETRY_CPP_LIBRARIES_TEST_TARGETS
otlp_recordable
otlp_grpc_client
otlp_grpc_exporter
<<<<<<< HEAD
otlp_grpc_log_record_exporter
otlp_grpc_metrics_exporter
otlp_http_client
otlp_http_exporter
otlp_http_log_record_exporter
otlp_http_metric_exporter
ostream_log_record_exporter
=======
otlp_grpc_log_exporter
otlp_grpc_metrics_exporter
otlp_http_client
otlp_http_exporter
otlp_http_log_exporter
otlp_http_metric_exporter
ostream_log_exporter
>>>>>>> 8d1f241d ([Metrics] Add OTLP http metric exporter (#1487))
ostream_metrics_exporter
ostream_span_exporter
prometheus_exporter
Expand Down
47 changes: 47 additions & 0 deletions exporters/otlp/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,35 @@ cc_library(
)

cc_library(
<<<<<<< HEAD
name = "otlp_grpc_log_record_exporter",
=======
name = "otlp_http_metric_exporter",
srcs = [
"src/otlp_http_metric_exporter.cc",
],
hdrs = [
"include/opentelemetry/exporters/otlp/otlp_environment.h",
"include/opentelemetry/exporters/otlp/otlp_http_metric_exporter.h",
"include/opentelemetry/exporters/otlp/protobuf_include_prefix.h",
"include/opentelemetry/exporters/otlp/protobuf_include_suffix.h",
],
strip_include_prefix = "include",
tags = [
"otlp",
"otlp_http_metric",
],
deps = [
":otlp_http_client",
":otlp_recordable",
"//sdk/src/metrics",
"@com_github_opentelemetry_proto//:metrics_service_proto_cc",
],
)

cc_library(
name = "otlp_grpc_log_exporter",
>>>>>>> 8d1f241d ([Metrics] Add OTLP http metric exporter (#1487))
srcs = [
"src/otlp_grpc_log_record_exporter.cc",
"src/otlp_grpc_log_record_exporter_factory.cc",
Expand Down Expand Up @@ -392,6 +420,7 @@ cc_test(
)

cc_test(
<<<<<<< HEAD
name = "otlp_http_log_record_exporter_factory_test",
srcs = ["test/otlp_http_log_record_exporter_factory_test.cc"],
tags = [
Expand All @@ -403,13 +432,31 @@ cc_test(
":otlp_http_log_record_exporter",
"//api",
"//test_common/src/http/client/nosend:http_client_nosend",
=======
name = "otlp_http_metric_exporter_test",
srcs = ["test/otlp_http_metric_exporter_test.cc"],
tags = [
"otlp",
"otlp_http_metric",
"test",
],
deps = [
":otlp_http_metric_exporter",
"//api",
"//ext/src/http/client/nosend:http_client_nosend",
>>>>>>> 8d1f241d ([Metrics] Add OTLP http metric exporter (#1487))
"@com_google_googletest//:gtest_main",
],
)

cc_test(
<<<<<<< HEAD
name = "otlp_grpc_log_record_exporter_test",
srcs = ["test/otlp_grpc_log_record_exporter_test.cc"],
=======
name = "otlp_grpc_log_exporter_test",
srcs = ["test/otlp_grpc_log_exporter_test.cc"],
>>>>>>> 8d1f241d ([Metrics] Add OTLP http metric exporter (#1487))
tags = [
"otlp",
"otlp_grpc_log",
Expand Down
37 changes: 37 additions & 0 deletions exporters/otlp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ if(WITH_OTLP_HTTP)
list(APPEND OPENTELEMETRY_OTLP_TARGETS opentelemetry_exporter_otlp_http_log)

endif()
<<<<<<< HEAD
add_library(
opentelemetry_exporter_otlp_http_metric
src/otlp_http_metric_exporter.cc src/otlp_http_metric_exporter_factory.cc)
Expand All @@ -158,6 +159,23 @@ if(WITH_OTLP_HTTP)

list(APPEND OPENTELEMETRY_OTLP_TARGETS
opentelemetry_exporter_otlp_http_metric)
=======
if(NOT WITH_METRICS_PREVIEW)
add_library(opentelemetry_exporter_otlp_http_metric
src/otlp_http_metric_exporter.cc)

set_target_properties(opentelemetry_exporter_otlp_http_metric
PROPERTIES EXPORT_NAME otlp_http_metric_exporter)

target_link_libraries(
opentelemetry_exporter_otlp_http_metric
PUBLIC opentelemetry_otlp_recordable
opentelemetry_exporter_otlp_http_client)

list(APPEND OPENTELEMETRY_OTLP_TARGETS
opentelemetry_exporter_otlp_http_metric)
endif()
>>>>>>> 8d1f241d ([Metrics] Add OTLP http metric exporter (#1487))
endif()

install(
Expand Down Expand Up @@ -326,6 +344,7 @@ if(BUILD_TESTING)
TEST_LIST otlp_http_log_record_exporter_factory_test)
endif()

<<<<<<< HEAD
add_executable(otlp_http_metric_exporter_test
test/otlp_http_metric_exporter_test.cc)
target_link_libraries(
Expand All @@ -351,5 +370,23 @@ if(BUILD_TESTING)
TARGET otlp_http_metric_exporter_factory_test
TEST_PREFIX exporter.otlp.
TEST_LIST otlp_http_metric_exporter_factory_test)
=======
if(NOT WITH_METRICS_PREVIEW)
add_executable(otlp_http_metric_exporter_test
test/otlp_http_metric_exporter_test.cc)
target_link_libraries(
otlp_http_metric_exporter_test
${GTEST_BOTH_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT}
${GMOCK_LIB}
opentelemetry_exporter_otlp_http_metric
opentelemetry_metrics
http_client_nosend)
gtest_add_tests(
TARGET otlp_http_metric_exporter_test
TEST_PREFIX exporter.otlp.
TEST_LIST otlp_http_metric_exporter_test)
endif()
>>>>>>> 8d1f241d ([Metrics] Add OTLP http metric exporter (#1487))
endif()
endif() # BUILD_TESTING
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,12 @@ inline OtlpHeaders GetOtlpDefaultLogHeaders()
return result;
}

<<<<<<< HEAD
// --- Metrics Environment Variables
inline const std::string GetOtlpDefaultMetricsEndpoint()
=======
inline const std::string GetOtlpDefaultHttpMetricEndpoint()
>>>>>>> 8d1f241d ([Metrics] Add OTLP http metric exporter (#1487))
{
constexpr char kOtlpMetricsEndpointEnv[] = "OTEL_EXPORTER_OTLP_METRICS_ENDPOINT";
constexpr char kOtlpEndpointEnv[] = "OTEL_EXPORTER_OTLP_ENDPOINT";
Expand All @@ -292,7 +296,11 @@ inline const std::string GetOtlpDefaultMetricsEndpoint()
return endpoint.size() ? endpoint : kOtlpEndpointDefault;
}

<<<<<<< HEAD
inline const std::chrono::system_clock::duration GetOtlpDefaultMetricsTimeout()
=======
inline const std::chrono::system_clock::duration GetOtlpDefaultMetricTimeout()
>>>>>>> 8d1f241d ([Metrics] Add OTLP http metric exporter (#1487))
{
constexpr char kOtlpMetricsTimeoutEnv[] = "OTEL_EXPORTER_OTLP_METRICS_TIMEOUT";
constexpr char kOtlpTimeoutEnv[] = "OTEL_EXPORTER_OTLP_TIMEOUT";
Expand All @@ -305,7 +313,11 @@ inline const std::chrono::system_clock::duration GetOtlpDefaultMetricsTimeout()
return GetOtlpTimeoutFromString(timeout.c_str());
}

<<<<<<< HEAD
inline OtlpHeaders GetOtlpDefaultMetricsHeaders()
=======
inline OtlpHeaders GetOtlpDefaultMetricHeaders()
>>>>>>> 8d1f241d ([Metrics] Add OTLP http metric exporter (#1487))
{
constexpr char kOtlpMetricsHeadersEnv[] = "OTEL_EXPORTER_OTLP_METRICS_HEADERS";
constexpr char kOtlpHeadersEnv[] = "OTEL_EXPORTER_OTLP_HEADERS";
Expand All @@ -320,6 +332,7 @@ inline OtlpHeaders GetOtlpDefaultMetricsHeaders()
return result;
}

<<<<<<< HEAD
inline bool GetOtlpDefaultMetricsIsSslEnable()
{
constexpr char kOtlpMetricsIsSslEnableEnv[] = "OTEL_EXPORTER_OTLP_METRICS_SSL_ENABLE";
Expand Down Expand Up @@ -364,6 +377,8 @@ inline const std::string GetOtlpDefaultMetricsSslCertificateString()
return ssl_cert.size() ? ssl_cert : "";
}

=======
>>>>>>> 8d1f241d ([Metrics] Add OTLP http metric exporter (#1487))
} // namespace otlp
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,11 @@ class OtlpHttpClient

// For testing
friend class OtlpHttpExporterTestPeer;
<<<<<<< HEAD
friend class OtlpHttpLogRecordExporterTestPeer;
=======
friend class OtlpHttpLogExporterTestPeer;
>>>>>>> 8d1f241d ([Metrics] Add OTLP http metric exporter (#1487))
friend class OtlpHttpMetricExporterTestPeer;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,62 @@ struct OtlpHttpLogRecordExporterOptions
# endif
};

<<<<<<< HEAD:exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_record_exporter_options.h
=======
/**
* The OTLP exporter exports log data in OpenTelemetry Protocol (OTLP) format.
*/
class OtlpHttpLogExporter final : public opentelemetry::sdk::logs::LogExporter
{
public:
/**
* Create an OtlpHttpLogExporter with default exporter options.
*/
OtlpHttpLogExporter();

/**
* Create an OtlpHttpLogExporter with user specified options.
* @param options An object containing the user's configuration options.
*/
OtlpHttpLogExporter(const OtlpHttpLogExporterOptions &options);

/**
* Creates a recordable that stores the data in a JSON object
*/
std::unique_ptr<opentelemetry::sdk::logs::Recordable> MakeRecordable() noexcept override;

/**
* Exports a vector of log records to the Elasticsearch instance. Guaranteed to return after a
* timeout specified from the options passed from the constructor.
* @param records A list of log records to send to Elasticsearch.
*/
opentelemetry::sdk::common::ExportResult Export(
const nostd::span<std::unique_ptr<opentelemetry::sdk::logs::Recordable>> &records) noexcept
override;

/**
* Shutdown this exporter.
* @param timeout The maximum time to wait for the shutdown method to return
*/
bool Shutdown(
std::chrono::microseconds timeout = std::chrono::microseconds::max()) noexcept override;

private:
// Configuration options for the exporter
const OtlpHttpLogExporterOptions options_;

// Object that stores the HTTP sessions that have been created
std::unique_ptr<OtlpHttpClient> http_client_;
// For testing
friend class OtlpHttpLogExporterTestPeer;
/**
* Create an OtlpHttpLogExporter using the specified http client.
* Only tests can call this constructor directly.
* @param http_client the http client to be used for exporting
*/
OtlpHttpLogExporter(std::unique_ptr<OtlpHttpClient> http_client);
};
>>>>>>> 8d1f241d ([Metrics] Add OTLP http metric exporter (#1487)):exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_exporter.h
} // namespace otlp
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE
Expand Down
Loading

0 comments on commit 39d04e6

Please sign in to comment.