From ef340e5fa06059db2ee5cc69be6b09e140b481cf Mon Sep 17 00:00:00 2001 From: Simon Larsen Date: Fri, 3 Jan 2025 15:22:35 +0000 Subject: [PATCH] feat: improve error logging for secret key validation and add proxy support for HTTP requests --- InfrastructureAgent/agent.go | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/InfrastructureAgent/agent.go b/InfrastructureAgent/agent.go index 5e6e0174e5..29218e99c8 100644 --- a/InfrastructureAgent/agent.go +++ b/InfrastructureAgent/agent.go @@ -46,7 +46,7 @@ func NewAgent(secretKey string, oneuptimeUrl string, proxyUrl string) *Agent { // check if secret key is valid if !checkIfSecretKeyIsValid(ag.SecretKey, ag.OneUptimeURL, ag.ProxyURL) { - slog.Error("Secret key is invalid") + slog.Error("Secret key is invalid. If you are sure that the secret key is correct, please check your network connection, OneUptime URL (" + ag.OneUptimeURL + "), Proxy URL (" + ag.ProxyURL + ") and try again.") os.Exit(1) return ag } @@ -133,19 +133,21 @@ func collectMetricsJob(secretKey string, oneuptimeUrl string, proxyUrl string) { return } - req, err := http.NewRequest(http.MethodPost, oneuptimeUrl+"/server-monitor/response/ingest/"+secretKey, bytes.NewBuffer(reqBody)) - if err != nil { - slog.Error("Failed to create request: ", err) - return + client := &http.Client{} + + if proxyUrl != "" { + proxyURL, _ := url.Parse(proxyUrl) + transport := &http.Transport{Proxy: http.ProxyURL(proxyURL)} + client = &http.Client{Transport: transport} + slog.Info("Using proxy to send request:" + proxyUrl) } - req.Header.Set("Content-Type", "application/json") - client := &http.Client{} - resp, err := client.Do(req) + resp, err := client.Post(oneuptimeUrl+"/server-monitor/response/ingest/"+secretKey, "application/json", bytes.NewBuffer(reqBody)) if err != nil { - slog.Error("Failed to send request: ", err) + slog.Error("Failed to create request: ", err) return } + defer resp.Body.Close() if resp.StatusCode != http.StatusOK { @@ -167,6 +169,7 @@ func checkIfSecretKeyIsValid(secretKey string, oneuptimeUrl string, proxyUrl str proxyURL, _ := url.Parse(proxyUrl) transport := &http.Transport{Proxy: http.ProxyURL(proxyURL)} client = &http.Client{Transport: transport} + slog.Info("Using proxy to send request:" + proxyUrl) } if secretKey == "" {