Skip to content

Commit 9323586

Browse files
weltekialexellis
authored andcommitted
Add OTEL zero code instumentation docs for Node.js and Python
Signed-off-by: Han Verstraete (OpenFaaS Ltd) <[email protected]>
1 parent e30b848 commit 9323586

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

docs/languages/node.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,3 +278,45 @@ This is useful when the function is expected to receive large amounts of JSON da
278278
MAX_JSON_SIZE: '5mb'
279279
```
280280

281+
## OpenTelemetry zero-code instrumentation
282+
283+
Using [OpenTelemetry zero-code instrumentation](https://opentelemetry.io/docs/zero-code/js/) for Node.js functions requires some minor modifications to the existing Node.js templates.
284+
285+
There are two ways to [customise a template](/cli/templates/#how-to-customise-a-template):
286+
287+
- Fork the template repository and modify the template. Recommended method that allows for distribution and reuse of the template.
288+
- Pull the template and apply patches directly in the `./template/<language_name>` directory. Good for quick iteration and experimentation with template modifications. The modified template can not be shared and reused. Changes may get overwritten when pulling templates again.
289+
290+
Add the required packages for zero code instrumentation to the template `package.json`:
291+
292+
```sh
293+
npm install --save @opentelemetry/api
294+
npm install --save @opentelemetry/auto-instrumentations-node
295+
```
296+
297+
Use your modified template to create a new function.
298+
299+
The OpenTelemetry agent can be configured using environment variables on the function:
300+
301+
```diff
302+
functions:
303+
echo:
304+
lang: node20
305+
handler: ./node
306+
image: echo:latest
307+
+ environment:
308+
+ OTEL_SERVICE_NAME: echo.${NAMESPACE:-openfaas-fn}
309+
+ OTEL_TRACES_EXPORTER: console,otlp
310+
+ OTEL_METRICS_EXPORTER: none
311+
+ OTEL_LOGS_EXPORTER: none
312+
+ OTEL_EXPORTER_OTLP_ENDPOINT:${OTEL_EXPORTER_OTLP_ENDPOINT:-collector:4317}
313+
+ NODE_OPTIONS: "--require @opentelemetry/auto-instrumentations-node/register"
314+
```
315+
316+
- `OTEL_SERVICE_NAME` sets the name of the service associated with the telemetry and is used to identify telemetry for a specific function. It can be set to any value you want be we recommend using the clear function identifier `<fn-name>.<fn-namespace>`.
317+
- `OTEL_TRACES_EXPORTER` specifies which tracer exporter to use. In this example traces are exported to `console` (stdout) and with `otlp`. The `otlp` option tells the instrumentation module to send the traces to an endpoint that accepts OTLP via gRPC.
318+
- setting `OTEL_METRICS_EXPORTER` and `OTEL_LOGS_EXPORTER` to `none` we disable the metrics and logs exporters. You can enable them if desired.
319+
- `OTEL_EXPORTER_OTLP_ENDPOINT` sets the endpoint where telemetry is exported to.
320+
- The `NODE_OPTIONS` environment variable needs to have the value `--require @opentelemetry/auto-instrumentations-node/register` to register and initialize the auto instrumentation module.
321+
322+
To see the full range of configuration options, see [Module Configuration](https://opentelemetry.io/docs/zero-code/js/configuration/).

docs/languages/python.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,3 +385,70 @@ faas-cli publish --filter fn1\
385385
--build-arg UPGRADE_PACKAGES=true
386386
```
387387

388+
## OpenTelemetry zero-code instrumentation
389+
390+
Using [OpenTelemetry zero-code instrumentation](https://opentelemetry.io/docs/zero-code/python/) for python functions requires some minor modifications to the existing Python templates.
391+
392+
There are two ways to [customise a template](/cli/templates/#how-to-customise-a-template):
393+
394+
- Fork the template repository and modify the template. Recommended method that allows for distribution and reuse of the template.
395+
- Pull the template and apply patches directly in the `./template/<language_name>` directory. Good for quick iteration and experimentation with template modifications. The modified template can not be shared and reused. Changes may get overwritten when pulling templates again.
396+
397+
Add the required packages for auto instrumentation to the `requirements.txt` file of the template:
398+
399+
```
400+
opentelemetry-distro
401+
opentelemetry-exporter-otlp
402+
```
403+
404+
Update the Dockerfile to run the bootstrap command after the the template and function packages have been installed:
405+
406+
```diff
407+
# Build the function directory and install any user-specified components
408+
USER app
409+
410+
RUN mkdir -p function
411+
RUN touch ./function/__init__.py
412+
WORKDIR /home/app/function/
413+
COPY --chown=app:app function/requirements.txt .
414+
RUN pip install --no-cache-dir --user -r requirements.txt
415+
+ RUN opentelemetry-bootstrap -a install
416+
```
417+
418+
The `opentelemetry-bootstrap -a install` command reads through the list of packages installed in your active site-packages folder, and installs the corresponding instrumentation libraries for these packages, if applicable. The OpenTelemetry Python agent uses [monkey patching](https://stackoverflow.com/questions/5626193/what-is-monkey-patching) to modify functions in these libraries at runtime.
419+
420+
421+
Update the fprocess ENV in the Dockerfile to start the OpenTelemetry agent:
422+
423+
```diff
424+
# configure WSGI server and healthcheck
425+
USER app
426+
427+
- ENV fprocess="python index.py"
428+
+ ENV fprocess="opentelemetry-instrument python index.py"
429+
```
430+
431+
Use your modified template to create a new function.
432+
433+
The OpenTelemetry agent can be configured using environment variables on the function:
434+
435+
```diff
436+
functions:
437+
greet:
438+
lang: python3-http-otel
439+
handler: ./greet
440+
image: greet:latest
441+
+ environment:
442+
+ OTEL_SERVICE_NAME: greet.${NAMESPACE:-openfaas-fn}
443+
+ OTEL_TRACES_EXPORTER: console,otlp
444+
+ OTEL_METRICS_EXPORTER: none
445+
+ OTEL_LOGS_EXPORTER: none
446+
+ OTEL_EXPORTER_OTLP_ENDPOINT: ${OTEL_EXPORTER_OTLP_ENDPOINT:-collector:4317}
447+
```
448+
449+
- `OTEL_SERVICE_NAME` sets the name of the service associated with the telemetry and is used to identify telemetry for a specific function. It can be set to any value you want be we recommend using the clear function identifier `<fn-name>.<fn-namespace>`.
450+
- `OTEL_TRACES_EXPORTER` specifies which tracer exporter to use. In this example traces are exported to `console` (stdout) and with `otlp`. The `otlp` option tells `opentelemetry-instrument` to send the traces to an endpoint that accepts OTLP via gRPC.
451+
- setting `OTEL_METRICS_EXPORTER` and `OTEL_LOGS_EXPORTER` to `none` we disable the metrics and logs exporters. You can enable them if desired.
452+
- `OTEL_EXPORTER_OTLP_ENDPOINT` sets the endpoint where telemetry is exported to.
453+
454+
To see the full range of configuration options, see [Agent Configuration](https://opentelemetry.io/docs/zero-code/python/configuration/)

0 commit comments

Comments
 (0)