feat: add proxy configuration support to agent and update related functions

This commit is contained in:
Simon Larsen
2025-01-03 14:55:16 +00:00
parent fe9d3d3020
commit 16a1051280
3 changed files with 26 additions and 20 deletions

View File

@@ -32,6 +32,7 @@ func NewAgent(secretKey string, oneuptimeUrl string, proxyUrl string) *Agent {
OneUptimeURL: oneuptimeUrl,
ProxyURL: proxyUrl,
}
utils.SetDefaultLogger()
slog.Info("Starting agent...")
slog.Info("Agent configuration:")
slog.Info("Secret key: " + ag.SecretKey)
@@ -57,7 +58,7 @@ func NewAgent(secretKey string, oneuptimeUrl string, proxyUrl string) *Agent {
return ag
}
job, err := scheduler.NewJob(gocron.DurationJob(30*time.Second), gocron.NewTask(collectMetricsJob, ag.SecretKey, ag.OneUptimeURL))
job, err := scheduler.NewJob(gocron.DurationJob(30*time.Second), gocron.NewTask(collectMetricsJob, ag.SecretKey, ag.OneUptimeURL, ag.ProxyURL))
if err != nil {
slog.Error(err.Error())
os.Exit(1)
@@ -163,7 +164,7 @@ func checkIfSecretKeyIsValid(secretKey string, oneuptimeUrl string, proxyUrl str
client := &http.Client{}
if proxyUrl != "" {
proxyURL, _ := url.Parse("http://your-proxy-server:port")
proxyURL, _ := url.Parse(proxyUrl)
transport := &http.Transport{Proxy: http.ProxyURL(proxyURL)}
client = &http.Client{Transport: transport}
}

View File

@@ -28,21 +28,6 @@ func (p *program) Start(s service.Service) error {
return nil
}
func SetDefaultLogger() {
logFile, err := os.OpenFile("output.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
slog.Default().Error("Failed to open log file", "error", err)
// If we can't open the log file, we'll log to the console instead
logFile = os.Stdout
}
//defer logFile.Close()
logger := slog.New(slog.NewTextHandler(logFile, nil))
slog.SetDefault(logger)
}
func (p *program) run() {
p.agent = NewAgent(p.config.SecretKey, p.config.OneUptimeURL, p.config.ProxyURL)
p.agent.Start()
@@ -74,8 +59,7 @@ func (p *program) Stop(s service.Service) error {
func main() {
SetDefaultLogger()
slog.Info("Starting OneUptime Infrastructure Agent")
slog.Info("OneUptime Infrastructure Agent")
// Set up the configuration
config.WithOptions(config.WithTagName("json"))
cfg := newConfigFile()
@@ -110,7 +94,7 @@ func main() {
switch cmd {
case "configure":
installFlags := flag.NewFlagSet("configure", flag.ExitOnError)
secretKey := installFlags.String("secret-key", "", "Secret key (required)")
secretKey := installFlags.String("secret-key", "", "Secret key of this monitor. You can find this on OneUptime dashboard (required)")
oneuptimeURL := installFlags.String("oneuptime-url", "", "Oneuptime endpoint root URL (required)")
// Take input - proxy URL, proxy port, username / password - all optional

View File

@@ -0,0 +1,21 @@
package utils
import (
slog "log/slog"
"os"
)
func SetDefaultLogger() {
logFile, err := os.OpenFile("output.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
slog.Default().Error("Failed to open log file", "error", err)
// If we can't open the log file, we'll log to the console instead
logFile = os.Stdout
}
//defer logFile.Close()
logger := slog.New(slog.NewTextHandler(logFile, nil))
slog.SetDefault(logger)
}