ALM_LLM / utils /tracing.py
AshenH's picture
Update utils/tracing.py
23dc37a verified
raw
history blame
1.15 kB
import os
import json
class Tracer:
def __init__(self):
self.enabled = False
self.client = None
self.trace = None
try:
from langfuse import Langfuse
pk = os.getenv("LANGFUSE_PUBLIC_KEY")
sk = os.getenv("LANGFUSE_SECRET_KEY")
host = os.getenv("LANGFUSE_HOST", "https://cloud.langfuse.com")
if pk and sk:
self.client = Langfuse(public_key=pk, secret_key=sk, host=host)
self.trace = self.client.trace(name="tabular-agentic-xai")
self.enabled = True
except Exception:
pass
@classmethod
def from_env(cls):
return cls()
@property
def trace_url(self):
try:
return self.trace.get_url() if (self.enabled and hasattr(self.trace, "get_url")) else None
except Exception:
return None
def trace_event(self, name: str, payload: dict):
if not self.enabled or not self.trace:
return
try:
self.trace.event(name=name, input=json.dumps(payload))
except Exception:
pass