Commit 90f7fa0
authored
Opt-in config and logic for deferral of instrumentation to only WSGI worker processes (#243)
### Issue:
OTel Python [has
issues](open-telemetry/opentelemetry-python#2767)
where the SDK is unable to report metrics for applications using a fork
process model WSGI server.
This affects ADOT when it tries to generate the OTel or Application
Signals metrics.
A solution to this is to [re-initialize the SDK in the worker processes
after the process forking as
happened](https://opentelemetry-python.readthedocs.io/en/latest/examples/fork-process-model/README.html).
A small caveat is that if the SDK has been initialized in the master
process, the worker process SDK won't work because Tracer/Meter
providers can be set globally only once. So to circumvent this, we need
to skip initializing the SDK in the master process and only do so in the
worker processes.
### Description of changes:
- Introducing an opt-in configuration environment variable
`OTEL_AWS_PYTHON_DEFER_TO_WORKERS_ENABLED` to enable if they are using a
WSGI (or a fork process model) server and want the ADOT SDK to defer
auto-instrumentation to worker processes.
- Whenever the ADOT SDK auto-instrumentation is loaded (either via the
`sitecustomize.py` file or the `opentelemetry-instrument` command), the
SDK will check if the above configuration is enabled and if the current
process is the master process, and will skip the instrumentation.
- The way we determine if the current process is master or worker is by
using an internal marker environment variable
`IS_WSGI_MASTER_PROCESS_ALREADY_SEEN`. The first time the ADOT SDK sees
a python process, this env var is not set and it will know this should
be a WSGI master process. We then set the env var and when a new worker
process forks, the master environment is copied to it (and so the env
var). So when the ADOT SDK checks this env var again (in worker) it
finds that the env var was already set to `true` in the master.
### Testing:
- Unit tests covering the functionalities bases on different
configurations of the `OTEL_AWS_PYTHON_DEFER_TO_WORKERS_ENABLED` and
`IS_WSGI_MASTER_PROCESS_ALREADY_SEEN` variables.
- Manual test using a sample application. Since this is an opt-in
configuration (a 2-way door), testing manually gives us a fair bit of
confidence.
By submitting this pull request, I confirm that you can use, modify,
copy, and redistribute this contribution, under the terms of your
choice.1 parent 1c675d3 commit 90f7fa0
2 files changed
Lines changed: 73 additions & 0 deletions
File tree
- aws-opentelemetry-distro
- src/amazon/opentelemetry/distro
- tests/amazon/opentelemetry/distro
Lines changed: 27 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
65 | 65 | | |
66 | 66 | | |
67 | 67 | | |
| 68 | + | |
68 | 69 | | |
69 | 70 | | |
70 | 71 | | |
| |||
85 | 86 | | |
86 | 87 | | |
87 | 88 | | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
88 | 94 | | |
89 | 95 | | |
90 | 96 | | |
| |||
156 | 162 | | |
157 | 163 | | |
158 | 164 | | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
159 | 186 | | |
160 | 187 | | |
161 | 188 | | |
| |||
aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/test_aws_opentelementry_configurator.py
Lines changed: 46 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| 19 | + | |
| 20 | + | |
19 | 21 | | |
20 | 22 | | |
21 | 23 | | |
| |||
305 | 307 | | |
306 | 308 | | |
307 | 309 | | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
308 | 354 | | |
309 | 355 | | |
310 | 356 | | |
| |||
0 commit comments