Skip to content

Commit 063ac74

Browse files
committed
Implement spans exporting for ClickHouse storage
Signed-off-by: haanhvu <[email protected]>
1 parent da5e236 commit 063ac74

File tree

10 files changed

+408
-8
lines changed

10 files changed

+408
-8
lines changed

cmd/jaeger/internal/exporters/storageexporter/exporter.go

+18-6
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@ import (
1414
"go.uber.org/zap"
1515

1616
"github.com/jaegertracing/jaeger/cmd/jaeger/internal/extension/jaegerstorage"
17+
ch "github.com/jaegertracing/jaeger/plugin/storage/clickhouse"
1718
"github.com/jaegertracing/jaeger/storage/spanstore"
1819
)
1920

2021
type storageExporter struct {
21-
config *Config
22-
logger *zap.Logger
23-
spanWriter spanstore.Writer
22+
config *Config
23+
logger *zap.Logger
24+
spanWriter spanstore.Writer
25+
exportTraces func(ctx context.Context, td ptrace.Traces) error
26+
requireBatchInsert bool
2427
}
2528

2629
func newExporter(config *Config, otel component.TelemetrySettings) *storageExporter {
@@ -30,14 +33,23 @@ func newExporter(config *Config, otel component.TelemetrySettings) *storageExpor
3033
}
3134
}
3235

33-
func (exp *storageExporter) start(_ context.Context, host component.Host) error {
36+
func (exp *storageExporter) start(ctx context.Context, host component.Host) error {
3437
f, err := jaegerstorage.GetStorageFactory(exp.config.TraceStorage, host)
3538
if err != nil {
3639
return fmt.Errorf("cannot find storage factory: %w", err)
3740
}
3841

39-
if exp.spanWriter, err = f.CreateSpanWriter(); err != nil {
40-
return fmt.Errorf("cannot create span writer: %w", err)
42+
switch t := f.(type) {
43+
case *ch.Factory:
44+
t.CreateSpansTable(ctx)
45+
exp.requireBatchInsert = true
46+
exp.exportTraces = t.ExportSpans
47+
default:
48+
if exp.spanWriter, err = f.CreateSpanWriter(); err != nil {
49+
return fmt.Errorf("cannot create span writer: %w", err)
50+
}
51+
exp.requireBatchInsert = false
52+
exp.exportTraces = exp.pushTraces
4153
}
4254

4355
return nil

cmd/jaeger/internal/exporters/storageexporter/factory.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,21 @@ func createDefaultConfig() component.Config {
3434
func createTracesExporter(ctx context.Context, set exporter.CreateSettings, config component.Config) (exporter.Traces, error) {
3535
cfg := config.(*Config)
3636
ex := newExporter(cfg, set.TelemetrySettings)
37+
38+
if ex.requireBatchInsert == true {
39+
return exporterhelper.NewTracesExporter(ctx, set, cfg,
40+
ex.exportTraces,
41+
exporterhelper.WithCapabilities(consumer.Capabilities{MutatesData: false}),
42+
exporterhelper.WithTimeout(exporterhelper.TimeoutSettings{Timeout: 0}),
43+
exporterhelper.WithRetry(exporterhelper.RetrySettings{Enabled: false}),
44+
//Enable queue settings for batch inserts
45+
exporterhelper.WithQueue(exporterhelper.QueueSettings{Enabled: true}),
46+
exporterhelper.WithStart(ex.start),
47+
exporterhelper.WithShutdown(ex.close),
48+
)
49+
}
3750
return exporterhelper.NewTracesExporter(ctx, set, cfg,
38-
ex.pushTraces,
51+
ex.exportTraces,
3952
exporterhelper.WithCapabilities(consumer.Capabilities{MutatesData: false}),
4053
// Disable Timeout/RetryOnFailure and SendingQueue
4154
exporterhelper.WithTimeout(exporterhelper.TimeoutSettings{Timeout: 0}),

cmd/jaeger/internal/extension/jaegerstorage/config.go

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package jaegerstorage
55

66
import (
77
memoryCfg "github.com/jaegertracing/jaeger/pkg/memory/config"
8+
ch "github.com/jaegertracing/jaeger/plugin/storage/clickhouse"
89
)
910

1011
// Config has the configuration for jaeger-query,
@@ -13,9 +14,16 @@ type Config struct {
1314
// TODO add other storage types here
1415
// TODO how will this work with 3rd party storage implementations?
1516
// Option: instead of looking for specific name, check interface.
17+
18+
ClickHouse map[string]ch.Config `mapstructure:"clickhouse"`
1619
}
1720

1821
type MemoryStorage struct {
1922
Name string `mapstructure:"name"`
2023
memoryCfg.Configuration
2124
}
25+
26+
type ClickHouseStorage struct {
27+
Name string `mapstructure:"name"`
28+
ch.Config
29+
}

cmd/jaeger/internal/extension/jaegerstorage/extension.go

+9
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"go.uber.org/zap"
1515

1616
"github.com/jaegertracing/jaeger/pkg/metrics"
17+
"github.com/jaegertracing/jaeger/plugin/storage/clickhouse"
1718
"github.com/jaegertracing/jaeger/plugin/storage/memory"
1819
"github.com/jaegertracing/jaeger/storage"
1920
)
@@ -71,6 +72,14 @@ func (s *storageExt) Start(ctx context.Context, host component.Host) error {
7172
)
7273
}
7374
// TODO add support for other backends
75+
76+
for name, chCfg := range s.config.ClickHouse {
77+
if _, ok := s.factories[name]; ok {
78+
return fmt.Errorf("duplicate clickhouse storage name %s", name)
79+
}
80+
s.factories[name] = clickhouse.NewFactory(ctx, chCfg, s.logger.With(zap.String("storage_name", name)))
81+
}
82+
7483
return nil
7584
}
7685

go.mod

+9-1
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,11 @@ require (
9595

9696
require (
9797
contrib.go.opencensus.io/exporter/prometheus v0.4.2 // indirect
98+
github.com/ClickHouse/ch-go v0.58.2 // indirect
99+
github.com/ClickHouse/clickhouse-go/v2 v2.15.0 // indirect
98100
github.com/IBM/sarama v1.41.2 // indirect
99101
github.com/VividCortex/gohistogram v1.0.0 // indirect
102+
github.com/andybalholm/brotli v1.0.6 // indirect
100103
github.com/aws/aws-sdk-go v1.45.26 // indirect
101104
github.com/beorn7/perks v1.0.1 // indirect
102105
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
@@ -105,12 +108,14 @@ require (
105108
github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect
106109
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
107110
github.com/dgraph-io/ristretto v0.1.1 // indirect
108-
github.com/dustin/go-humanize v1.0.0 // indirect
111+
github.com/dustin/go-humanize v1.0.1 // indirect
109112
github.com/eapache/go-resiliency v1.4.0 // indirect
110113
github.com/eapache/go-xerial-snappy v0.0.0-20230731223053-c322873962e3 // indirect
111114
github.com/eapache/queue v1.1.0 // indirect
112115
github.com/fatih/color v1.14.1 // indirect
113116
github.com/felixge/httpsnoop v1.0.3 // indirect
117+
github.com/go-faster/city v1.0.1 // indirect
118+
github.com/go-faster/errors v0.6.1 // indirect
114119
github.com/go-kit/log v0.2.1 // indirect
115120
github.com/go-logfmt/logfmt v0.5.1 // indirect
116121
github.com/go-logr/logr v1.3.0 // indirect
@@ -169,6 +174,7 @@ require (
169174
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/zipkin v0.88.0 // indirect
170175
github.com/opentracing/opentracing-go v1.2.0 // indirect
171176
github.com/openzipkin/zipkin-go v0.4.2 // indirect
177+
github.com/paulmach/orb v0.10.0 // indirect
172178
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
173179
github.com/pierrec/lz4 v2.6.1+incompatible // indirect
174180
github.com/pierrec/lz4/v4 v4.1.18 // indirect
@@ -182,8 +188,10 @@ require (
182188
github.com/russross/blackfriday/v2 v2.1.0 // indirect
183189
github.com/sagikazarmark/locafero v0.3.0 // indirect
184190
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
191+
github.com/segmentio/asm v1.2.0 // indirect
185192
github.com/shirou/gopsutil/v3 v3.23.10 // indirect
186193
github.com/shoenig/go-m1cpu v0.1.6 // indirect
194+
github.com/shopspring/decimal v1.3.1 // indirect
187195
github.com/sourcegraph/conc v0.3.0 // indirect
188196
github.com/spf13/afero v1.10.0 // indirect
189197
github.com/spf13/cast v1.5.1 // indirect

go.sum

+23
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ contrib.go.opencensus.io/exporter/prometheus v0.4.2/go.mod h1:dvEHbiKmgvbr5pjaF9
4343
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
4444
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
4545
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
46+
github.com/ClickHouse/ch-go v0.58.2 h1:jSm2szHbT9MCAB1rJ3WuCJqmGLi5UTjlNu+f530UTS0=
47+
github.com/ClickHouse/ch-go v0.58.2/go.mod h1:Ap/0bEmiLa14gYjCiRkYGbXvbe8vwdrfTYWhsuQ99aw=
48+
github.com/ClickHouse/clickhouse-go v1.5.4 h1:cKjXeYLNWVJIx2J1K6H2CqyRmfwVJVY1OV1coaaFcI0=
49+
github.com/ClickHouse/clickhouse-go v1.5.4/go.mod h1:EaI/sW7Azgz9UATzd5ZdZHRUhHgv5+JMS9NSr2smCJI=
50+
github.com/ClickHouse/clickhouse-go/v2 v2.15.0 h1:G0hTKyO8fXXR1bGnZ0DY3vTG01xYfOGW76zgjg5tmC4=
51+
github.com/ClickHouse/clickhouse-go/v2 v2.15.0/go.mod h1:kXt1SRq0PIRa6aKZD7TnFnY9PQKmc2b13sHtOYcK6cQ=
4652
github.com/HdrHistogram/hdrhistogram-go v1.1.2 h1:5IcZpTvzydCQeHzK4Ef/D5rrSqwxob0t8PQPMybUNFM=
4753
github.com/HdrHistogram/hdrhistogram-go v1.1.2/go.mod h1:yDgFjdqOqDEKOvasDdhWNXYg9BVp4O+o5f6V/ehm6Oo=
4854
github.com/IBM/sarama v1.41.2 h1:ZDBZfGPHAD4uuAtSv4U22fRZBgst0eEwGFzLj0fb85c=
@@ -64,6 +70,8 @@ github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRF
6470
github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
6571
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho=
6672
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE=
73+
github.com/andybalholm/brotli v1.0.6 h1:Yf9fFpf49Zrxb9NlQaluyE92/+X7UVHlhMNJN2sxfOI=
74+
github.com/andybalholm/brotli v1.0.6/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
6775
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
6876
github.com/apache/thrift v0.19.0 h1:sOqkWPzMj7w6XaYbJQG7m4sGqVolaW/0D28Ln7yPzMk=
6977
github.com/apache/thrift v0.19.0/go.mod h1:SUALL216IiaOw2Oy+5Vs9lboJ/t9g40C+G07Dc0QC1I=
@@ -124,6 +132,8 @@ github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2 h1:tdlZCpZ/P9DhczC
124132
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=
125133
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
126134
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
135+
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
136+
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
127137
github.com/eapache/go-resiliency v1.2.0/go.mod h1:kFI+JgMyC7bLPUVY133qvEBtVayf5mFgVsvEsIPBvNs=
128138
github.com/eapache/go-resiliency v1.4.0 h1:3OK9bWpPk5q6pbFAaYSEwD9CLUSHG8bnZuqX2yMt3B0=
129139
github.com/eapache/go-resiliency v1.4.0/go.mod h1:5yPzW0MIvSe0JDsv0v+DvcjEv2FyD6iZYSs1ZI+iQho=
@@ -155,6 +165,10 @@ github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4
155165
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
156166
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
157167
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
168+
github.com/go-faster/city v1.0.1 h1:4WAxSZ3V2Ws4QRDrscLEDcibJY8uf41H6AhXDrNDcGw=
169+
github.com/go-faster/city v1.0.1/go.mod h1:jKcUJId49qdW3L1qKHH/3wPeUstCVpVSXTM6vO3VcTw=
170+
github.com/go-faster/errors v0.6.1 h1:nNIPOBkprlKzkThvS/0YaX8Zs9KewLCOSFQS5BU06FI=
171+
github.com/go-faster/errors v0.6.1/go.mod h1:5MGV2/2T9yvlrbhe9pD9LO5Z/2zCSq2T8j+Jpi2LAyY=
158172
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
159173
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
160174
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
@@ -527,6 +541,9 @@ github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+
527541
github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc=
528542
github.com/openzipkin/zipkin-go v0.4.2 h1:zjqfqHjUpPmB3c1GlCvvgsM1G4LkvqQbBDueDOCg/jA=
529543
github.com/openzipkin/zipkin-go v0.4.2/go.mod h1:ZeVkFjuuBiSy13y8vpSDCjMi9GoI3hPpCJSBx/EYFhY=
544+
github.com/paulmach/orb v0.10.0 h1:guVYVqzxHE/CQ1KpfGO077TR0ATHSNjp4s6XGLn3W9s=
545+
github.com/paulmach/orb v0.10.0/go.mod h1:5mULz1xQfs3bmQm63QEJA6lNGujuRafwA5S/EnuLaLU=
546+
github.com/paulmach/protoscan v0.2.1/go.mod h1:SpcSwydNLrxUGSDvXvO0P7g7AuhJ7lcKfDlhJCDw2gY=
530547
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
531548
github.com/pelletier/go-toml v1.7.0/go.mod h1:vwGMzjaWMwyfHwgIBhI2YUM4fB6nL6lVAvS1LBMMhTE=
532549
github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4=
@@ -600,12 +617,16 @@ github.com/sagikazarmark/locafero v0.3.0 h1:zT7VEGWC2DTflmccN/5T1etyKvxSxpHsjb9c
600617
github.com/sagikazarmark/locafero v0.3.0/go.mod h1:w+v7UsPNFwzF1cHuOajOOzoq4U7v/ig1mpRjqV+Bu1U=
601618
github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=
602619
github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ=
620+
github.com/segmentio/asm v1.2.0 h1:9BQrFxC+YOHJlTlHGkTrFWf59nbL3XnCoFLTwDCI7ys=
621+
github.com/segmentio/asm v1.2.0/go.mod h1:BqMnlJP91P8d+4ibuonYZw9mfnzI9HfxselHZr5aAcs=
603622
github.com/shirou/gopsutil/v3 v3.23.10 h1:/N42opWlYzegYaVkWejXWJpbzKv2JDy3mrgGzKsh9hM=
604623
github.com/shirou/gopsutil/v3 v3.23.10/go.mod h1:JIE26kpucQi+innVlAUnIEOSBhBUkirr5b44yr55+WE=
605624
github.com/shoenig/go-m1cpu v0.1.6 h1:nxdKQNcEB6vzgA2E2bvzKIYRuNj7XNJ4S/aRSwKzFtM=
606625
github.com/shoenig/go-m1cpu v0.1.6/go.mod h1:1JJMcUBvfNwpq05QDQVAnx3gUHr9IYF7GNg9SUEw2VQ=
607626
github.com/shoenig/test v0.6.4 h1:kVTaSd7WLz5WZ2IaoM0RSzRsUD+m8wRR+5qvntpn4LU=
608627
github.com/shoenig/test v0.6.4/go.mod h1:byHiCGXqrVaflBLAMq/srcZIHynQPQgeyvkvXnjqq0k=
628+
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
629+
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
609630
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
610631
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
611632
github.com/sirupsen/logrus v1.4.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
@@ -693,6 +714,7 @@ github.com/yusufpapurcu/wmi v1.2.3/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQ
693714
go.mongodb.org/mongo-driver v1.7.3/go.mod h1:NqaYOwnXWr5Pm7AOpO5QFxKJ503nbMse/R79oO62zWg=
694715
go.mongodb.org/mongo-driver v1.7.5/go.mod h1:VXEWRZ6URJIkUq2SCAyapmhH0ZLRBP+FT4xhp5Zvxng=
695716
go.mongodb.org/mongo-driver v1.10.0/go.mod h1:wsihk0Kdgv8Kqu1Anit4sfK+22vSFbUrAVEYRhCXrA8=
717+
go.mongodb.org/mongo-driver v1.11.4/go.mod h1:PTSz5yu21bkT/wXpkS7WR5f0ddqw5quethTUn9WM+2g=
696718
go.mongodb.org/mongo-driver v1.11.6 h1:XM7G6PjiGAO5betLF13BIa5TlLUUE3uJ/2Ox3Lz1K+o=
697719
go.mongodb.org/mongo-driver v1.11.6/go.mod h1:G9TgswdsWjX4tmDA5zfs2+6AEPpYJwqblyjsfuh8oXY=
698720
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
@@ -1224,6 +1246,7 @@ google.golang.org/protobuf v1.24.0/go.mod h1:r/3tXBNzIEhYS9I1OUVjXDlt8tc493IdKGj
12241246
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
12251247
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
12261248
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
1249+
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
12271250
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
12281251
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
12291252
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=

plugin/storage/clickhouse/config.go

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Copyright (c) 2023 The Jaeger Authors.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package clickhouse
5+
6+
import (
7+
"context"
8+
"database/sql"
9+
"errors"
10+
"fmt"
11+
"net/url"
12+
13+
"github.com/ClickHouse/clickhouse-go/v2"
14+
)
15+
16+
type Config struct {
17+
Endpoint string `mapstructure:"endpoint"`
18+
Username string `mapstructure:"username"`
19+
Password string `mapstructure:"password"`
20+
Database string `mapstructure:"database"`
21+
SpansTableName string `mapstructure:"spans_table_name"`
22+
//materialized views' names?
23+
}
24+
25+
const (
26+
defaultDatabase = "default"
27+
defaultUsername = "default"
28+
defaultPassword = ""
29+
)
30+
31+
func (cfg *Config) NewClient(ctx context.Context) (*sql.DB, error) {
32+
if cfg.Endpoint == "" {
33+
return nil, errors.New("no endpoints specified")
34+
}
35+
36+
dsnURL, err := url.Parse(cfg.Endpoint)
37+
if err != nil {
38+
return nil, err
39+
}
40+
41+
queryParams := dsnURL.Query()
42+
43+
if dsnURL.Scheme == "https" {
44+
queryParams.Set("secure", "true")
45+
}
46+
47+
if cfg.Database != "" {
48+
dsnURL.Path = cfg.Database
49+
} else {
50+
dsnURL.Path = defaultDatabase
51+
}
52+
53+
if cfg.Username != "" {
54+
cfg.Username = defaultUsername
55+
}
56+
57+
if cfg.Password != "" {
58+
cfg.Password = defaultPassword
59+
}
60+
61+
dsnURL.User = url.UserPassword(cfg.Username, cfg.Password)
62+
63+
dsnURL.RawQuery = queryParams.Encode()
64+
65+
dsn := dsnURL.String()
66+
67+
if _, err = clickhouse.ParseDSN(dsn); err != nil {
68+
return nil, err
69+
}
70+
71+
db, err := sql.Open("clickhouse", dsn)
72+
if err != nil {
73+
return nil, err
74+
}
75+
76+
createDBquery := fmt.Sprintf("CREATE DATABASE IF NOT EXISTS %s", cfg.Database)
77+
_, err = db.ExecContext(ctx, createDBquery)
78+
if err != nil {
79+
return nil, err
80+
}
81+
82+
return db, nil
83+
}

0 commit comments

Comments
 (0)