@@ -21,6 +21,7 @@ import { Logger, type LogLevel } from "@trigger.dev/core/logger";
21
21
import type { Agent as HttpAgent } from "http" ;
22
22
import type { Agent as HttpsAgent } from "https" ;
23
23
import { ClickhouseQueryBuilder } from "./queryBuilder.js" ;
24
+ import { randomUUID } from "node:crypto" ;
24
25
25
26
export type ClickhouseConfig = {
26
27
name : string ;
@@ -103,18 +104,22 @@ export class ClickhouseClient implements ClickhouseReader, ClickhouseWriter {
103
104
settings ?: ClickHouseSettings ;
104
105
} ) : ClickhouseQueryFunction < z . input < TIn > , z . output < TOut > > {
105
106
return async ( params , options ) => {
107
+ const queryId = randomUUID ( ) ;
108
+
106
109
return await startSpan ( this . tracer , "query" , async ( span ) => {
107
110
this . logger . debug ( "Querying clickhouse" , {
108
111
name : req . name ,
109
112
query : req . query . replace ( / \s + / g, " " ) ,
110
113
params,
111
114
settings : req . settings ,
112
115
attributes : options ?. attributes ,
116
+ queryId,
113
117
} ) ;
114
118
115
119
span . setAttributes ( {
116
120
"clickhouse.clientName" : this . name ,
117
121
"clickhouse.operationName" : req . name ,
122
+ "clickhouse.queryId" : queryId ,
118
123
...flattenAttributes ( req . settings , "clickhouse.settings" ) ,
119
124
...flattenAttributes ( options ?. attributes ) ,
120
125
} ) ;
@@ -129,6 +134,7 @@ export class ClickhouseClient implements ClickhouseReader, ClickhouseWriter {
129
134
error : validParams . error ,
130
135
query : req . query ,
131
136
params,
137
+ queryId,
132
138
} ) ;
133
139
134
140
return [
@@ -146,6 +152,7 @@ export class ClickhouseClient implements ClickhouseReader, ClickhouseWriter {
146
152
query : req . query ,
147
153
query_params : validParams ?. data ,
148
154
format : "JSONEachRow" ,
155
+ query_id : queryId ,
149
156
...options ?. params ,
150
157
clickhouse_settings : {
151
158
...req . settings ,
@@ -160,6 +167,7 @@ export class ClickhouseClient implements ClickhouseReader, ClickhouseWriter {
160
167
error : clickhouseError ,
161
168
query : req . query ,
162
169
params,
170
+ queryId,
163
171
} ) ;
164
172
165
173
recordClickhouseError ( span , clickhouseError ) ;
@@ -195,6 +203,7 @@ export class ClickhouseClient implements ClickhouseReader, ClickhouseWriter {
195
203
error : parsed . error ,
196
204
query : req . query ,
197
205
params,
206
+ queryId,
198
207
} ) ;
199
208
200
209
const queryError = new QueryError ( generateErrorMessage ( parsed . error . issues ) , {
@@ -235,11 +244,25 @@ export class ClickhouseClient implements ClickhouseReader, ClickhouseWriter {
235
244
settings ?: ClickHouseSettings ;
236
245
} ) : ClickhouseInsertFunction < z . input < TSchema > > {
237
246
return async ( events , options ) => {
247
+ const queryId = randomUUID ( ) ;
248
+
238
249
return await startSpan ( this . tracer , "insert" , async ( span ) => {
250
+ this . logger . debug ( "Inserting into clickhouse" , {
251
+ clientName : this . name ,
252
+ name : req . name ,
253
+ table : req . table ,
254
+ events : Array . isArray ( events ) ? events . length : 1 ,
255
+ settings : req . settings ,
256
+ attributes : options ?. attributes ,
257
+ options,
258
+ queryId,
259
+ } ) ;
260
+
239
261
span . setAttributes ( {
240
262
"clickhouse.clientName" : this . name ,
241
263
"clickhouse.tableName" : req . table ,
242
264
"clickhouse.operationName" : req . name ,
265
+ "clickhouse.queryId" : queryId ,
243
266
...flattenAttributes ( req . settings , "clickhouse.settings" ) ,
244
267
...flattenAttributes ( options ?. attributes ) ,
245
268
} ) ;
@@ -271,6 +294,7 @@ export class ClickhouseClient implements ClickhouseReader, ClickhouseWriter {
271
294
table : req . table ,
272
295
format : "JSONEachRow" ,
273
296
values : Array . isArray ( validatedEvents ) ? validatedEvents : [ validatedEvents ] ,
297
+ query_id : queryId ,
274
298
...options ?. params ,
275
299
clickhouse_settings : {
276
300
...req . settings ,
@@ -291,6 +315,14 @@ export class ClickhouseClient implements ClickhouseReader, ClickhouseWriter {
291
315
return [ new InsertError ( clickhouseError . message ) , null ] ;
292
316
}
293
317
318
+ this . logger . debug ( "Inserted into clickhouse" , {
319
+ clientName : this . name ,
320
+ name : req . name ,
321
+ table : req . table ,
322
+ result,
323
+ queryId,
324
+ } ) ;
325
+
294
326
span . setAttributes ( {
295
327
"clickhouse.query_id" : result . query_id ,
296
328
"clickhouse.executed" : result . executed ,
0 commit comments