From 7cb56774c8682c0695ad4ea4148e1b6a6616f147 Mon Sep 17 00:00:00 2001 From: vepatil Date: Thu, 3 Dec 2026 16:03:00 +0530 Subject: [PATCH] fix(opentelemetry): export OTEL_* variables to NGINX workers NGINX replaces the environment of its worker processes, so only variables declared with the `env` directive survive the fork. The OpenTelemetry module resolves some of its settings in the worker (`OtelNgxStart` -> `Resource::Create` and the OTLP exporter), which meant OTEL_RESOURCE_ATTRIBUTES and OTEL_EXPORTER_OTLP_HEADERS were always read from an empty environment and silently ignored. Declare every OTEL_* variable found in the environment with the `env` directive when OpenTelemetry is enabled. Only the names are emitted, so credentials in OTEL_EXPORTER_OTLP_HEADERS are not written to the rendered nginx.conf. Co-Authored-By: Claude Opus 5 --- CHANGELOG.md | 1 + .../README.md | 3 ++ .../docker-compose.yml | 1 + doc/parameters.md | 21 ++++++++ gateway/conf/nginx.conf.liquid | 6 +++ spec/cli/nginx_conf_spec.lua | 52 +++++++++++++++++++ 6 files changed, 84 insertions(+) create mode 100644 spec/cli/nginx_conf_spec.lua diff --git a/CHANGELOG.md b/CHANGELOG.md index d7cfe8838..77801fe23 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - 3scale Batcher Policy: read the batched report keys while holding the lock, so a concurrent flush cannot leave the lock orphaned and stall requests for the lock timeout [Issue #1610](https://github.com/3scale/APIcast/issues/1610) - TimerTask: run the task inside a `pcall` so a failing run cannot stop the task from being scheduled again [Issue #1610](https://github.com/3scale/APIcast/issues/1610) - Docs: clarify that `APICAST_SERVICES_FILTER_BY_URL` matches the public base URL host name of a service, not the API backend, and that the regexp must not include the scheme [Issue #1069](https://github.com/3scale/APIcast/issues/1069) +- OpenTelemetry: declare every `OTEL_*` environment variable with the NGINX `env` directive so the OpenTelemetry SDK can read them from the worker processes, fixing `OTEL_RESOURCE_ATTRIBUTES` and `OTEL_EXPORTER_OTLP_HEADERS` being ignored [Issue #1427](https://github.com/3scale/APIcast/issues/1427) ### Added - Update APIcast schema manifest [PR #1550](https://github.com/3scale/APIcast/pull/1550) diff --git a/dev-environments/opentelemetry-instrumented-gateway/README.md b/dev-environments/opentelemetry-instrumented-gateway/README.md index abf168825..e3151996d 100644 --- a/dev-environments/opentelemetry-instrumented-gateway/README.md +++ b/dev-environments/opentelemetry-instrumented-gateway/README.md @@ -30,6 +30,9 @@ Open in local browser jaeger dashboard http://127.0.0.1:16686/search ``` +The gateway is started with `OTEL_RESOURCE_ATTRIBUTES=deployment.environment=dev-environment`, +so every trace shown in Jaeger is expected to carry a `deployment.environment` process tag. + ## Testing `GET` request diff --git a/dev-environments/opentelemetry-instrumented-gateway/docker-compose.yml b/dev-environments/opentelemetry-instrumented-gateway/docker-compose.yml index e3cada328..afd594591 100644 --- a/dev-environments/opentelemetry-instrumented-gateway/docker-compose.yml +++ b/dev-environments/opentelemetry-instrumented-gateway/docker-compose.yml @@ -18,6 +18,7 @@ services: APICAST_CONFIGURATION_CACHE: "0" OPENTELEMETRY: "1" OPENTELEMETRY_CONFIG: /opt/app-root/src/tracing-configs/otel.toml + OTEL_RESOURCE_ATTRIBUTES: deployment.environment=dev-environment expose: - "8080" - "8090" diff --git a/doc/parameters.md b/doc/parameters.md index 79404286e..d883a938c 100644 --- a/doc/parameters.md +++ b/doc/parameters.md @@ -586,3 +586,24 @@ name = "AlwaysOn" # Also: AlwaysOff, TraceIdRatioBased ratio = 0.1 parent_based = false ``` + +### `OTEL_*` + +**Example:** `OTEL_RESOURCE_ATTRIBUTES=deployment.environment=staging` + +When `OPENTELEMETRY` is enabled, every environment variable prefixed with `OTEL_` is +declared with the [`env` NGINX directive](https://nginx.org/en/docs/ngx_core_module.html#env) +and is therefore available to the OpenTelemetry SDK in the NGINX worker processes. This +covers the variables defined by the +[OpenTelemetry specification](https://opentelemetry.io/docs/specs/otel/configuration/sdk-environment-variables/) +that the bundled exporter understands, such as: + +| Variable | Description | +| -------- | ----------- | +| `OTEL_RESOURCE_ATTRIBUTES` | Comma separated `key=value` pairs added to the resource of every span, for example `deployment.environment=staging` | +| `OTEL_SERVICE_NAME` | Overrides the `[service] name` of the configuration file | +| `OTEL_EXPORTER_OTLP_ENDPOINT` | Overrides the `[exporters.otlp] host`/`port` of the configuration file | +| `OTEL_EXPORTER_OTLP_HEADERS` | Comma separated `key=value` pairs sent as gRPC metadata to the collector, for example `Authorization=Bearer ` | + +Values set in `OTEL_RESOURCE_ATTRIBUTES` do not override `service.name`, which is always +taken from `OTEL_SERVICE_NAME` or from the `[service]` section of `OPENTELEMETRY_CONFIG`. diff --git a/gateway/conf/nginx.conf.liquid b/gateway/conf/nginx.conf.liquid index 2ad5bcf9a..4d79adcd5 100644 --- a/gateway/conf/nginx.conf.liquid +++ b/gateway/conf/nginx.conf.liquid @@ -9,6 +9,12 @@ env OPENSSL_VERIFY; {% for file in "modules/otel_ngx_module.so" | filesystem %} load_module {{file}}; {% endfor %} +# NGINX replaces the environment of its worker processes, so variables that the +# OpenTelemetry SDK reads after the workers are forked (OTEL_RESOURCE_ATTRIBUTES, +# OTEL_EXPORTER_OTLP_HEADERS, ...) are invisible unless they are declared with +# the `env` directive. Export every OTEL_* variable found in the environment. + {% for variable in env %}{% if variable.name | starts_with: 'OTEL_' %} +env {{ variable.name }};{% endif %}{% endfor %} {% else %} {% if timer_resolution %} timer_resolution {{ timer_resolution }}; diff --git a/spec/cli/nginx_conf_spec.lua b/spec/cli/nginx_conf_spec.lua new file mode 100644 index 000000000..13d292f51 --- /dev/null +++ b/spec/cli/nginx_conf_spec.lua @@ -0,0 +1,52 @@ +local resty_env = require('resty.env') +local Template = require('apicast.cli.template') +local Environment = require('apicast.cli.environment') + +describe('conf/nginx.conf.liquid', function () + + local function render() + return Template:new(Environment.new():context(), 'gateway', true) + :render('conf/nginx.conf.liquid') + end + + local function contains(output, str) + return output:find(str, 1, true) ~= nil + end + + before_each(function() + resty_env.set('OPENTELEMETRY', '1') + resty_env.set('OTEL_RESOURCE_ATTRIBUTES', 'deployment.environment=staging') + resty_env.set('OTEL_EXPORTER_OTLP_HEADERS', 'Authorization=Bearer secret') + end) + + after_each(function() + resty_env.set('OPENTELEMETRY', nil) + resty_env.set('OTEL_RESOURCE_ATTRIBUTES', nil) + resty_env.set('OTEL_EXPORTER_OTLP_HEADERS', nil) + end) + + it('declares OTEL_* variables so that workers inherit them', function() + local output = render() + + assert.is_true(contains(output, 'env OTEL_RESOURCE_ATTRIBUTES;')) + assert.is_true(contains(output, 'env OTEL_EXPORTER_OTLP_HEADERS;')) + end) + + it('does not write the value of OTEL_* variables to the config', function() + assert.is_false(contains(render(), 'Bearer secret')) + end) + + it('does not declare variables that are not OTEL_*', function() + resty_env.set('NOT_AN_OTEL_VARIABLE', '1') + finally(function() resty_env.set('NOT_AN_OTEL_VARIABLE', nil) end) + + assert.is_false(contains(render(), 'env NOT_AN_OTEL_VARIABLE;')) + end) + + it('does not declare OTEL_* variables when OpenTelemetry is disabled', function() + resty_env.set('OPENTELEMETRY', nil) + + assert.is_false(contains(render(), 'env OTEL_RESOURCE_ATTRIBUTES;')) + end) + +end)