famous-xylophone-36532
02/23/2023, 2:05 PMfamous-xylophone-36532
02/23/2023, 2:20 PMfamous-xylophone-36532
02/23/2023, 2:21 PMfamous-xylophone-36532
02/23/2023, 2:21 PMenough-analyst-54434
02/23/2023, 2:41 PMfamous-xylophone-36532
02/23/2023, 2:48 PMfamous-xylophone-36532
02/23/2023, 2:53 PM@famous-xylophone-36532
not super familiar with pex internals. Auto instrumentation works
through setting PYTHONPATH to include a sitecustomize.py file which does
the monkey patching at startup. If pex is overriding the PYTHONPATH
before it starts the interpreter, that might be the issue. You can try
logging the sys.path in your code to check?
enough-analyst-54434
02/23/2023, 3:08 PMPYTHONPATH
.famous-xylophone-36532
02/23/2023, 3:12 PMfamous-xylophone-36532
02/23/2023, 3:29 PMfamous-xylophone-36532
02/23/2023, 3:30 PMopentelemetry-instrument --traces_exporter console --metrics_exporter console --service_name your-service-name python ./bin.pex
(FastAPI application) built with execution_mode="venv"
and inherit_path="prefer"
and it does not emit any logs.famous-xylophone-36532
02/23/2023, 3:36 PMsys.path
from inside a pex, even running in venv mode and with inherit_path, seems to indicate that that isn't working:
['', '/root/.pex/venvs/e3d3f05fa9939d70cefbb33a63372359b532d899/779eb2cc0ca9e2fdd204774cbc41848e4e7c5055', '/usr/local/lib/python39.zip', '/usr/local/lib/python3.9', '/usr/local/lib/python3.9/lib-dynload', '/root/.pex/venvs/e3d3f05fa9939d70cefbb33a63372359b532d899/779eb2cc0ca9e2fdd204774cbc41848e4e7c5055/lib/python3.9/site-packages']
famous-xylophone-36532
02/23/2023, 3:38 PM['/mnt/wd', '/usr/local/lib/python3.9/site-packages/opentelemetry/instrumentation/auto_instrumentation', '/mnt/wd', '/usr/local/lib/python39.zip', '/usr/local/lib/python3.9', '/usr/local/lib/python3.9/lib-dynload', '/usr/local/lib/python3.9/site-packages']
famous-xylophone-36532
02/23/2023, 3:39 PMfamous-xylophone-36532
02/23/2023, 3:53 PMsys.path
.famous-xylophone-36532
02/23/2023, 3:54 PMenough-analyst-54434
02/23/2023, 3:58 PMfamous-xylophone-36532
02/23/2023, 3:58 PMenough-analyst-54434
02/23/2023, 3:59 PMfamous-xylophone-36532
02/23/2023, 3:59 PMopentelemetry-instrument
(https://github.com/open-telemetry/opentelemetry-operator)famous-xylophone-36532
02/23/2023, 4:00 PMfamous-xylophone-36532
02/23/2023, 4:04 PMopentelemetry-autoinstrument
cli: https://opentelemetry.io/docs/instrumentation/python/automatic/
How opentelemetry injects in Kubernetes with the above-posted operator:
https://github.com/open-telemetry/opentelemetry-operator/blob/0067c3547b1032c0a1c9f7d45391e18d79b68289/pkg/instrumentation/python.go#L54-L59enough-analyst-54434
02/23/2023, 5:18 PM$ tree
.
āāā src
āāā app.py
1 directory, 1 file
$ cat src/app.py
import os
import sys
from random import randint
from flask import Flask, request
app = Flask(__name__)
@app.route("/rolldice")
def roll_dice():
return str(do_roll())
def do_roll():
return randint(1, 6)
print(f"sys.path:\n{os.linesep.join(sys.path)}", file=sys.stderr)
# The PEX must contain all needed opentelemetry requirements:
$ pex opentelemetry-distro opentelemetry-instrumentation-flask flask requests -D src -c opentelemetry-instrument -o app.pex --include-tools
# The PEX must then be installed in a venv and its console scripts both ppre-pended to the PATH and made non-hermetic so PYTHONPATH can leak through and influence them.
$ PEX_TOOLS=1 ./app.pex venv --non-hermetic-scripts --bin-path prepend app/venv
$ app/venv/pex --traces_exporter console --metrics_exporter console flask --app app run
sys.path:
/home/jsirois/support/OpenTelemetryPex
/home/jsirois/support/OpenTelemetryPex/app/venv/bin
/home/jsirois/support/OpenTelemetryPex/app/venv/lib/python3.10/site-packages/opentelemetry/instrumentation/auto_instrumentation
/home/jsirois/support/OpenTelemetryPex
/usr/lib/python310.zip
/usr/lib/python3.10
/usr/lib/python3.10/lib-dynload
/home/jsirois/support/OpenTelemetryPex/app/venv/lib/python3.10/site-packages
* Serving Flask app 'app'
* Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on <http://127.0.0.1:5000>
Press CTRL+C to quit
^Z
[1]+ Stopped app/venv/pex --traces_exporter console --metrics_exporter console flask --app app run
$ bg %1
[1]+ app/venv/pex --traces_exporter console --metrics_exporter console flask --app app run &
# Test it:
$ curl <http://127.0.0.1:5000/rolldice>
127.0.0.1 - - [23/Feb/2023 09:17:18] "GET /rolldice HTTP/1.1" 200 -
5jsirois@Gill-Windows:~/support/OpenTelemetryPex $ {
"name": "/rolldice",
"context": {
"trace_id": "0x2ebd6f04e90dc521d927d4009b16fb98",
"span_id": "0x8cb36c0be87b2f65",
"trace_state": "[]"
},
"kind": "SpanKind.SERVER",
"parent_id": null,
"start_time": "2023-02-23T17:17:18.281424Z",
"end_time": "2023-02-23T17:17:18.284099Z",
"status": {
"status_code": "UNSET"
},
"attributes": {
"http.method": "GET",
"http.server_name": "127.0.0.1",
"http.scheme": "http",
"net.host.port": 5000,
"http.host": "127.0.0.1:5000",
"http.target": "/rolldice",
"net.peer.ip": "127.0.0.1",
"http.user_agent": "curl/7.81.0",
"net.peer.port": 48440,
"http.flavor": "1.1",
"http.route": "/rolldice",
"http.status_code": 200
},
"events": [],
"links": [],
"resource": {
"attributes": {
"telemetry.sdk.language": "python",
"telemetry.sdk.name": "opentelemetry",
"telemetry.sdk.version": "1.16.0",
"telemetry.auto.version": "0.37b0",
"service.name": "unknown_service"
},
"schema_url": ""
}
}
^C
$ kill %1
[1]+ Terminated app/venv/pex --traces_exporter console --metrics_exporter console flask --app app run
enough-analyst-54434
02/23/2023, 5:19 PMpex-tool
(PEX_TOOLS=1
) and that venv needs, critically, both of: --non-hermetic-scripts --bin-path prepend
2. The PEX must contain all the opentelemetry packages needed.enough-analyst-54434
02/23/2023, 5:20 PMPEX_TOOLS=1
already.enough-analyst-54434
02/23/2023, 5:20 PMfamous-xylophone-36532
02/24/2023, 7:57 AMfamous-xylophone-36532
02/24/2023, 12:21 PMfamous-xylophone-36532
02/24/2023, 12:54 PMopentelemetry-instrument ./my-pex.pex
). Anyways, I cannot get any telemetry from the current implementation either: https://github.com/Peder2911/otel_pants_reprex
Not sure if I did this correctly? Also, would be interesting to look into whether the above snippet could work with modifications to Pex?famous-xylophone-36532
02/24/2023, 1:22 PMopentelemetry-instrument
command is defined here: https://github.com/open-telemetry/opentelemetry-python-contrib/blob/main/opentelem[ā¦]/opentelemetry/instrumentation/auto_instrumentation/__init__.pyfamous-xylophone-36532
02/24/2023, 1:47 PMenough-analyst-54434
02/24/2023, 1:57 PMPEX_TOOLS...
step I indicated above was critical. Is there a reason you don't wish to add a RUN ...
line to your Dockerfile to do that required step?famous-xylophone-36532
02/24/2023, 1:58 PMfamous-xylophone-36532
02/24/2023, 1:59 PMenough-analyst-54434
02/24/2023, 2:05 PMfamous-xylophone-36532
02/24/2023, 2:11 PMopentelemetry-instrument
ing the pex). I do mention the fix below, and I'd be happy to use your working fix in our repo if I can get it to work. I'll add the README right away.famous-xylophone-36532
02/24/2023, 2:17 PMfamous-xylophone-36532
02/24/2023, 2:50 PMfamous-xylophone-36532
02/24/2023, 2:51 PMenough-analyst-54434
02/24/2023, 3:06 PMdiff --git a/apps/dockerfiles/app.dockerfile b/apps/dockerfiles/app.dockerfile
index 5b47ef0..7138bdb 100644
--- a/apps/dockerfiles/app.dockerfile
+++ b/apps/dockerfiles/app.dockerfile
@@ -3,4 +3,4 @@ FROM python:3.9
ARG PEX_FILE
COPY $PEX_FILE /app.pex
RUN PEX_TOOLS=1 /app.pex venv --non-hermetic-scripts --bin-path prepend /app/venv
-ENTRYPOINT ./app.pex --metrics_exporter console --traces_exporter console uvicorn <http://asciinator.app:app|asciinator.app:app> --host 0.0.0.0
+ENTRYPOINT /app/venv/pex --metrics_exporter console --traces_exporter console uvicorn <http://asciinator.app:app|asciinator.app:app> --host 0.0.0.0
enough-analyst-54434
02/24/2023, 3:07 PMfamous-xylophone-36532
02/24/2023, 3:07 PMfamous-xylophone-36532
02/24/2023, 3:07 PMenough-analyst-54434
02/24/2023, 3:08 PMfamous-xylophone-36532
02/24/2023, 3:09 PMenough-analyst-54434
02/24/2023, 3:09 PMfamous-xylophone-36532
02/24/2023, 3:20 PMenough-analyst-54434
02/24/2023, 3:25 PMenough-analyst-54434
02/24/2023, 3:26 PM'/usr/local/lib/python3.9/site-packages/opentelemetry/instrumentation/auto_instrumentation',
'/usr/local/lib/python3.9/site-packages'
That's a snip from a plain-old venv use - you provided that snip. A key point here is the 2nd entry.enough-analyst-54434
02/24/2023, 3:26 PMenough-analyst-54434
02/24/2023, 3:27 PMenough-analyst-54434
02/24/2023, 3:27 PMenough-analyst-54434
02/24/2023, 3:28 PMfamous-xylophone-36532
02/24/2023, 3:30 PMPEX_SCRIPT
to opentelemetry-instrument $EXISTING_ENTRYPOINT
and have my instrumented app?enough-analyst-54434
02/24/2023, 3:31 PMfamous-xylophone-36532
02/24/2023, 3:32 PMenough-analyst-54434
02/24/2023, 3:33 PMenough-analyst-54434
02/24/2023, 3:34 PM--non-hermetic-scripts
as a Pex build option like --venv
. Right now you can only get `--non-hermetic-scripts`when using PEX_TOOLS
to install a venv by hand.enough-analyst-54434
02/24/2023, 3:34 PMfamous-xylophone-36532
02/24/2023, 3:36 PMPEX_SCRIPT
env variable in the Docker image before executing the PEX itself to be something like opentelemetry-instrument app/venv/pex
?enough-analyst-54434
02/24/2023, 3:39 PMPEX_SCRIPT=opentelemetry-instrument
- the rest of the existing (fixed) Dockerfile ENTRYPOINT
remains the same.enough-analyst-54434
02/24/2023, 3:40 PM-c opentelemetry-instrument
says, make the opentelemetry-instrument
console script the entrypoint. Setting the PEX_SCRIPT=opentelemetry-instrument
env var does the same thing, but at runtime.famous-xylophone-36532
02/24/2023, 3:41 PMfamous-xylophone-36532
02/24/2023, 3:41 PMenough-analyst-54434
02/24/2023, 3:51 PM$ head -1 app/venv/pex
#!/home/jsirois/support/OpenTelemetryPex/app/venv/bin/python3.10 -sE
That -E tells python to ignore PYTHON*
env vars.enough-analyst-54434
02/24/2023, 3:52 PMenough-analyst-54434
02/24/2023, 3:52 PM--no-hermetic-scripts
.enough-analyst-54434
02/24/2023, 3:53 PMfamous-xylophone-36532
02/24/2023, 3:53 PMfamous-xylophone-36532
02/24/2023, 3:54 PMenough-analyst-54434
02/27/2023, 8:05 PMopentelemetry-instrument
from the use PEX by creating a single opentelemetry-instrument.pex
that can then be combined with user PEXes only in container images. I think this works anyhow and I'll describe success or failure on that ticket.famous-xylophone-36532
02/28/2023, 8:17 AM