mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Loki: Improve error logging in case statusCode is not 200 (#75075)
* improve loki logging * fix error log * fix logger arg
This commit is contained in:
parent
b6bb22a67a
commit
727c83d562
@ -177,18 +177,22 @@ func (api *LokiAPI) DataQuery(ctx context.Context, query lokiQuery, responseOpts
|
||||
return nil, err
|
||||
}
|
||||
|
||||
api.log.Info("Response received from loki", "duration", time.Since(start), "statusCode", resp.StatusCode, "contentLength", resp.Header.Get("Content-Length"), "stage", stageDatabaseRequest, "status", "ok", "query", query.Expr)
|
||||
|
||||
defer func() {
|
||||
if err := resp.Body.Close(); err != nil {
|
||||
api.log.Warn("Failed to close response body", "error", err)
|
||||
}
|
||||
}()
|
||||
|
||||
lp := []any{"duration", time.Since(start), "stage", stageDatabaseRequest, "statusCode", resp.StatusCode, "contentLength", resp.Header.Get("Content-Length")}
|
||||
lp = append(lp, queryAttrs...)
|
||||
if resp.StatusCode/100 != 2 {
|
||||
err := readLokiError(resp.Body)
|
||||
api.log.Error("Error received from loki", "error", err, "status", resp.StatusCode)
|
||||
lp = append(lp, "status", "error", "error", err)
|
||||
api.log.Error("Error received from Loki", lp...)
|
||||
return nil, err
|
||||
} else {
|
||||
lp = append(lp, "status", "ok")
|
||||
api.log.Info("Response received from loki", lp...)
|
||||
}
|
||||
|
||||
start = time.Now()
|
||||
|
@ -10,6 +10,7 @@ import (
|
||||
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||
"github.com/grafana/grafana-plugin-sdk-go/experimental"
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
@ -60,7 +61,7 @@ func TestSuccessResponse(t *testing.T) {
|
||||
bytes, err := os.ReadFile(responseFileName)
|
||||
require.NoError(t, err)
|
||||
|
||||
frames, err := runQuery(context.Background(), makeMockedAPI(http.StatusOK, "application/json", bytes, nil), &query, responseOpts)
|
||||
frames, err := runQuery(context.Background(), makeMockedAPI(http.StatusOK, "application/json", bytes, nil), &query, responseOpts, log.New("test"))
|
||||
require.NoError(t, err)
|
||||
|
||||
dr := &backend.DataResponse{
|
||||
@ -125,7 +126,7 @@ func TestErrorResponse(t *testing.T) {
|
||||
|
||||
for _, test := range tt {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
frames, err := runQuery(context.Background(), makeMockedAPI(400, test.contentType, test.body, nil), &lokiQuery{QueryType: QueryTypeRange, Direction: DirectionBackward}, ResponseOpts{})
|
||||
frames, err := runQuery(context.Background(), makeMockedAPI(400, test.contentType, test.body, nil), &lokiQuery{QueryType: QueryTypeRange, Direction: DirectionBackward}, ResponseOpts{}, log.New("test"))
|
||||
|
||||
require.Len(t, frames, 0)
|
||||
require.Error(t, err)
|
||||
|
@ -200,7 +200,7 @@ func queryData(ctx context.Context, req *backend.QueryDataRequest, dsInfo *datas
|
||||
span.SetAttributes("query_group_id", req.GetHTTPHeader("X-Query-Group-Id"), attribute.Key("query_group_id").String(req.GetHTTPHeader("X-Query-Group-Id")))
|
||||
}
|
||||
|
||||
frames, err := runQuery(ctx, api, query, responseOpts)
|
||||
frames, err := runQuery(ctx, api, query, responseOpts, plog)
|
||||
|
||||
queryRes := backend.DataResponse{}
|
||||
|
||||
@ -219,10 +219,10 @@ func queryData(ctx context.Context, req *backend.QueryDataRequest, dsInfo *datas
|
||||
}
|
||||
|
||||
// we extracted this part of the functionality to make it easy to unit-test it
|
||||
func runQuery(ctx context.Context, api *LokiAPI, query *lokiQuery, responseOpts ResponseOpts) (data.Frames, error) {
|
||||
func runQuery(ctx context.Context, api *LokiAPI, query *lokiQuery, responseOpts ResponseOpts, plog log.Logger) (data.Frames, error) {
|
||||
frames, err := api.DataQuery(ctx, *query, responseOpts)
|
||||
if err != nil {
|
||||
logger.Error("Error querying loki", "error", err)
|
||||
plog.Error("Error querying loki", "error", err)
|
||||
return data.Frames{}, err
|
||||
}
|
||||
|
||||
@ -230,7 +230,7 @@ func runQuery(ctx context.Context, api *LokiAPI, query *lokiQuery, responseOpts
|
||||
err = adjustFrame(frame, query, !responseOpts.metricDataplane, responseOpts.logsDataplane)
|
||||
|
||||
if err != nil {
|
||||
logger.Error("Error adjusting frame", "error", err)
|
||||
plog.Error("Error adjusting frame", "error", err)
|
||||
return data.Frames{}, err
|
||||
}
|
||||
}
|
||||
|
@ -7,6 +7,8 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
)
|
||||
|
||||
// when memory-profiling these benchmarks these commands are recommended
|
||||
@ -17,7 +19,7 @@ func BenchmarkMatrixJson(b *testing.B) {
|
||||
|
||||
b.ResetTimer()
|
||||
for n := 0; n < b.N; n++ {
|
||||
_, _ = runQuery(context.Background(), makeMockedAPI(http.StatusOK, "application/json", bytes, nil), &lokiQuery{}, ResponseOpts{})
|
||||
_, _ = runQuery(context.Background(), makeMockedAPI(http.StatusOK, "application/json", bytes, nil), &lokiQuery{}, ResponseOpts{}, log.New("test"))
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user