From 7924e1ee74d662e52824ab3fb8fe177c926b98f3 Mon Sep 17 00:00:00 2001 From: Spencer Torres Date: Wed, 28 Aug 2024 22:11:01 -0400 Subject: [PATCH 01/21] add client info to sql driver --- exporter/clickhouseexporter/config.go | 14 +++++--- exporter/clickhouseexporter/version_info.go | 39 +++++++++++++++++++++ 2 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 exporter/clickhouseexporter/version_info.go diff --git a/exporter/clickhouseexporter/config.go b/exporter/clickhouseexporter/config.go index 05ddf2e09903..daae0e4f870c 100644 --- a/exporter/clickhouseexporter/config.go +++ b/exporter/clickhouseexporter/config.go @@ -168,14 +168,20 @@ func (cfg *Config) buildDB() (*sql.DB, error) { return nil, err } - // ClickHouse sql driver will read clickhouse settings from the DSN string. - // It also ensures defaults. - // See https://github.com/ClickHouse/clickhouse-go/blob/08b27884b899f587eb5c509769cd2bdf74a9e2a1/clickhouse_std.go#L189 - conn, err := sql.Open(driverName, dsn) + opts, err := clickhouse.ParseDSN(dsn) if err != nil { return nil, err } + if opts.ClientInfo.Products == nil { + opts.ClientInfo.Products = []struct { + Name string + Version string + }{{"otel-exporter", getCollectorVersion()}} + } + + conn := clickhouse.OpenDB(opts) + return conn, nil } diff --git a/exporter/clickhouseexporter/version_info.go b/exporter/clickhouseexporter/version_info.go new file mode 100644 index 000000000000..1266252b055b --- /dev/null +++ b/exporter/clickhouseexporter/version_info.go @@ -0,0 +1,39 @@ +package clickhouseexporter + +import ( + "runtime" + "runtime/debug" + "strings" + "sync" +) + +var ( + once sync.Once + cachedVersion string +) + +func getCollectorVersion() string { + once.Do(func() { + osInformation := runtime.GOOS[:3] + "-" + runtime.GOARCH + unknownVersion := "otelc-unknown-" + osInformation + + info, ok := debug.ReadBuildInfo() + if !ok { + cachedVersion = unknownVersion + return + } + + for _, mod := range info.Deps { + if mod.Path == "go.opentelemetry.io/collector" { + // Extract the semantic version without metadata. + semVer := strings.SplitN(mod.Version, "-", 2)[0] + cachedVersion = "otelc-" + semVer + "-" + osInformation + return + } + } + + cachedVersion = unknownVersion + }) + + return cachedVersion +} From 85d2a5ef3de6600350412079a605aef113891112 Mon Sep 17 00:00:00 2001 From: Spencer Torres Date: Wed, 28 Aug 2024 22:13:37 -0400 Subject: [PATCH 02/21] changelog --- .chloggen/clickhouse-add-client-info.yaml | 28 +++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 .chloggen/clickhouse-add-client-info.yaml diff --git a/.chloggen/clickhouse-add-client-info.yaml b/.chloggen/clickhouse-add-client-info.yaml new file mode 100644 index 000000000000..078ef4fcd353 --- /dev/null +++ b/.chloggen/clickhouse-add-client-info.yaml @@ -0,0 +1,28 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: enhancement + +# 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: Add client info to queries + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [] + +# (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: This change adds client product info to the system.query_log for more insight on where queries originate + + +# If your change doesn't affect end users or the exported elements of any package, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [] From 85f7d95cf49b9fc93eb07137f729ecf3661986a4 Mon Sep 17 00:00:00 2001 From: Spencer Torres Date: Wed, 28 Aug 2024 22:19:46 -0400 Subject: [PATCH 03/21] version string --- exporter/clickhouseexporter/version_info.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/exporter/clickhouseexporter/version_info.go b/exporter/clickhouseexporter/version_info.go index 1266252b055b..532e305313f2 100644 --- a/exporter/clickhouseexporter/version_info.go +++ b/exporter/clickhouseexporter/version_info.go @@ -15,7 +15,7 @@ var ( func getCollectorVersion() string { once.Do(func() { osInformation := runtime.GOOS[:3] + "-" + runtime.GOARCH - unknownVersion := "otelc-unknown-" + osInformation + unknownVersion := "unknown-" + osInformation info, ok := debug.ReadBuildInfo() if !ok { @@ -27,7 +27,7 @@ func getCollectorVersion() string { if mod.Path == "go.opentelemetry.io/collector" { // Extract the semantic version without metadata. semVer := strings.SplitN(mod.Version, "-", 2)[0] - cachedVersion = "otelc-" + semVer + "-" + osInformation + cachedVersion = semVer + "-" + osInformation return } } From 915f1037991df4a76be1c71191ddaa1cf126c35e Mon Sep 17 00:00:00 2001 From: Spencer Torres Date: Wed, 28 Aug 2024 22:26:06 -0400 Subject: [PATCH 04/21] changelog --- .chloggen/clickhouse-add-client-info.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.chloggen/clickhouse-add-client-info.yaml b/.chloggen/clickhouse-add-client-info.yaml index 078ef4fcd353..ccc37300811f 100644 --- a/.chloggen/clickhouse-add-client-info.yaml +++ b/.chloggen/clickhouse-add-client-info.yaml @@ -10,7 +10,7 @@ component: clickhouseexporter note: Add client info to queries # Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. -issues: [] +issues: [34915] # (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. From b8249cf4203117c04c29801671644f471e72d19a Mon Sep 17 00:00:00 2001 From: Spencer Torres Date: Fri, 30 Aug 2024 15:47:33 -0400 Subject: [PATCH 05/21] update version source --- .../clickhouseexporter/integration_test.go | 11 ++++------ exporter/clickhouseexporter/version_info.go | 21 +++++-------------- 2 files changed, 9 insertions(+), 23 deletions(-) diff --git a/exporter/clickhouseexporter/integration_test.go b/exporter/clickhouseexporter/integration_test.go index 74ecd52dde6b..c44629eb4a37 100644 --- a/exporter/clickhouseexporter/integration_test.go +++ b/exporter/clickhouseexporter/integration_test.go @@ -1,9 +1,6 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 -//go:build integration -// +build integration - package clickhouseexporter import ( @@ -27,10 +24,10 @@ func TestIntegration(t *testing.T) { image string }{ // TODO: Skipping due to https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/32530 - // { - // name: "test clickhouse 24-alpine", - // image: "clickhouse/clickhouse-server:24-alpine", - // }, + { + name: "test clickhouse 24-alpine", + image: "clickhouse/clickhouse-server:24-alpine", + }, // { // name: "test clickhouse 23-alpine", // image: "clickhouse/clickhouse-server:23-alpine", diff --git a/exporter/clickhouseexporter/version_info.go b/exporter/clickhouseexporter/version_info.go index 532e305313f2..42fe3700e433 100644 --- a/exporter/clickhouseexporter/version_info.go +++ b/exporter/clickhouseexporter/version_info.go @@ -3,37 +3,26 @@ package clickhouseexporter import ( "runtime" "runtime/debug" - "strings" "sync" ) var ( - once sync.Once - cachedVersion string + once sync.Once + version string ) func getCollectorVersion() string { once.Do(func() { osInformation := runtime.GOOS[:3] + "-" + runtime.GOARCH - unknownVersion := "unknown-" + osInformation + version = "unknown-" + osInformation info, ok := debug.ReadBuildInfo() if !ok { - cachedVersion = unknownVersion return } - for _, mod := range info.Deps { - if mod.Path == "go.opentelemetry.io/collector" { - // Extract the semantic version without metadata. - semVer := strings.SplitN(mod.Version, "-", 2)[0] - cachedVersion = semVer + "-" + osInformation - return - } - } - - cachedVersion = unknownVersion + version = info.Main.Version + "-" + osInformation }) - return cachedVersion + return version } From 77771f2fb28ef78b0f44b329b678ed830820df9e Mon Sep 17 00:00:00 2001 From: Spencer Torres Date: Fri, 30 Aug 2024 15:49:13 -0400 Subject: [PATCH 06/21] rename client info to "otelcol" --- exporter/clickhouseexporter/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exporter/clickhouseexporter/config.go b/exporter/clickhouseexporter/config.go index daae0e4f870c..cda8e57b3bc5 100644 --- a/exporter/clickhouseexporter/config.go +++ b/exporter/clickhouseexporter/config.go @@ -177,7 +177,7 @@ func (cfg *Config) buildDB() (*sql.DB, error) { opts.ClientInfo.Products = []struct { Name string Version string - }{{"otel-exporter", getCollectorVersion()}} + }{{"otelcol", getCollectorVersion()}} } conn := clickhouse.OpenDB(opts) From b5d33081028d0ea774b62d3e8dc2820b3feb6935 Mon Sep 17 00:00:00 2001 From: Spencer Torres Date: Fri, 30 Aug 2024 15:50:13 -0400 Subject: [PATCH 07/21] remove local integration test changes --- exporter/clickhouseexporter/integration_test.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/exporter/clickhouseexporter/integration_test.go b/exporter/clickhouseexporter/integration_test.go index c44629eb4a37..74ecd52dde6b 100644 --- a/exporter/clickhouseexporter/integration_test.go +++ b/exporter/clickhouseexporter/integration_test.go @@ -1,6 +1,9 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 +//go:build integration +// +build integration + package clickhouseexporter import ( @@ -24,10 +27,10 @@ func TestIntegration(t *testing.T) { image string }{ // TODO: Skipping due to https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/32530 - { - name: "test clickhouse 24-alpine", - image: "clickhouse/clickhouse-server:24-alpine", - }, + // { + // name: "test clickhouse 24-alpine", + // image: "clickhouse/clickhouse-server:24-alpine", + // }, // { // name: "test clickhouse 23-alpine", // image: "clickhouse/clickhouse-server:23-alpine", From 1476d9ed43e7d123db855f5f401b1e0f2b2eb7c3 Mon Sep 17 00:00:00 2001 From: Spencer Torres Date: Tue, 26 Nov 2024 18:50:25 -0500 Subject: [PATCH 08/21] use DSN for setting product info --- exporter/clickhouseexporter/config.go | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/exporter/clickhouseexporter/config.go b/exporter/clickhouseexporter/config.go index cda8e57b3bc5..040e71cde1fd 100644 --- a/exporter/clickhouseexporter/config.go +++ b/exporter/clickhouseexporter/config.go @@ -147,6 +147,15 @@ func (cfg *Config) buildDSN() (string, error) { queryParams.Set("compress", cfg.Compress) } + productInfo := queryParams.Get("client_info_product") + binaryProductInfo := fmt.Sprintf("%s/%s", "otelcol", getCollectorVersion()) + if productInfo == "" { + productInfo = binaryProductInfo + } else { + productInfo = fmt.Sprintf("%s,%s", productInfo, binaryProductInfo) + } + queryParams.Set("client_info_product", productInfo) + // Use database from config if not specified in path, or if config is not default. if dsnURL.Path == "" || cfg.Database != defaultDatabase { dsnURL.Path = cfg.Database @@ -168,20 +177,14 @@ func (cfg *Config) buildDB() (*sql.DB, error) { return nil, err } - opts, err := clickhouse.ParseDSN(dsn) + // ClickHouse sql driver will read clickhouse settings from the DSN string. + // It also ensures defaults. + // See https://github.com/ClickHouse/clickhouse-go/blob/08b27884b899f587eb5c509769cd2bdf74a9e2a1/clickhouse_std.go#L189 + conn, err := sql.Open(driverName, dsn) if err != nil { return nil, err } - if opts.ClientInfo.Products == nil { - opts.ClientInfo.Products = []struct { - Name string - Version string - }{{"otelcol", getCollectorVersion()}} - } - - conn := clickhouse.OpenDB(opts) - return conn, nil } From e41546443647bf4dd974fe5b824df3cb8e5eeb96 Mon Sep 17 00:00:00 2001 From: Spencer Torres Date: Tue, 26 Nov 2024 18:51:46 -0500 Subject: [PATCH 09/21] rename variables to de-clutter package scope --- exporter/clickhouseexporter/version_info.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/exporter/clickhouseexporter/version_info.go b/exporter/clickhouseexporter/version_info.go index 42fe3700e433..d47d20002436 100644 --- a/exporter/clickhouseexporter/version_info.go +++ b/exporter/clickhouseexporter/version_info.go @@ -7,22 +7,22 @@ import ( ) var ( - once sync.Once - version string + versionOnce sync.Once + collectorVersion string ) func getCollectorVersion() string { - once.Do(func() { + versionOnce.Do(func() { osInformation := runtime.GOOS[:3] + "-" + runtime.GOARCH - version = "unknown-" + osInformation + collectorVersion = "unknown-" + osInformation info, ok := debug.ReadBuildInfo() if !ok { return } - version = info.Main.Version + "-" + osInformation + collectorVersion = info.Main.Version + "-" + osInformation }) - return version + return collectorVersion } From 5cd818a99d8ee4a731841cfc50de6d725d4dbc99 Mon Sep 17 00:00:00 2001 From: Spencer Torres Date: Tue, 26 Nov 2024 18:58:51 -0500 Subject: [PATCH 10/21] add note to readme --- exporter/clickhouseexporter/README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/exporter/clickhouseexporter/README.md b/exporter/clickhouseexporter/README.md index aa8d8ea9808d..d38f2a8b4ce5 100644 --- a/exporter/clickhouseexporter/README.md +++ b/exporter/clickhouseexporter/README.md @@ -290,6 +290,11 @@ Connection options: - `compress` (default = lz4): Controls the compression algorithm. Valid options: `none` (disabled), `zstd`, `lz4` (default), `gzip`, `deflate`, `br`, `true` (lz4). Ignored if `compress` is set in the `endpoint` or `connection_params`. - `async_insert` (default = true): Enables [async inserts](https://clickhouse.com/docs/en/optimize/asynchronous-inserts). Ignored if async inserts are configured in the `endpoint` or `connection_params`. Async inserts may still be overridden server-side. +Additional DSN features: + +The underlying `clickhouse-go` module offers additional configuration. These can be set in the exporter's `endpoint` or `connection_params` config values. +- `client_info_product` Must be in `productName/version` format. By default the exporter will append its binary build information. You can use this information to track the origin of `INSERT` statements in the `system.query_log` table. + ClickHouse tables: - `logs_table_name` (default = otel_logs): The table name for logs. From 963b3885ad1a7e3986493b5831615f0ca939c66b Mon Sep 17 00:00:00 2001 From: Spencer Torres Date: Tue, 26 Nov 2024 19:34:37 -0500 Subject: [PATCH 11/21] use di for safer testing --- exporter/clickhouseexporter/config.go | 4 +- exporter/clickhouseexporter/factory.go | 2 + exporter/clickhouseexporter/version_info.go | 39 +++++++++++-------- .../clickhouseexporter/version_info_test.go | 18 +++++++++ 4 files changed, 46 insertions(+), 17 deletions(-) create mode 100644 exporter/clickhouseexporter/version_info_test.go diff --git a/exporter/clickhouseexporter/config.go b/exporter/clickhouseexporter/config.go index 040e71cde1fd..8bdb379678e5 100644 --- a/exporter/clickhouseexporter/config.go +++ b/exporter/clickhouseexporter/config.go @@ -20,6 +20,8 @@ import ( // Config defines configuration for Elastic exporter. type Config struct { + collectorVersionResolver CollectorVersionResolver + TimeoutSettings exporterhelper.TimeoutConfig `mapstructure:",squash"` configretry.BackOffConfig `mapstructure:"retry_on_failure"` QueueSettings exporterhelper.QueueConfig `mapstructure:"sending_queue"` @@ -148,7 +150,7 @@ func (cfg *Config) buildDSN() (string, error) { } productInfo := queryParams.Get("client_info_product") - binaryProductInfo := fmt.Sprintf("%s/%s", "otelcol", getCollectorVersion()) + binaryProductInfo := fmt.Sprintf("%s/%s", "otelcol", cfg.collectorVersionResolver.GetVersion()) if productInfo == "" { productInfo = binaryProductInfo } else { diff --git a/exporter/clickhouseexporter/factory.go b/exporter/clickhouseexporter/factory.go index 4c545d5a9fb8..fac88fdd549e 100644 --- a/exporter/clickhouseexporter/factory.go +++ b/exporter/clickhouseexporter/factory.go @@ -32,6 +32,8 @@ func NewFactory() exporter.Factory { func createDefaultConfig() component.Config { return &Config{ + collectorVersionResolver: NewBinaryCollectorVersionResolver(), + TimeoutSettings: exporterhelper.NewDefaultTimeoutConfig(), QueueSettings: exporterhelper.NewDefaultQueueConfig(), BackOffConfig: configretry.NewDefaultBackOffConfig(), diff --git a/exporter/clickhouseexporter/version_info.go b/exporter/clickhouseexporter/version_info.go index d47d20002436..76c322436da4 100644 --- a/exporter/clickhouseexporter/version_info.go +++ b/exporter/clickhouseexporter/version_info.go @@ -3,26 +3,33 @@ package clickhouseexporter import ( "runtime" "runtime/debug" - "sync" ) -var ( - versionOnce sync.Once - collectorVersion string -) +type CollectorVersionResolver interface { + // GetVersion returns the collector build information for use in query tracking. + // Version should not include any slashes. + GetVersion() string +} + +// BinaryCollectorVersionResolver will use the Go binary to detect the collector version. +type BinaryCollectorVersionResolver struct { + version string +} + +func NewBinaryCollectorVersionResolver() *BinaryCollectorVersionResolver { + resolver := BinaryCollectorVersionResolver{} -func getCollectorVersion() string { - versionOnce.Do(func() { - osInformation := runtime.GOOS[:3] + "-" + runtime.GOARCH - collectorVersion = "unknown-" + osInformation + osInformation := runtime.GOOS[:3] + "-" + runtime.GOARCH + resolver.version = "unknown-" + osInformation - info, ok := debug.ReadBuildInfo() - if !ok { - return - } + info, ok := debug.ReadBuildInfo() + if ok { + resolver.version = info.Main.Version + "-" + osInformation + } - collectorVersion = info.Main.Version + "-" + osInformation - }) + return &resolver +} - return collectorVersion +func (r *BinaryCollectorVersionResolver) GetVersion() string { + return r.version } diff --git a/exporter/clickhouseexporter/version_info_test.go b/exporter/clickhouseexporter/version_info_test.go new file mode 100644 index 000000000000..3097b87ed19e --- /dev/null +++ b/exporter/clickhouseexporter/version_info_test.go @@ -0,0 +1,18 @@ +package clickhouseexporter + +// TestCollectorVersionResolver will return a constant value for the collector version. +type TestCollectorVersionResolver struct { + version string +} + +func NewTestCollectorVersionResolver(version string) *TestCollectorVersionResolver { + return &TestCollectorVersionResolver{version: version} +} + +func NewDefaultTestCollectorVersionResolver() *TestCollectorVersionResolver { + return &TestCollectorVersionResolver{version: "test"} +} + +func (r *TestCollectorVersionResolver) GetVersion() string { + return r.version +} From c7f424d2eca4ad4d6381ea2f1b9022f8c753102d Mon Sep 17 00:00:00 2001 From: Spencer Torres Date: Tue, 26 Nov 2024 19:38:42 -0500 Subject: [PATCH 12/21] add/update tests --- exporter/clickhouseexporter/config_test.go | 78 ++++++++++++++-------- 1 file changed, 49 insertions(+), 29 deletions(-) diff --git a/exporter/clickhouseexporter/config_test.go b/exporter/clickhouseexporter/config_test.go index b3167ed52fc7..ee489022967c 100644 --- a/exporter/clickhouseexporter/config_test.go +++ b/exporter/clickhouseexporter/config_test.go @@ -32,6 +32,7 @@ func TestLoadConfig(t *testing.T) { require.NoError(t, err) defaultCfg := createDefaultConfig() + defaultCfg.(*Config).collectorVersionResolver = NewDefaultTestCollectorVersionResolver() defaultCfg.(*Config).Endpoint = defaultEndpoint storageID := component.MustNewIDWithName("file_storage", "clickhouse") @@ -47,14 +48,15 @@ func TestLoadConfig(t *testing.T) { { id: component.NewIDWithName(metadata.Type, "full"), expected: &Config{ - Endpoint: defaultEndpoint, - Database: "otel", - Username: "foo", - Password: "bar", - TTL: 72 * time.Hour, - LogsTableName: "otel_logs", - TracesTableName: "otel_traces", - CreateSchema: true, + collectorVersionResolver: NewDefaultTestCollectorVersionResolver(), + Endpoint: defaultEndpoint, + Database: "otel", + Username: "foo", + Password: "bar", + TTL: 72 * time.Hour, + LogsTableName: "otel_logs", + TracesTableName: "otel_traces", + CreateSchema: true, TimeoutSettings: exporterhelper.TimeoutConfig{ Timeout: 5 * time.Second, }, @@ -89,6 +91,7 @@ func TestLoadConfig(t *testing.T) { t.Run(tt.id.String(), func(t *testing.T) { factory := NewFactory() cfg := factory.CreateDefaultConfig() + cfg.(*Config).collectorVersionResolver = NewDefaultTestCollectorVersionResolver() sub, err := cm.Sub(tt.id.String()) require.NoError(t, err) @@ -282,7 +285,7 @@ func TestConfig_buildDSN(t *testing.T) { wantChOptions: ChOptions{ Secure: false, }, - want: "clickhouse://127.0.0.1:9000/default?async_insert=true&compress=lz4", + want: "clickhouse://127.0.0.1:9000/default?async_insert=true&client_info_product=otelcol%2Ftest&compress=lz4", }, { name: "Support tcp scheme", @@ -292,7 +295,7 @@ func TestConfig_buildDSN(t *testing.T) { wantChOptions: ChOptions{ Secure: false, }, - want: "tcp://127.0.0.1:9000/default?async_insert=true&compress=lz4", + want: "tcp://127.0.0.1:9000/default?async_insert=true&client_info_product=otelcol%2Ftest&compress=lz4", }, { name: "prefers database name from config over from DSN", @@ -305,7 +308,7 @@ func TestConfig_buildDSN(t *testing.T) { wantChOptions: ChOptions{ Secure: false, }, - want: "clickhouse://foo:bar@127.0.0.1:9000/otel?async_insert=true&compress=lz4", + want: "clickhouse://foo:bar@127.0.0.1:9000/otel?async_insert=true&client_info_product=otelcol%2Ftest&compress=lz4", }, { name: "use database name from DSN if not set in config", @@ -317,7 +320,7 @@ func TestConfig_buildDSN(t *testing.T) { wantChOptions: ChOptions{ Secure: false, }, - want: "clickhouse://foo:bar@127.0.0.1:9000/otel?async_insert=true&compress=lz4", + want: "clickhouse://foo:bar@127.0.0.1:9000/otel?async_insert=true&client_info_product=otelcol%2Ftest&compress=lz4", }, { name: "invalid config", @@ -337,7 +340,7 @@ func TestConfig_buildDSN(t *testing.T) { wantChOptions: ChOptions{ Secure: true, }, - want: "https://127.0.0.1:9000/default?async_insert=true&compress=lz4&secure=true", + want: "https://127.0.0.1:9000/default?async_insert=true&client_info_product=otelcol%2Ftest&compress=lz4&secure=true", }, { name: "Preserve query parameters", @@ -347,7 +350,7 @@ func TestConfig_buildDSN(t *testing.T) { wantChOptions: ChOptions{ Secure: true, }, - want: "clickhouse://127.0.0.1:9000/default?async_insert=true&compress=lz4&foo=bar&secure=true", + want: "clickhouse://127.0.0.1:9000/default?async_insert=true&client_info_product=otelcol%2Ftest&compress=lz4&foo=bar&secure=true", }, { name: "Parse clickhouse settings", @@ -359,7 +362,7 @@ func TestConfig_buildDSN(t *testing.T) { DialTimeout: 30 * time.Second, Compress: clickhouse.CompressionBrotli, }, - want: "https://127.0.0.1:9000/default?async_insert=true&compress=br&dial_timeout=30s&secure=true", + want: "https://127.0.0.1:9000/default?async_insert=true&client_info_product=otelcol%2Ftest&compress=br&dial_timeout=30s&secure=true", }, { name: "Should respect connection parameters", @@ -370,7 +373,7 @@ func TestConfig_buildDSN(t *testing.T) { wantChOptions: ChOptions{ Secure: true, }, - want: "clickhouse://127.0.0.1:9000/default?async_insert=true&compress=lz4&foo=bar&secure=true", + want: "clickhouse://127.0.0.1:9000/default?async_insert=true&client_info_product=otelcol%2Ftest&compress=lz4&foo=bar&secure=true", }, { name: "support replace database in DSN with config to override database", @@ -378,21 +381,21 @@ func TestConfig_buildDSN(t *testing.T) { Endpoint: "tcp://127.0.0.1:9000/otel", Database: "override", }, - want: "tcp://127.0.0.1:9000/override?async_insert=true&compress=lz4", + want: "tcp://127.0.0.1:9000/override?async_insert=true&client_info_product=otelcol%2Ftest&compress=lz4", }, { name: "when config option is missing, preserve async_insert false in DSN", fields: fields{ Endpoint: "tcp://127.0.0.1:9000?async_insert=false", }, - want: "tcp://127.0.0.1:9000/default?async_insert=false&compress=lz4", + want: "tcp://127.0.0.1:9000/default?async_insert=false&client_info_product=otelcol%2Ftest&compress=lz4", }, { name: "when config option is missing, preserve async_insert true in DSN", fields: fields{ Endpoint: "tcp://127.0.0.1:9000?async_insert=true", }, - want: "tcp://127.0.0.1:9000/default?async_insert=true&compress=lz4", + want: "tcp://127.0.0.1:9000/default?async_insert=true&client_info_product=otelcol%2Ftest&compress=lz4", }, { name: "ignore config option when async_insert is present in connection params as false", @@ -402,7 +405,7 @@ func TestConfig_buildDSN(t *testing.T) { AsyncInsert: &configTrue, }, - want: "tcp://127.0.0.1:9000/default?async_insert=false&compress=lz4", + want: "tcp://127.0.0.1:9000/default?async_insert=false&client_info_product=otelcol%2Ftest&compress=lz4", }, { name: "ignore config option when async_insert is present in connection params as true", @@ -412,7 +415,7 @@ func TestConfig_buildDSN(t *testing.T) { AsyncInsert: &configFalse, }, - want: "tcp://127.0.0.1:9000/default?async_insert=true&compress=lz4", + want: "tcp://127.0.0.1:9000/default?async_insert=true&client_info_product=otelcol%2Ftest&compress=lz4", }, { name: "ignore config option when async_insert is present in DSN as false", @@ -421,7 +424,7 @@ func TestConfig_buildDSN(t *testing.T) { AsyncInsert: &configTrue, }, - want: "tcp://127.0.0.1:9000/default?async_insert=false&compress=lz4", + want: "tcp://127.0.0.1:9000/default?async_insert=false&client_info_product=otelcol%2Ftest&compress=lz4", }, { name: "use async_insert true config option when it is not present in DSN", @@ -430,7 +433,7 @@ func TestConfig_buildDSN(t *testing.T) { AsyncInsert: &configTrue, }, - want: "tcp://127.0.0.1:9000/default?async_insert=true&compress=lz4", + want: "tcp://127.0.0.1:9000/default?async_insert=true&client_info_product=otelcol%2Ftest&compress=lz4", }, { name: "use async_insert false config option when it is not present in DSN", @@ -439,7 +442,7 @@ func TestConfig_buildDSN(t *testing.T) { AsyncInsert: &configFalse, }, - want: "tcp://127.0.0.1:9000/default?async_insert=false&compress=lz4", + want: "tcp://127.0.0.1:9000/default?async_insert=false&client_info_product=otelcol%2Ftest&compress=lz4", }, { name: "set async_insert to true when not present in config or DSN", @@ -447,7 +450,7 @@ func TestConfig_buildDSN(t *testing.T) { Endpoint: "tcp://127.0.0.1:9000", }, - want: "tcp://127.0.0.1:9000/default?async_insert=true&compress=lz4", + want: "tcp://127.0.0.1:9000/default?async_insert=true&client_info_product=otelcol%2Ftest&compress=lz4", }, { name: "connection_params takes priority over endpoint and async_insert option.", @@ -457,7 +460,7 @@ func TestConfig_buildDSN(t *testing.T) { AsyncInsert: &configFalse, }, - want: "tcp://127.0.0.1:9000/default?async_insert=true&compress=lz4", + want: "tcp://127.0.0.1:9000/default?async_insert=true&client_info_product=otelcol%2Ftest&compress=lz4", }, { name: "use compress br config option when it is not present in DSN", @@ -466,7 +469,7 @@ func TestConfig_buildDSN(t *testing.T) { Compress: "br", }, - want: "tcp://127.0.0.1:9000/default?async_insert=true&compress=br", + want: "tcp://127.0.0.1:9000/default?async_insert=true&client_info_product=otelcol%2Ftest&compress=br", }, { name: "set compress to lz4 when not present in config or DSN", @@ -474,7 +477,7 @@ func TestConfig_buildDSN(t *testing.T) { Endpoint: "tcp://127.0.0.1:9000", }, - want: "tcp://127.0.0.1:9000/default?async_insert=true&compress=lz4", + want: "tcp://127.0.0.1:9000/default?async_insert=true&client_info_product=otelcol%2Ftest&compress=lz4", }, { name: "connection_params takes priority over endpoint and compress option.", @@ -483,12 +486,29 @@ func TestConfig_buildDSN(t *testing.T) { ConnectionParams: map[string]string{"compress": "br"}, Compress: "lz4", }, - want: "tcp://127.0.0.1:9000/default?async_insert=true&compress=br", + want: "tcp://127.0.0.1:9000/default?async_insert=true&client_info_product=otelcol%2Ftest&compress=br", + }, + { + name: "include default otel product info in DSN", + fields: fields{ + Endpoint: "tcp://127.0.0.1:9000", + }, + + want: "tcp://127.0.0.1:9000/default?async_insert=true&client_info_product=otelcol%2Ftest&compress=lz4", + }, + { + name: "correctly append default product info when value is included in DSN", + fields: fields{ + Endpoint: "tcp://127.0.0.1:9000?client_info_product=customProductInfo%2Fv1.2.3", + }, + + want: "tcp://127.0.0.1:9000/default?async_insert=true&client_info_product=customProductInfo%2Fv1.2.3%2Cotelcol%2Ftest&compress=lz4", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { cfg := createDefaultConfig().(*Config) + cfg.collectorVersionResolver = NewDefaultTestCollectorVersionResolver() mergeConfigWithFields(cfg, tt.fields) dsn, err := cfg.buildDSN() From 1045914eed92de6b86a23feb4753088be41acb7a Mon Sep 17 00:00:00 2001 From: Spencer Torres Date: Tue, 26 Nov 2024 19:44:05 -0500 Subject: [PATCH 13/21] prevent empty build version --- exporter/clickhouseexporter/version_info.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exporter/clickhouseexporter/version_info.go b/exporter/clickhouseexporter/version_info.go index 76c322436da4..9cd1d062ef2d 100644 --- a/exporter/clickhouseexporter/version_info.go +++ b/exporter/clickhouseexporter/version_info.go @@ -23,7 +23,7 @@ func NewBinaryCollectorVersionResolver() *BinaryCollectorVersionResolver { resolver.version = "unknown-" + osInformation info, ok := debug.ReadBuildInfo() - if ok { + if ok && info.Main.Version != "" { resolver.version = info.Main.Version + "-" + osInformation } From ef546ca7fe2c5eade58242a03f936c2d65eafe95 Mon Sep 17 00:00:00 2001 From: Spencer Torres Date: Tue, 26 Nov 2024 19:57:03 -0500 Subject: [PATCH 14/21] mention comma separation for multiple entries in readme --- exporter/clickhouseexporter/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exporter/clickhouseexporter/README.md b/exporter/clickhouseexporter/README.md index d38f2a8b4ce5..ae0e54feae07 100644 --- a/exporter/clickhouseexporter/README.md +++ b/exporter/clickhouseexporter/README.md @@ -293,7 +293,7 @@ Connection options: Additional DSN features: The underlying `clickhouse-go` module offers additional configuration. These can be set in the exporter's `endpoint` or `connection_params` config values. -- `client_info_product` Must be in `productName/version` format. By default the exporter will append its binary build information. You can use this information to track the origin of `INSERT` statements in the `system.query_log` table. +- `client_info_product` Must be in `productName/version` format with comma separated entries. By default the exporter will append its binary build information. You can use this information to track the origin of `INSERT` statements in the `system.query_log` table. ClickHouse tables: From 25a2639ab3b2172976c64e1684efeb913d6afdd4 Mon Sep 17 00:00:00 2001 From: Spencer Torres Date: Tue, 26 Nov 2024 20:33:12 -0500 Subject: [PATCH 15/21] add license headers --- exporter/clickhouseexporter/version_info.go | 3 +++ exporter/clickhouseexporter/version_info_test.go | 3 +++ 2 files changed, 6 insertions(+) diff --git a/exporter/clickhouseexporter/version_info.go b/exporter/clickhouseexporter/version_info.go index 9cd1d062ef2d..112c5e286294 100644 --- a/exporter/clickhouseexporter/version_info.go +++ b/exporter/clickhouseexporter/version_info.go @@ -1,3 +1,6 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + package clickhouseexporter import ( diff --git a/exporter/clickhouseexporter/version_info_test.go b/exporter/clickhouseexporter/version_info_test.go index 3097b87ed19e..19eb32ee5ad3 100644 --- a/exporter/clickhouseexporter/version_info_test.go +++ b/exporter/clickhouseexporter/version_info_test.go @@ -1,3 +1,6 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + package clickhouseexporter // TestCollectorVersionResolver will return a constant value for the collector version. From e7f12f78f74ea28c4e65ea0b5e251196cfbad479 Mon Sep 17 00:00:00 2001 From: Spencer Torres Date: Tue, 26 Nov 2024 20:41:48 -0500 Subject: [PATCH 16/21] fix exposed exporter API --- exporter/clickhouseexporter/config.go | 2 +- exporter/clickhouseexporter/config_test.go | 8 ++++---- exporter/clickhouseexporter/factory.go | 2 +- exporter/clickhouseexporter/version_info.go | 12 ++++++------ exporter/clickhouseexporter/version_info_test.go | 14 +++++++------- 5 files changed, 19 insertions(+), 19 deletions(-) diff --git a/exporter/clickhouseexporter/config.go b/exporter/clickhouseexporter/config.go index 8bdb379678e5..197a2c3cd795 100644 --- a/exporter/clickhouseexporter/config.go +++ b/exporter/clickhouseexporter/config.go @@ -20,7 +20,7 @@ import ( // Config defines configuration for Elastic exporter. type Config struct { - collectorVersionResolver CollectorVersionResolver + collectorVersionResolver collectorVersionResolver TimeoutSettings exporterhelper.TimeoutConfig `mapstructure:",squash"` configretry.BackOffConfig `mapstructure:"retry_on_failure"` diff --git a/exporter/clickhouseexporter/config_test.go b/exporter/clickhouseexporter/config_test.go index ee489022967c..675411bcdefd 100644 --- a/exporter/clickhouseexporter/config_test.go +++ b/exporter/clickhouseexporter/config_test.go @@ -32,7 +32,7 @@ func TestLoadConfig(t *testing.T) { require.NoError(t, err) defaultCfg := createDefaultConfig() - defaultCfg.(*Config).collectorVersionResolver = NewDefaultTestCollectorVersionResolver() + defaultCfg.(*Config).collectorVersionResolver = newDefaultTestCollectorVersionResolver() defaultCfg.(*Config).Endpoint = defaultEndpoint storageID := component.MustNewIDWithName("file_storage", "clickhouse") @@ -48,7 +48,7 @@ func TestLoadConfig(t *testing.T) { { id: component.NewIDWithName(metadata.Type, "full"), expected: &Config{ - collectorVersionResolver: NewDefaultTestCollectorVersionResolver(), + collectorVersionResolver: newDefaultTestCollectorVersionResolver(), Endpoint: defaultEndpoint, Database: "otel", Username: "foo", @@ -91,7 +91,7 @@ func TestLoadConfig(t *testing.T) { t.Run(tt.id.String(), func(t *testing.T) { factory := NewFactory() cfg := factory.CreateDefaultConfig() - cfg.(*Config).collectorVersionResolver = NewDefaultTestCollectorVersionResolver() + cfg.(*Config).collectorVersionResolver = newDefaultTestCollectorVersionResolver() sub, err := cm.Sub(tt.id.String()) require.NoError(t, err) @@ -508,7 +508,7 @@ func TestConfig_buildDSN(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { cfg := createDefaultConfig().(*Config) - cfg.collectorVersionResolver = NewDefaultTestCollectorVersionResolver() + cfg.collectorVersionResolver = newDefaultTestCollectorVersionResolver() mergeConfigWithFields(cfg, tt.fields) dsn, err := cfg.buildDSN() diff --git a/exporter/clickhouseexporter/factory.go b/exporter/clickhouseexporter/factory.go index fac88fdd549e..3be99bf366aa 100644 --- a/exporter/clickhouseexporter/factory.go +++ b/exporter/clickhouseexporter/factory.go @@ -32,7 +32,7 @@ func NewFactory() exporter.Factory { func createDefaultConfig() component.Config { return &Config{ - collectorVersionResolver: NewBinaryCollectorVersionResolver(), + collectorVersionResolver: newBinaryCollectorVersionResolver(), TimeoutSettings: exporterhelper.NewDefaultTimeoutConfig(), QueueSettings: exporterhelper.NewDefaultQueueConfig(), diff --git a/exporter/clickhouseexporter/version_info.go b/exporter/clickhouseexporter/version_info.go index 112c5e286294..c2d00e71f520 100644 --- a/exporter/clickhouseexporter/version_info.go +++ b/exporter/clickhouseexporter/version_info.go @@ -8,19 +8,19 @@ import ( "runtime/debug" ) -type CollectorVersionResolver interface { +type collectorVersionResolver interface { // GetVersion returns the collector build information for use in query tracking. // Version should not include any slashes. GetVersion() string } -// BinaryCollectorVersionResolver will use the Go binary to detect the collector version. -type BinaryCollectorVersionResolver struct { +// binaryCollectorVersionResolver will use the Go binary to detect the collector version. +type binaryCollectorVersionResolver struct { version string } -func NewBinaryCollectorVersionResolver() *BinaryCollectorVersionResolver { - resolver := BinaryCollectorVersionResolver{} +func newBinaryCollectorVersionResolver() *binaryCollectorVersionResolver { + resolver := binaryCollectorVersionResolver{} osInformation := runtime.GOOS[:3] + "-" + runtime.GOARCH resolver.version = "unknown-" + osInformation @@ -33,6 +33,6 @@ func NewBinaryCollectorVersionResolver() *BinaryCollectorVersionResolver { return &resolver } -func (r *BinaryCollectorVersionResolver) GetVersion() string { +func (r *binaryCollectorVersionResolver) GetVersion() string { return r.version } diff --git a/exporter/clickhouseexporter/version_info_test.go b/exporter/clickhouseexporter/version_info_test.go index 19eb32ee5ad3..890b4bde8e93 100644 --- a/exporter/clickhouseexporter/version_info_test.go +++ b/exporter/clickhouseexporter/version_info_test.go @@ -3,19 +3,19 @@ package clickhouseexporter -// TestCollectorVersionResolver will return a constant value for the collector version. -type TestCollectorVersionResolver struct { +// testCollectorVersionResolver will return a constant value for the collector version. +type testCollectorVersionResolver struct { version string } -func NewTestCollectorVersionResolver(version string) *TestCollectorVersionResolver { - return &TestCollectorVersionResolver{version: version} +func newTestCollectorVersionResolver(version string) *testCollectorVersionResolver { + return &testCollectorVersionResolver{version: version} } -func NewDefaultTestCollectorVersionResolver() *TestCollectorVersionResolver { - return &TestCollectorVersionResolver{version: "test"} +func newDefaultTestCollectorVersionResolver() *testCollectorVersionResolver { + return &testCollectorVersionResolver{version: "test"} } -func (r *TestCollectorVersionResolver) GetVersion() string { +func (r *testCollectorVersionResolver) GetVersion() string { return r.version } From e231dcfbdd0e6573380ccddcba62fbfc0aa53336 Mon Sep 17 00:00:00 2001 From: Spencer Torres Date: Tue, 26 Nov 2024 20:42:47 -0500 Subject: [PATCH 17/21] remove unused func --- exporter/clickhouseexporter/version_info_test.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/exporter/clickhouseexporter/version_info_test.go b/exporter/clickhouseexporter/version_info_test.go index 890b4bde8e93..6ac66cb3ce3b 100644 --- a/exporter/clickhouseexporter/version_info_test.go +++ b/exporter/clickhouseexporter/version_info_test.go @@ -8,10 +8,6 @@ type testCollectorVersionResolver struct { version string } -func newTestCollectorVersionResolver(version string) *testCollectorVersionResolver { - return &testCollectorVersionResolver{version: version} -} - func newDefaultTestCollectorVersionResolver() *testCollectorVersionResolver { return &testCollectorVersionResolver{version: "test"} } From 7c7688a3c9aa5b3514d9138ec49fd0b2ba0cbf28 Mon Sep 17 00:00:00 2001 From: Spencer Torres Date: Tue, 26 Nov 2024 20:48:36 -0500 Subject: [PATCH 18/21] re-run `make goporto` --- exporter/clickhouseexporter/version_info.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exporter/clickhouseexporter/version_info.go b/exporter/clickhouseexporter/version_info.go index c2d00e71f520..80086e2c5dd9 100644 --- a/exporter/clickhouseexporter/version_info.go +++ b/exporter/clickhouseexporter/version_info.go @@ -1,7 +1,7 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 -package clickhouseexporter +package clickhouseexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/clickhouseexporter" import ( "runtime" From 3aaa94b560848c7abbfa7841740f480e6ce60e74 Mon Sep 17 00:00:00 2001 From: Spencer Torres Date: Fri, 10 Jan 2025 12:54:50 -0500 Subject: [PATCH 19/21] changelog --- .chloggen/clickhouse-add-client-info.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.chloggen/clickhouse-add-client-info.yaml b/.chloggen/clickhouse-add-client-info.yaml index ccc37300811f..f21c1c2d8ac8 100644 --- a/.chloggen/clickhouse-add-client-info.yaml +++ b/.chloggen/clickhouse-add-client-info.yaml @@ -10,7 +10,7 @@ component: clickhouseexporter note: Add client info to queries # Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. -issues: [34915] +issues: [34915, 37146] # (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. From 0f62008e3f9a680813f0e4b2cea84e121ccdac91 Mon Sep 17 00:00:00 2001 From: Spencer Torres Date: Thu, 30 Jan 2025 16:05:51 -0500 Subject: [PATCH 20/21] use build info from settings instead of binary build info --- exporter/clickhouseexporter/config.go | 9 +++-- exporter/clickhouseexporter/config_test.go | 22 +++++------ exporter/clickhouseexporter/factory.go | 9 +++-- exporter/clickhouseexporter/version_info.go | 38 ------------------- .../clickhouseexporter/version_info_test.go | 17 --------- 5 files changed, 21 insertions(+), 74 deletions(-) delete mode 100644 exporter/clickhouseexporter/version_info.go delete mode 100644 exporter/clickhouseexporter/version_info_test.go diff --git a/exporter/clickhouseexporter/config.go b/exporter/clickhouseexporter/config.go index 197a2c3cd795..8bcb2dd6c3f5 100644 --- a/exporter/clickhouseexporter/config.go +++ b/exporter/clickhouseexporter/config.go @@ -20,7 +20,8 @@ import ( // Config defines configuration for Elastic exporter. type Config struct { - collectorVersionResolver collectorVersionResolver + // collectorVersion is the build version of the collector. This is overridden when an exporter is initialized. + collectorVersion string TimeoutSettings exporterhelper.TimeoutConfig `mapstructure:",squash"` configretry.BackOffConfig `mapstructure:"retry_on_failure"` @@ -150,11 +151,11 @@ func (cfg *Config) buildDSN() (string, error) { } productInfo := queryParams.Get("client_info_product") - binaryProductInfo := fmt.Sprintf("%s/%s", "otelcol", cfg.collectorVersionResolver.GetVersion()) + collectorProductInfo := fmt.Sprintf("%s/%s", "otelcol", cfg.collectorVersion) if productInfo == "" { - productInfo = binaryProductInfo + productInfo = collectorProductInfo } else { - productInfo = fmt.Sprintf("%s,%s", productInfo, binaryProductInfo) + productInfo = fmt.Sprintf("%s,%s", productInfo, collectorProductInfo) } queryParams.Set("client_info_product", productInfo) diff --git a/exporter/clickhouseexporter/config_test.go b/exporter/clickhouseexporter/config_test.go index 675411bcdefd..eace48911f89 100644 --- a/exporter/clickhouseexporter/config_test.go +++ b/exporter/clickhouseexporter/config_test.go @@ -32,7 +32,6 @@ func TestLoadConfig(t *testing.T) { require.NoError(t, err) defaultCfg := createDefaultConfig() - defaultCfg.(*Config).collectorVersionResolver = newDefaultTestCollectorVersionResolver() defaultCfg.(*Config).Endpoint = defaultEndpoint storageID := component.MustNewIDWithName("file_storage", "clickhouse") @@ -48,15 +47,15 @@ func TestLoadConfig(t *testing.T) { { id: component.NewIDWithName(metadata.Type, "full"), expected: &Config{ - collectorVersionResolver: newDefaultTestCollectorVersionResolver(), - Endpoint: defaultEndpoint, - Database: "otel", - Username: "foo", - Password: "bar", - TTL: 72 * time.Hour, - LogsTableName: "otel_logs", - TracesTableName: "otel_traces", - CreateSchema: true, + collectorVersion: "unknown", + Endpoint: defaultEndpoint, + Database: "otel", + Username: "foo", + Password: "bar", + TTL: 72 * time.Hour, + LogsTableName: "otel_logs", + TracesTableName: "otel_traces", + CreateSchema: true, TimeoutSettings: exporterhelper.TimeoutConfig{ Timeout: 5 * time.Second, }, @@ -91,7 +90,6 @@ func TestLoadConfig(t *testing.T) { t.Run(tt.id.String(), func(t *testing.T) { factory := NewFactory() cfg := factory.CreateDefaultConfig() - cfg.(*Config).collectorVersionResolver = newDefaultTestCollectorVersionResolver() sub, err := cm.Sub(tt.id.String()) require.NoError(t, err) @@ -508,7 +506,7 @@ func TestConfig_buildDSN(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { cfg := createDefaultConfig().(*Config) - cfg.collectorVersionResolver = newDefaultTestCollectorVersionResolver() + cfg.collectorVersion = "test" mergeConfigWithFields(cfg, tt.fields) dsn, err := cfg.buildDSN() diff --git a/exporter/clickhouseexporter/factory.go b/exporter/clickhouseexporter/factory.go index 3be99bf366aa..0a366e6bbde8 100644 --- a/exporter/clickhouseexporter/factory.go +++ b/exporter/clickhouseexporter/factory.go @@ -8,11 +8,11 @@ package clickhouseexporter // import "github.com/open-telemetry/opentelemetry-co import ( "context" "fmt" - "time" - "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/config/configretry" "go.opentelemetry.io/collector/exporter" + "time" + "go.opentelemetry.io/collector/exporter/exporterhelper" "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/clickhouseexporter/internal" @@ -32,7 +32,7 @@ func NewFactory() exporter.Factory { func createDefaultConfig() component.Config { return &Config{ - collectorVersionResolver: newBinaryCollectorVersionResolver(), + collectorVersion: "unknown", TimeoutSettings: exporterhelper.NewDefaultTimeoutConfig(), QueueSettings: exporterhelper.NewDefaultQueueConfig(), @@ -62,6 +62,7 @@ func createLogsExporter( cfg component.Config, ) (exporter.Logs, error) { c := cfg.(*Config) + c.collectorVersion = set.BuildInfo.Version exporter, err := newLogsExporter(set.Logger, c) if err != nil { return nil, fmt.Errorf("cannot configure clickhouse logs exporter: %w", err) @@ -88,6 +89,7 @@ func createTracesExporter( cfg component.Config, ) (exporter.Traces, error) { c := cfg.(*Config) + c.collectorVersion = set.BuildInfo.Version exporter, err := newTracesExporter(set.Logger, c) if err != nil { return nil, fmt.Errorf("cannot configure clickhouse traces exporter: %w", err) @@ -112,6 +114,7 @@ func createMetricExporter( cfg component.Config, ) (exporter.Metrics, error) { c := cfg.(*Config) + c.collectorVersion = set.BuildInfo.Version exporter, err := newMetricsExporter(set.Logger, c) if err != nil { return nil, fmt.Errorf("cannot configure clickhouse metrics exporter: %w", err) diff --git a/exporter/clickhouseexporter/version_info.go b/exporter/clickhouseexporter/version_info.go deleted file mode 100644 index 80086e2c5dd9..000000000000 --- a/exporter/clickhouseexporter/version_info.go +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 - -package clickhouseexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/clickhouseexporter" - -import ( - "runtime" - "runtime/debug" -) - -type collectorVersionResolver interface { - // GetVersion returns the collector build information for use in query tracking. - // Version should not include any slashes. - GetVersion() string -} - -// binaryCollectorVersionResolver will use the Go binary to detect the collector version. -type binaryCollectorVersionResolver struct { - version string -} - -func newBinaryCollectorVersionResolver() *binaryCollectorVersionResolver { - resolver := binaryCollectorVersionResolver{} - - osInformation := runtime.GOOS[:3] + "-" + runtime.GOARCH - resolver.version = "unknown-" + osInformation - - info, ok := debug.ReadBuildInfo() - if ok && info.Main.Version != "" { - resolver.version = info.Main.Version + "-" + osInformation - } - - return &resolver -} - -func (r *binaryCollectorVersionResolver) GetVersion() string { - return r.version -} diff --git a/exporter/clickhouseexporter/version_info_test.go b/exporter/clickhouseexporter/version_info_test.go deleted file mode 100644 index 6ac66cb3ce3b..000000000000 --- a/exporter/clickhouseexporter/version_info_test.go +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 - -package clickhouseexporter - -// testCollectorVersionResolver will return a constant value for the collector version. -type testCollectorVersionResolver struct { - version string -} - -func newDefaultTestCollectorVersionResolver() *testCollectorVersionResolver { - return &testCollectorVersionResolver{version: "test"} -} - -func (r *testCollectorVersionResolver) GetVersion() string { - return r.version -} From 54bb010857b675e3d8d74550aa5a02149dab00f0 Mon Sep 17 00:00:00 2001 From: Spencer Torres Date: Thu, 30 Jan 2025 16:16:49 -0500 Subject: [PATCH 21/21] fix import fmt --- exporter/clickhouseexporter/factory.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/exporter/clickhouseexporter/factory.go b/exporter/clickhouseexporter/factory.go index 0a366e6bbde8..778029552534 100644 --- a/exporter/clickhouseexporter/factory.go +++ b/exporter/clickhouseexporter/factory.go @@ -8,11 +8,11 @@ package clickhouseexporter // import "github.com/open-telemetry/opentelemetry-co import ( "context" "fmt" + "time" + "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/config/configretry" "go.opentelemetry.io/collector/exporter" - "time" - "go.opentelemetry.io/collector/exporter/exporterhelper" "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/clickhouseexporter/internal"