-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathclient.py
More file actions
157 lines (138 loc) · 4.48 KB
/
client.py
File metadata and controls
157 lines (138 loc) · 4.48 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
"""ClickHouse client using clickhouse-connect."""
from datetime import UTC, datetime
from typing import Any
import clickhouse_connect
from clickhouse_connect.driver.client import Client
from shared.config import settings
class ClickHouseClient:
"""ClickHouse client wrapper for trace data operations."""
def __init__(self, client: Client):
self._client = client
@classmethod
def from_settings(cls) -> "ClickHouseClient":
"""Create client from centralized settings."""
ch = settings.clickhouse
client = clickhouse_connect.get_client(
host=ch.host,
port=ch.port,
username=ch.user,
password=ch.password,
database=ch.database,
)
return cls(client)
def insert_traces_batch(self, traces: list[dict[str, Any]]) -> None:
"""Insert multiple trace records."""
if not traces:
return
now = datetime.now(UTC)
rows = []
for t in traces:
rows.append(
[
t["trace_id"],
t["project_id"],
t["trace_start_time"],
t["name"],
t.get("user_id"),
t.get("session_id"),
t.get("environment", "default"),
t.get("release"),
t.get("input"),
t.get("output"),
t.get("metadata"),
now, # ch_create_time
now, # ch_update_time
]
)
self._client.insert(
"traces",
rows,
column_names=[
"trace_id",
"project_id",
"trace_start_time",
"name",
"user_id",
"session_id",
"environment",
"release",
"input",
"output",
"metadata",
"ch_create_time",
"ch_update_time",
],
)
def insert_spans_batch(self, spans: list[dict[str, Any]]) -> None:
"""Insert multiple span records."""
if not spans:
return
now = datetime.now(UTC)
rows = []
for s in spans:
rows.append(
[
s["span_id"],
s["trace_id"],
s.get("parent_span_id"),
s["project_id"],
s["span_start_time"],
s.get("span_end_time"),
s["name"],
s["span_kind"],
s.get("status", "OK"),
s.get("status_message"),
s.get("model_name"),
s.get("cost"),
s.get("input_tokens"),
s.get("output_tokens"),
s.get("total_tokens"),
s.get("input"),
s.get("output"),
s.get("environment", "default"),
s.get("metadata"),
now, # ch_create_time
now, # ch_update_time
]
)
self._client.insert(
"spans",
rows,
column_names=[
"span_id",
"trace_id",
"parent_span_id",
"project_id",
"span_start_time",
"span_end_time",
"name",
"span_kind",
"status",
"status_message",
"model_name",
"cost",
"input_tokens",
"output_tokens",
"total_tokens",
"input",
"output",
"environment",
"metadata",
"ch_create_time",
"ch_update_time",
],
)
def query(self, query: str, parameters: dict[str, Any] | None = None):
"""Execute a query and return the result."""
return self._client.query(query, parameters=parameters)
def close(self) -> None:
"""Close the client connection."""
self._client.close()
# Singleton instance
_client: ClickHouseClient | None = None
def get_clickhouse_client() -> ClickHouseClient:
"""Get or create the singleton ClickHouse client."""
global _client
if _client is None:
_client = ClickHouseClient.from_settings()
return _client