Skip to content

Commit

Permalink
TTL - added hours in addition to only days
Browse files Browse the repository at this point in the history
  • Loading branch information
leartbeqiraj1 committed Nov 9, 2023
1 parent 65b745f commit 4b99908
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 13 deletions.
9 changes: 7 additions & 2 deletions exporter/clickhouseexporter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ import (
"go.opentelemetry.io/collector/exporter/exporterhelper"
)

type TTLConfig struct {
Value int `mapstructure:"value"`
Unit string `mapstructure:"unit"`
}

// Config defines configuration for Elastic exporter.
type Config struct {
exporterhelper.TimeoutSettings `mapstructure:",squash"`
Expand All @@ -36,8 +41,8 @@ type Config struct {
TracesTableName string `mapstructure:"traces_table_name"`
// MetricsTableName is the table name for metrics. default is `otel_metrics`.
MetricsTableName string `mapstructure:"metrics_table_name"`
// TTLDays is The data time-to-live in days, 0 means no ttl.
TTLDays uint `mapstructure:"ttl_days"`
// TTL is The data time-to-live in days or hours, 0 means no ttl.
TTL TTLConfig `mapstructure:"ttl"`
}

const defaultDatabase = "default"
Expand Down
9 changes: 7 additions & 2 deletions exporter/clickhouseexporter/exporter_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,13 @@ func createLogsTable(ctx context.Context, cfg *Config, db *sql.DB) error {

func renderCreateLogsTableSQL(cfg *Config) string {
var ttlExpr string
if cfg.TTLDays > 0 {
ttlExpr = fmt.Sprintf(`TTL toDateTime(Timestamp) + toIntervalDay(%d)`, cfg.TTLDays)
if cfg.TTL.Value > 0 {
switch cfg.TTL.Unit {
case "days":
ttlExpr = fmt.Sprintf(`TTL toDateTime(Timestamp) + toIntervalDay(%d)`, cfg.TTL.Value)
case "hours":
ttlExpr = fmt.Sprintf(`TTL toDateTime(Timestamp) + toIntervalHour(%d)`, cfg.TTL.Value)
}
}
return fmt.Sprintf(createLogsTableSQL, cfg.LogsTableName, ttlExpr)
}
Expand Down
2 changes: 1 addition & 1 deletion exporter/clickhouseexporter/exporter_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (e *metricsExporter) start(ctx context.Context, _ component.Host) error {
}

internal.SetLogger(e.logger)
return internal.NewMetricsTable(ctx, e.cfg.MetricsTableName, e.cfg.TTLDays, e.client)
return internal.NewMetricsTable(ctx, e.cfg.MetricsTableName, e.cfg.TTL.Value, e.cfg.TTL.Unit, e.client)
}

// shutdown will shut down the exporter.
Expand Down
18 changes: 14 additions & 4 deletions exporter/clickhouseexporter/exporter_traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,16 +292,26 @@ func renderInsertTracesSQL(cfg *Config) string {

func renderCreateTracesTableSQL(cfg *Config) string {
var ttlExpr string
if cfg.TTLDays > 0 {
ttlExpr = fmt.Sprintf(`TTL toDateTime(Timestamp) + toIntervalDay(%d)`, cfg.TTLDays)
if cfg.TTL.Value > 0 {
switch cfg.TTL.Unit {
case "days":
ttlExpr = fmt.Sprintf(`TTL toDateTime(Timestamp) + toIntervalDay(%d)`, cfg.TTL.Value)
case "hours":
ttlExpr = fmt.Sprintf(`TTL toDateTime(Timestamp) + toIntervalHour(%d)`, cfg.TTL.Value)
}
}
return fmt.Sprintf(createTracesTableSQL, cfg.TracesTableName, ttlExpr)
}

func renderCreateTraceIDTsTableSQL(cfg *Config) string {
var ttlExpr string
if cfg.TTLDays > 0 {
ttlExpr = fmt.Sprintf(`TTL toDateTime(Start) + toIntervalDay(%d)`, cfg.TTLDays)
if cfg.TTL.Value > 0 {
switch cfg.TTL.Unit {
case "days":
ttlExpr = fmt.Sprintf(`TTL toDateTime(Timestamp) + toIntervalDay(%d)`, cfg.TTL.Value)
case "hours":
ttlExpr = fmt.Sprintf(`TTL toDateTime(Timestamp) + toIntervalHour(%d)`, cfg.TTL.Value)
}
}
return fmt.Sprintf(createTraceIDTsTableSQL, cfg.TracesTableName, ttlExpr)
}
Expand Down
5 changes: 4 additions & 1 deletion exporter/clickhouseexporter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ func createDefaultConfig() component.Config {
LogsTableName: "otel_logs",
TracesTableName: "otel_traces",
MetricsTableName: "otel_metrics",
TTLDays: 0,
TTL: TTLConfig{
Value: 0,
Unit: "hours",
},
}
}

Expand Down
11 changes: 8 additions & 3 deletions exporter/clickhouseexporter/internal/metrics_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,15 @@ func SetLogger(l *zap.Logger) {
}

// NewMetricsTable create metric tables with an expiry time to storage metric telemetry data
func NewMetricsTable(ctx context.Context, tableName string, ttlDays uint, db *sql.DB) error {
func NewMetricsTable(ctx context.Context, tableName string, Value int, Unit string, db *sql.DB) error {
var ttlExpr string
if ttlDays > 0 {
ttlExpr = fmt.Sprintf(`TTL toDateTime(TimeUnix) + toIntervalDay(%d)`, ttlDays)
if Value > 0 {
switch Unit {
case "days":
ttlExpr = fmt.Sprintf(`TTL toDateTime(TimeUnix) + toIntervalDay(%d)`, Value)
case "hours":
ttlExpr = fmt.Sprintf(`TTL toDateTime(TimeUnix) + toIntervalHour(%d)`, Value)
}
}
for table := range supportedMetricTypes {
query := fmt.Sprintf(table, tableName, ttlExpr)
Expand Down

0 comments on commit 4b99908

Please sign in to comment.