Skip to content

Commit

Permalink
fix: influxdbexporter init panics
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobmarble committed Nov 16, 2023
1 parent 1d886ea commit 419d58c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
12 changes: 9 additions & 3 deletions exporter/influxdbexporter/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,21 @@ func composeWriteURL(config *Config) (string, error) {
queryValues.Set("db", config.V1Compatibility.DB)

if config.V1Compatibility.Username != "" && config.V1Compatibility.Password != "" {
var basicAuth []byte
base64.StdEncoding.Encode(basicAuth, []byte(config.V1Compatibility.Username+":"+string(config.V1Compatibility.Password)))
config.HTTPClientSettings.Headers["Authorization"] = configopaque.String("Basic " + string(basicAuth))
basicAuth := base64.StdEncoding.EncodeToString(
[]byte(config.V1Compatibility.Username + ":" + string(config.V1Compatibility.Password)))
if config.HTTPClientSettings.Headers == nil {
config.HTTPClientSettings.Headers = make(map[string]configopaque.String, 1)
}
config.HTTPClientSettings.Headers["Authorization"] = configopaque.String("Basic " + basicAuth)
}
} else {
queryValues.Set("org", config.Org)
queryValues.Set("bucket", config.Bucket)

if config.Token != "" {
if config.HTTPClientSettings.Headers == nil {
config.HTTPClientSettings.Headers = make(map[string]configopaque.String, 1)
}
config.HTTPClientSettings.Headers["Authorization"] = "Token " + config.Token
}
}
Expand Down
21 changes: 21 additions & 0 deletions exporter/influxdbexporter/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,24 @@ func Test_influxHTTPWriterBatch_EnqueuePoint_emptyTagValue(t *testing.T) {
assert.Equal(t, "m,k=v f=1i 1000000002000", strings.TrimSpace(string(recordedRequestBody)))
}
}

func Test_composeWriteURL_doesNotPanic(t *testing.T) {
assert.NotPanics(t, func() {
cfg := &Config{}
_, err := composeWriteURL(cfg)
assert.NoError(t, err)
})

assert.NotPanics(t, func() {
cfg := &Config{
V1Compatibility: V1Compatibility{
Enabled: true,
DB: "my-db",
Username: "my-username",
Password: "my-password",
},
}
_, err := composeWriteURL(cfg)
assert.NoError(t, err)
})
}

0 comments on commit 419d58c

Please sign in to comment.