-
Notifications
You must be signed in to change notification settings - Fork 440
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. Add
otlp_environment.h
to maintain shared codes of `OtlpGrpcExpo…
…rter` and `OtlpHttpExporter` 2. Add `timeout` and `metadata` options for `OtlpGrpcExporter` 3. Load environment variables `OTEL_EXPORTER_OTLP_TIMEOUT` , `OTEL_EXPORTER_OTLP_TRACES_TIMEOUT` , `OTEL_EXPORTER_OTLP_HEADERS` , `OTEL_EXPORTER_OTLP_TRACES_HEADERS` to initialize default options of `OtlpGrpcExporter` and `OtlpHttpExporter` 4. Load environment variables `OTEL_EXPORTER_OTLP_ENDPOINT` , `OTEL_EXPORTER_OTLP_TRACES_ENDPOINT` to initialize default options of `OtlpHttpExporter` Signed-off-by: owentou <owentou@tencent.com>
- Loading branch information
Showing
10 changed files
with
354 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
203 changes: 203 additions & 0 deletions
203
exporters/otlp/include/opentelemetry/exporters/otlp/otlp_environment.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#pragma once | ||
|
||
#include <algorithm> | ||
#include <chrono> | ||
#include <map> | ||
#include <string> | ||
|
||
#include "opentelemetry/nostd/string_view.h" | ||
|
||
#include "opentelemetry/sdk/common/attribute_utils.h" | ||
#include "opentelemetry/sdk/common/env_variables.h" | ||
|
||
OPENTELEMETRY_BEGIN_NAMESPACE | ||
namespace exporter | ||
{ | ||
namespace otlp | ||
{ | ||
|
||
inline const std::string GetOtlpDefaultGrpcEndpoint() | ||
{ | ||
constexpr char kOtlpTracesEndpointEnv[] = "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT"; | ||
constexpr char kOtlpEndpointEnv[] = "OTEL_EXPORTER_OTLP_ENDPOINT"; | ||
constexpr char kOtlpEndpointDefault[] = "http://localhost:4317"; | ||
|
||
auto endpoint = opentelemetry::sdk::common::GetEnvironmentVariable(kOtlpTracesEndpointEnv); | ||
if (endpoint.empty()) | ||
{ | ||
endpoint = opentelemetry::sdk::common::GetEnvironmentVariable(kOtlpEndpointEnv); | ||
} | ||
return endpoint.size() ? endpoint : kOtlpEndpointDefault; | ||
} | ||
|
||
inline const std::string GetOtlpDefaultHttpEndpoint() | ||
{ | ||
constexpr char kOtlpTracesEndpointEnv[] = "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT"; | ||
constexpr char kOtlpEndpointEnv[] = "OTEL_EXPORTER_OTLP_ENDPOINT"; | ||
constexpr char kOtlpEndpointDefault[] = "http://localhost:4317/v1/traces"; | ||
|
||
auto endpoint = opentelemetry::sdk::common::GetEnvironmentVariable(kOtlpTracesEndpointEnv); | ||
if (endpoint.empty()) | ||
{ | ||
endpoint = opentelemetry::sdk::common::GetEnvironmentVariable(kOtlpEndpointEnv); | ||
} | ||
return endpoint.size() ? endpoint : kOtlpEndpointDefault; | ||
} | ||
|
||
inline const bool GetOtlpDefaultIsSslEnable() | ||
{ | ||
constexpr char kOtlpTracesIsSslEnableEnv[] = "OTEL_EXPORTER_OTLP_TRACES_SSL_ENABLE"; | ||
constexpr char kOtlpIsSslEnableEnv[] = "OTEL_EXPORTER_OTLP_SSL_ENABLE"; | ||
|
||
auto ssl_enable = opentelemetry::sdk::common::GetEnvironmentVariable(kOtlpTracesIsSslEnableEnv); | ||
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 GetOtlpDefaultSslCertificatePath() | ||
{ | ||
constexpr char kOtlpTracesSslCertificate[] = "OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE"; | ||
constexpr char kOtlpSslCertificate[] = "OTEL_EXPORTER_OTLP_CERTIFICATE "; | ||
auto ssl_cert_path = | ||
opentelemetry::sdk::common::GetEnvironmentVariable(kOtlpTracesSslCertificate); | ||
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 GetOtlpDefaultSslCertificateString() | ||
{ | ||
constexpr char kOtlpTracesSslCertificateString[] = "OTEL_EXPORTER_OTLP_CERTIFICATE_STRING"; | ||
constexpr char kOtlpSslCertificateString[] = "OTEL_EXPORTER_OTLP_TRACES_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 : ""; | ||
} | ||
|
||
inline const std::chrono::system_clock::duration GetOtlpTimeoutFromString(const char *input) | ||
{ | ||
if (nullptr == input || 0 == *input) | ||
{ | ||
return std::chrono::duration_cast<std::chrono::system_clock::duration>( | ||
std::chrono::seconds{10}); | ||
} | ||
|
||
std::chrono::system_clock::duration::rep result = 0; | ||
// Skip spaces | ||
for (; *input && (' ' == *input || '\t' == *input || '\r' == *input || '\n' == *input); ++input) | ||
; | ||
|
||
for (; *input && (*input >= '0' && *input <= '9'); ++input) | ||
{ | ||
result = result * 10 + (*input - '0'); | ||
} | ||
|
||
opentelemetry::nostd::string_view unit{input}; | ||
if ("ns" == unit) | ||
{ | ||
return std::chrono::duration_cast<std::chrono::system_clock::duration>( | ||
std::chrono::nanoseconds{result}); | ||
} | ||
else if ("us" == unit) | ||
{ | ||
return std::chrono::duration_cast<std::chrono::system_clock::duration>( | ||
std::chrono::microseconds{result}); | ||
} | ||
else if ("ms" == unit) | ||
{ | ||
return std::chrono::duration_cast<std::chrono::system_clock::duration>( | ||
std::chrono::milliseconds{result}); | ||
} | ||
else if ("m" == unit) | ||
{ | ||
return std::chrono::duration_cast<std::chrono::system_clock::duration>( | ||
std::chrono::minutes{result}); | ||
} | ||
else if ("h" == unit) | ||
{ | ||
return std::chrono::duration_cast<std::chrono::system_clock::duration>( | ||
std::chrono::hours{result}); | ||
} | ||
else | ||
{ | ||
return std::chrono::duration_cast<std::chrono::system_clock::duration>( | ||
std::chrono::seconds{result}); | ||
} | ||
} | ||
|
||
inline const std::chrono::system_clock::duration GetOtlpDefaultTimeout() | ||
{ | ||
constexpr char kOtlpTracesTimeoutEnv[] = "OTEL_EXPORTER_OTLP_TRACES_TIMEOUT"; | ||
constexpr char kOtlpTimeoutEnv[] = "OTEL_EXPORTER_OTLP_TIMEOUT"; | ||
|
||
auto timeout = opentelemetry::sdk::common::GetEnvironmentVariable(kOtlpTracesTimeoutEnv); | ||
if (timeout.empty()) | ||
{ | ||
timeout = opentelemetry::sdk::common::GetEnvironmentVariable(kOtlpTimeoutEnv); | ||
} | ||
return GetOtlpTimeoutFromString(timeout.c_str()); | ||
} | ||
|
||
struct cmp_ic | ||
{ | ||
bool operator()(const std::string &s1, const std::string &s2) const | ||
{ | ||
return std::lexicographical_compare( | ||
s1.begin(), s1.end(), s2.begin(), s2.end(), | ||
[](char c1, char c2) { return ::tolower(c1) < ::tolower(c2); }); | ||
} | ||
}; | ||
using OtlpHeaders = std::multimap<std::string, std::string, cmp_ic>; | ||
|
||
inline void DumpOtlpHeaders(OtlpHeaders &output, const char *env_var_name) | ||
{ | ||
auto value = opentelemetry::sdk::common::GetEnvironmentVariable(env_var_name); | ||
if (value.empty()) | ||
{ | ||
return; | ||
} | ||
|
||
opentelemetry::common::KeyValueStringTokenizer tokenizer{value}; | ||
opentelemetry::nostd::string_view header_key; | ||
opentelemetry::nostd::string_view header_value; | ||
bool header_valid = true; | ||
|
||
while (tokenizer.next(header_valid, header_key, header_value)) | ||
{ | ||
if (header_valid) | ||
{ | ||
output.emplace(std::make_pair((std::string)header_key, (std::string)header_value)); | ||
} | ||
} | ||
} | ||
|
||
inline OtlpHeaders GetOtlpDefaultHeaders() | ||
{ | ||
constexpr char kOtlpTracesHeadersEnv[] = "OTEL_EXPORTER_OTLP_TRACES_HEADERS"; | ||
constexpr char kOtlpHeadersEnv[] = "OTEL_EXPORTER_OTLP_HEADERS"; | ||
|
||
OtlpHeaders result; | ||
DumpOtlpHeaders(result, kOtlpHeadersEnv); | ||
DumpOtlpHeaders(result, kOtlpTracesHeadersEnv); | ||
return result; | ||
} | ||
|
||
} // namespace otlp | ||
} // namespace exporter | ||
OPENTELEMETRY_END_NAMESPACE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.