File size: 1,376 Bytes
040ec71 a8ed903 040ec71 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | """Minimal sender for Agent Observability API. Never send secrets or raw PII."""
import json, os, urllib.request
class AgentObservability:
def __init__(self, endpoint: str, api_key: str | None = None, timeout: float = 8.0):
self.endpoint=endpoint.rstrip('/')
self.api_key=api_key or os.environ.get('AGENT_OBS_API_KEY','')
self.timeout=timeout
if not self.endpoint.startswith(('http://127.0.0.1','http://localhost','https://')):
raise ValueError('Use localhost for local development or HTTPS for remote endpoints')
if not self.api_key: raise ValueError('AGENT_OBS_API_KEY is required')
def record(self, *, agent_name, status, duration_ms, input_tokens=0, output_tokens=0, estimated_cost_usd=0, error_type=None, request_id=None):
body=json.dumps({
'agent_name':agent_name,'status':status,'duration_ms':duration_ms,
'input_tokens':input_tokens,'output_tokens':output_tokens,
'estimated_cost_usd':estimated_cost_usd,'error_type':error_type,'request_id':request_id
}).encode()
req=urllib.request.Request(self.endpoint+'/api/v1/traces',data=body,headers={'Content-Type':'application/json','X-API-Key':self.api_key},method='POST')
with urllib.request.urlopen(req,timeout=self.timeout) as response:
return json.loads(response.read().decode())
|