Plugin sync logging improvements (#4963)

# What this PR does
- Fix logging when no error
- Add more logging to request errors during sync"

## Which issue(s) this PR closes

Related to [issue link here]

<!--
*Note*: If you want the issue to be auto-closed once the PR is merged,
change "Related to" to "Closes" in the line above.
If you have more than one GitHub issue that this PR closes, be sure to
preface
each issue link with a [closing
keyword](https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/using-keywords-in-issues-and-pull-requests#linking-a-pull-request-to-an-issue).
This ensures that the issue(s) are auto-closed once the PR has been
merged.
-->

## Checklist

- [x] Unit, integration, and e2e (if applicable) tests updated
- [x] Documentation added (or `pr:no public docs` PR label added if not
required)
- [x] Added the relevant release notes label (see labels prefixed w/
`release:`). These labels dictate how your PR will
    show up in the autogenerated release notes.
This commit is contained in:
Michael Derynck 2024-08-30 10:42:12 -06:00 committed by GitHub
parent 90de23ccc2
commit 437620776c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -8,6 +8,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"github.com/grafana/grafana-plugin-sdk-go/backend/log" "github.com/grafana/grafana-plugin-sdk-go/backend/log"
"io"
"net/http" "net/http"
"net/url" "net/url"
"strconv" "strconv"
@ -83,7 +84,7 @@ func (a *App) doSync(ctx context.Context, forceSend bool) {
var cacheAlreadyLocked *SyncCacheAlreadyLocked var cacheAlreadyLocked *SyncCacheAlreadyLocked
if errors.As(err, &cacheAlreadyLocked) { if errors.As(err, &cacheAlreadyLocked) {
log.DefaultLogger.Info("Skipping sync", "message", err) log.DefaultLogger.Info("Skipping sync", "message", err)
} else { } else if err != nil {
log.DefaultLogger.Error("Error making sync request", "error", err) log.DefaultLogger.Error("Error making sync request", "error", err)
} }
}() }()
@ -175,6 +176,17 @@ func (a *App) makeSyncRequest(ctx context.Context, forceSend bool) error {
} }
defer res.Body.Close() defer res.Body.Close()
if res.StatusCode != http.StatusOK {
bodyBytes, err := io.ReadAll(res.Body)
if err != nil {
log.DefaultLogger.Error("failed to read response body", "error", err, "status", res.StatusCode)
} else {
log.DefaultLogger.Error("sync not ok", "status", res.StatusCode, "message", string(bodyBytes))
}
} else {
log.DefaultLogger.Info("sync ok", "status", res.StatusCode)
}
a.lastOnCallSync = onCallSync a.lastOnCallSync = onCallSync
return nil return nil
} }