Skip to content

Commit 5967fb2

Browse files
lim123123123atoulme
authored andcommitted
[exporter/clickhouse] Fix database creation (open-telemetry#38829)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description In the current version of the exporter, the createDatabase function uses cfg.buildDSN, which appends the database from the config into the dsn that is passed into the clickhouse driver. As a result, the exporter tries to connect to the database before it is actually created, causing a ClickHouse exception in the start function This pr couldn't be merged until [the integration test fix](open-telemetry#38798) <!--Describe what testing was performed and which tests were added.--> #### Testing I've added an integration test that checks whether the database was successfully created. It fails now but works with the fix. <!--Please delete paragraphs that you did not use before submitting.--> --------- Co-authored-by: Antoine Toulme <[email protected]>
1 parent 78dc532 commit 5967fb2

File tree

6 files changed

+100
-24
lines changed

6 files changed

+100
-24
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: bug_fix
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: clickhouseexporter
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: clickhouseexporter doesn't set the database name in the dsn of the query that creates the database
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [38829]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: []

exporter/clickhouseexporter/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ include ../../Makefile.Common
22

33
local-run-example:
44
cd ../../cmd/otelcontribcol && GOOS=linux go build -o ../../local/otelcontribcol
5-
cd example && docker-compose up -d
5+
cd example && docker compose up -d
66
recreate-otel-collector:
77
cd ../../ && make otelcontribcol
8-
cd example && docker-compose up --build otelcollector
8+
cd example && docker compose up --build otelcollector

exporter/clickhouseexporter/example/docker-compose.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ services:
2424
- "24224:24224" # fluentforwarder
2525
- "24224:24224/udp" # fluentforwarder
2626
depends_on:
27-
- clickhouse
27+
clickhouse:
28+
condition: service_healthy
2829
networks:
2930
- otel-clickhouse
3031

@@ -33,8 +34,13 @@ services:
3334
ports:
3435
- "9000:9000"
3536
- "8123:8123"
37+
healthcheck:
38+
test: wget --no-verbose --tries=1 --spider http://localhost:8123/ping || exit 1
3639
networks:
3740
- otel-clickhouse
41+
environment:
42+
- CLICKHOUSE_USER=default
43+
- CLICKHOUSE_PASSWORD=default
3844

3945
grafana:
4046
image: grafana/grafana:latest

exporter/clickhouseexporter/example/otel-collector-config.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ exporters:
3737
initial_interval: 5s
3838
max_interval: 30s
3939
max_elapsed_time: 300s
40+
username: default
41+
password: default
4042
extensions:
4143
health_check:
4244
pprof:

exporter/clickhouseexporter/exporter_logs.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,15 @@ func createDatabase(ctx context.Context, cfg *Config) error {
212212
return nil
213213
}
214214

215+
// We couldn't set a database in the dsn while creating the database,
216+
// otherwise, there would be an exception from clickhouse
217+
targetDatabase := cfg.Database
218+
cfg.Database = defaultDatabase
219+
215220
db, err := cfg.buildDB()
221+
cfg.Database = targetDatabase
216222
if err != nil {
217-
return err
223+
return fmt.Errorf("can't connect to clickhouse: %w", err)
218224
}
219225
defer func() {
220226
_ = db.Close()

exporter/clickhouseexporter/integration_test.go

Lines changed: 55 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,38 +41,55 @@ func TestIntegration(t *testing.T) {
4141
}
4242

4343
for _, c := range testCase {
44-
t.Run(c.name, func(t *testing.T) {
45-
port := randPort()
46-
req := testcontainers.ContainerRequest{
47-
Image: c.image,
48-
ExposedPorts: []string{fmt.Sprintf("%s:9000", port)},
49-
WaitingFor: wait.ForListeningPort("9000").
50-
WithStartupTimeout(2 * time.Minute),
51-
}
52-
c := getContainer(t, req)
53-
defer func() {
54-
err := c.Terminate(context.Background())
55-
require.NoError(t, err)
56-
}()
57-
58-
host, err := c.Host(context.Background())
59-
require.NoError(t, err)
60-
endpoint := fmt.Sprintf("tcp://%s:%s", host, port)
61-
44+
// Container creation is slow, so create it once per test
45+
endpoint := createTestClickhouseContainer(t, c.image)
46+
t.Run(c.name+" exporting", func(t *testing.T) {
6247
logExporter := newTestLogsExporter(t, endpoint)
6348
verifyExportLog(t, logExporter)
6449

6550
traceExporter := newTestTracesExporter(t, endpoint)
66-
require.NoError(t, err)
6751
verifyExporterTrace(t, traceExporter)
6852

6953
metricExporter := newTestMetricsExporter(t, endpoint)
70-
require.NoError(t, err)
7154
verifyExporterMetric(t, metricExporter)
7255
})
56+
t.Run(c.name+" database creation", func(t *testing.T) {
57+
// Verify that start function returns no error
58+
logExporter := newTestLogsExporter(t, endpoint, func(c *Config) { c.Database = "otel" })
59+
// Verify that the table was created
60+
verifyLogTable(t, logExporter)
61+
62+
traceExporter := newTestTracesExporter(t, endpoint, func(c *Config) { c.Database = "otel" })
63+
verifyTraceTable(t, traceExporter)
64+
65+
metricsExporter := newTestMetricsExporter(t, endpoint, func(c *Config) { c.Database = "otel" })
66+
verifyMetricTable(t, metricsExporter)
67+
})
7368
}
7469
}
7570

71+
// returns endpoint
72+
func createTestClickhouseContainer(t *testing.T, image string) string {
73+
port := randPort()
74+
req := testcontainers.ContainerRequest{
75+
Image: image,
76+
ExposedPorts: []string{fmt.Sprintf("%s:9000", port)},
77+
WaitingFor: wait.ForListeningPort("9000").
78+
WithStartupTimeout(2 * time.Minute),
79+
}
80+
c := getContainer(t, req)
81+
t.Cleanup(func() {
82+
err := c.Terminate(context.Background())
83+
require.NoError(t, err)
84+
})
85+
86+
host, err := c.Host(context.Background())
87+
require.NoError(t, err)
88+
endpoint := fmt.Sprintf("tcp://%s:%s", host, port)
89+
90+
return endpoint
91+
}
92+
7693
func getContainer(t *testing.T, req testcontainers.ContainerRequest) testcontainers.Container {
7794
require.NoError(t, req.Validate())
7895
container, err := testcontainers.GenericContainer(
@@ -603,6 +620,24 @@ func verifySummaryMetric(t *testing.T, db *sqlx.DB) {
603620
require.Equal(t, expectSummary, actualSummary)
604621
}
605622

623+
func verifyLogTable(t *testing.T, logExporter *logsExporter) {
624+
db := sqlx.NewDb(logExporter.client, clickhouseDriverName)
625+
_, err := db.Query("select * from otel.otel_logs")
626+
require.NoError(t, err)
627+
}
628+
629+
func verifyTraceTable(t *testing.T, traceExporter *tracesExporter) {
630+
db := sqlx.NewDb(traceExporter.client, clickhouseDriverName)
631+
_, err := db.Query("select * from otel.otel_traces")
632+
require.NoError(t, err)
633+
}
634+
635+
func verifyMetricTable(t *testing.T, metricExporter *metricsExporter) {
636+
db := sqlx.NewDb(metricExporter.client, clickhouseDriverName)
637+
_, err := db.Query("select * from otel.otel_metrics_gauge")
638+
require.NoError(t, err)
639+
}
640+
606641
func randPort() string {
607642
return strconv.Itoa(rand.IntN(999) + 9000)
608643
}

0 commit comments

Comments
 (0)