To trace your API using Phoenix with the endpoint URL you have, follow these steps:
1. *Install OpenTelemetry and Phoenix Packages*: Ensure you have the necessary packages installed in your application environment.
```
pip install opentelemetry-sdk opentelemetry-exporter-otlp-proto-http arize-phoenix
```
2. *Configure the Tracer*: Set up the OpenTelemetry tracer to send traces to your Phoenix endpoint.
```
from opentelemetry import trace
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
# Set your Phoenix endpoint
endpoint = "http://your-phoenix-endpoint/v1/traces"
# Configure the tracer provider
tracer_provider = TracerProvider()
span_exporter = OTLPSpanExporter(endpoint=endpoint)
span_processor = SimpleSpanProcessor(span_exporter)
tracer_provider.add_span_processor(span_processor)
trace.set_tracer_provider(tracer_provider)
# Get a tracer
tracer = trace.get_tracer(__name__)
```
3. *Instrument Your API*: Use the tracer to create spans around your API logic.
```
from flask import Flask, request
app = Flask(__name__)
@app.route('/your-api-endpoint', methods=['POST'])
def your_api_function():
with tracer.start_as_current_span("api_request") as span:
# Add request details to the span
span.set_attribute("http.method", request.method)
span.set_attribute("http.url", request.url)
# Your API logic here
response = "Your response"
# Add response details to the span
span.set_attribute("http.status_code", 200)
return response
if __name__ == "__main__":
app.run(host='0.0.0.0', port=5000)
```
4. *Run Your Application*: Start your application and ensure it sends traces to the Phoenix endpoint.
This setup will allow you to trace requests and responses of your API using Phoenix. Would you like more details on any specific part of this setup?
Sources:
- <https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-haystack/README.md|Phoenix Documentation>