From 202a82afa03254f17e92d3d51d68ffb90473f2c8 Mon Sep 17 00:00:00 2001 From: Mahsa Soleimani Date: Tue, 23 Jan 2024 14:01:04 +0330 Subject: [PATCH] Monitoring client proxy (#5) * Adding the proxy client code base + dockerfile * Add the Helm chart for proxy client * Delete workflows * Changes on global variables, metric type, error handling, and channels for graceful shutdown * Change checkProxy function to accept context, Changes on ticker function * Mostly change on ticker loop * Final changes for client proxy * Changes on checkproxy func client, Add log for successMetric.Inc() * Fix doecker image tag issue on image workflow * Delete workflows --------- Co-authored-by: Mahsa --- clients/proxy-client/main.go | 81 ++++++++++++++++++------------------ 1 file changed, 41 insertions(+), 40 deletions(-) diff --git a/clients/proxy-client/main.go b/clients/proxy-client/main.go index e8cb3bc..73dbcff 100644 --- a/clients/proxy-client/main.go +++ b/clients/proxy-client/main.go @@ -26,29 +26,33 @@ func init() { prometheus.MustRegister(successMetric) } -func checkProxy(ctx context.Context, proxyURL, targetURL string) error { +func checkProxy(ctx context.Context, proxyURL, targetURL string) { + transport, _ := http.DefaultTransport.(*http.Transport) + transport.Proxy = http.ProxyURL(mustParseURL(proxyURL)) + client := &http.Client{ - Transport: &http.Transport{ - Proxy: http.ProxyURL(mustParseURL(proxyURL)), - }, + Transport: transport, } - req, err := http.NewRequestWithContext(ctx, "HEAD", targetURL, nil) + req, err := http.NewRequestWithContext(ctx, "GET", targetURL, nil) if err != nil { - return err + log.Printf("Error creating HTTP request: %s\n", err) + return } resp, err := client.Do(req) if err != nil { - return err + log.Printf("Error performing HTTP request: %s\n", err) + return } defer resp.Body.Close() if resp.StatusCode == http.StatusOK { successMetric.Inc() + log.Printf("Proxy check succeed, Healthy status.") + } else { + log.Printf("Proxy check failed. Status code: %d\n", resp.StatusCode) } - - return nil } func mustParseURL(rawURL string) *url.URL { @@ -67,16 +71,12 @@ func main() { targetURL := "https://ifconfig.me" http.Handle("/metrics", promhttp.Handler()) - go func() { - err := http.ListenAndServe(":9090", nil) - if err != nil { - log.Printf("Error starting Prometheus HTTP server: %s\n", err) - os.Exit(1) - } - }() + + server := &http.Server{ + Addr: ":9090", + } var wg sync.WaitGroup - wg.Add(1) ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -88,32 +88,33 @@ func main() { <-signalCh log.Println("Received signal, shutting down gracefully...") cancel() + server.Shutdown(ctx) }() - defer wg.Done() - - defer func() { - log.Println("Waiting for all goroutines to finish...") - wg.Wait() - log.Println("All goroutines shut down. Exiting.") + wg.Add(1) + go func() { + defer wg.Done() + ticker := time.NewTicker(5 * time.Minute) + defer ticker.Stop() + + for { + select { + case <-ctx.Done(): + log.Println("Ticker routine shutting down...") + return + case <-ticker.C: + checkProxy(ctx, proxyURL, targetURL) + } + } }() - ticker := time.NewTicker(5 * time.Minute) - defer ticker.Stop() - - tickerFunc := func() { - if err := checkProxy(ctx, proxyURL, targetURL); err != nil { - log.Printf("Proxy check failed: %s\n", err) - } - } + wg.Add(1) + go func() { + defer wg.Done() + log.Fatal(server.ListenAndServe()) + }() - for { - select { - case <-ctx.Done(): - log.Println("Shutting down...") - return - case <-ticker.C: - tickerFunc() - } - } + log.Println("Waiting for the HTTP server to finish...") + wg.Wait() + log.Println("All goroutines shut down. Exiting.") }