@@ -163,42 +163,42 @@ const (
163
163
// language=ClickHouse SQL
164
164
createTracesTableSQL = `
165
165
CREATE TABLE IF NOT EXISTS %s %s (
166
- Timestamp DateTime64(9) CODEC(Delta, ZSTD(1)),
167
- TraceId String CODEC(ZSTD(1)),
168
- SpanId String CODEC(ZSTD(1)),
169
- ParentSpanId String CODEC(ZSTD(1)),
170
- TraceState String CODEC(ZSTD(1)),
171
- SpanName LowCardinality(String) CODEC(ZSTD(1)),
172
- SpanKind LowCardinality(String) CODEC(ZSTD(1)),
173
- ServiceName LowCardinality(String) CODEC(ZSTD(1)),
174
- ResourceAttributes Map(LowCardinality(String), String) CODEC(ZSTD(1)),
175
- ScopeName String CODEC(ZSTD(1)),
176
- ScopeVersion String CODEC(ZSTD(1)),
177
- SpanAttributes Map(LowCardinality(String), String) CODEC(ZSTD(1)),
178
- Duration Int64 CODEC(ZSTD(1)),
179
- StatusCode LowCardinality(String) CODEC(ZSTD(1)),
180
- StatusMessage String CODEC(ZSTD(1)),
181
- Events Nested (
182
- Timestamp DateTime64(9),
183
- Name LowCardinality(String),
184
- Attributes Map(LowCardinality(String), String)
185
- ) CODEC(ZSTD(1)),
186
- Links Nested (
187
- TraceId String,
188
- SpanId String,
189
- TraceState String,
190
- Attributes Map(LowCardinality(String), String)
191
- ) CODEC(ZSTD(1)),
192
- INDEX idx_trace_id TraceId TYPE bloom_filter(0.001) GRANULARITY 1,
193
- INDEX idx_res_attr_key mapKeys(ResourceAttributes) TYPE bloom_filter(0.01) GRANULARITY 1,
194
- INDEX idx_res_attr_value mapValues(ResourceAttributes) TYPE bloom_filter(0.01) GRANULARITY 1,
195
- INDEX idx_span_attr_key mapKeys(SpanAttributes) TYPE bloom_filter(0.01) GRANULARITY 1,
196
- INDEX idx_span_attr_value mapValues(SpanAttributes) TYPE bloom_filter(0.01) GRANULARITY 1,
197
- INDEX idx_duration Duration TYPE minmax GRANULARITY 1
166
+ Timestamp DateTime64(9) CODEC(Delta, ZSTD(1)),
167
+ TraceId String CODEC(ZSTD(1)),
168
+ SpanId String CODEC(ZSTD(1)),
169
+ ParentSpanId String CODEC(ZSTD(1)),
170
+ TraceState String CODEC(ZSTD(1)),
171
+ SpanName LowCardinality(String) CODEC(ZSTD(1)),
172
+ SpanKind LowCardinality(String) CODEC(ZSTD(1)),
173
+ ServiceName LowCardinality(String) CODEC(ZSTD(1)),
174
+ ResourceAttributes Map(LowCardinality(String), String) CODEC(ZSTD(1)),
175
+ ScopeName String CODEC(ZSTD(1)),
176
+ ScopeVersion String CODEC(ZSTD(1)),
177
+ SpanAttributes Map(LowCardinality(String), String) CODEC(ZSTD(1)),
178
+ Duration UInt64 CODEC(ZSTD(1)),
179
+ StatusCode LowCardinality(String) CODEC(ZSTD(1)),
180
+ StatusMessage String CODEC(ZSTD(1)),
181
+ Events Nested (
182
+ Timestamp DateTime64(9),
183
+ Name LowCardinality(String),
184
+ Attributes Map(LowCardinality(String), String)
185
+ ) CODEC(ZSTD(1)),
186
+ Links Nested (
187
+ TraceId String,
188
+ SpanId String,
189
+ TraceState String,
190
+ Attributes Map(LowCardinality(String), String)
191
+ ) CODEC(ZSTD(1)),
192
+ INDEX idx_trace_id TraceId TYPE bloom_filter(0.001) GRANULARITY 1,
193
+ INDEX idx_res_attr_key mapKeys(ResourceAttributes) TYPE bloom_filter(0.01) GRANULARITY 1,
194
+ INDEX idx_res_attr_value mapValues(ResourceAttributes) TYPE bloom_filter(0.01) GRANULARITY 1,
195
+ INDEX idx_span_attr_key mapKeys(SpanAttributes) TYPE bloom_filter(0.01) GRANULARITY 1,
196
+ INDEX idx_span_attr_value mapValues(SpanAttributes) TYPE bloom_filter(0.01) GRANULARITY 1,
197
+ INDEX idx_duration Duration TYPE minmax GRANULARITY 1
198
198
) ENGINE = %s
199
- %s
200
199
PARTITION BY toDate(Timestamp)
201
- ORDER BY (ServiceName, SpanName, toUnixTimestamp(Timestamp), TraceId)
200
+ ORDER BY (ServiceName, SpanName, toDateTime(Timestamp))
201
+ %s
202
202
SETTINGS index_granularity=8192, ttl_only_drop_parts = 1;
203
203
`
204
204
// language=ClickHouse SQL
@@ -253,26 +253,27 @@ SETTINGS index_granularity=8192, ttl_only_drop_parts = 1;
253
253
254
254
const (
255
255
createTraceIDTsTableSQL = `
256
- create table IF NOT EXISTS %s_trace_id_ts %s (
256
+ CREATE TABLE IF NOT EXISTS %s_trace_id_ts %s (
257
257
TraceId String CODEC(ZSTD(1)),
258
- Start DateTime64(9) CODEC(Delta, ZSTD(1)),
259
- End DateTime64(9) CODEC(Delta, ZSTD(1)),
258
+ Start DateTime CODEC(Delta, ZSTD(1)),
259
+ End DateTime CODEC(Delta, ZSTD(1)),
260
260
INDEX idx_trace_id TraceId TYPE bloom_filter(0.01) GRANULARITY 1
261
261
) ENGINE = %s
262
+ PARTITION BY toDate(Start)
263
+ ORDER BY (TraceId, Start)
262
264
%s
263
- ORDER BY (TraceId, toUnixTimestamp(Start))
264
- SETTINGS index_granularity=8192;
265
+ SETTINGS index_granularity=8192, ttl_only_drop_parts = 1;
265
266
`
266
267
createTraceIDTsMaterializedViewSQL = `
267
268
CREATE MATERIALIZED VIEW IF NOT EXISTS %s_trace_id_ts_mv %s
268
269
TO %s.%s_trace_id_ts
269
270
AS SELECT
270
- TraceId,
271
- min(Timestamp) as Start,
272
- max(Timestamp) as End
271
+ TraceId,
272
+ min(Timestamp) as Start,
273
+ max(Timestamp) as End
273
274
FROM
274
275
%s.%s
275
- WHERE TraceId!= ''
276
+ WHERE TraceId != ''
276
277
GROUP BY TraceId;
277
278
`
278
279
)
@@ -282,10 +283,10 @@ func createTracesTable(ctx context.Context, cfg *Config, db *sql.DB) error {
282
283
return fmt .Errorf ("exec create traces table sql: %w" , err )
283
284
}
284
285
if _ , err := db .ExecContext (ctx , renderCreateTraceIDTsTableSQL (cfg )); err != nil {
285
- return fmt .Errorf ("exec create traceIDTs table sql: %w" , err )
286
+ return fmt .Errorf ("exec create traceID timestamp table sql: %w" , err )
286
287
}
287
288
if _ , err := db .ExecContext (ctx , renderTraceIDTsMaterializedViewSQL (cfg )); err != nil {
288
- return fmt .Errorf ("exec create traceIDTs view sql: %w" , err )
289
+ return fmt .Errorf ("exec create traceID timestamp view sql: %w" , err )
289
290
}
290
291
return nil
291
292
}
@@ -295,12 +296,12 @@ func renderInsertTracesSQL(cfg *Config) string {
295
296
}
296
297
297
298
func renderCreateTracesTableSQL (cfg * Config ) string {
298
- ttlExpr := generateTTLExpr (cfg .TTL , "toDateTime (Timestamp)" )
299
+ ttlExpr := generateTTLExpr (cfg .TTL , "toDate (Timestamp)" )
299
300
return fmt .Sprintf (createTracesTableSQL , cfg .TracesTableName , cfg .clusterString (), cfg .tableEngineString (), ttlExpr )
300
301
}
301
302
302
303
func renderCreateTraceIDTsTableSQL (cfg * Config ) string {
303
- ttlExpr := generateTTLExpr (cfg .TTL , "toDateTime (Start)" )
304
+ ttlExpr := generateTTLExpr (cfg .TTL , "toDate (Start)" )
304
305
return fmt .Sprintf (createTraceIDTsTableSQL , cfg .TracesTableName , cfg .clusterString (), cfg .tableEngineString (), ttlExpr )
305
306
}
306
307
0 commit comments