Skip to content

Commit

Permalink
Add timeout for fetching Mist metrics from catalyst-api
Browse files Browse the repository at this point in the history
  • Loading branch information
leszko committed Aug 26, 2024
1 parent c69d2fd commit 9b27036
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions mapic/mistmetrics.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
package mistapiconnector

import (
"context"
"fmt"
"github.com/golang/glog"
"io"
"net/http"
"strings"
"time"
)

const mistMetricsCallTimeeout = 10 * time.Second

func (mc *mac) MistMetricsHandler() http.Handler {
return http.HandlerFunc(
func(w http.ResponseWriter, req *http.Request) {
mistMetrics, err := mc.queryMistMetrics()
ctx, cancel := context.WithTimeout(req.Context(), mistMetricsCallTimeeout)
defer cancel()
mistMetrics, err := mc.queryMistMetrics(ctx)
if err != nil {
glog.Warningf("error fetching Mist prometheus metrics: %s", err)
http.Error(w, fmt.Sprintf("error fetching Mist prometheus metrics: %s", err), http.StatusInternalServerError)
return
}
Expand All @@ -26,9 +33,13 @@ func (mc *mac) MistMetricsHandler() http.Handler {
})
}

func (mc *mac) queryMistMetrics() (string, error) {
func (mc *mac) queryMistMetrics(ctx context.Context) (string, error) {
mistMetricsURL := fmt.Sprintf("http://%s:%d/%s", mc.config.MistHost, mc.config.MistPort, mc.config.MistPrometheus)
resp, err := http.Get(mistMetricsURL)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, mistMetricsURL, nil)
if err != nil {
return "", err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
Expand Down

0 comments on commit 9b27036

Please sign in to comment.