123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- // Copyright The OpenTelemetry Authors
- // SPDX-License-Identifier: Apache-2.0
- //go:generate mdatagen metadata.yaml
- package clickhouseexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/clickhouseexporter"
- import (
- "context"
- "fmt"
- "time"
- "go.opentelemetry.io/collector/component"
- "go.opentelemetry.io/collector/exporter"
- "go.opentelemetry.io/collector/exporter/exporterhelper"
- "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/clickhouseexporter/internal/metadata"
- )
- // NewFactory creates a factory for Elastic exporter.
- func NewFactory() exporter.Factory {
- return exporter.NewFactory(
- metadata.Type,
- createDefaultConfig,
- exporter.WithLogs(createLogsExporter, metadata.LogsStability),
- exporter.WithTraces(createTracesExporter, metadata.TracesStability),
- exporter.WithMetrics(createMetricExporter, metadata.MetricsStability),
- )
- }
- func createDefaultConfig() component.Config {
- queueSettings := exporterhelper.NewDefaultQueueSettings()
- queueSettings.NumConsumers = 1
- return &Config{
- TimeoutSettings: exporterhelper.NewDefaultTimeoutSettings(),
- QueueSettings: queueSettings,
- RetrySettings: exporterhelper.NewDefaultRetrySettings(),
- ConnectionParams: map[string]string{},
- Database: defaultDatabase,
- LogsTableName: "otel_logs",
- TracesTableName: "otel_traces",
- MetricsTableName: "otel_metrics",
- TTL: 0,
- }
- }
- // createLogsExporter creates a new exporter for logs.
- // Logs are directly insert into clickhouse.
- func createLogsExporter(
- ctx context.Context,
- set exporter.CreateSettings,
- cfg component.Config,
- ) (exporter.Logs, error) {
- c := cfg.(*Config)
- exporter, err := newLogsExporter(set.Logger, c)
- if err != nil {
- return nil, fmt.Errorf("cannot configure clickhouse logs exporter: %w", err)
- }
- return exporterhelper.NewLogsExporter(
- ctx,
- set,
- cfg,
- exporter.pushLogsData,
- exporterhelper.WithStart(exporter.start),
- exporterhelper.WithShutdown(exporter.shutdown),
- exporterhelper.WithTimeout(c.TimeoutSettings),
- exporterhelper.WithQueue(c.QueueSettings),
- exporterhelper.WithRetry(c.RetrySettings),
- )
- }
- // createTracesExporter creates a new exporter for traces.
- // Traces are directly insert into clickhouse.
- func createTracesExporter(
- ctx context.Context,
- set exporter.CreateSettings,
- cfg component.Config,
- ) (exporter.Traces, error) {
- c := cfg.(*Config)
- exporter, err := newTracesExporter(set.Logger, c)
- if err != nil {
- return nil, fmt.Errorf("cannot configure clickhouse traces exporter: %w", err)
- }
- return exporterhelper.NewTracesExporter(
- ctx,
- set,
- cfg,
- exporter.pushTraceData,
- exporterhelper.WithStart(exporter.start),
- exporterhelper.WithShutdown(exporter.shutdown),
- exporterhelper.WithTimeout(c.TimeoutSettings),
- exporterhelper.WithQueue(c.QueueSettings),
- exporterhelper.WithRetry(c.RetrySettings),
- )
- }
- func createMetricExporter(
- ctx context.Context,
- set exporter.CreateSettings,
- cfg component.Config,
- ) (exporter.Metrics, error) {
- c := cfg.(*Config)
- exporter, err := newMetricsExporter(set.Logger, c)
- if err != nil {
- return nil, fmt.Errorf("cannot configure clickhouse metrics exporter: %w", err)
- }
- return exporterhelper.NewMetricsExporter(
- ctx,
- set,
- cfg,
- exporter.pushMetricsData,
- exporterhelper.WithStart(exporter.start),
- exporterhelper.WithShutdown(exporter.shutdown),
- exporterhelper.WithTimeout(c.TimeoutSettings),
- exporterhelper.WithQueue(c.QueueSettings),
- exporterhelper.WithRetry(c.RetrySettings),
- )
- }
- func generateTTLExpr(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 ""
- }
|