diff --git a/exporter/clickhouseexporter/README.md b/exporter/clickhouseexporter/README.md index 03b0f8a1d2b8..a6ff96226656 100644 --- a/exporter/clickhouseexporter/README.md +++ b/exporter/clickhouseexporter/README.md @@ -283,6 +283,7 @@ Connection options: - `ttl` (default = 0): The data time-to-live example 30m, 48h. Also, 0 means no ttl. - `database` (default = otel): The database name. - `connection_params` (default = {}). Params is the extra connection parameters with map format. +- `create_db_and_tables` (default = false). On startup, create the otel database and tables ClickHouse tables: diff --git a/exporter/clickhouseexporter/config.go b/exporter/clickhouseexporter/config.go index 1b1b7ed9640a..1b58d442e450 100644 --- a/exporter/clickhouseexporter/config.go +++ b/exporter/clickhouseexporter/config.go @@ -41,7 +41,8 @@ type Config struct { // Deprecated: Use 'ttl' instead TTLDays uint `mapstructure:"ttl_days"` // TTL is The data time-to-live example 30m, 48h. 0 means no ttl. - TTL time.Duration `mapstructure:"ttl"` + TTL time.Duration `mapstructure:"ttl"` + CreateDBAndTables bool `mapstructure:"create_db_and_tables"` } const defaultDatabase = "default" diff --git a/exporter/clickhouseexporter/example/otel-collector-config.yml b/exporter/clickhouseexporter/example/otel-collector-config.yml index 4749d46791e6..4dea1fff4b84 100644 --- a/exporter/clickhouseexporter/example/otel-collector-config.yml +++ b/exporter/clickhouseexporter/example/otel-collector-config.yml @@ -117,6 +117,7 @@ processors: # action: upsert exporters: clickhouse: + create_db_and_tables: true endpoint: http://clickhouse:8123 database: otel logs_table_name: otel_logs_1 @@ -124,7 +125,7 @@ exporters: connection_params: async_insert: 1 wait_for_async_insert: 0 - ttl_days: 12h + ttl_days: 1 timeout: 10s sending_queue: queue_size: 100 diff --git a/exporter/clickhouseexporter/exporter_logs.go b/exporter/clickhouseexporter/exporter_logs.go index b75ee1a0bba3..8306001a9f90 100644 --- a/exporter/clickhouseexporter/exporter_logs.go +++ b/exporter/clickhouseexporter/exporter_logs.go @@ -42,11 +42,14 @@ func newLogsExporter(logger *zap.Logger, cfg *Config) (*logsExporter, error) { } func (e *logsExporter) start(ctx context.Context, _ component.Host) error { - if err := createDatabase(ctx, e.cfg); err != nil { - return err - } + if e.cfg.CreateDBAndTables { + if err := createDatabase(ctx, e.cfg); err != nil { + return err + } - return createLogsTable(ctx, e.cfg, e.client) + return createLogsTable(ctx, e.cfg, e.client) + } + return nil } // shutdown will shut down the exporter. diff --git a/exporter/clickhouseexporter/exporter_metrics.go b/exporter/clickhouseexporter/exporter_metrics.go index 340b3fe071ea..68d9c9db3739 100644 --- a/exporter/clickhouseexporter/exporter_metrics.go +++ b/exporter/clickhouseexporter/exporter_metrics.go @@ -37,14 +37,19 @@ func newMetricsExporter(logger *zap.Logger, cfg *Config) (*metricsExporter, erro } func (e *metricsExporter) start(ctx context.Context, _ component.Host) error { - if err := createDatabase(ctx, e.cfg); err != nil { - return err + if e.cfg.CreateDBAndTables { + if err := createDatabase(ctx, e.cfg); err != nil { + return err + } } internal.SetLogger(e.logger) ttlExpr := generateTTLExpr(e.cfg.TTLDays, e.cfg.TTL) - return internal.NewMetricsTable(ctx, e.cfg.MetricsTableName, ttlExpr, e.client) + if e.cfg.CreateDBAndTables { + return internal.NewMetricsTable(ctx, e.cfg.MetricsTableName, ttlExpr, e.client) + } + return nil } // shutdown will shut down the exporter. diff --git a/exporter/clickhouseexporter/exporter_traces.go b/exporter/clickhouseexporter/exporter_traces.go index eef63f488c02..63e381137e23 100644 --- a/exporter/clickhouseexporter/exporter_traces.go +++ b/exporter/clickhouseexporter/exporter_traces.go @@ -42,11 +42,13 @@ func newTracesExporter(logger *zap.Logger, cfg *Config) (*tracesExporter, error) } func (e *tracesExporter) start(ctx context.Context, _ component.Host) error { - if err := createDatabase(ctx, e.cfg); err != nil { - return err + if e.cfg.CreateDBAndTables { + if err := createDatabase(ctx, e.cfg); err != nil { + return err + } + return createTracesTable(ctx, e.cfg, e.client) } - - return createTracesTable(ctx, e.cfg, e.client) + return nil } // shutdown will shut down the exporter.