Skip to content

Commit ef24169

Browse files
authored
[exporter/influxdb] fix panic on init (#29296)
When InfluxDB v1 compatibility is enabled AND username&password are set, the exporter panics. Not any more! Fixes #27084 **Testing:** I've added one regression test.
1 parent 6817ece commit ef24169

File tree

3 files changed

+57
-3
lines changed

3 files changed

+57
-3
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: bug_fix
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: influxdbexporter
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: When InfluxDB v1 compatibility is enabled AND username&password are set, the exporter panics. Not any more!
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [27084]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: [user]

exporter/influxdbexporter/writer.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,21 @@ func composeWriteURL(config *Config) (string, error) {
8888
queryValues.Set("db", config.V1Compatibility.DB)
8989

9090
if config.V1Compatibility.Username != "" && config.V1Compatibility.Password != "" {
91-
var basicAuth []byte
92-
base64.StdEncoding.Encode(basicAuth, []byte(config.V1Compatibility.Username+":"+string(config.V1Compatibility.Password)))
93-
config.HTTPClientSettings.Headers["Authorization"] = configopaque.String("Basic " + string(basicAuth))
91+
basicAuth := base64.StdEncoding.EncodeToString(
92+
[]byte(config.V1Compatibility.Username + ":" + string(config.V1Compatibility.Password)))
93+
if config.HTTPClientSettings.Headers == nil {
94+
config.HTTPClientSettings.Headers = make(map[string]configopaque.String, 1)
95+
}
96+
config.HTTPClientSettings.Headers["Authorization"] = configopaque.String("Basic " + basicAuth)
9497
}
9598
} else {
9699
queryValues.Set("org", config.Org)
97100
queryValues.Set("bucket", config.Bucket)
98101

99102
if config.Token != "" {
103+
if config.HTTPClientSettings.Headers == nil {
104+
config.HTTPClientSettings.Headers = make(map[string]configopaque.String, 1)
105+
}
100106
config.HTTPClientSettings.Headers["Authorization"] = "Token " + config.Token
101107
}
102108
}

exporter/influxdbexporter/writer_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,24 @@ func Test_influxHTTPWriterBatch_EnqueuePoint_emptyTagValue(t *testing.T) {
186186
assert.Equal(t, "m,k=v f=1i 1000000002000", strings.TrimSpace(string(recordedRequestBody)))
187187
}
188188
}
189+
190+
func Test_composeWriteURL_doesNotPanic(t *testing.T) {
191+
assert.NotPanics(t, func() {
192+
cfg := &Config{}
193+
_, err := composeWriteURL(cfg)
194+
assert.NoError(t, err)
195+
})
196+
197+
assert.NotPanics(t, func() {
198+
cfg := &Config{
199+
V1Compatibility: V1Compatibility{
200+
Enabled: true,
201+
DB: "my-db",
202+
Username: "my-username",
203+
Password: "my-password",
204+
},
205+
}
206+
_, err := composeWriteURL(cfg)
207+
assert.NoError(t, err)
208+
})
209+
}

0 commit comments

Comments
 (0)