Skip to content

Commit

Permalink
add optional table TTL
Browse files Browse the repository at this point in the history
  • Loading branch information
tommyzli committed May 17, 2024
1 parent b680d6e commit 451e190
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions pkg/sinks/clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type ClickHouseConfig struct {
TLS *TLS `yaml:"tls"`
CreateTable bool `yaml:"createTable"`
TableEngine string `yaml:"tableEngine"`
TableTTLDays *int `yaml:"tableTtlDays"`
Compress string `yaml:"compress"`
MaxIdleConns *int `yaml:"maxIdleConns"`
MaxOpenConns *int `yaml:"maxOpenConns"`
Expand Down Expand Up @@ -58,7 +59,10 @@ const (
ReportingInstance Nullable(String),
) ENGINE = %s
ORDER BY (Name, Namespace, LastTimestamp)
PARTITION BY toYYYYMM(LastTimestamp);`
PARTITION BY toYYYYMM(LastTimestamp)
%s
;`
ttlStatementFmt = "TTL toDateTime(LastTimestamp) + toIntervalDay(%d)"
insertStatementFmt = `INSERT INTO %s (
KubeClusterName,
Reason,
Expand Down Expand Up @@ -175,7 +179,11 @@ func NewClickHouse(cfg *ClickHouseConfig) (*ClickHouse, error) {
if cfg.TableEngine != "" {
tableEngine = cfg.TableEngine
}
_, err = db.Exec(fmt.Sprintf(ddlFmt, cfg.TableName, tableEngine))
ttlStatement := ""
if cfg.TableTTLDays != nil {
ttlStatement = fmt.Sprintf(ttlStatementFmt, *cfg.TableTTLDays)
}
_, err = db.Exec(fmt.Sprintf(ddlFmt, cfg.TableName, tableEngine, ttlStatement))
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 451e190

Please sign in to comment.