Skip to content

Commit

Permalink
[TT-1167] shorten grafana url or return empty string (#1012)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tofel authored Jun 27, 2024
1 parent ca284b7 commit 94e2101
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 23 deletions.
46 changes: 31 additions & 15 deletions logstream/grafana.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"encoding/json"
"fmt"
"net/http"
"time"

"github.com/avast/retry-go"
)

const (
Expand All @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion logstream/logstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 6 additions & 7 deletions logstream/logstream_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 94e2101

Please sign in to comment.