File size: 1,145 Bytes
23dc37a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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