Skip to content

Commit

Permalink
Monitoring client proxy (#5)
Browse files Browse the repository at this point in the history
* 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 <mahsa.soleimani@snapp.cab>
  • Loading branch information
divergentluna and Mahsa committed Jan 23, 2024
1 parent 7fec14d commit 202a82a
Showing 1 changed file with 41 additions and 40 deletions.
81 changes: 41 additions & 40 deletions clients/proxy-client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()
Expand All @@ -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.")
}

0 comments on commit 202a82a

Please sign in to comment.