2021-05-27 12:52:34 +05:30
package clickhouseReader
import (
2022-05-03 11:20:57 +05:30
"context"
2021-05-27 12:52:34 +05:30
"time"
2022-05-03 11:20:57 +05:30
"github.com/ClickHouse/clickhouse-go/v2"
2023-08-10 17:20:34 +05:30
"go.uber.org/zap"
2021-05-27 12:52:34 +05:30
)
type Encoding string
const (
// EncodingJSON is used for spans encoded as JSON.
EncodingJSON Encoding = "json"
// EncodingProto is used for spans encoded as Protobuf.
EncodingProto Encoding = "protobuf"
)
const (
2022-08-04 11:57:05 +05:30
defaultDatasource string = "tcp://localhost:9000"
defaultTraceDB string = "signoz_traces"
2022-12-02 12:30:28 +05:30
defaultOperationsTable string = "distributed_signoz_operations"
defaultIndexTable string = "distributed_signoz_index_v2"
defaultErrorTable string = "distributed_signoz_error_index_v2"
defaultDurationTable string = "distributed_durationSort"
defaultUsageExplorerTable string = "distributed_usage_explorer"
defaultSpansTable string = "distributed_signoz_spans"
2023-03-28 22:15:46 +05:30
defaultDependencyGraphTable string = "distributed_dependency_graph_minutes_v2"
2022-12-02 12:30:28 +05:30
defaultTopLevelOperationsTable string = "distributed_top_level_operations"
2023-04-13 15:33:08 +05:30
defaultSpanAttributeTable string = "distributed_span_attributes"
2023-04-25 21:53:46 +05:30
defaultSpanAttributeKeysTable string = "distributed_span_attributes_keys"
2022-08-04 17:32:45 +05:30
defaultLogsDB string = "signoz_logs"
2022-12-02 12:30:28 +05:30
defaultLogsTable string = "distributed_logs"
defaultLogsLocalTable string = "logs"
2023-02-21 10:52:03 +05:30
defaultLogAttributeKeysTable string = "distributed_logs_attribute_keys"
2022-12-02 12:30:28 +05:30
defaultLogResourceKeysTable string = "distributed_logs_resource_keys"
2023-04-06 13:32:24 +05:30
defaultLogTagAttributeTable string = "distributed_tag_attributes"
2023-07-20 17:53:55 +05:30
defaultLiveTailRefreshSeconds int = 5
2022-08-04 11:57:05 +05:30
defaultWriteBatchDelay time . Duration = 5 * time . Second
defaultWriteBatchSize int = 10000
defaultEncoding Encoding = EncodingJSON
2021-05-27 12:52:34 +05:30
)
// NamespaceConfig is Clickhouse's internal configuration data
type namespaceConfig struct {
2022-08-04 11:57:05 +05:30
namespace string
Enabled bool
Datasource string
2023-08-10 17:20:34 +05:30
MaxIdleConns int
MaxOpenConns int
DialTimeout time . Duration
2022-08-04 11:57:05 +05:30
TraceDB string
OperationsTable string
IndexTable string
DurationTable string
2022-08-04 12:55:21 +05:30
UsageExplorerTable string
2022-08-04 11:57:05 +05:30
SpansTable string
ErrorTable string
2023-04-13 15:33:08 +05:30
SpanAttributeTable string
2023-04-25 21:53:46 +05:30
SpanAttributeKeysTable string
2022-08-04 12:38:53 +05:30
DependencyGraphTable string
2022-08-04 11:57:05 +05:30
TopLevelOperationsTable string
2022-08-04 17:32:45 +05:30
LogsDB string
LogsTable string
2022-12-02 12:30:28 +05:30
LogsLocalTable string
2022-08-04 17:32:45 +05:30
LogsAttributeKeysTable string
LogsResourceKeysTable string
2023-04-06 13:32:24 +05:30
LogsTagAttributeTable string
2022-08-04 17:32:45 +05:30
LiveTailRefreshSeconds int
2022-08-04 11:57:05 +05:30
WriteBatchDelay time . Duration
WriteBatchSize int
Encoding Encoding
Connector Connector
2021-05-27 12:52:34 +05:30
}
// Connecto defines how to connect to the database
2022-05-03 11:20:57 +05:30
type Connector func ( cfg * namespaceConfig ) ( clickhouse . Conn , error )
2021-05-27 12:52:34 +05:30
2022-05-03 11:20:57 +05:30
func defaultConnector ( cfg * namespaceConfig ) ( clickhouse . Conn , error ) {
ctx := context . Background ( )
2024-02-28 02:11:00 +08:00
options , err := clickhouse . ParseDSN ( cfg . Datasource )
2023-08-10 17:20:34 +05:30
if err != nil {
return nil , err
}
2024-02-28 02:11:00 +08:00
// Check if the DSN contained any of the following options, if not set from configuration
if options . MaxIdleConns == 0 {
options . MaxIdleConns = cfg . MaxIdleConns
}
if options . MaxOpenConns == 0 {
options . MaxOpenConns = cfg . MaxOpenConns
2022-05-03 11:20:57 +05:30
}
2024-02-28 02:11:00 +08:00
if options . DialTimeout == 0 {
options . DialTimeout = cfg . DialTimeout
2022-05-03 11:20:57 +05:30
}
2024-02-28 02:11:00 +08:00
2024-03-27 00:07:29 +05:30
zap . L ( ) . Info ( "Connecting to Clickhouse" , zap . String ( "at" , options . Addr [ 0 ] ) , zap . Int ( "MaxIdleConns" , options . MaxIdleConns ) , zap . Int ( "MaxOpenConns" , options . MaxOpenConns ) , zap . Duration ( "DialTimeout" , options . DialTimeout ) )
2022-05-03 11:20:57 +05:30
db , err := clickhouse . Open ( options )
2021-05-27 12:52:34 +05:30
if err != nil {
return nil , err
}
2022-05-03 11:20:57 +05:30
if err := db . Ping ( ctx ) ; err != nil {
2021-05-27 12:52:34 +05:30
return nil , err
}
return db , nil
}
// Options store storage plugin related configs
type Options struct {
primary * namespaceConfig
others map [ string ] * namespaceConfig
}
// NewOptions creates a new Options struct.
2023-08-10 17:20:34 +05:30
func NewOptions (
datasource string ,
maxIdleConns int ,
maxOpenConns int ,
dialTimeout time . Duration ,
primaryNamespace string ,
otherNamespaces ... string ,
) * Options {
2021-05-27 12:52:34 +05:30
if datasource == "" {
datasource = defaultDatasource
}
options := & Options {
primary : & namespaceConfig {
2022-08-04 11:57:05 +05:30
namespace : primaryNamespace ,
Enabled : true ,
Datasource : datasource ,
2023-08-10 17:20:34 +05:30
MaxIdleConns : maxIdleConns ,
MaxOpenConns : maxOpenConns ,
DialTimeout : dialTimeout ,
2022-08-04 11:57:05 +05:30
TraceDB : defaultTraceDB ,
OperationsTable : defaultOperationsTable ,
IndexTable : defaultIndexTable ,
ErrorTable : defaultErrorTable ,
DurationTable : defaultDurationTable ,
2022-08-04 12:55:21 +05:30
UsageExplorerTable : defaultUsageExplorerTable ,
2022-08-04 11:57:05 +05:30
SpansTable : defaultSpansTable ,
2023-04-13 15:33:08 +05:30
SpanAttributeTable : defaultSpanAttributeTable ,
2023-04-25 21:53:46 +05:30
SpanAttributeKeysTable : defaultSpanAttributeKeysTable ,
2022-08-04 12:38:53 +05:30
DependencyGraphTable : defaultDependencyGraphTable ,
2022-08-04 11:57:05 +05:30
TopLevelOperationsTable : defaultTopLevelOperationsTable ,
2022-08-04 17:32:45 +05:30
LogsDB : defaultLogsDB ,
LogsTable : defaultLogsTable ,
2022-12-02 12:30:28 +05:30
LogsLocalTable : defaultLogsLocalTable ,
2022-08-04 17:32:45 +05:30
LogsAttributeKeysTable : defaultLogAttributeKeysTable ,
LogsResourceKeysTable : defaultLogResourceKeysTable ,
2023-04-06 13:32:24 +05:30
LogsTagAttributeTable : defaultLogTagAttributeTable ,
2022-08-04 17:32:45 +05:30
LiveTailRefreshSeconds : defaultLiveTailRefreshSeconds ,
2022-08-04 11:57:05 +05:30
WriteBatchDelay : defaultWriteBatchDelay ,
WriteBatchSize : defaultWriteBatchSize ,
Encoding : defaultEncoding ,
Connector : defaultConnector ,
2021-05-27 12:52:34 +05:30
} ,
others : make ( map [ string ] * namespaceConfig , len ( otherNamespaces ) ) ,
}
for _ , namespace := range otherNamespaces {
if namespace == archiveNamespace {
options . others [ namespace ] = & namespaceConfig {
2022-07-12 16:38:26 +05:30
namespace : namespace ,
Datasource : datasource ,
TraceDB : "" ,
OperationsTable : "" ,
IndexTable : "" ,
ErrorTable : "" ,
LogsDB : "" ,
LogsTable : "" ,
2022-12-02 12:30:28 +05:30
LogsLocalTable : "" ,
2022-07-12 16:38:26 +05:30
LogsAttributeKeysTable : "" ,
LogsResourceKeysTable : "" ,
2022-07-25 14:42:58 +05:30
LiveTailRefreshSeconds : defaultLiveTailRefreshSeconds ,
2022-07-12 16:38:26 +05:30
WriteBatchDelay : defaultWriteBatchDelay ,
WriteBatchSize : defaultWriteBatchSize ,
Encoding : defaultEncoding ,
Connector : defaultConnector ,
2021-05-27 12:52:34 +05:30
}
} else {
options . others [ namespace ] = & namespaceConfig { namespace : namespace }
}
}
return options
}
// GetPrimary returns the primary namespace configuration
func ( opt * Options ) getPrimary ( ) * namespaceConfig {
return opt . primary
}