Skip to content

Commit 1620fc6

Browse files
[exporter/clickhouse] Upgrade trace table (#34245)
This PR updates the default `otel_traces` table schema. There are no breaking changes, this schema is compatible with the previous `INSERT` statement. This does not affect existing users, only new users that have `create_schema` enabled in the config. Notable changes: - Optimized ORDER BY to use a 32 bit DateTime without TraceId - Reduced DateTime64(9) to DateTime on the trace timestamp table - TTLs now use Date instead of DateTime since parts will only be dropped on the partition date No major column changes for now, but there were some small performance benefits observed with these changes. As always, it is best to edit these tables to fit your needs and query patterns. I expect further changes in the future as we continue to optimize this table for a generalized use case. **Testing:** Integration tests are disabled, but I enabled them locally to test that this change works as intended. Unit tests are passing as well. **Documentation:** - Updated README - Example DDL has been updated, and now includes the trace timestamp table and materialized view --------- Co-authored-by: Dmitrii Anoshin <[email protected]>
1 parent e6274cd commit 1620fc6

File tree

5 files changed

+137
-88
lines changed

5 files changed

+137
-88
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: clickhouseexporter
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Updated the default trace table
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [34245]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext: Reduced data types, improved partitioning and time range queries
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: []

exporter/clickhouseexporter/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ Limit 100;
128128
- Find spans with specific attribute.
129129

130130
```sql
131-
SELECT Timestamp as log_time,
131+
SELECT Timestamp,
132132
TraceId,
133133
SpanId,
134134
ParentSpanId,
@@ -156,7 +156,7 @@ WITH
156156
'391dae938234560b16bb63f51501cb6f' as trace_id,
157157
(SELECT min(Start) FROM otel_traces_trace_id_ts WHERE TraceId = trace_id) as start,
158158
(SELECT max(End) + 1 FROM otel_traces_trace_id_ts WHERE TraceId = trace_id) as end
159-
SELECT Timestamp as log_time,
159+
SELECT Timestamp,
160160
TraceId,
161161
SpanId,
162162
ParentSpanId,
@@ -180,7 +180,7 @@ Limit 100;
180180
- Find spans is error.
181181

182182
```sql
183-
SELECT Timestamp as log_time,
183+
SELECT Timestamp,
184184
TraceId,
185185
SpanId,
186186
ParentSpanId,
@@ -196,15 +196,15 @@ SELECT Timestamp as log_time,
196196
toString(Links.TraceId)
197197
FROM otel_traces
198198
WHERE ServiceName = 'clickhouse-exporter'
199-
AND StatusCode = 'STATUS_CODE_ERROR'
199+
AND StatusCode = 'Error'
200200
AND Timestamp >= NOW() - INTERVAL 1 HOUR
201201
Limit 100;
202202
```
203203

204204
- Find slow spans.
205205

206206
```sql
207-
SELECT Timestamp as log_time,
207+
SELECT Timestamp,
208208
TraceId,
209209
SpanId,
210210
ParentSpanId,
Lines changed: 57 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,63 @@
11
-- Default Trace table DDL
22

33
CREATE TABLE IF NOT EXISTS otel_traces (
4-
Timestamp DateTime64(9) CODEC(Delta, ZSTD(1)),
5-
TraceId String CODEC(ZSTD(1)),
6-
SpanId String CODEC(ZSTD(1)),
7-
ParentSpanId String CODEC(ZSTD(1)),
8-
TraceState String CODEC(ZSTD(1)),
9-
SpanName LowCardinality(String) CODEC(ZSTD(1)),
10-
SpanKind LowCardinality(String) CODEC(ZSTD(1)),
11-
ServiceName LowCardinality(String) CODEC(ZSTD(1)),
12-
ResourceAttributes Map(LowCardinality(String), String) CODEC(ZSTD(1)),
13-
ScopeName String CODEC(ZSTD(1)),
14-
ScopeVersion String CODEC(ZSTD(1)),
15-
SpanAttributes Map(LowCardinality(String), String) CODEC(ZSTD(1)),
16-
Duration Int64 CODEC(ZSTD(1)),
17-
StatusCode LowCardinality(String) CODEC(ZSTD(1)),
18-
StatusMessage String CODEC(ZSTD(1)),
19-
Events Nested (
20-
Timestamp DateTime64(9),
21-
Name LowCardinality(String),
22-
Attributes Map(LowCardinality(String), String)
23-
) CODEC(ZSTD(1)),
24-
Links Nested (
25-
TraceId String,
26-
SpanId String,
27-
TraceState String,
28-
Attributes Map(LowCardinality(String), String)
29-
) CODEC(ZSTD(1)),
30-
INDEX idx_trace_id TraceId TYPE bloom_filter(0.001) GRANULARITY 1,
31-
INDEX idx_res_attr_key mapKeys(ResourceAttributes) TYPE bloom_filter(0.01) GRANULARITY 1,
32-
INDEX idx_res_attr_value mapValues(ResourceAttributes) TYPE bloom_filter(0.01) GRANULARITY 1,
33-
INDEX idx_span_attr_key mapKeys(SpanAttributes) TYPE bloom_filter(0.01) GRANULARITY 1,
34-
INDEX idx_span_attr_value mapValues(SpanAttributes) TYPE bloom_filter(0.01) GRANULARITY 1,
35-
INDEX idx_duration Duration TYPE minmax GRANULARITY 1
4+
Timestamp DateTime64(9) CODEC(Delta, ZSTD(1)),
5+
TraceId String CODEC(ZSTD(1)),
6+
SpanId String CODEC(ZSTD(1)),
7+
ParentSpanId String CODEC(ZSTD(1)),
8+
TraceState String CODEC(ZSTD(1)),
9+
SpanName LowCardinality(String) CODEC(ZSTD(1)),
10+
SpanKind LowCardinality(String) CODEC(ZSTD(1)),
11+
ServiceName LowCardinality(String) CODEC(ZSTD(1)),
12+
ResourceAttributes Map(LowCardinality(String), String) CODEC(ZSTD(1)),
13+
ScopeName String CODEC(ZSTD(1)),
14+
ScopeVersion String CODEC(ZSTD(1)),
15+
SpanAttributes Map(LowCardinality(String), String) CODEC(ZSTD(1)),
16+
Duration UInt64 CODEC(ZSTD(1)),
17+
StatusCode LowCardinality(String) CODEC(ZSTD(1)),
18+
StatusMessage String CODEC(ZSTD(1)),
19+
Events Nested (
20+
Timestamp DateTime64(9),
21+
Name LowCardinality(String),
22+
Attributes Map(LowCardinality(String), String)
23+
) CODEC(ZSTD(1)),
24+
Links Nested (
25+
TraceId String,
26+
SpanId String,
27+
TraceState String,
28+
Attributes Map(LowCardinality(String), String)
29+
) CODEC(ZSTD(1)),
30+
INDEX idx_trace_id TraceId TYPE bloom_filter(0.001) GRANULARITY 1,
31+
INDEX idx_res_attr_key mapKeys(ResourceAttributes) TYPE bloom_filter(0.01) GRANULARITY 1,
32+
INDEX idx_res_attr_value mapValues(ResourceAttributes) TYPE bloom_filter(0.01) GRANULARITY 1,
33+
INDEX idx_span_attr_key mapKeys(SpanAttributes) TYPE bloom_filter(0.01) GRANULARITY 1,
34+
INDEX idx_span_attr_value mapValues(SpanAttributes) TYPE bloom_filter(0.01) GRANULARITY 1,
35+
INDEX idx_duration Duration TYPE minmax GRANULARITY 1
3636
) ENGINE = MergeTree()
37-
TTL toDateTime("Timestamp") + toIntervalDay(180)
3837
PARTITION BY toDate(Timestamp)
39-
ORDER BY (ServiceName, SpanName, toUnixTimestamp(Timestamp), TraceId)
38+
ORDER BY (ServiceName, SpanName, toDateTime(Timestamp))
39+
TTL toDate(Timestamp) + toIntervalDay(180)
40+
SETTINGS index_granularity=8192, ttl_only_drop_parts = 1;
41+
42+
43+
CREATE TABLE IF NOT EXISTS otel_traces_trace_id_ts (
44+
TraceId String CODEC(ZSTD(1)),
45+
Start DateTime CODEC(Delta, ZSTD(1)),
46+
End DateTime CODEC(Delta, ZSTD(1)),
47+
INDEX idx_trace_id TraceId TYPE bloom_filter(0.01) GRANULARITY 1
48+
) ENGINE = MergeTree()
49+
PARTITION BY toDate(Start)
50+
ORDER BY (TraceId, Start)
51+
TTL toDate(Start) + toIntervalDay(180)
4052
SETTINGS index_granularity=8192, ttl_only_drop_parts = 1;
53+
54+
55+
CREATE MATERIALIZED VIEW IF NOT EXISTS otel_traces_trace_id_ts_mv
56+
TO otel_traces_trace_id_ts
57+
AS SELECT
58+
TraceId,
59+
min(Timestamp) as Start,
60+
max(Timestamp) as End
61+
FROM otel_traces
62+
WHERE TraceId != ''
63+
GROUP BY TraceId;

exporter/clickhouseexporter/exporter_traces.go

Lines changed: 48 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -163,42 +163,42 @@ const (
163163
// language=ClickHouse SQL
164164
createTracesTableSQL = `
165165
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
198198
) ENGINE = %s
199-
%s
200199
PARTITION BY toDate(Timestamp)
201-
ORDER BY (ServiceName, SpanName, toUnixTimestamp(Timestamp), TraceId)
200+
ORDER BY (ServiceName, SpanName, toDateTime(Timestamp))
201+
%s
202202
SETTINGS index_granularity=8192, ttl_only_drop_parts = 1;
203203
`
204204
// language=ClickHouse SQL
@@ -253,26 +253,27 @@ SETTINGS index_granularity=8192, ttl_only_drop_parts = 1;
253253

254254
const (
255255
createTraceIDTsTableSQL = `
256-
create table IF NOT EXISTS %s_trace_id_ts %s (
256+
CREATE TABLE IF NOT EXISTS %s_trace_id_ts %s (
257257
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)),
260260
INDEX idx_trace_id TraceId TYPE bloom_filter(0.01) GRANULARITY 1
261261
) ENGINE = %s
262+
PARTITION BY toDate(Start)
263+
ORDER BY (TraceId, Start)
262264
%s
263-
ORDER BY (TraceId, toUnixTimestamp(Start))
264-
SETTINGS index_granularity=8192;
265+
SETTINGS index_granularity=8192, ttl_only_drop_parts = 1;
265266
`
266267
createTraceIDTsMaterializedViewSQL = `
267268
CREATE MATERIALIZED VIEW IF NOT EXISTS %s_trace_id_ts_mv %s
268269
TO %s.%s_trace_id_ts
269270
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
273274
FROM
274275
%s.%s
275-
WHERE TraceId!=''
276+
WHERE TraceId != ''
276277
GROUP BY TraceId;
277278
`
278279
)
@@ -282,10 +283,10 @@ func createTracesTable(ctx context.Context, cfg *Config, db *sql.DB) error {
282283
return fmt.Errorf("exec create traces table sql: %w", err)
283284
}
284285
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)
286287
}
287288
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)
289290
}
290291
return nil
291292
}
@@ -295,12 +296,12 @@ func renderInsertTracesSQL(cfg *Config) string {
295296
}
296297

297298
func renderCreateTracesTableSQL(cfg *Config) string {
298-
ttlExpr := generateTTLExpr(cfg.TTL, "toDateTime(Timestamp)")
299+
ttlExpr := generateTTLExpr(cfg.TTL, "toDate(Timestamp)")
299300
return fmt.Sprintf(createTracesTableSQL, cfg.TracesTableName, cfg.clusterString(), cfg.tableEngineString(), ttlExpr)
300301
}
301302

302303
func renderCreateTraceIDTsTableSQL(cfg *Config) string {
303-
ttlExpr := generateTTLExpr(cfg.TTL, "toDateTime(Start)")
304+
ttlExpr := generateTTLExpr(cfg.TTL, "toDate(Start)")
304305
return fmt.Sprintf(createTraceIDTsTableSQL, cfg.TracesTableName, cfg.clusterString(), cfg.tableEngineString(), ttlExpr)
305306
}
306307

exporter/clickhouseexporter/integration_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ func verifyExportLog(t *testing.T, logExporter *logsExporter) {
9595

9696
type log struct {
9797
Timestamp string `db:"Timestamp"`
98-
TimestampDate string `db:"TimestampDate"`
9998
TimestampTime string `db:"TimestampTime"`
10099
TraceID string `db:"TraceId"`
101100
SpanID string `db:"SpanId"`
@@ -117,7 +116,6 @@ func verifyExportLog(t *testing.T, logExporter *logsExporter) {
117116

118117
expectLog := log{
119118
Timestamp: "2023-12-25T09:53:49Z",
120-
TimestampDate: "2023-12-25T00:00:00Z",
121119
TimestampTime: "2023-12-25T09:53:49Z",
122120
TraceID: "01020300000000000000000000000000",
123121
SpanID: "0102030000000000",

0 commit comments

Comments
 (0)