|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +#pragma once |
| 5 | + |
| 6 | +#include <algorithm> |
| 7 | +#include <chrono> |
| 8 | +#include <map> |
| 9 | +#include <string> |
| 10 | + |
| 11 | +#include "opentelemetry/nostd/string_view.h" |
| 12 | + |
| 13 | +#include "opentelemetry/sdk/common/attribute_utils.h" |
| 14 | +#include "opentelemetry/sdk/common/env_variables.h" |
| 15 | + |
| 16 | +OPENTELEMETRY_BEGIN_NAMESPACE |
| 17 | +namespace exporter |
| 18 | +{ |
| 19 | +namespace otlp |
| 20 | +{ |
| 21 | + |
| 22 | +inline const std::string GetOtlpDefaultGrpcEndpoint() |
| 23 | +{ |
| 24 | + constexpr char kOtlpTracesEndpointEnv[] = "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT"; |
| 25 | + constexpr char kOtlpEndpointEnv[] = "OTEL_EXPORTER_OTLP_ENDPOINT"; |
| 26 | + constexpr char kOtlpEndpointDefault[] = "http://localhost:4317"; |
| 27 | + |
| 28 | + auto endpoint = opentelemetry::sdk::common::GetEnvironmentVariable(kOtlpTracesEndpointEnv); |
| 29 | + if (endpoint.empty()) |
| 30 | + { |
| 31 | + endpoint = opentelemetry::sdk::common::GetEnvironmentVariable(kOtlpEndpointEnv); |
| 32 | + } |
| 33 | + return endpoint.size() ? endpoint : kOtlpEndpointDefault; |
| 34 | +} |
| 35 | + |
| 36 | +inline const std::string GetOtlpDefaultHttpEndpoint() |
| 37 | +{ |
| 38 | + constexpr char kOtlpTracesEndpointEnv[] = "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT"; |
| 39 | + constexpr char kOtlpEndpointEnv[] = "OTEL_EXPORTER_OTLP_ENDPOINT"; |
| 40 | + constexpr char kOtlpEndpointDefault[] = "http://localhost:4317/v1/traces"; |
| 41 | + |
| 42 | + auto endpoint = opentelemetry::sdk::common::GetEnvironmentVariable(kOtlpTracesEndpointEnv); |
| 43 | + if (endpoint.empty()) |
| 44 | + { |
| 45 | + endpoint = opentelemetry::sdk::common::GetEnvironmentVariable(kOtlpEndpointEnv); |
| 46 | + } |
| 47 | + return endpoint.size() ? endpoint : kOtlpEndpointDefault; |
| 48 | +} |
| 49 | + |
| 50 | +inline const bool GetOtlpDefaultIsSslEnable() |
| 51 | +{ |
| 52 | + constexpr char kOtlpTracesIsSslEnableEnv[] = "OTEL_EXPORTER_OTLP_TRACES_SSL_ENABLE"; |
| 53 | + constexpr char kOtlpIsSslEnableEnv[] = "OTEL_EXPORTER_OTLP_SSL_ENABLE"; |
| 54 | + |
| 55 | + auto ssl_enable = opentelemetry::sdk::common::GetEnvironmentVariable(kOtlpTracesIsSslEnableEnv); |
| 56 | + if (ssl_enable.empty()) |
| 57 | + { |
| 58 | + ssl_enable = opentelemetry::sdk::common::GetEnvironmentVariable(kOtlpIsSslEnableEnv); |
| 59 | + } |
| 60 | + if (ssl_enable == "True" || ssl_enable == "TRUE" || ssl_enable == "true" || ssl_enable == "1") |
| 61 | + { |
| 62 | + return true; |
| 63 | + } |
| 64 | + return false; |
| 65 | +} |
| 66 | + |
| 67 | +inline const std::string GetOtlpDefaultSslCertificatePath() |
| 68 | +{ |
| 69 | + constexpr char kOtlpTracesSslCertificate[] = "OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE"; |
| 70 | + constexpr char kOtlpSslCertificate[] = "OTEL_EXPORTER_OTLP_CERTIFICATE "; |
| 71 | + auto ssl_cert_path = |
| 72 | + opentelemetry::sdk::common::GetEnvironmentVariable(kOtlpTracesSslCertificate); |
| 73 | + if (ssl_cert_path.empty()) |
| 74 | + { |
| 75 | + ssl_cert_path = opentelemetry::sdk::common::GetEnvironmentVariable(kOtlpSslCertificate); |
| 76 | + } |
| 77 | + return ssl_cert_path.size() ? ssl_cert_path : ""; |
| 78 | +} |
| 79 | + |
| 80 | +inline const std::string GetOtlpDefaultSslCertificateString() |
| 81 | +{ |
| 82 | + constexpr char kOtlpTracesSslCertificateString[] = "OTEL_EXPORTER_OTLP_CERTIFICATE_STRING"; |
| 83 | + constexpr char kOtlpSslCertificateString[] = "OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE_STRING "; |
| 84 | + auto ssl_cert = |
| 85 | + opentelemetry::sdk::common::GetEnvironmentVariable(kOtlpTracesSslCertificateString); |
| 86 | + if (ssl_cert.empty()) |
| 87 | + { |
| 88 | + ssl_cert = opentelemetry::sdk::common::GetEnvironmentVariable(kOtlpSslCertificateString); |
| 89 | + } |
| 90 | + return ssl_cert.size() ? ssl_cert : ""; |
| 91 | +} |
| 92 | + |
| 93 | +inline const std::chrono::system_clock::duration GetOtlpTimeoutFromString(const char *input) |
| 94 | +{ |
| 95 | + if (nullptr == input || 0 == *input) |
| 96 | + { |
| 97 | + return std::chrono::duration_cast<std::chrono::system_clock::duration>( |
| 98 | + std::chrono::seconds{10}); |
| 99 | + } |
| 100 | + |
| 101 | + std::chrono::system_clock::duration::rep result = 0; |
| 102 | + // Skip spaces |
| 103 | + for (; *input && (' ' == *input || '\t' == *input || '\r' == *input || '\n' == *input); ++input) |
| 104 | + ; |
| 105 | + |
| 106 | + for (; *input && (*input >= '0' && *input <= '9'); ++input) |
| 107 | + { |
| 108 | + result = result * 10 + (*input - '0'); |
| 109 | + } |
| 110 | + |
| 111 | + opentelemetry::nostd::string_view unit{input}; |
| 112 | + if ("ns" == unit) |
| 113 | + { |
| 114 | + return std::chrono::duration_cast<std::chrono::system_clock::duration>( |
| 115 | + std::chrono::nanoseconds{result}); |
| 116 | + } |
| 117 | + else if ("us" == unit) |
| 118 | + { |
| 119 | + return std::chrono::duration_cast<std::chrono::system_clock::duration>( |
| 120 | + std::chrono::microseconds{result}); |
| 121 | + } |
| 122 | + else if ("ms" == unit) |
| 123 | + { |
| 124 | + return std::chrono::duration_cast<std::chrono::system_clock::duration>( |
| 125 | + std::chrono::milliseconds{result}); |
| 126 | + } |
| 127 | + else if ("m" == unit) |
| 128 | + { |
| 129 | + return std::chrono::duration_cast<std::chrono::system_clock::duration>( |
| 130 | + std::chrono::minutes{result}); |
| 131 | + } |
| 132 | + else if ("h" == unit) |
| 133 | + { |
| 134 | + return std::chrono::duration_cast<std::chrono::system_clock::duration>( |
| 135 | + std::chrono::hours{result}); |
| 136 | + } |
| 137 | + else |
| 138 | + { |
| 139 | + return std::chrono::duration_cast<std::chrono::system_clock::duration>( |
| 140 | + std::chrono::seconds{result}); |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +inline const std::chrono::system_clock::duration GetOtlpDefaultTimeout() |
| 145 | +{ |
| 146 | + constexpr char kOtlpTracesTimeoutEnv[] = "OTEL_EXPORTER_OTLP_TRACES_TIMEOUT"; |
| 147 | + constexpr char kOtlpTimeoutEnv[] = "OTEL_EXPORTER_OTLP_TIMEOUT"; |
| 148 | + |
| 149 | + auto timeout = opentelemetry::sdk::common::GetEnvironmentVariable(kOtlpTracesTimeoutEnv); |
| 150 | + if (timeout.empty()) |
| 151 | + { |
| 152 | + timeout = opentelemetry::sdk::common::GetEnvironmentVariable(kOtlpTimeoutEnv); |
| 153 | + } |
| 154 | + return GetOtlpTimeoutFromString(timeout.c_str()); |
| 155 | +} |
| 156 | + |
| 157 | +struct cmp_ic |
| 158 | +{ |
| 159 | + bool operator()(const std::string &s1, const std::string &s2) const |
| 160 | + { |
| 161 | + return std::lexicographical_compare( |
| 162 | + s1.begin(), s1.end(), s2.begin(), s2.end(), |
| 163 | + [](char c1, char c2) { return ::tolower(c1) < ::tolower(c2); }); |
| 164 | + } |
| 165 | +}; |
| 166 | +using OtlpHeaders = std::multimap<std::string, std::string, cmp_ic>; |
| 167 | + |
| 168 | +inline void DumpOtlpHeaders(OtlpHeaders &output, const char *env_var_name) |
| 169 | +{ |
| 170 | + auto value = opentelemetry::sdk::common::GetEnvironmentVariable(env_var_name); |
| 171 | + if (value.empty()) |
| 172 | + { |
| 173 | + return; |
| 174 | + } |
| 175 | + |
| 176 | + opentelemetry::common::KeyValueStringTokenizer tokenizer{value}; |
| 177 | + opentelemetry::nostd::string_view header_key; |
| 178 | + opentelemetry::nostd::string_view header_value; |
| 179 | + bool header_valid = true; |
| 180 | + |
| 181 | + while (tokenizer.next(header_valid, header_key, header_value)) |
| 182 | + { |
| 183 | + if (header_valid) |
| 184 | + { |
| 185 | + output.emplace(std::make_pair((std::string)header_key, (std::string)header_value)); |
| 186 | + } |
| 187 | + } |
| 188 | +} |
| 189 | + |
| 190 | +inline OtlpHeaders GetOtlpDefaultHeaders() |
| 191 | +{ |
| 192 | + constexpr char kOtlpTracesHeadersEnv[] = "OTEL_EXPORTER_OTLP_TRACES_HEADERS"; |
| 193 | + constexpr char kOtlpHeadersEnv[] = "OTEL_EXPORTER_OTLP_HEADERS"; |
| 194 | + |
| 195 | + OtlpHeaders result; |
| 196 | + DumpOtlpHeaders(result, kOtlpHeadersEnv); |
| 197 | + DumpOtlpHeaders(result, kOtlpTracesHeadersEnv); |
| 198 | + return result; |
| 199 | +} |
| 200 | + |
| 201 | +} // namespace otlp |
| 202 | +} // namespace exporter |
| 203 | +OPENTELEMETRY_END_NAMESPACE |
0 commit comments