Hi! I have a fast-api app that makes calls to the Openrouter API. I have various methods that I want to track under one request to an endpoint. I've decorated the @app.post('/endpoint') with @tracer.chain and all other subsequent method calls with the same but it only tracks the first one. Is there a fundamental thing I'm doing incorrectly or is that the desired behaviour?
To track multiple method calls under a single request in your FastAPI app, ensure that each method is correctly instrumented to be part of the same trace. The @tracer.chain decorator should be applied to each method you want to track, and they should all be executed within the context of a parent span. If only the first method is being tracked, it might be due to the context not being properly propagated across method calls.According to the Phoenix documentation, you can use context managers to ensure that all spans are part of the same trace. Here's an example of how you can structure your code: LBKSfrom opentelemetry import traceLBKStracer = trace.get_tracer(__name__)LBKS@app.post('/endpoint') async def endpoint(): with tracer.start_as_current_span("parent_span") as parent_span: # Call your methods here method_one() method_two()LBKS@tracer.chain def method_one(): # Method logic here passLBKS@tracer.chain def method_two(): # Method logic here passLBKSThis approach ensures that all method calls are nested under the parent_span, allowing them to be tracked as part of the same trace. Would you like more detail on this process?Sources:
- Phoenix documentation
馃挕聽Hint: Mention RunLLM in the thread for followups.
What happens if my methods are not in the same file? RunLLM
RunLLM also doesn't Phoenix support auto-instrumentation for Openrouter?
If your methods are not in the same file, you need to ensure that the tracing context is properly propagated across files. This can be achieved by importing the same tracer instance in each file and using context propagation to maintain the trace context across method calls. You can use the opentelemetry.propagate module to inject and extract the context as needed.Regarding auto-instrumentation for Openrouter, Phoenix does not explicitly mention support for Openrouter in the provided data sources. Phoenix provides auto-instrumentation for several frameworks and SDKs, such as OpenAI, LangChain, and LlamaIndex, but Openrouter is not listed among them. You may need to manually instrument your Openrouter calls using OpenTelemetry.Would you like more detail on how to manually instrument your methods across different files?Sources:
- Phoenix documentation
馃挕聽Hint: Mention RunLLM in the thread for followups.
Hi Samanvya T. can you elaborate what you mean by it only tracking the first one? In general the decorators start and end a span when a function is called. It might be that you've added the decorator too high? It sounds like you are only getting a span when the route is mounted. Not sure. You might want to just open / close a span inside the route handler itself to see if that works.
Thanks for your reply Mikyo. I was able to sort out the issue by using the with tracer.start_as_current_span, and manually set attributes. Thank you! For me, the issue was even though I had set @tracer.chain for the methods (not in the same file as my main app), Phoenix wasn't able to track the spans. Let me know if I am missing something very obvious as I'm new to OTEL/Telemetry in general.
