@@ -21,6 +21,7 @@ import { Logger, type LogLevel } from "@trigger.dev/core/logger";
2121import type { Agent as HttpAgent } from "http" ;
2222import type { Agent as HttpsAgent } from "https" ;
2323import { ClickhouseQueryBuilder } from "./queryBuilder.js" ;
24+ import { randomUUID } from "node:crypto" ;
2425
2526export type ClickhouseConfig = {
2627 name : string ;
@@ -103,18 +104,22 @@ export class ClickhouseClient implements ClickhouseReader, ClickhouseWriter {
103104 settings ?: ClickHouseSettings ;
104105 } ) : ClickhouseQueryFunction < z . input < TIn > , z . output < TOut > > {
105106 return async ( params , options ) => {
107+ const queryId = randomUUID ( ) ;
108+
106109 return await startSpan ( this . tracer , "query" , async ( span ) => {
107110 this . logger . debug ( "Querying clickhouse" , {
108111 name : req . name ,
109112 query : req . query . replace ( / \s + / g, " " ) ,
110113 params,
111114 settings : req . settings ,
112115 attributes : options ?. attributes ,
116+ queryId,
113117 } ) ;
114118
115119 span . setAttributes ( {
116120 "clickhouse.clientName" : this . name ,
117121 "clickhouse.operationName" : req . name ,
122+ "clickhouse.queryId" : queryId ,
118123 ...flattenAttributes ( req . settings , "clickhouse.settings" ) ,
119124 ...flattenAttributes ( options ?. attributes ) ,
120125 } ) ;
@@ -129,6 +134,7 @@ export class ClickhouseClient implements ClickhouseReader, ClickhouseWriter {
129134 error : validParams . error ,
130135 query : req . query ,
131136 params,
137+ queryId,
132138 } ) ;
133139
134140 return [
@@ -146,6 +152,7 @@ export class ClickhouseClient implements ClickhouseReader, ClickhouseWriter {
146152 query : req . query ,
147153 query_params : validParams ?. data ,
148154 format : "JSONEachRow" ,
155+ query_id : queryId ,
149156 ...options ?. params ,
150157 clickhouse_settings : {
151158 ...req . settings ,
@@ -160,6 +167,7 @@ export class ClickhouseClient implements ClickhouseReader, ClickhouseWriter {
160167 error : clickhouseError ,
161168 query : req . query ,
162169 params,
170+ queryId,
163171 } ) ;
164172
165173 recordClickhouseError ( span , clickhouseError ) ;
@@ -195,6 +203,7 @@ export class ClickhouseClient implements ClickhouseReader, ClickhouseWriter {
195203 error : parsed . error ,
196204 query : req . query ,
197205 params,
206+ queryId,
198207 } ) ;
199208
200209 const queryError = new QueryError ( generateErrorMessage ( parsed . error . issues ) , {
@@ -235,11 +244,25 @@ export class ClickhouseClient implements ClickhouseReader, ClickhouseWriter {
235244 settings ?: ClickHouseSettings ;
236245 } ) : ClickhouseInsertFunction < z . input < TSchema > > {
237246 return async ( events , options ) => {
247+ const queryId = randomUUID ( ) ;
248+
238249 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+
239261 span . setAttributes ( {
240262 "clickhouse.clientName" : this . name ,
241263 "clickhouse.tableName" : req . table ,
242264 "clickhouse.operationName" : req . name ,
265+ "clickhouse.queryId" : queryId ,
243266 ...flattenAttributes ( req . settings , "clickhouse.settings" ) ,
244267 ...flattenAttributes ( options ?. attributes ) ,
245268 } ) ;
@@ -271,6 +294,7 @@ export class ClickhouseClient implements ClickhouseReader, ClickhouseWriter {
271294 table : req . table ,
272295 format : "JSONEachRow" ,
273296 values : Array . isArray ( validatedEvents ) ? validatedEvents : [ validatedEvents ] ,
297+ query_id : queryId ,
274298 ...options ?. params ,
275299 clickhouse_settings : {
276300 ...req . settings ,
@@ -291,6 +315,14 @@ export class ClickhouseClient implements ClickhouseReader, ClickhouseWriter {
291315 return [ new InsertError ( clickhouseError . message ) , null ] ;
292316 }
293317
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+
294326 span . setAttributes ( {
295327 "clickhouse.query_id" : result . query_id ,
296328 "clickhouse.executed" : result . executed ,
0 commit comments