Skip to content

Commit

Permalink
STAC-20844: optional traces table creation
Browse files Browse the repository at this point in the history
  • Loading branch information
fvlankvelt committed Feb 21, 2024
1 parent 7dac9e4 commit 2f542ea
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 57 deletions.
2 changes: 2 additions & 0 deletions exporter/clickhousestsexporter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ ClickHouse tables:
- `logs_table_name` (default = otel_logs): The table name for logs.
- `traces_table_name` (default = otel_traces): The table name for traces.
- `metrics_table_name` (default = otel_metrics): The table name for metrics.
- `create_traces_table` (default = true): Create the traces table on startup

Processing:

Expand Down Expand Up @@ -329,6 +330,7 @@ exporters:
logs_table_name: otel_logs
traces_table_name: otel_traces
metrics_table_name: otel_metrics
create_traces_table: true
timeout: 5s
retry_on_failure:
enabled: true
Expand Down
2 changes: 2 additions & 0 deletions exporter/clickhousestsexporter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ type Config struct {
TTLDays uint `mapstructure:"ttl_days"`
// TTL is The data time-to-live example 30m, 48h. 0 means no ttl.
TTL time.Duration `mapstructure:"ttl"`
// Create the traces table on startup
CreateTracesTable bool `mapstructure:"create_traces_table"`
}

const defaultDatabase = "default"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ exporters:
database: otel
logs_table_name: otel_logs
traces_table_name: otel_traces
create_traces_table: true
ttl: 12h
timeout: 10s
sending_queue:
Expand Down
52 changes: 4 additions & 48 deletions exporter/clickhousestsexporter/exporter_traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ func newTracesExporter(logger *zap.Logger, cfg *Config) (*tracesExporter, error)
}

func (e *tracesExporter) start(ctx context.Context, _ component.Host) error {
if !e.cfg.CreateTracesTable {
return nil
}

if err := createDatabase(ctx, e.cfg); err != nil {
return err
}
Expand Down Expand Up @@ -200,12 +204,6 @@ CREATE TABLE IF NOT EXISTS %s (
TraceState String,
Attributes Map(LowCardinality(String), String)
) CODEC(ZSTD(1)),
INDEX idx_trace_id TraceId TYPE bloom_filter(0.001) GRANULARITY 1,
INDEX idx_res_attr_key mapKeys(ResourceAttributes) TYPE bloom_filter(0.01) GRANULARITY 1,
INDEX idx_res_attr_value mapValues(ResourceAttributes) TYPE bloom_filter(0.01) GRANULARITY 1,
INDEX idx_span_attr_key mapKeys(SpanAttributes) TYPE bloom_filter(0.01) GRANULARITY 1,
INDEX idx_span_attr_value mapValues(SpanAttributes) TYPE bloom_filter(0.01) GRANULARITY 1,
INDEX idx_duration Duration TYPE minmax GRANULARITY 1
) ENGINE MergeTree()
%s
PARTITION BY toDate(Timestamp)
Expand Down Expand Up @@ -264,42 +262,10 @@ SETTINGS index_granularity=8192, ttl_only_drop_parts = 1;
)`
)

const (
createTraceIDTsTableSQL = `
create table IF NOT EXISTS %s_trace_id_ts (
TraceId String CODEC(ZSTD(1)),
Start DateTime64(9) CODEC(Delta, ZSTD(1)),
End DateTime64(9) CODEC(Delta, ZSTD(1)),
INDEX idx_trace_id TraceId TYPE bloom_filter(0.01) GRANULARITY 1
) ENGINE MergeTree()
%s
ORDER BY (TraceId, toUnixTimestamp(Start))
SETTINGS index_granularity=8192;
`
createTraceIDTsMaterializedViewSQL = `
CREATE MATERIALIZED VIEW IF NOT EXISTS %s_trace_id_ts_mv
TO %s.%s_trace_id_ts
AS SELECT
TraceId,
min(Timestamp) as Start,
max(Timestamp) as End
FROM
%s.%s
WHERE TraceId!=''
GROUP BY TraceId;
`
)

func createTracesTable(ctx context.Context, cfg *Config, db *sql.DB) error {
if _, err := db.ExecContext(ctx, renderCreateTracesTableSQL(cfg)); err != nil {
return fmt.Errorf("exec create traces table sql: %w", err)
}
if _, err := db.ExecContext(ctx, renderCreateTraceIDTsTableSQL(cfg)); err != nil {
return fmt.Errorf("exec create traceIDTs table sql: %w", err)
}
if _, err := db.ExecContext(ctx, renderTraceIDTsMaterializedViewSQL(cfg)); err != nil {
return fmt.Errorf("exec create traceIDTs view sql: %w", err)
}
return nil
}

Expand All @@ -311,13 +277,3 @@ func renderCreateTracesTableSQL(cfg *Config) string {
ttlExpr := generateTTLExpr(cfg.TTLDays, cfg.TTL, "Timestamp")
return fmt.Sprintf(createTracesTableSQL, cfg.TracesTableName, ttlExpr)
}

func renderCreateTraceIDTsTableSQL(cfg *Config) string {
ttlExpr := generateTTLExpr(cfg.TTLDays, cfg.TTL, "Start")
return fmt.Sprintf(createTraceIDTsTableSQL, cfg.TracesTableName, ttlExpr)
}

func renderTraceIDTsMaterializedViewSQL(cfg *Config) string {
return fmt.Sprintf(createTraceIDTsMaterializedViewSQL, cfg.TracesTableName,
cfg.Database, cfg.TracesTableName, cfg.Database, cfg.TracesTableName)
}
19 changes: 10 additions & 9 deletions exporter/clickhousestsexporter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,16 @@ func createDefaultConfig() component.Config {
queueSettings.NumConsumers = 1

return &Config{
TimeoutSettings: exporterhelper.NewDefaultTimeoutSettings(),
QueueSettings: queueSettings,
BackOffConfig: configretry.NewDefaultBackOffConfig(),
ConnectionParams: map[string]string{},
Database: defaultDatabase,
LogsTableName: "otel_logs",
TracesTableName: "otel_traces",
MetricsTableName: "otel_metrics",
TTL: 0,
TimeoutSettings: exporterhelper.NewDefaultTimeoutSettings(),
QueueSettings: queueSettings,
BackOffConfig: configretry.NewDefaultBackOffConfig(),
ConnectionParams: map[string]string{},
Database: defaultDatabase,
LogsTableName: "otel_logs",
TracesTableName: "otel_traces",
MetricsTableName: "otel_metrics",
CreateTracesTable: true,
TTL: 0,
}
}

Expand Down
1 change: 1 addition & 0 deletions exporter/clickhousestsexporter/testdata/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ clickhousests/full:
ttl: 72h
logs_table_name: otel_logs
traces_table_name: otel_traces
create_traces_table: true
timeout: 5s
retry_on_failure:
enabled: true
Expand Down

0 comments on commit 2f542ea

Please sign in to comment.