Kai Jennissen
commited on
tracing with phoenix
Browse files- README.md +5 -6
- agent.py +7 -6
- tracing.py +33 -39
README.md
CHANGED
|
@@ -43,17 +43,16 @@ The application includes a modular tracing system that supports both:
|
|
| 43 |
```
|
| 44 |
- View traces at http://localhost:6006
|
| 45 |
|
| 46 |
-
You can
|
| 47 |
|
| 48 |
```python
|
| 49 |
-
# Use
|
| 50 |
initialize_tracing(provider="langfuse")
|
| 51 |
|
| 52 |
-
# Use
|
| 53 |
initialize_tracing(provider="phoenix")
|
| 54 |
-
|
| 55 |
-
# Use both (default)
|
| 56 |
-
initialize_tracing(provider="both")
|
| 57 |
```
|
| 58 |
|
|
|
|
|
|
|
| 59 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 43 |
```
|
| 44 |
- View traces at http://localhost:6006
|
| 45 |
|
| 46 |
+
You can configure which tracing system to use:
|
| 47 |
|
| 48 |
```python
|
| 49 |
+
# Use Langfuse (default)
|
| 50 |
initialize_tracing(provider="langfuse")
|
| 51 |
|
| 52 |
+
# Use Phoenix
|
| 53 |
initialize_tracing(provider="phoenix")
|
|
|
|
|
|
|
|
|
|
| 54 |
```
|
| 55 |
|
| 56 |
+
Due to limitations in the OpenTelemetry integration, only one provider can be active at a time. Simply choose the provider that best meets your needs.
|
| 57 |
+
|
| 58 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
agent.py
CHANGED
|
@@ -14,13 +14,13 @@ load_dotenv()
|
|
| 14 |
trace_provider = None
|
| 15 |
|
| 16 |
|
| 17 |
-
def initialize_tracing(enabled=True, provider="
|
| 18 |
"""
|
| 19 |
Initialize tracing for the agent module
|
| 20 |
|
| 21 |
Args:
|
| 22 |
enabled: Whether tracing should be active
|
| 23 |
-
provider: Which provider
|
| 24 |
"""
|
| 25 |
global trace_provider
|
| 26 |
if trace_provider is None:
|
|
@@ -63,8 +63,9 @@ def get_agent():
|
|
| 63 |
|
| 64 |
|
| 65 |
if __name__ == "__main__":
|
| 66 |
-
# Initialize tracing when run directly
|
| 67 |
-
|
|
|
|
| 68 |
|
| 69 |
# Get agent with tracing already configured
|
| 70 |
agent = get_agent()
|
|
@@ -76,6 +77,6 @@ if __name__ == "__main__":
|
|
| 76 |
)
|
| 77 |
print(f"Result: {result}")
|
| 78 |
print(
|
| 79 |
-
"
|
| 80 |
)
|
| 81 |
-
print("
|
|
|
|
| 14 |
trace_provider = None
|
| 15 |
|
| 16 |
|
| 17 |
+
def initialize_tracing(enabled=True, provider="langfuse"):
|
| 18 |
"""
|
| 19 |
Initialize tracing for the agent module
|
| 20 |
|
| 21 |
Args:
|
| 22 |
enabled: Whether tracing should be active
|
| 23 |
+
provider: Which provider to use - "langfuse" or "phoenix"
|
| 24 |
"""
|
| 25 |
global trace_provider
|
| 26 |
if trace_provider is None:
|
|
|
|
| 63 |
|
| 64 |
|
| 65 |
if __name__ == "__main__":
|
| 66 |
+
# Initialize tracing when run directly
|
| 67 |
+
# Choose one provider: "langfuse" (default) or "phoenix"
|
| 68 |
+
initialize_tracing(enabled=True, provider="phoenix")
|
| 69 |
|
| 70 |
# Get agent with tracing already configured
|
| 71 |
agent = get_agent()
|
|
|
|
| 77 |
)
|
| 78 |
print(f"Result: {result}")
|
| 79 |
print(
|
| 80 |
+
"If using Phoenix: run 'python -m phoenix.server.main serve' and view at http://localhost:6006"
|
| 81 |
)
|
| 82 |
+
print("If using Langfuse: view traces at https://cloud.langfuse.com")
|
tracing.py
CHANGED
|
@@ -11,15 +11,15 @@ from openinference.instrumentation.smolagents import SmolagentsInstrumentor
|
|
| 11 |
def setup_tracing(
|
| 12 |
service_name: str = "agent-service",
|
| 13 |
enabled: bool = True,
|
| 14 |
-
provider: Literal["langfuse", "phoenix"
|
| 15 |
) -> Optional[TracerProvider]:
|
| 16 |
"""
|
| 17 |
-
Configure and set up OpenTelemetry tracing with Langfuse
|
| 18 |
|
| 19 |
Args:
|
| 20 |
service_name: Name of the service for trace identification
|
| 21 |
enabled: Whether tracing should be active
|
| 22 |
-
provider: Which tracing provider
|
| 23 |
|
| 24 |
Returns:
|
| 25 |
Configured TracerProvider instance or None if disabled
|
|
@@ -27,15 +27,12 @@ def setup_tracing(
|
|
| 27 |
if not enabled:
|
| 28 |
return None
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
# Setup Phoenix if requested
|
| 33 |
-
if provider in ["phoenix", "both"]:
|
| 34 |
try:
|
| 35 |
from phoenix.otel import register
|
| 36 |
|
| 37 |
# Create Phoenix tracer provider
|
| 38 |
-
|
| 39 |
trace_provider = register(
|
| 40 |
project_name=service_name,
|
| 41 |
endpoint="http://127.0.0.1:6006/v1/traces",
|
|
@@ -43,11 +40,16 @@ def setup_tracing(
|
|
| 43 |
)
|
| 44 |
print(f"✅ Phoenix tracing enabled for {service_name}")
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
except ImportError:
|
| 47 |
print("⚠️ Phoenix not installed. Run: pip install 'smolagents[telemetry]'")
|
|
|
|
| 48 |
|
| 49 |
-
# Setup Langfuse
|
| 50 |
-
|
| 51 |
# Configure Langfuse host
|
| 52 |
langfuse_host = os.environ.get("LANGFUSE_HOST", "https://cloud.langfuse.com")
|
| 53 |
|
|
@@ -57,38 +59,30 @@ def setup_tracing(
|
|
| 57 |
|
| 58 |
if not langfuse_public_key or not langfuse_secret_key:
|
| 59 |
print("⚠️ Langfuse keys not found in environment")
|
|
|
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
f"{langfuse_host}/api/public/otel"
|
| 72 |
-
)
|
| 73 |
-
os.environ["OTEL_EXPORTER_OTLP_HEADERS"] = (
|
| 74 |
-
f"Authorization=Basic {langfuse_auth}"
|
| 75 |
-
)
|
| 76 |
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
trace_provider = TracerProvider()
|
| 80 |
-
# Set as the global default tracer provider
|
| 81 |
-
trace.set_tracer_provider(trace_provider)
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
print(f"✅ Langfuse tracing enabled for {service_name}")
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
SmolagentsInstrumentor().instrument(tracer_provider=trace_provider)
|
| 90 |
|
| 91 |
-
|
| 92 |
-
trace.get_tracer(service_name)
|
| 93 |
|
| 94 |
-
|
|
|
|
|
|
|
|
|
| 11 |
def setup_tracing(
|
| 12 |
service_name: str = "agent-service",
|
| 13 |
enabled: bool = True,
|
| 14 |
+
provider: Literal["langfuse", "phoenix"] = "langfuse",
|
| 15 |
) -> Optional[TracerProvider]:
|
| 16 |
"""
|
| 17 |
+
Configure and set up OpenTelemetry tracing with Langfuse or Phoenix integration.
|
| 18 |
|
| 19 |
Args:
|
| 20 |
service_name: Name of the service for trace identification
|
| 21 |
enabled: Whether tracing should be active
|
| 22 |
+
provider: Which tracing provider to use - "langfuse" or "phoenix"
|
| 23 |
|
| 24 |
Returns:
|
| 25 |
Configured TracerProvider instance or None if disabled
|
|
|
|
| 27 |
if not enabled:
|
| 28 |
return None
|
| 29 |
|
| 30 |
+
# Setup Phoenix
|
| 31 |
+
if provider == "phoenix":
|
|
|
|
|
|
|
| 32 |
try:
|
| 33 |
from phoenix.otel import register
|
| 34 |
|
| 35 |
# Create Phoenix tracer provider
|
|
|
|
| 36 |
trace_provider = register(
|
| 37 |
project_name=service_name,
|
| 38 |
endpoint="http://127.0.0.1:6006/v1/traces",
|
|
|
|
| 40 |
)
|
| 41 |
print(f"✅ Phoenix tracing enabled for {service_name}")
|
| 42 |
|
| 43 |
+
# Instrument smolagents with Phoenix provider
|
| 44 |
+
SmolagentsInstrumentor().instrument(tracer_provider=trace_provider)
|
| 45 |
+
return trace_provider
|
| 46 |
+
|
| 47 |
except ImportError:
|
| 48 |
print("⚠️ Phoenix not installed. Run: pip install 'smolagents[telemetry]'")
|
| 49 |
+
return None
|
| 50 |
|
| 51 |
+
# Setup Langfuse (default)
|
| 52 |
+
else:
|
| 53 |
# Configure Langfuse host
|
| 54 |
langfuse_host = os.environ.get("LANGFUSE_HOST", "https://cloud.langfuse.com")
|
| 55 |
|
|
|
|
| 59 |
|
| 60 |
if not langfuse_public_key or not langfuse_secret_key:
|
| 61 |
print("⚠️ Langfuse keys not found in environment")
|
| 62 |
+
return None
|
| 63 |
|
| 64 |
+
# Configure Langfuse
|
| 65 |
+
langfuse_auth = base64.b64encode(
|
| 66 |
+
f"{langfuse_public_key}:{langfuse_secret_key}".encode()
|
| 67 |
+
).decode()
|
| 68 |
+
|
| 69 |
+
# Set environment variables for OTLP exporter
|
| 70 |
+
os.environ["OTEL_EXPORTER_OTLP_ENDPOINT"] = f"{langfuse_host}/api/public/otel"
|
| 71 |
+
os.environ["OTEL_EXPORTER_OTLP_HEADERS"] = (
|
| 72 |
+
f"Authorization=Basic {langfuse_auth}"
|
| 73 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
+
# Create a new TracerProvider for Langfuse
|
| 76 |
+
langfuse_provider = TracerProvider()
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
+
# Add Langfuse exporter
|
| 79 |
+
langfuse_provider.add_span_processor(SimpleSpanProcessor(OTLPSpanExporter()))
|
|
|
|
| 80 |
|
| 81 |
+
# Set as the global default tracer provider
|
| 82 |
+
trace.set_tracer_provider(langfuse_provider)
|
|
|
|
| 83 |
|
| 84 |
+
print(f"✅ Langfuse tracing enabled for {service_name}")
|
|
|
|
| 85 |
|
| 86 |
+
# Instrument smolagents
|
| 87 |
+
SmolagentsInstrumentor().instrument(tracer_provider=langfuse_provider)
|
| 88 |
+
return langfuse_provider
|