Skip to content

Commit

Permalink
ttl config refactor. DRY principle applied
Browse files Browse the repository at this point in the history
  • Loading branch information
leartbeqiraj1 committed Nov 10, 2023
1 parent f9ad075 commit acd5f53
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 77 deletions.
19 changes: 1 addition & 18 deletions exporter/clickhouseexporter/exporter_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,24 +234,7 @@ 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 > 0 {
switch {
case cfg.TTL%(24*time.Hour) == 0:
ttlExpr = fmt.Sprintf(`TTL toDateTime(Timestamp) + toIntervalDay(%d)`, cfg.TTL/(24*time.Hour))
case cfg.TTL%(time.Hour) == 0:
ttlExpr = fmt.Sprintf(`TTL toDateTime(Timestamp) + toIntervalHour(%d)`, cfg.TTL/time.Hour)
case cfg.TTL%(time.Minute) == 0:
ttlExpr = fmt.Sprintf(`TTL toDateTime(Timestamp) + toIntervalMinute(%d)`, cfg.TTL/time.Minute)
default:
ttlExpr = fmt.Sprintf(`TTL toDateTime(Timestamp) + toIntervalSecond(%d)`, cfg.TTL/time.Second)
}
}
ttlExpr := ttlConfig(cfg.TTLDays, cfg.TTL)
return fmt.Sprintf(createLogsTableSQL, cfg.LogsTableName, ttlExpr)
}

Expand Down
4 changes: 3 additions & 1 deletion exporter/clickhouseexporter/exporter_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ 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.cfg.TTL, e.client)

ttlExpr := ttlConfig(e.cfg.TTLDays, e.cfg.TTL)
return internal.NewMetricsTable(ctx, e.cfg.MetricsTableName, ttlExpr, e.client)
}

// shutdown will shut down the exporter.
Expand Down
39 changes: 2 additions & 37 deletions exporter/clickhouseexporter/exporter_traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,47 +291,12 @@ func renderInsertTracesSQL(cfg *Config) string {
}

func renderCreateTracesTableSQL(cfg *Config) string {
var ttlExpr string

// deprecated and will be removed
if cfg.TTLDays > 0 {
ttlExpr = fmt.Sprintf(`TTL toDateTime(Timestamp) + toIntervalDay(%d)`, cfg.TTLDays)
}

if cfg.TTL > 0 {
switch {
case cfg.TTL%(24*time.Hour) == 0:
ttlExpr = fmt.Sprintf(`TTL toDateTime(Timestamp) + toIntervalDay(%d)`, cfg.TTL/(24*time.Hour))
case cfg.TTL%(time.Hour) == 0:
ttlExpr = fmt.Sprintf(`TTL toDateTime(Timestamp) + toIntervalHour(%d)`, cfg.TTL/time.Hour)
case cfg.TTL%(time.Minute) == 0:
ttlExpr = fmt.Sprintf(`TTL toDateTime(Timestamp) + toIntervalMinute(%d)`, cfg.TTL/time.Minute)
default:
ttlExpr = fmt.Sprintf(`TTL toDateTime(Timestamp) + toIntervalSecond(%d)`, cfg.TTL/time.Second)
}
}
ttlExpr := ttlConfig(cfg.TTLDays, cfg.TTL)
return fmt.Sprintf(createTracesTableSQL, cfg.TracesTableName, ttlExpr)
}

func renderCreateTraceIDTsTableSQL(cfg *Config) string {
var ttlExpr string

if cfg.TTLDays > 0 {
ttlExpr = fmt.Sprintf(`TTL toDateTime(Timestamp) + toIntervalDay(%d)`, cfg.TTLDays)
}

if cfg.TTL > 0 {
switch {
case cfg.TTL%(24*time.Hour) == 0:
ttlExpr = fmt.Sprintf(`TTL toDateTime(Timestamp) + toIntervalDay(%d)`, cfg.TTL/(24*time.Hour))
case cfg.TTL%(time.Hour) == 0:
ttlExpr = fmt.Sprintf(`TTL toDateTime(Timestamp) + toIntervalHour(%d)`, cfg.TTL/time.Hour)
case cfg.TTL%(time.Minute) == 0:
ttlExpr = fmt.Sprintf(`TTL toDateTime(Timestamp) + toIntervalMinute(%d)`, cfg.TTL/time.Minute)
default:
ttlExpr = fmt.Sprintf(`TTL toDateTime(Timestamp) + toIntervalSecond(%d)`, cfg.TTL/time.Second)
}
}
ttlExpr := ttlConfig(cfg.TTLDays, cfg.TTL)
return fmt.Sprintf(createTraceIDTsTableSQL, cfg.TracesTableName, ttlExpr)
}

Expand Down
21 changes: 21 additions & 0 deletions exporter/clickhouseexporter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package clickhouseexporter // import "github.com/open-telemetry/opentelemetry-co
import (
"context"
"fmt"
"time"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/exporter"
Expand Down Expand Up @@ -119,3 +120,23 @@ func createMetricExporter(
exporterhelper.WithRetry(c.RetrySettings),
)
}

func ttlConfig(ttlDays uint, ttl time.Duration) string {
if ttlDays > 0 {
return fmt.Sprintf(`TTL toDateTime(Timestamp) + toIntervalDay(%d)`, ttlDays)
}

if ttl > 0 {
switch {
case ttl%(24*time.Hour) == 0:
return fmt.Sprintf(`TTL toDateTime(Timestamp) + toIntervalDay(%d)`, ttl/(24*time.Hour))
case ttl%(time.Hour) == 0:
return fmt.Sprintf(`TTL toDateTime(Timestamp) + toIntervalHour(%d)`, ttl/time.Hour)
case ttl%(time.Minute) == 0:
return fmt.Sprintf(`TTL toDateTime(Timestamp) + toIntervalMinute(%d)`, ttl/time.Minute)
default:
return fmt.Sprintf(`TTL toDateTime(Timestamp) + toIntervalSecond(%d)`, ttl/time.Second)
}
}
return ""
}
22 changes: 1 addition & 21 deletions exporter/clickhouseexporter/internal/metrics_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"fmt"
"strings"
"sync"
"time"

"github.com/ClickHouse/clickhouse-go/v2"
"go.opentelemetry.io/collector/pdata/pcommon"
Expand Down Expand Up @@ -52,26 +51,7 @@ 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, ttl time.Duration, db *sql.DB) error {
var ttlExpr string

// deprecated and will be removed. Use 'ttl' instead.
if ttlDays > 0 {
ttlExpr = fmt.Sprintf(`TTL toDateTime(Timestamp) + toIntervalDay(%d)`, ttlDays)
}

if ttl > 0 {
switch {
case ttl%(24*time.Hour) == 0:
ttlExpr = fmt.Sprintf(`TTL toDateTime(Timestamp) + toIntervalDay(%d)`, ttl/(24*time.Hour))
case ttl%(time.Hour) == 0:
ttlExpr = fmt.Sprintf(`TTL toDateTime(Timestamp) + toIntervalHour(%d)`, ttl/time.Hour)
case ttl%(time.Minute) == 0:
ttlExpr = fmt.Sprintf(`TTL toDateTime(Timestamp) + toIntervalMinute(%d)`, ttl/time.Minute)
default:
ttlExpr = fmt.Sprintf(`TTL toDateTime(Timestamp) + toIntervalSecond(%d)`, ttl/time.Second)
}
}
func NewMetricsTable(ctx context.Context, tableName string, ttlExpr string, db *sql.DB) error {
for table := range supportedMetricTypes {
query := fmt.Sprintf(table, tableName, ttlExpr)
if _, err := db.ExecContext(ctx, query); err != nil {
Expand Down

0 comments on commit acd5f53

Please sign in to comment.