From acd5f53ddefe6b8c4e7b2e0329e53d211cc8311f Mon Sep 17 00:00:00 2001 From: LeartBeqiraj Date: Fri, 10 Nov 2023 18:32:27 +0100 Subject: [PATCH] ttl config refactor. DRY principle applied --- exporter/clickhouseexporter/exporter_logs.go | 19 +-------- .../clickhouseexporter/exporter_metrics.go | 4 +- .../clickhouseexporter/exporter_traces.go | 39 +------------------ exporter/clickhouseexporter/factory.go | 21 ++++++++++ .../internal/metrics_model.go | 22 +---------- 5 files changed, 28 insertions(+), 77 deletions(-) diff --git a/exporter/clickhouseexporter/exporter_logs.go b/exporter/clickhouseexporter/exporter_logs.go index 501df62dbf86..43b8d73dc11c 100644 --- a/exporter/clickhouseexporter/exporter_logs.go +++ b/exporter/clickhouseexporter/exporter_logs.go @@ -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) } diff --git a/exporter/clickhouseexporter/exporter_metrics.go b/exporter/clickhouseexporter/exporter_metrics.go index 7b64a717be75..1eba2d2a3e24 100644 --- a/exporter/clickhouseexporter/exporter_metrics.go +++ b/exporter/clickhouseexporter/exporter_metrics.go @@ -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. diff --git a/exporter/clickhouseexporter/exporter_traces.go b/exporter/clickhouseexporter/exporter_traces.go index b02ba47202ad..754528e91975 100644 --- a/exporter/clickhouseexporter/exporter_traces.go +++ b/exporter/clickhouseexporter/exporter_traces.go @@ -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) } diff --git a/exporter/clickhouseexporter/factory.go b/exporter/clickhouseexporter/factory.go index 6a071dd6ccbf..be94c3ec0551 100644 --- a/exporter/clickhouseexporter/factory.go +++ b/exporter/clickhouseexporter/factory.go @@ -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" @@ -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 "" +} diff --git a/exporter/clickhouseexporter/internal/metrics_model.go b/exporter/clickhouseexporter/internal/metrics_model.go index cc7a644eaf31..ceb810b9bbf3 100644 --- a/exporter/clickhouseexporter/internal/metrics_model.go +++ b/exporter/clickhouseexporter/internal/metrics_model.go @@ -11,7 +11,6 @@ import ( "fmt" "strings" "sync" - "time" "github.com/ClickHouse/clickhouse-go/v2" "go.opentelemetry.io/collector/pdata/pcommon" @@ -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 {