Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
21 changes: 21 additions & 0 deletions doc/parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <token>` |

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`.
6 changes: 6 additions & 0 deletions gateway/conf/nginx.conf.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -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 }};
Expand Down
52 changes: 52 additions & 0 deletions spec/cli/nginx_conf_spec.lua
Original file line number Diff line number Diff line change
@@ -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)
Loading