@@ -19,8 +19,8 @@ namespace {
19
19
20
20
// Return whether the specified `request` is a subrequest for which tracing
21
21
// ("logging") is disabled.
22
- bool is_untraced_subrequest (ngx_http_request_t * request) {
23
- auto core_loc_conf = static_cast <ngx_http_core_loc_conf_t *>(
22
+ bool is_untraced_subrequest (ngx_http_request_t * request) {
23
+ auto core_loc_conf = static_cast <ngx_http_core_loc_conf_t *>(
24
24
ngx_http_get_module_loc_conf (request, ngx_http_core_module));
25
25
26
26
return request->parent != nullptr && !core_loc_conf->log_subrequest ;
@@ -33,13 +33,9 @@ bool is_untraced_subrequest(ngx_http_request_t* request) {
33
33
// if valid, will resolve to some property on the active span, i.e.
34
34
// `datadog_trace_id` resolves to a string containing the trace ID. Return
35
35
// `NGX_OK` on success or another value if an error occurs.
36
- static ngx_int_t expand_span_variable (ngx_http_request_t * request,
37
- ngx_http_variable_value_t * variable_value,
36
+ static ngx_int_t expand_span_variable (ngx_http_request_t * request,
37
+ ngx_http_variable_value_t * variable_value,
38
38
uintptr_t data) noexcept try {
39
- auto variable_name = to_string_view (*reinterpret_cast <ngx_str_t *>(data));
40
- auto prefix_length = TracingLibrary::span_variables ().prefix .size ();
41
- auto suffix = slice (variable_name, prefix_length);
42
-
43
39
auto context = get_datadog_context (request);
44
40
// Context can be null if tracing is disabled.
45
41
if (context == nullptr || is_untraced_subrequest (request)) {
@@ -52,16 +48,29 @@ static ngx_int_t expand_span_variable(ngx_http_request_t* request,
52
48
return NGX_OK;
53
49
}
54
50
55
- auto span_variable_value =
56
- context->lookup_span_variable_value (request, suffix);
57
- variable_value->len = span_variable_value.len ;
58
- variable_value->valid = true ;
59
- variable_value->no_cacheable = true ;
60
- variable_value->not_found = false ;
61
- variable_value->data = span_variable_value.data ;
51
+ auto set_variable = [&](std::string_view suffix) {
52
+ auto span_variable_value =
53
+ context->lookup_span_variable_value (request, suffix);
54
+ variable_value->len = span_variable_value.len ;
55
+ variable_value->valid = true ;
56
+ variable_value->no_cacheable = true ;
57
+ variable_value->not_found = false ;
58
+ variable_value->data = span_variable_value.data ;
59
+ };
60
+
61
+ auto variable_name = to_string_view (*reinterpret_cast <ngx_str_t *>(data));
62
+ if (variable_name.starts_with (" datadog_" )) {
63
+ auto suffix =
64
+ slice (variable_name, TracingLibrary::span_variables ().prefix .size ());
65
+ set_variable (suffix);
66
+ } else if (std::string_view otel (" opentelemetry_" );
67
+ variable_name.starts_with (otel)) {
68
+ auto suffix = slice (variable_name, otel.size ());
69
+ set_variable (suffix);
70
+ }
62
71
63
72
return NGX_OK;
64
- } catch (const std::exception & e) {
73
+ } catch (const std::exception & e) {
65
74
ngx_log_error (NGX_LOG_ERR, request->connection ->log , 0 ,
66
75
" failed to expand %V"
67
76
" for request %p: %s" ,
@@ -79,9 +88,9 @@ static ngx_int_t expand_span_variable(ngx_http_request_t* request,
79
88
// `TracingLibrary::environment_variable_names`. Return `NGX_OK` on success or
80
89
// another value if an error occurs.
81
90
static ngx_int_t expand_environment_variable (
82
- ngx_http_request_t * request, ngx_http_variable_value_t * variable_value,
91
+ ngx_http_request_t * request, ngx_http_variable_value_t * variable_value,
83
92
uintptr_t data) noexcept {
84
- auto variable_name = to_string_view (*reinterpret_cast <ngx_str_t *>(data));
93
+ auto variable_name = to_string_view (*reinterpret_cast <ngx_str_t *>(data));
85
94
auto prefix_length =
86
95
TracingLibrary::environment_variable_name_prefix ().size ();
87
96
auto suffix = slice (variable_name, prefix_length);
@@ -91,7 +100,7 @@ static ngx_int_t expand_environment_variable(
91
100
to_upper);
92
101
93
102
const auto allow_list = TracingLibrary::environment_variable_names ();
94
- const char * env_value = nullptr ;
103
+ const char * env_value = nullptr ;
95
104
if (std::find (allow_list.begin (), allow_list.end (), env_var_name) !=
96
105
allow_list.end ()) {
97
106
env_value = std::getenv (env_var_name.c_str ());
@@ -123,13 +132,13 @@ static ngx_int_t expand_environment_variable(
123
132
// evaluates to a JSON representation of the tracer configuration. Return
124
133
// `NGX_OK` on success or another value if an error occurs.
125
134
static ngx_int_t expand_configuration_variable (
126
- ngx_http_request_t * request, ngx_http_variable_value_t * variable_value,
135
+ ngx_http_request_t * request, ngx_http_variable_value_t * variable_value,
127
136
uintptr_t /* data*/ ) noexcept {
128
137
variable_value->valid = true ;
129
138
variable_value->no_cacheable = true ;
130
139
variable_value->not_found = false ;
131
140
132
- const dd::Tracer* tracer = global_tracer ();
141
+ const dd::Tracer * tracer = global_tracer ();
133
142
if (tracer == nullptr ) {
134
143
// No tracer, no config. Evaluate to "-" (hyphen).
135
144
const ngx_str_t not_found_str = ngx_string (" -" );
@@ -150,21 +159,21 @@ static ngx_int_t expand_configuration_variable(
150
159
variable_value->data = json_str.data ;
151
160
}
152
161
153
- auto & alloc = doc.GetAllocator ();
162
+ auto & alloc = doc.GetAllocator ();
154
163
155
164
// override
156
- const auto & loc_conf = *static_cast <datadog::nginx::datadog_loc_conf_t *>(
165
+ const auto & loc_conf = *static_cast <datadog::nginx::datadog_loc_conf_t *>(
157
166
ngx_http_get_module_loc_conf (request, ngx_http_datadog_module));
158
167
159
168
auto append_to_doc = [&](std::string_view key,
160
- ngx_http_complex_value_t * value) {
169
+ ngx_http_complex_value_t * value) {
161
170
if (value == nullptr ) return ;
162
171
163
172
ngx_str_t res;
164
173
if (ngx_http_complex_value (request, value, &res) == NGX_OK &&
165
174
res.len != 0 ) {
166
175
doc.AddMember (rapidjson::Value (key.data (), key.size (), alloc),
167
- rapidjson::Value ((char *)res.data , res.len , alloc), alloc);
176
+ rapidjson::Value ((char *)res.data , res.len , alloc), alloc);
168
177
}
169
178
};
170
179
@@ -215,9 +224,9 @@ static ngx_int_t expand_configuration_variable(
215
224
//
216
225
// Return `NGX_OK` on success or another value if an error occurs.
217
226
static ngx_int_t expand_location_variable (
218
- ngx_http_request_t * request, ngx_http_variable_value_t * variable_value,
227
+ ngx_http_request_t * request, ngx_http_variable_value_t * variable_value,
219
228
uintptr_t /* data*/ ) noexcept {
220
- const auto core_loc_conf = static_cast <ngx_http_core_loc_conf_t *>(
229
+ const auto core_loc_conf = static_cast <ngx_http_core_loc_conf_t *>(
221
230
ngx_http_get_module_loc_conf (request, ngx_http_core_module));
222
231
223
232
if (core_loc_conf == nullptr ) {
@@ -240,9 +249,9 @@ static ngx_int_t expand_location_variable(
240
249
return NGX_OK;
241
250
}
242
251
243
- ngx_int_t add_variables (ngx_conf_t * cf) noexcept {
252
+ ngx_int_t add_variables (ngx_conf_t * cf) noexcept {
244
253
ngx_str_t prefix;
245
- ngx_http_variable_t * variable;
254
+ ngx_http_variable_t * variable;
246
255
247
256
// Register the variable name prefix for span variables.
248
257
prefix = to_ngx_str (TracingLibrary::span_variables ().prefix );
@@ -261,6 +270,15 @@ ngx_int_t add_variables(ngx_conf_t* cf) noexcept {
261
270
variable->get_handler = expand_environment_variable;
262
271
variable->data = 0 ;
263
272
273
+ // Register the variable name prefix for OpenTelemetry-relevant environment
274
+ // variables.
275
+ prefix = ngx_string (" opentelemetry_" );
276
+ variable = ngx_http_add_variable (
277
+ cf, &prefix,
278
+ NGX_HTTP_VAR_NOCACHEABLE | NGX_HTTP_VAR_NOHASH | NGX_HTTP_VAR_PREFIX);
279
+ variable->get_handler = expand_span_variable;
280
+ variable->data = 0 ;
281
+
264
282
// Register the variable name for getting the tracer configuration.
265
283
ngx_str_t name =
266
284
to_ngx_str (TracingLibrary::configuration_json_variable_name ());
0 commit comments