Skip to content

Commit

Permalink
use di for safer testing
Browse files Browse the repository at this point in the history
  • Loading branch information
SpencerTorres committed Nov 27, 2024
1 parent 5cd818a commit 963b388
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 17 deletions.
4 changes: 3 additions & 1 deletion exporter/clickhouseexporter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions exporter/clickhouseexporter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
39 changes: 23 additions & 16 deletions exporter/clickhouseexporter/version_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
18 changes: 18 additions & 0 deletions exporter/clickhouseexporter/version_info_test.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 963b388

Please sign in to comment.