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 +}