Tomer Friedman
05/15/2023, 12:10 PMOpenTelemetry
(Its the last part of our micro-service architecture thats missing it)
I was looking online and only managed to find a single old thread and one attempt that doesn't look working/maintained.
Anyone tried this? Know if its doable even?
• It would require passing the Open Telemetry trace id from a parent flow to each of its subflows/tasks. Prefect run context which might be an option for this does not appear to allow additional fields.
• The tasks/flows themselves would also need to be instrumented of course
Would appreciate any help 🙏
Thanks allAndrew Brookins
05/15/2023, 11:39 PMimport sys
from opentelemetry.instrumentation.asyncpg import AsyncPGInstrumentor
from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor
from opentelemetry.instrumentation.sqlalchemy import SQLAlchemyInstrumentor
from prefect.cli import app
def run():
AsyncPGInstrumentor().instrument()
HTTPXClientInstrumentor().instrument()
# Just FYI, the default settings for this one add comments to all SQL
# statements to include a trace ID, which is a bit noisy.
SQLAlchemyInstrumentor().instrument()
# Set up OTEL span collectors, exporters, etc.
# Pass through commands to the Prefect CLI.
app(sys.argv[1:])
if __name__ == "__main__":
run()
The one that’s hard is FastAPI instrumentation, which requires adding the instrumentation as FastAPI middleware. You’d have to have access to the FastAPI application object that we create when we start the server. There isn’t a hook that would give you access to this object currently, nor does it appear to be possible to patch our create_app
function with something like mock
due to the way uvicorn imports the function. (Or there is and I just can’t figure it out. 😄)
If you maintain a fork of Prefect, you can modify the code directly to add the OTEL instrumentation middleware. You’d add it here, in a second add_middleware
call: https://github.com/PrefectHQ/prefect/blob/d54ae148cfe98ecd4d535ec2186821bb54730ee3/src/prefect/server/api/server.py
You can see an example of how to do this in the OTEL docs.Andrew Brookins
05/15/2023, 11:51 PMFlow
or Task
subclass that added instrumentation by wrapping calls in OTEL spans.Andrew Brookins
05/16/2023, 3:33 AMstart
CLI wrapper, where you’d need an alternative create_app
implementation (that you can point to in dot notation, i.e., your.module.create_app
, which itself attaches the middleware.Tomer Friedman
05/16/2023, 10:41 AMAndrew Brookins
05/16/2023, 4:25 PMTomer Friedman
05/17/2023, 7:43 AMJosep Batallé Oronich
05/17/2023, 10:41 AM