From 94e2101f6a489c4756883e450d2dce9eb0716008 Mon Sep 17 00:00:00 2001 From: Bartek Tofel Date: Thu, 27 Jun 2024 14:47:19 +0200 Subject: [PATCH] [TT-1167] shorten grafana url or return empty string (#1012) --- logstream/grafana.go | 46 ++++++++++++++++++++++----------- logstream/logstream.go | 2 +- logstream/logstream_handlers.go | 13 +++++----- 3 files changed, 38 insertions(+), 23 deletions(-) diff --git a/logstream/grafana.go b/logstream/grafana.go index 2c159846a..2e44c0418 100644 --- a/logstream/grafana.go +++ b/logstream/grafana.go @@ -5,6 +5,9 @@ import ( "encoding/json" "fmt" "net/http" + "time" + + "github.com/avast/retry-go" ) const ( @@ -20,25 +23,38 @@ func ShortenUrl(grafanaUrl, urlToShorten, bearerToken string) (string, error) { Url string `json:"url"` } - req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%sapi/short-urls", grafanaUrl), bodyReader) - if err != nil { + var res *http.Response + + if err := retry.Do( + func() error { + req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%sapi/short-urls", grafanaUrl), bodyReader) + if err != nil { + return err + } + + req.Header.Add("Authorization", "Bearer "+bearerToken) + req.Header.Add("Content-Type", "application/json") + + res, err = http.DefaultClient.Do(req) + if err != nil { + return err + } + + if res.StatusCode != http.StatusOK { + return err + } + + return nil + }, + retry.DelayType(retry.FixedDelay), + retry.Attempts(10), + retry.Delay(time.Duration(1)*time.Second), + ); err != nil { return "", fmt.Errorf("%s: %w", ShorteningFailedErr, err) } - req.Header.Add("Authorization", "Bearer "+bearerToken) - req.Header.Add("Content-Type", "application/json") - - res, err := http.DefaultClient.Do(req) - if err != nil { - return "", fmt.Errorf("%s: %w", ShorteningFailedErr, err) - } - - if res.StatusCode != http.StatusOK { - return "", fmt.Errorf("%s: status code: %s", ShorteningFailedErr, res.Status) - } - defer res.Body.Close() - err = json.NewDecoder(res.Body).Decode(&responseObject) + err := json.NewDecoder(res.Body).Decode(&responseObject) if err != nil { return "", fmt.Errorf("%s: %w", ShorteningFailedErr, err) } diff --git a/logstream/logstream.go b/logstream/logstream.go index b0d1aaf84..b4becc9ee 100644 --- a/logstream/logstream.go +++ b/logstream/logstream.go @@ -425,7 +425,7 @@ func (m *LogStream) SaveLogTargetsLocations(writer LogWriter) { m.consumerMutex.RUnlock() if err != nil { if strings.Contains(err.Error(), ShorteningFailedErr) { - m.log.Warn().Str("Handler", name).Err(err).Msg("Failed to shorten Grafana URL, will use original one") + m.log.Warn().Str("Handler", name).Err(err).Msg("Failed to shorten Grafana URL, won't output any url") } else { m.log.Error().Str("Handler", name).Err(err).Msg("Failed to get log location") continue diff --git a/logstream/logstream_handlers.go b/logstream/logstream_handlers.go index 8dff3734d..76c7ab6b4 100644 --- a/logstream/logstream_handlers.go +++ b/logstream/logstream_handlers.go @@ -163,7 +163,7 @@ func (h *LokiLogHandler) GetLogLocation(consumers map[string]*ContainerLogConsum return "", errors.New("no Loki consumers found") } - // if no Grafana URL has been set let's at least print query parameters that can be manually added to the dashboard url + // if no Grafana URL has been set lets at least print query parameters that can be manually added to the dashboard url baseUrl := "" if h.loggingConfig.Grafana != nil && h.loggingConfig.Grafana.BaseUrl != nil { baseUrl = *h.loggingConfig.Grafana.BaseUrl @@ -206,18 +206,17 @@ func (h *LokiLogHandler) GetLogLocation(consumers map[string]*ContainerLogConsum sb.WriteString(fmt.Sprintf("&var-test=%s", testName)) } - relativeUrl := sb.String() - h.grafanaUrl = baseUrl + sb.String() - + var shorteningErr error // try to shorten the URL only if we have all the required configuration parameters if baseUrl != "" && dabshoardUrl != "" && h.loggingConfig.Grafana.BearerToken != nil { - shortened, err := ShortenUrl(baseUrl, relativeUrl, *h.loggingConfig.Grafana.BearerToken) - if err == nil { + var shortened string + shortened, shorteningErr = ShortenUrl(baseUrl, sb.String(), *h.loggingConfig.Grafana.BearerToken) + if shorteningErr == nil { h.grafanaUrl = shortened } } - return h.grafanaUrl, nil + return h.grafanaUrl, shorteningErr } func (h LokiLogHandler) GetTarget() LogTarget {