5
5
#include < cstdlib>
6
6
#include < exception>
7
7
#include < iterator>
8
+ #include < string>
8
9
#include < utility>
9
10
10
11
#include " datadog_conf.h"
@@ -25,6 +26,7 @@ extern "C" {
25
26
#include < ngx_config.h>
26
27
#include < ngx_core.h>
27
28
#include < ngx_http.h>
29
+ #include < stdlib.h> // ::setenv
28
30
}
29
31
30
32
// clang-format off
@@ -68,7 +70,6 @@ static ngx_uint_t anywhere =
68
70
| NGX_HTTP_MAIN_CONF; // the toplevel configuration, e.g. where modules are loaded
69
71
70
72
static ngx_command_t datadog_commands[] = {
71
-
72
73
{ ngx_string (" opentracing" ),
73
74
anywhere | NGX_CONF_TAKE1,
74
75
toggle_opentracing,
@@ -213,7 +214,7 @@ static ngx_command_t datadog_commands[] = {
213
214
NGX_HTTP_LOC_CONF_OFFSET,
214
215
0 ,
215
216
nullptr },
216
-
217
+
217
218
ngx_null_command
218
219
};
219
220
@@ -247,52 +248,7 @@ ngx_module_t ngx_http_datadog_module = {
247
248
};
248
249
// clang-format on
249
250
250
- // Configure nginx to set the environment variable as indicated by the
251
- // specified `entry` in the context of the specified `cycle`. `entry` is a
252
- // string in one of the following forms:
253
- //
254
- // 1. "FOO"
255
- // 2. "FOO=value"
256
- //
257
- // The environment variable name in this example is "FOO". In the case of the
258
- // first form, the value of the environment variable will be inherited from the
259
- // parent process. In the case of the second form, the value of the
260
- // environment variable will be as specified after the equal sign.
261
- //
262
- // Note that `ngx_set_env` is adapted from the function of the same name in
263
- // `nginx.c` within the nginx source code.
264
- static void *ngx_set_env (string_view entry, ngx_cycle_t *cycle) {
265
- ngx_core_conf_t *ccf = (ngx_core_conf_t *)ngx_get_conf (cycle->conf_ctx , ngx_core_module);
266
-
267
- ngx_str_t *value, *var;
268
- ngx_uint_t i;
269
-
270
- var = (ngx_str_t *)ngx_array_push (&ccf->env );
271
- if (var == NULL ) {
272
- return NGX_CONF_ERROR;
273
- }
274
-
275
- const ngx_str_t entry_str = to_ngx_str (entry);
276
- *var = entry_str;
277
-
278
- for (i = 0 ; i < var->len ; i++) {
279
- if (var->data [i] == ' =' ) {
280
- var->len = i;
281
- return NGX_CONF_OK;
282
- }
283
- }
284
-
285
- return NGX_CONF_OK;
286
- }
287
-
288
251
static ngx_int_t datadog_master_process_post_config (ngx_cycle_t *cycle) noexcept {
289
- // Forward tracer-specific environment variables to worker processes.
290
- for (const auto &env_var_name : TracingLibrary::environment_variable_names ()) {
291
- if (const void *const error = ngx_set_env (env_var_name, cycle)) {
292
- return ngx_int_t (error);
293
- }
294
- }
295
-
296
252
// If tracing has not so far been configured, then give it a default
297
253
// configuration. This means that the nginx configuration did not use the
298
254
// `datadog` directive, and did not use any overridden directives, such as
@@ -304,6 +260,16 @@ static ngx_int_t datadog_master_process_post_config(ngx_cycle_t *cycle) noexcept
304
260
main_conf->tracer_conf = ngx_string (" " ); // default config
305
261
}
306
262
263
+ // Forward tracer-specific environment variables to worker processes.
264
+ std::string name;
265
+ for (const auto &env_var_name : TracingLibrary::environment_variable_names ()) {
266
+ name = env_var_name;
267
+ if (const char *value = std::getenv (name.c_str ())) {
268
+ main_conf->environment_variables .push_back (
269
+ environment_variable_t {.name = name, .value = value});
270
+ }
271
+ }
272
+
307
273
return NGX_OK;
308
274
}
309
275
@@ -343,6 +309,11 @@ static ngx_int_t datadog_init_worker(ngx_cycle_t *cycle) noexcept try {
343
309
return NGX_OK;
344
310
}
345
311
312
+ for (const auto &entry : main_conf->environment_variables ) {
313
+ const bool overwrite = false ;
314
+ ::setenv (entry.name.c_str(), entry.value.c_str(), overwrite);
315
+ }
316
+
346
317
std::shared_ptr<ot::Tracer> tracer = load_tracer (cycle->log , str (main_conf->tracer_conf ));
347
318
if (!tracer) {
348
319
return NGX_ERROR;
0 commit comments