From cf4fc97b84e672c035da484f4e712c8f4b189565 Mon Sep 17 00:00:00 2001 From: Murphy Chen Date: Mon, 26 Jun 2023 23:52:09 +0800 Subject: [PATCH] [exporter/clickhouse] Fix create database failure (#23666) resolve: https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/23664 --- .chloggen/fix-create-database-fail.yaml | 20 ++++++++++++++++++++ exporter/clickhouseexporter/config.go | 11 +++++++++-- exporter/clickhouseexporter/config_test.go | 15 +++++++++++++-- 3 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 .chloggen/fix-create-database-fail.yaml diff --git a/.chloggen/fix-create-database-fail.yaml b/.chloggen/fix-create-database-fail.yaml new file mode 100644 index 000000000000..0dc836561d23 --- /dev/null +++ b/.chloggen/fix-create-database-fail.yaml @@ -0,0 +1,20 @@ +# Use this changelog template to create an entry for release notes. +# If your change doesn't affect end users, such as a test fix or a tooling change, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: bug_fix + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: clickhouseexporter + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Fix clickhouse exporter create database fail + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [23664] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: diff --git a/exporter/clickhouseexporter/config.go b/exporter/clickhouseexporter/config.go index b97bc1ced063..9f8978cd8863 100644 --- a/exporter/clickhouseexporter/config.go +++ b/exporter/clickhouseexporter/config.go @@ -103,8 +103,15 @@ func (cfg *Config) buildDSN(database string) (string, error) { // Override database if specified in config. if cfg.Database != "" { dsnURL.Path = cfg.Database - } else if database == "" && cfg.Database == "" && dsnURL.Path == "" { - // Use default database if not specified in any other place. + } + + // Override database if specified in database param. + if database != "" { + dsnURL.Path = database + } + + // Use default database if not specified in any other place. + if database == "" && cfg.Database == "" && dsnURL.Path == "" { dsnURL.Path = defaultDatabase } diff --git a/exporter/clickhouseexporter/config_test.go b/exporter/clickhouseexporter/config_test.go index 7465f48be1c3..8359deb024a3 100644 --- a/exporter/clickhouseexporter/config_test.go +++ b/exporter/clickhouseexporter/config_test.go @@ -147,7 +147,7 @@ func TestConfig_buildDSN(t *testing.T) { Database: "otel", }, args: args{ - database: defaultDatabase, + database: "otel", }, wantChOptions: ChOptions{ Secure: false, @@ -201,7 +201,8 @@ func TestConfig_buildDSN(t *testing.T) { }, args: args{}, want: "clickhouse://127.0.0.1:9000/default?foo=bar&secure=true", - }, { + }, + { name: "Parse clickhouse settings", fields: fields{ Endpoint: "https://127.0.0.1:9000?secure=true&dial_timeout=30s&compress=lz4", @@ -226,6 +227,16 @@ func TestConfig_buildDSN(t *testing.T) { args: args{}, want: "clickhouse://127.0.0.1:9000/default?foo=bar&secure=true", }, + { + name: "support replace database in DSN to default database", + fields: fields{ + Endpoint: "tcp://127.0.0.1:9000/otel", + }, + args: args{ + database: defaultDatabase, + }, + want: "tcp://127.0.0.1:9000/default", + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {