Skip to content

Commit

Permalink
[METRICS SDK] Fix OTLP gRPC Metrics env variables (#1543)
Browse files Browse the repository at this point in the history
  • Loading branch information
lalitb authored Aug 8, 2022
1 parent 8b61c99 commit 319d854
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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())
Expand Down Expand Up @@ -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";
Expand All @@ -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";
Expand All @@ -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";
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 =
Expand Down
2 changes: 1 addition & 1 deletion exporters/otlp/test/otlp_http_metric_exporter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 319d854

Please sign in to comment.