@@ -378,6 +378,7 @@ TTL toDateTime(Timestamp) + toIntervalDay(3)
378378SETTINGS ttl_only_drop_parts = 1
379379` ;
380380
381+ // Legacy table - keeping for backwards compatibility
381382const CREATE_CUSTOM_EVENTS_TABLE = `
382383CREATE TABLE IF NOT EXISTS ${ ANALYTICS_DATABASE } .custom_events (
383384 id UUID,
@@ -394,6 +395,30 @@ ORDER BY (client_id, timestamp, id)
394395SETTINGS index_granularity = 8192
395396` ;
396397
398+ /**
399+ * Lean custom event spans table
400+ * Uses JSON for flexible metadata
401+ */
402+ const CREATE_CUSTOM_EVENT_SPANS_TABLE = `
403+ CREATE TABLE IF NOT EXISTS ${ ANALYTICS_DATABASE } .custom_event_spans (
404+ client_id String CODEC(ZSTD(1)),
405+ session_id String CODEC(ZSTD(1)),
406+
407+ timestamp DateTime64(3, 'UTC') CODEC(Delta(8), ZSTD(1)),
408+ path String CODEC(ZSTD(1)),
409+
410+ event_name LowCardinality(String) CODEC(ZSTD(1)),
411+ properties JSON CODEC(ZSTD(1)),
412+
413+ INDEX idx_session_id session_id TYPE bloom_filter(0.01) GRANULARITY 1,
414+ INDEX idx_event_name event_name TYPE bloom_filter(0.01) GRANULARITY 1
415+ ) ENGINE = MergeTree
416+ PARTITION BY toDate(timestamp)
417+ ORDER BY (client_id, event_name, path, timestamp)
418+ TTL toDateTime(timestamp) + INTERVAL 90 DAY
419+ SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1
420+ ` ;
421+
397422const CREATE_CUSTOM_OUTGOING_LINKS_TABLE = `
398423CREATE TABLE IF NOT EXISTS ${ ANALYTICS_DATABASE } .outgoing_links (
399424 id UUID,
@@ -625,6 +650,7 @@ export type OTelLogs = {
625650 LogAttributes : Record < string , string > ;
626651}
627652
653+ // Legacy type - keeping for backwards compatibility
628654export type CustomEvent = {
629655 id : string ;
630656 client_id : string ;
@@ -635,6 +661,19 @@ export type CustomEvent = {
635661 timestamp : number ;
636662}
637663
664+ /**
665+ * Lean custom event span
666+ * properties is flexible JSON
667+ */
668+ export type CustomEventSpan = {
669+ client_id : string ;
670+ session_id : string ;
671+ timestamp : number ;
672+ path : string ;
673+ event_name : string ;
674+ properties : Record < string , unknown > ;
675+ }
676+
638677export type CustomOutgoingLink = {
639678 id : string ;
640679 client_id : string ;
@@ -741,6 +780,7 @@ export async function initClickHouseSchema() {
741780 { name : "blocked_traffic" , query : CREATE_BLOCKED_TRAFFIC_TABLE } ,
742781 { name : "email_events" , query : CREATE_EMAIL_EVENTS_TABLE } ,
743782 { name : "custom_events" , query : CREATE_CUSTOM_EVENTS_TABLE } ,
783+ { name : "custom_event_spans" , query : CREATE_CUSTOM_EVENT_SPANS_TABLE } ,
744784 { name : "outgoing_links" , query : CREATE_CUSTOM_OUTGOING_LINKS_TABLE } ,
745785 ] ;
746786
0 commit comments