-
Notifications
You must be signed in to change notification settings - Fork 629
Support environment variables for both OtlpGrpcExporter and OtlpHttpExporter
#983
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
lalitb
merged 2 commits into
open-telemetry:main
from
owent:environments_for_otlp_http_exporter
Sep 21, 2021
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
221 changes: 221 additions & 0 deletions
221
exporters/otlp/include/opentelemetry/exporters/otlp/otlp_environment.h
This file contains hidden or 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,221 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <algorithm> | ||
| #include <chrono> | ||
| #include <map> | ||
| #include <string> | ||
| #include <unordered_set> | ||
|
|
||
| #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, | ||
| std::unordered_set<std::string> &remove_cache) | ||
| { | ||
| 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) | ||
| { | ||
| std::string key = static_cast<std::string>(header_key); | ||
| if (remove_cache.end() == remove_cache.find(key)) | ||
| { | ||
| remove_cache.insert(key); | ||
| auto range = output.equal_range(key); | ||
| if (range.first != range.second) | ||
| { | ||
| output.erase(range.first, range.second); | ||
| } | ||
| } | ||
|
|
||
| output.emplace(std::make_pair(std::move(key), static_cast<std::string>(header_value))); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| inline OtlpHeaders GetOtlpDefaultHeaders() | ||
| { | ||
| constexpr char kOtlpTracesHeadersEnv[] = "OTEL_EXPORTER_OTLP_TRACES_HEADERS"; | ||
| constexpr char kOtlpHeadersEnv[] = "OTEL_EXPORTER_OTLP_HEADERS"; | ||
|
|
||
| OtlpHeaders result; | ||
| std::unordered_set<std::string> trace_remove_cache; | ||
| DumpOtlpHeaders(result, kOtlpHeadersEnv, trace_remove_cache); | ||
|
|
||
| trace_remove_cache.clear(); | ||
| DumpOtlpHeaders(result, kOtlpTracesHeadersEnv, trace_remove_cache); | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| } // namespace otlp | ||
| } // namespace exporter | ||
| OPENTELEMETRY_END_NAMESPACE | ||
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.