From e8d49ca16b77bc166c6419aa26e468be4a2e3e7e Mon Sep 17 00:00:00 2001 From: Lalit Date: Fri, 5 Aug 2022 15:22:41 -0700 Subject: [PATCH 1/4] fix --- .../exporters/otlp/otlp_environment.h | 58 +++++++++++++++++-- .../otlp/otlp_grpc_metric_exporter.h | 25 +++++++- .../otlp/otlp_http_metric_exporter.h | 6 +- .../test/otlp_http_metric_exporter_test.cc | 2 +- 4 files changed, 80 insertions(+), 11 deletions(-) diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_environment.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_environment.h index 26f0fbf241..38ff0ef958 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_environment.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_environment.h @@ -73,7 +73,7 @@ inline bool GetOtlpDefaultIsSslEnable() inline const std::string GetOtlpDefaultSslCertificatePath() { constexpr char kOtlpTracesSslCertificate[] = "OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE"; - constexpr char kOtlpSslCertificate[] = "OTEL_EXPORTER_OTLP_CERTIFICATE "; + constexpr char kOtlpSslCertificate[] = "OTEL_EXPORTER_OTLP_CERTIFICATE"; auto ssl_cert_path = opentelemetry::sdk::common::GetEnvironmentVariable(kOtlpTracesSslCertificate); if (ssl_cert_path.empty()) @@ -85,8 +85,8 @@ inline const std::string GetOtlpDefaultSslCertificatePath() inline const std::string GetOtlpDefaultSslCertificateString() { - constexpr char kOtlpTracesSslCertificateString[] = "OTEL_EXPORTER_OTLP_CERTIFICATE_STRING"; - constexpr char kOtlpSslCertificateString[] = "OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE_STRING "; + constexpr char kOtlpTracesSslCertificateString[] = "OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE_STRING"; + constexpr char kOtlpSslCertificateString[] = "OTEL_EXPORTER_OTLP_CERTIFICATE_STRING"; auto ssl_cert = opentelemetry::sdk::common::GetEnvironmentVariable(kOtlpTracesSslCertificateString); if (ssl_cert.empty()) @@ -267,7 +267,9 @@ inline OtlpHeaders GetOtlpDefaultLogHeaders() return result; } -inline const std::string GetOtlpDefaultHttpMetricEndpoint() +// --- Metrics Environment Variables + +inline const std::string GetOtlpDefaultMetricsEndpoint() { constexpr char kOtlpMetricsEndpointEnv[] = "OTEL_EXPORTER_OTLP_METRICS_ENDPOINT"; constexpr char kOtlpEndpointEnv[] = "OTEL_EXPORTER_OTLP_ENDPOINT"; @@ -285,7 +287,7 @@ inline const std::string GetOtlpDefaultHttpMetricEndpoint() return endpoint.size() ? endpoint : kOtlpEndpointDefault; } -inline const std::chrono::system_clock::duration GetOtlpDefaultMetricTimeout() +inline const std::chrono::system_clock::duration GetOtlpDefaultMetricsTimeout() { constexpr char kOtlpMetricsTimeoutEnv[] = "OTEL_EXPORTER_OTLP_METRICS_TIMEOUT"; constexpr char kOtlpTimeoutEnv[] = "OTEL_EXPORTER_OTLP_TIMEOUT"; @@ -298,7 +300,7 @@ inline const std::chrono::system_clock::duration GetOtlpDefaultMetricTimeout() return GetOtlpTimeoutFromString(timeout.c_str()); } -inline OtlpHeaders GetOtlpDefaultMetricHeaders() +inline OtlpHeaders GetOtlpDefaultMetricsHeaders() { constexpr char kOtlpMetricsHeadersEnv[] = "OTEL_EXPORTER_OTLP_METRICS_HEADERS"; constexpr char kOtlpHeadersEnv[] = "OTEL_EXPORTER_OTLP_HEADERS"; @@ -313,6 +315,50 @@ inline OtlpHeaders GetOtlpDefaultMetricHeaders() return result; } +inline bool GetOtlpDefaultMetricsIsSslEnable() +{ + constexpr char kOtlpMetricsIsSslEnableEnv[] = "OTEL_EXPORTER_OTLP_METRICS_SSL_ENABLE"; + constexpr char kOtlpIsSslEnableEnv[] = "OTEL_EXPORTER_OTLP_SSL_ENABLE"; + + auto ssl_enable = opentelemetry::sdk::common::GetEnvironmentVariable(kOtlpMetricsIsSslEnableEnv); + if (ssl_enable.empty()) + { + ssl_enable = opentelemetry::sdk::common::GetEnvironmentVariable(kOtlpIsSslEnableEnv); + } + if (ssl_enable == "True" || ssl_enable == "TRUE" || ssl_enable == "true" || ssl_enable == "1") + { + return true; + } + return false; +} + +inline const std::string GetOtlpDefaultMetricsSslCertificatePath() +{ + constexpr char kOtlpMetricsSslCertificate[] = "OTEL_EXPORTER_OTLP_METRICS_CERTIFICATE"; + constexpr char kOtlpSslCertificate[] = "OTEL_EXPORTER_OTLP_CERTIFICATE "; + auto ssl_cert_path = + opentelemetry::sdk::common::GetEnvironmentVariable(kOtlpMetricsSslCertificate); + if (ssl_cert_path.empty()) + { + ssl_cert_path = opentelemetry::sdk::common::GetEnvironmentVariable(kOtlpSslCertificate); + } + return ssl_cert_path.size() ? ssl_cert_path : ""; +} + +inline const std::string GetOtlpDefaultMetricsSslCertificateString() +{ + constexpr char kOtlpTracesSslCertificateString[] = + "OTEL_EXPORTER_OTLP_METRICS_CERTIFICATE_STRING"; + constexpr char kOtlpSslCertificateString[] = "OTEL_EXPORTER_OTLP_CERTIFICATE_STRING "; + auto ssl_cert = + opentelemetry::sdk::common::GetEnvironmentVariable(kOtlpTracesSslCertificateString); + if (ssl_cert.empty()) + { + ssl_cert = opentelemetry::sdk::common::GetEnvironmentVariable(kOtlpSslCertificateString); + } + return ssl_cert.size() ? ssl_cert : ""; +} + } // namespace otlp } // namespace exporter OPENTELEMETRY_END_NAMESPACE diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_metric_exporter.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_metric_exporter.h index fa8ba66c14..d0fb08d520 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_metric_exporter.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_metric_exporter.h @@ -14,7 +14,6 @@ // 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 @@ -23,6 +22,30 @@ namespace exporter namespace otlp { +/** + * Struct to hold OTLP metrics exporter options. + */ +struct OtlpGrpcMetricExporterOptions +{ + + // The endpoint to export to. By default the OpenTelemetry Collector's default endpoint. + std::string endpoint = GetOtlpDefaultMetricsEndpoint(); + // By default when false, uses grpc::InsecureChannelCredentials(); If true, + // uses ssl_credentials_cacert_path if non-empty, else uses ssl_credentials_cacert_as_string + bool use_ssl_credentials = GetOtlpDefaultMetricsIsSslEnable(); + // ssl_credentials_cacert_path specifies path to .pem file to be used for SSL encryption. + std::string ssl_credentials_cacert_path = GetOtlpDefaultMetricsSslCertificatePath(); + // ssl_credentials_cacert_as_string in-memory string representation of .pem file to be used for + // SSL encryption. + std::string ssl_credentials_cacert_as_string = GetOtlpDefaultMetricsSslCertificateString(); + // Timeout for grpc deadline + std::chrono::system_clock::duration timeout = GetOtlpDefaultMetricsTimeout(); + // Additional HTTP headers + OtlpHeaders metadata = GetOtlpDefaultMetricsHeaders(); + opentelemetry::sdk::metrics::AggregationTemporality aggregation_temporality = + opentelemetry::sdk::metrics::AggregationTemporality::kDelta; +}; + /** * The OTLP exporter exports metrics data in OpenTelemetry Protocol (OTLP) format in gRPC. */ diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_metric_exporter.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_metric_exporter.h index 311f68494e..32508995e4 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_metric_exporter.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_metric_exporter.h @@ -30,7 +30,7 @@ struct OtlpHttpMetricExporterOptions // @see // https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/otlp.md // @see https://github.com/open-telemetry/opentelemetry-collector/tree/main/receiver/otlpreceiver - std::string url = GetOtlpDefaultHttpMetricEndpoint(); + std::string url = GetOtlpDefaultMetricsEndpoint(); // By default, post json data HttpRequestContentType content_type = HttpRequestContentType::kJson; @@ -47,10 +47,10 @@ struct OtlpHttpMetricExporterOptions bool console_debug = false; // TODO: Enable/disable to verify SSL certificate - std::chrono::system_clock::duration timeout = GetOtlpDefaultMetricTimeout(); + std::chrono::system_clock::duration timeout = GetOtlpDefaultMetricsTimeout(); // Additional HTTP headers - OtlpHeaders http_headers = GetOtlpDefaultMetricHeaders(); + OtlpHeaders http_headers = GetOtlpDefaultMetricsHeaders(); # ifdef ENABLE_ASYNC_EXPORT // Concurrent requests diff --git a/exporters/otlp/test/otlp_http_metric_exporter_test.cc b/exporters/otlp/test/otlp_http_metric_exporter_test.cc index 46a7c5eac8..aa63b4d158 100644 --- a/exporters/otlp/test/otlp_http_metric_exporter_test.cc +++ b/exporters/otlp/test/otlp_http_metric_exporter_test.cc @@ -953,7 +953,7 @@ TEST_F(OtlpHttpMetricExporterTestPeer, ConfigFromMetricsEnv) TEST_F(OtlpHttpMetricExporterTestPeer, DefaultEndpoint) { - EXPECT_EQ("http://localhost:4318/v1/metrics", GetOtlpDefaultHttpMetricEndpoint()); + EXPECT_EQ("http://localhost:4318/v1/metrics", GetOtlpDefaultMetricsEndpoint()); } # endif From a3bceb074c10da5f3c79cc6634b74768ad15a177 Mon Sep 17 00:00:00 2001 From: Lalit Date: Fri, 5 Aug 2022 15:34:10 -0700 Subject: [PATCH 2/4] review comment --- .../include/opentelemetry/exporters/otlp/otlp_environment.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_environment.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_environment.h index 38ff0ef958..07492cdb81 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_environment.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_environment.h @@ -349,7 +349,7 @@ inline const std::string GetOtlpDefaultMetricsSslCertificateString() { constexpr char kOtlpTracesSslCertificateString[] = "OTEL_EXPORTER_OTLP_METRICS_CERTIFICATE_STRING"; - constexpr char kOtlpSslCertificateString[] = "OTEL_EXPORTER_OTLP_CERTIFICATE_STRING "; + constexpr char kOtlpSslCertificateString[] = "OTEL_EXPORTER_OTLP_CERTIFICATE_STRING"; auto ssl_cert = opentelemetry::sdk::common::GetEnvironmentVariable(kOtlpTracesSslCertificateString); if (ssl_cert.empty()) From 3d6cbf083412f9aae480ec02eab2c6026b67e4e2 Mon Sep 17 00:00:00 2001 From: Lalit Date: Fri, 5 Aug 2022 15:51:51 -0700 Subject: [PATCH 3/4] trailing space in env var --- .../include/opentelemetry/exporters/otlp/otlp_environment.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_environment.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_environment.h index 07492cdb81..4ef3a30639 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_environment.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_environment.h @@ -335,7 +335,7 @@ inline bool GetOtlpDefaultMetricsIsSslEnable() inline const std::string GetOtlpDefaultMetricsSslCertificatePath() { constexpr char kOtlpMetricsSslCertificate[] = "OTEL_EXPORTER_OTLP_METRICS_CERTIFICATE"; - constexpr char kOtlpSslCertificate[] = "OTEL_EXPORTER_OTLP_CERTIFICATE "; + constexpr char kOtlpSslCertificate[] = "OTEL_EXPORTER_OTLP_CERTIFICATE"; auto ssl_cert_path = opentelemetry::sdk::common::GetEnvironmentVariable(kOtlpMetricsSslCertificate); if (ssl_cert_path.empty()) From 981d1a41583b960ee76a619cab969ce325bb0945 Mon Sep 17 00:00:00 2001 From: Lalit Date: Mon, 8 Aug 2022 09:26:00 -0700 Subject: [PATCH 4/4] review comments --- .../otlp/include/opentelemetry/exporters/otlp/otlp_environment.h | 1 - 1 file changed, 1 deletion(-) diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_environment.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_environment.h index 4ef3a30639..787f852442 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_environment.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_environment.h @@ -268,7 +268,6 @@ inline OtlpHeaders GetOtlpDefaultLogHeaders() } // --- Metrics Environment Variables - inline const std::string GetOtlpDefaultMetricsEndpoint() { constexpr char kOtlpMetricsEndpointEnv[] = "OTEL_EXPORTER_OTLP_METRICS_ENDPOINT";