Skip to content

Commit f07480b

Browse files
committed
Add basic auth support for logging endpoint
1 parent 5b483d9 commit f07480b

1 file changed

Lines changed: 26 additions & 1 deletion

File tree

agent/internal/logs/victoria.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,47 @@ import (
66
"fmt"
77
"log"
88
"net/http"
9+
"net/url"
910
"strings"
1011
"time"
1112
)
1213

1314
type VictoriaLogsSender struct {
1415
endpoint string
1516
serverID string
17+
username string
18+
password string
1619
client *http.Client
1720
}
1821

1922
func NewVictoriaLogsSender(endpoint, serverID string) *VictoriaLogsSender {
23+
var username, password string
24+
cleanEndpoint := endpoint
25+
26+
if parsedURL, err := url.Parse(endpoint); err == nil && parsedURL.User != nil {
27+
username = parsedURL.User.Username()
28+
password, _ = parsedURL.User.Password()
29+
parsedURL.User = nil
30+
cleanEndpoint = parsedURL.String()
31+
}
32+
2033
return &VictoriaLogsSender{
21-
endpoint: endpoint,
34+
endpoint: cleanEndpoint,
2235
serverID: serverID,
36+
username: username,
37+
password: password,
2338
client: &http.Client{
2439
Timeout: 60 * time.Second,
2540
},
2641
}
2742
}
2843

44+
func (v *VictoriaLogsSender) setAuthHeader(req *http.Request) {
45+
if v.username != "" {
46+
req.SetBasicAuth(v.username, v.password)
47+
}
48+
}
49+
2950
type victoriaLogEntry struct {
3051
Msg string `json:"_msg"`
3152
Time string `json:"_time"`
@@ -71,6 +92,7 @@ func (v *VictoriaLogsSender) SendLogs(batch *LogBatch) error {
7192
return fmt.Errorf("failed to create request: %w", err)
7293
}
7394
req.Header.Set("Content-Type", "application/json")
95+
v.setAuthHeader(req)
7496

7597
start := time.Now()
7698
resp, err := v.client.Do(req)
@@ -137,6 +159,7 @@ func (v *VictoriaLogsSender) SendHTTPLogs(logs []HTTPLogEntry) error {
137159
return fmt.Errorf("failed to create request: %w", err)
138160
}
139161
req.Header.Set("Content-Type", "application/json")
162+
v.setAuthHeader(req)
140163

141164
start := time.Now()
142165
resp, err := v.client.Do(req)
@@ -211,6 +234,7 @@ func (v *VictoriaLogsSender) SendBuildLogs(buildID, serviceID, projectID string,
211234
return fmt.Errorf("failed to create request: %w", err)
212235
}
213236
req.Header.Set("Content-Type", "application/json")
237+
v.setAuthHeader(req)
214238

215239
resp, err := v.client.Do(req)
216240
if err != nil {
@@ -254,6 +278,7 @@ func (v *VictoriaLogsSender) SendAgentLogs(logs []AgentLog) error {
254278
return fmt.Errorf("failed to create request: %w", err)
255279
}
256280
req.Header.Set("Content-Type", "application/json")
281+
v.setAuthHeader(req)
257282

258283
resp, err := v.client.Do(req)
259284
if err != nil {

0 commit comments

Comments
 (0)