From 319d85436598039f877162d9fd613153b55227ca Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Mon, 8 Aug 2022 10:52:21 -0700 Subject: [PATCH] [METRICS SDK] Fix OTLP gRPC Metrics env variables (#1543) --- .../exporters/otlp/otlp_environment.h | 57 +++++++++++++++++-- .../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, 79 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..787f852442 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,8 @@ 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 +286,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 +299,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 +314,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 643a0cd3d5..b741df905a 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 f73966ee22..217882e715 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 @@ -31,7 +31,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; @@ -48,10 +48,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(); // Preferred Aggregation Temporality sdk::metrics::AggregationTemporality aggregation_temporality = diff --git a/exporters/otlp/test/otlp_http_metric_exporter_test.cc b/exporters/otlp/test/otlp_http_metric_exporter_test.cc index 891812c4d1..3d51eebd50 100644 --- a/exporters/otlp/test/otlp_http_metric_exporter_test.cc +++ b/exporters/otlp/test/otlp_http_metric_exporter_test.cc @@ -957,7 +957,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