|
6 | 6 | // Each `enum Variable` denotes an environment variable. The enum value names |
7 | 7 | // are the same as the names of the environment variables. |
8 | 8 | // |
9 | | -// `variable_names` is an array of the names of the environment variables. Nginx |
10 | | -// uses `variable_names` as an allow list of environment variables to forward to |
11 | | -// worker processes. |
12 | | -// |
13 | 9 | // `name` returns the name of a specified `Variable`. |
14 | 10 | // |
15 | 11 | // `lookup` retrieves the value of `Variable` in the environment. |
16 | 12 |
|
| 13 | +#include <datadog/environment_registry.h> |
| 14 | +#include <datadog/expected.h> |
17 | 15 | #include <datadog/optional.h> |
18 | 16 | #include <datadog/string_view.h> |
19 | 17 |
|
| 18 | +#include <cstdint> |
| 19 | +#include <cstdlib> |
| 20 | + |
20 | 21 | namespace datadog { |
21 | 22 | namespace tracing { |
22 | 23 | namespace environment { |
23 | 24 |
|
24 | | -// To enforce correspondence between `enum Variable` and `variable_names`, the |
25 | | -// preprocessor is used so that the DD_* symbols are listed exactly once. |
26 | | -#define LIST_ENVIRONMENT_VARIABLES(MACRO) \ |
27 | | - MACRO(DD_AGENT_HOST) \ |
28 | | - MACRO(DD_ENV) \ |
29 | | - MACRO(DD_INSTRUMENTATION_TELEMETRY_ENABLED) \ |
30 | | - MACRO(DD_PROPAGATION_STYLE_EXTRACT) \ |
31 | | - MACRO(DD_PROPAGATION_STYLE_INJECT) \ |
32 | | - MACRO(DD_REMOTE_CONFIGURATION_ENABLED) \ |
33 | | - MACRO(DD_REMOTE_CONFIG_POLL_INTERVAL_SECONDS) \ |
34 | | - MACRO(DD_SERVICE) \ |
35 | | - MACRO(DD_SPAN_SAMPLING_RULES) \ |
36 | | - MACRO(DD_SPAN_SAMPLING_RULES_FILE) \ |
37 | | - MACRO(DD_TRACE_PROPAGATION_STYLE_EXTRACT) \ |
38 | | - MACRO(DD_TRACE_PROPAGATION_STYLE_INJECT) \ |
39 | | - MACRO(DD_TRACE_PROPAGATION_STYLE) \ |
40 | | - MACRO(DD_TAGS) \ |
41 | | - MACRO(DD_TRACE_AGENT_PORT) \ |
42 | | - MACRO(DD_TRACE_AGENT_URL) \ |
43 | | - MACRO(DD_TRACE_DEBUG) \ |
44 | | - MACRO(DD_TRACE_ENABLED) \ |
45 | | - MACRO(DD_TRACE_RATE_LIMIT) \ |
46 | | - MACRO(DD_TRACE_REPORT_HOSTNAME) \ |
47 | | - MACRO(DD_TRACE_SAMPLE_RATE) \ |
48 | | - MACRO(DD_TRACE_SAMPLING_RULES) \ |
49 | | - MACRO(DD_TRACE_STARTUP_LOGS) \ |
50 | | - MACRO(DD_TRACE_TAGS_PROPAGATION_MAX_LENGTH) \ |
51 | | - MACRO(DD_VERSION) \ |
52 | | - MACRO(DD_TRACE_128_BIT_TRACEID_GENERATION_ENABLED) \ |
53 | | - MACRO(DD_TELEMETRY_HEARTBEAT_INTERVAL) \ |
54 | | - MACRO(DD_TELEMETRY_METRICS_ENABLED) \ |
55 | | - MACRO(DD_TELEMETRY_METRICS_INTERVAL_SECONDS) \ |
56 | | - MACRO(DD_TELEMETRY_DEBUG) \ |
57 | | - MACRO(DD_TRACE_BAGGAGE_MAX_ITEMS) \ |
58 | | - MACRO(DD_TRACE_BAGGAGE_MAX_BYTES) \ |
59 | | - MACRO(DD_TELEMETRY_LOG_COLLECTION_ENABLED) \ |
60 | | - MACRO(DD_INSTRUMENTATION_INSTALL_ID) \ |
61 | | - MACRO(DD_INSTRUMENTATION_INSTALL_TYPE) \ |
62 | | - MACRO(DD_INSTRUMENTATION_INSTALL_TIME) \ |
63 | | - MACRO(DD_APM_TRACING_ENABLED) \ |
64 | | - MACRO(DD_TRACE_RESOURCE_RENAMING_ENABLED) \ |
65 | | - MACRO(DD_TRACE_RESOURCE_RENAMING_ALWAYS_SIMPLIFIED_ENDPOINT) \ |
66 | | - MACRO(DD_EXTERNAL_ENV) |
67 | | - |
68 | | -#define WITH_COMMA(ARG) ARG, |
69 | | - |
70 | | -enum Variable { LIST_ENVIRONMENT_VARIABLES(WITH_COMMA) }; |
| 25 | +enum class VariableType { |
| 26 | + STRING, |
| 27 | + BOOLEAN, |
| 28 | + INT, |
| 29 | + DECIMAL, |
| 30 | + ARRAY, |
| 31 | + MAP, |
| 32 | +}; |
| 33 | + |
| 34 | +struct VariableSpec { |
| 35 | + StringView name; |
| 36 | + VariableType type; |
| 37 | +}; |
| 38 | + |
| 39 | +#define VARIABLE_ENUM_VALUE(DATA, NAME, TYPE, DEFAULT_VALUE) NAME, |
| 40 | + |
| 41 | +enum Variable { DD_ENVIRONMENT_VARIABLES(VARIABLE_ENUM_VALUE, ~) }; |
71 | 42 |
|
72 | 43 | // Quoting a macro argument requires this two-step. |
73 | 44 | #define QUOTED_IMPL(ARG) #ARG |
74 | 45 | #define QUOTED(ARG) QUOTED_IMPL(ARG) |
75 | 46 |
|
76 | | -#define QUOTED_WITH_COMMA(ARG) WITH_COMMA(QUOTED(ARG)) |
| 47 | +#define VARIABLE_SPEC_WITH_COMMA(DATA, NAME, TYPE, DEFAULT_VALUE) \ |
| 48 | + VariableSpec{StringView{QUOTED(NAME)}, VariableType::TYPE}, |
| 49 | + |
| 50 | +inline const VariableSpec variable_specs[] = { |
| 51 | + DD_ENVIRONMENT_VARIABLES(VARIABLE_SPEC_WITH_COMMA, ~)}; |
| 52 | + |
| 53 | +template <VariableType type> |
| 54 | +struct LookupResultByType; |
| 55 | + |
| 56 | +template <> |
| 57 | +struct LookupResultByType<VariableType::STRING> { |
| 58 | + using type = Optional<StringView>; |
| 59 | +}; |
| 60 | + |
| 61 | +template <> |
| 62 | +struct LookupResultByType<VariableType::BOOLEAN> { |
| 63 | + using type = Optional<bool>; |
| 64 | +}; |
| 65 | + |
| 66 | +template <> |
| 67 | +struct LookupResultByType<VariableType::INT> { |
| 68 | + using type = Expected<Optional<std::uint64_t>>; |
| 69 | +}; |
| 70 | + |
| 71 | +template <> |
| 72 | +struct LookupResultByType<VariableType::DECIMAL> { |
| 73 | + using type = Expected<Optional<double>>; |
| 74 | +}; |
77 | 75 |
|
78 | | -inline const char *const variable_names[] = { |
79 | | - LIST_ENVIRONMENT_VARIABLES(QUOTED_WITH_COMMA)}; |
| 76 | +template <> |
| 77 | +struct LookupResultByType<VariableType::ARRAY> { |
| 78 | + using type = Optional<StringView>; |
| 79 | +}; |
80 | 80 |
|
81 | | -#undef QUOTED_WITH_COMMA |
| 81 | +template <> |
| 82 | +struct LookupResultByType<VariableType::MAP> { |
| 83 | + using type = Optional<StringView>; |
| 84 | +}; |
| 85 | + |
| 86 | +template <Variable variable> |
| 87 | +struct VariableTraits; |
| 88 | + |
| 89 | +#define VARIABLE_TRAITS_VALUE(DATA, NAME, TYPE, DEFAULT_VALUE) \ |
| 90 | + template <> \ |
| 91 | + struct VariableTraits<NAME> { \ |
| 92 | + static constexpr VariableType variable_type = VariableType::TYPE; \ |
| 93 | + static constexpr const char *name() { return QUOTED(NAME); } \ |
| 94 | + using lookup_result = typename LookupResultByType<variable_type>::type; \ |
| 95 | + }; |
| 96 | + |
| 97 | +DD_ENVIRONMENT_VARIABLES(VARIABLE_TRAITS_VALUE, ~) |
| 98 | + |
| 99 | +template <Variable variable> |
| 100 | +using LookupResult = typename VariableTraits<variable>::lookup_result; |
| 101 | + |
| 102 | +namespace detail { |
| 103 | +template <VariableType> |
| 104 | +inline constexpr bool unsupported_variable_type_v = false; |
| 105 | + |
| 106 | +template <Variable variable> |
| 107 | +Optional<StringView> lookup_raw() { |
| 108 | + const char *value = std::getenv(VariableTraits<variable>::name()); |
| 109 | + if (!value) { |
| 110 | + return nullopt; |
| 111 | + } |
| 112 | + return StringView{value}; |
| 113 | +} |
| 114 | + |
| 115 | +Optional<bool> lookup_bool_from_raw(Optional<StringView> value); |
| 116 | +Expected<Optional<std::uint64_t>> lookup_uint64_from_raw( |
| 117 | + Optional<StringView> value); |
| 118 | +Expected<Optional<double>> lookup_double_from_raw(Optional<StringView> value); |
| 119 | +} // namespace detail |
| 120 | + |
| 121 | +template <Variable variable> |
| 122 | +LookupResult<variable> lookup() { |
| 123 | + constexpr VariableType type = VariableTraits<variable>::variable_type; |
| 124 | + const auto raw = detail::lookup_raw<variable>(); |
| 125 | + if constexpr (type == VariableType::STRING || type == VariableType::ARRAY || |
| 126 | + type == VariableType::MAP) { |
| 127 | + return raw; |
| 128 | + } else if constexpr (type == VariableType::BOOLEAN) { |
| 129 | + return detail::lookup_bool_from_raw(raw); |
| 130 | + } else if constexpr (type == VariableType::INT) { |
| 131 | + return detail::lookup_uint64_from_raw(raw); |
| 132 | + } else if constexpr (type == VariableType::DECIMAL) { |
| 133 | + return detail::lookup_double_from_raw(raw); |
| 134 | + } else { |
| 135 | + static_assert(detail::unsupported_variable_type_v<type>, |
| 136 | + "Unsupported environment variable type"); |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +#undef VARIABLE_SPEC_WITH_COMMA |
| 141 | +#undef VARIABLE_TRAITS_VALUE |
82 | 142 | #undef QUOTED |
83 | 143 | #undef QUOTED_IMPL |
84 | | -#undef WITH_COMMA |
85 | | -#undef LIST_ENVIRONMENT_VARIABLES |
| 144 | +#undef VARIABLE_ENUM_VALUE |
| 145 | + |
| 146 | +// Return the metadata for the specified environment `variable`. |
| 147 | +const VariableSpec &spec(Variable variable); |
86 | 148 |
|
87 | 149 | // Return the name of the specified environment `variable`. |
88 | 150 | StringView name(Variable variable); |
89 | 151 |
|
90 | | -// Return the value of the specified environment `variable`, or return |
91 | | -// `nullopt` if that variable is not set in the environment. |
92 | | -Optional<StringView> lookup(Variable variable); |
93 | | - |
94 | 152 | std::string to_json(); |
95 | 153 |
|
96 | 154 | } // namespace environment |
|
0 commit comments