Skip to content

Commit 43d8fe4

Browse files
lalitbThomsonTan
andauthored
Make Otlp exporter configuration environment variables specs-compliant (#974)
* add documentation for GRPC Env variables * Update exporters/otlp/README.md Co-authored-by: Tom Tan <[email protected]> * Update exporters/otlp/README.md Co-authored-by: Tom Tan <[email protected]> * spec compliant env var * markdown lint Co-authored-by: Tom Tan <[email protected]>
1 parent 0da25b3 commit 43d8fe4

File tree

4 files changed

+76
-28
lines changed

4 files changed

+76
-28
lines changed

exporters/otlp/README.md

+21-4
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,28 @@ options.url = "localhost:12345";
4242
auto exporter = std::unique_ptr<sdktrace::SpanExporter>(new otlp::OtlpHttpExporter(options));
4343
```
4444

45-
### Configuration options
45+
### Configuration options ( OTLP GRPC Exporter )
4646

47-
| Option | Default |
48-
| ------------ |----------------- |
49-
| `endpoint` | `localhost:4317` |
47+
| Option | Env Variable |Default | Description |
48+
| ------------ |---------------|------------ |----------------|
49+
| `endpoint` | `OTEL_EXPORTER_OTLP_ENDPOINT` | `http://localhost:4317`| The OTLP GRPC endpoint to connect to |
50+
| | `OTEL_EXPORTER_OTLP_TRACES_ENDPOINT` | | |
51+
| `use_ssl_credentials` | `OTEL_EXPORTER_OTLP_SSL_ENABLE`| `false` | Whether the endpoint is SSL enabled |
52+
| | `OTEL_EXPORTER_OTLP_TRACES_SSL_ENABLE` | | |
53+
| `ssl_credentials_cacert_path` | `OTEL_EXPORTER_OTLP_CERTIFICATE` | `""` | SSL Certificate file path |
54+
| | `OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE` | | |
55+
| `ssl_credentials_cacert_as_string` | `OTEL_EXPORTER_OTLP_CERTIFICATE_STRING` | `""` | SSL Certifcate as in-memory string |
56+
| | `OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE_STRING` | | | |
57+
58+
### Configuration options ( OTLP HTTP Exporter )
59+
60+
| Option | Env Variable |Default | Description |
61+
| ------------ |-----|------------ |------|
62+
| `url` | n/a | `http://localhost:4317/v1/traces` | The OTLP HTTP endpoint to connect to |
63+
| `content_type` | n/a | `application/json` | Data format used - JSON or Binary |
64+
| `json_bytes_mapping` | n/a | `JsonBytesMappingKind::kHexId` | Encoding used for trace_id and span_id |
65+
| `use_json_name` | n/a | `false` | Whether to use json name of protobuf field to set the key of json |
66+
| `timeout` | n/a | `30000 ms` | http timeout |
5067

5168
## Example
5269

exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_exporter.h

+45-15
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,60 @@ namespace exporter
1919
namespace otlp
2020
{
2121

22-
inline const std::string GetOtlpGrpcDefaultEndpoint()
22+
inline const std::string GetOtlpDefaultEndpoint()
2323
{
24-
constexpr char kOtlpGrpcEndpointEnv[] = "OTEL_EXPORTER_OTLP_GRPC_ENDPOINT";
25-
constexpr char kOtlpGrpcEndpointDefault[] = "localhost:4317";
24+
constexpr char kOtlpTracesEndpointEnv[] = "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT";
25+
constexpr char kOtlpEndpointEnv[] = "OTEL_EXPORTER_OTLP_ENDPOINT";
26+
constexpr char kOtlpEndpointDefault[] = "http://localhost:4317";
2627

27-
auto endpoint = opentelemetry::sdk::common::GetEnvironmentVariable(kOtlpGrpcEndpointEnv);
28-
return endpoint.size() ? endpoint : kOtlpGrpcEndpointDefault;
28+
auto endpoint = opentelemetry::sdk::common::GetEnvironmentVariable(kOtlpTracesEndpointEnv);
29+
if (endpoint.size() == 0)
30+
{
31+
endpoint = opentelemetry::sdk::common::GetEnvironmentVariable(kOtlpEndpointEnv);
32+
}
33+
return endpoint.size() ? endpoint : kOtlpEndpointDefault;
2934
}
3035

31-
inline const bool GetOtlpGrpcDefaultIsSslEnable()
36+
inline const bool GetOtlpDefaultIsSslEnable()
3237
{
33-
constexpr char kOtlpGrpcIsSslEnableEnv[] = "OTEL_EXPORTER_OTLP_GRPC_SSL_ENABLE";
34-
auto ssl_enable = opentelemetry::sdk::common::GetEnvironmentVariable(kOtlpGrpcIsSslEnableEnv);
38+
constexpr char kOtlpTracesIsSslEnableEnv[] = "OTEL_EXPORTER_OTLP_TRACES_SSL_ENABLE";
39+
constexpr char kOtlpIsSslEnableEnv[] = "OTEL_EXPORTER_OTLP_SSL_ENABLE";
40+
41+
auto ssl_enable = opentelemetry::sdk::common::GetEnvironmentVariable(kOtlpTracesIsSslEnableEnv);
42+
if (ssl_enable.size() == 0)
43+
{
44+
ssl_enable = opentelemetry::sdk::common::GetEnvironmentVariable(kOtlpIsSslEnableEnv);
45+
}
3546
if (ssl_enable == "True" || ssl_enable == "TRUE" || ssl_enable == "true" || ssl_enable == "1")
3647
{
3748
return true;
3849
}
3950
return false;
4051
}
4152

42-
inline const std::string GetOtlpGrpcDefaultSslCertificate()
53+
inline const std::string GetOtlpDefaultSslCertificatePath()
54+
{
55+
constexpr char kOtlpTracesSslCertificate[] = "OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE";
56+
constexpr char kOtlpSslCertificate[] = "OTEL_EXPORTER_OTLP_CERTIFICATE ";
57+
auto ssl_cert_path =
58+
opentelemetry::sdk::common::GetEnvironmentVariable(kOtlpTracesSslCertificate);
59+
if (ssl_cert_path.size() == 0)
60+
{
61+
ssl_cert_path = opentelemetry::sdk::common::GetEnvironmentVariable(kOtlpSslCertificate);
62+
}
63+
return ssl_cert_path.size() ? ssl_cert_path : "";
64+
}
65+
66+
inline const std::string GetOtlpDefaultSslCertificateString()
4367
{
44-
constexpr char kOtlpGrpcSslCertificate[] = "OTEL_EXPORTER_OTLP_GRPC_SSL_CERTIFICATE";
45-
auto ssl_cert = opentelemetry::sdk::common::GetEnvironmentVariable(kOtlpGrpcSslCertificate);
68+
constexpr char kOtlpTracesSslCertificateString[] = "OTEL_EXPORTER_OTLP_CERTIFICATE_STRING";
69+
constexpr char kOtlpSslCertificateString[] = "OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE_STRING ";
70+
auto ssl_cert =
71+
opentelemetry::sdk::common::GetEnvironmentVariable(kOtlpTracesSslCertificateString);
72+
if (ssl_cert.size() == 0)
73+
{
74+
ssl_cert = opentelemetry::sdk::common::GetEnvironmentVariable(kOtlpSslCertificateString);
75+
}
4676
return ssl_cert.size() ? ssl_cert : "";
4777
}
4878

@@ -52,15 +82,15 @@ inline const std::string GetOtlpGrpcDefaultSslCertificate()
5282
struct OtlpGrpcExporterOptions
5383
{
5484
// The endpoint to export to. By default the OpenTelemetry Collector's default endpoint.
55-
std::string endpoint = GetOtlpGrpcDefaultEndpoint();
85+
std::string endpoint = GetOtlpDefaultEndpoint();
5686
// By default when false, uses grpc::InsecureChannelCredentials(); If true,
5787
// uses ssl_credentials_cacert_path if non-empty, else uses ssl_credentials_cacert_as_string
58-
bool use_ssl_credentials = GetOtlpGrpcDefaultIsSslEnable();
88+
bool use_ssl_credentials = GetOtlpDefaultIsSslEnable();
5989
// ssl_credentials_cacert_path specifies path to .pem file to be used for SSL encryption.
60-
std::string ssl_credentials_cacert_path = "";
90+
std::string ssl_credentials_cacert_path = GetOtlpDefaultSslCertificatePath();
6191
// ssl_credentials_cacert_as_string in-memory string representation of .pem file to be used for
6292
// SSL encryption.
63-
std::string ssl_credentials_cacert_as_string = GetOtlpGrpcDefaultSslCertificate();
93+
std::string ssl_credentials_cacert_as_string = GetOtlpDefaultSslCertificateString();
6494
};
6595

6696
/**

exporters/otlp/src/otlp_grpc_exporter.cc

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ std::unique_ptr<proto::collector::trace::v1::TraceService::Stub> MakeServiceStub
5959
const OtlpGrpcExporterOptions &options)
6060
{
6161
std::shared_ptr<grpc::Channel> channel;
62+
6263
if (options.use_ssl_credentials)
6364
{
6465
grpc::SslCredentialsOptions ssl_opts;

exporters/otlp/test/otlp_grpc_exporter_test.cc

+9-9
Original file line numberDiff line numberDiff line change
@@ -139,27 +139,27 @@ TEST_F(OtlpGrpcExporterTestPeer, ConfigSslCredentialsTest)
139139
TEST_F(OtlpGrpcExporterTestPeer, ConfigFromEnv)
140140
{
141141
const std::string cacert_str = "--begin and end fake cert--";
142-
const std::string cacert_env = "OTEL_EXPORTER_OTLP_GRPC_SSL_CERTIFICATE=" + cacert_str;
142+
const std::string cacert_env = "OTEL_EXPORTER_OTLP_CERTIFICATE_STRING=" + cacert_str;
143143
putenv(const_cast<char *>(cacert_env.data()));
144-
char ssl_enable_env[] = "OTEL_EXPORTER_OTLP_GRPC_SSL_ENABLE=True";
144+
char ssl_enable_env[] = "OTEL_EXPORTER_OTLP_SSL_ENABLE=True";
145145
putenv(ssl_enable_env);
146146
const std::string endpoint = "http://localhost:9999";
147-
const std::string endpoint_env = "OTEL_EXPORTER_OTLP_GRPC_ENDPOINT=" + endpoint;
147+
const std::string endpoint_env = "OTEL_EXPORTER_OTLP_ENDPOINT=" + endpoint;
148148
putenv(const_cast<char *>(endpoint_env.data()));
149149

150150
std::unique_ptr<OtlpGrpcExporter> exporter(new OtlpGrpcExporter());
151151
EXPECT_EQ(GetOptions(exporter).ssl_credentials_cacert_as_string, cacert_str);
152152
EXPECT_EQ(GetOptions(exporter).use_ssl_credentials, true);
153153
EXPECT_EQ(GetOptions(exporter).endpoint, endpoint);
154154
# if defined(_MSC_VER)
155-
putenv("OTEL_EXPORTER_OTLP_GRPC_ENDPOINT=");
156-
putenv("OTEL_EXPORTER_OTLP_GRPC_SSL_CERTIFICATE=");
157-
putenv("OTEL_EXPORTER_OTLP_GRPC_SSL_ENABLE=");
155+
putenv("OTEL_EXPORTER_OTLP_ENDPOINT=");
156+
putenv("OTEL_EXPORTER_OTLP_CERTIFICATE_STRING=");
157+
putenv("OTEL_EXPORTER_OTLP_SSL_ENABLE=");
158158

159159
# else
160-
unsetenv("OTEL_EXPORTER_OTLP_GRPC_ENDPOINT");
161-
unsetenv("OTEL_EXPORTER_OTLP_GRPC_SSL_CERTIFICATE");
162-
unsetenv("OTEL_EXPORTER_OTLP_GRPC_SSL_ENABLE");
160+
unsetenv("OTEL_EXPORTER_OTLP_ENDPOINT");
161+
unsetenv("OTEL_EXPORTER_OTLP_CERTIFICATE_STRING");
162+
unsetenv("OTEL_EXPORTER_OTLP_SSL_ENABLE");
163163

164164
# endif
165165
}

0 commit comments

Comments
 (0)