datasources: querier: log caller (#115087)

This commit is contained in:
Gábor Farkas
2025-12-10 18:23:59 +01:00
committed by GitHub
parent 9e1fe16873
commit a847f36df2
2 changed files with 23 additions and 3 deletions
+13 -2
View File
@@ -10,6 +10,8 @@ import (
"strconv"
"strings"
"github.com/grafana/authlib/authn"
claims "github.com/grafana/authlib/types"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/experimental/apis/data/v0alpha1"
"github.com/grafana/grafana/pkg/api/dtos"
@@ -154,11 +156,11 @@ func (r *queryREST) Connect(connectCtx context.Context, name string, _ runtime.O
}
}
}
connectLogger.Debug("responder sending status code", "statusCode", statusCode)
connectLogger.Debug("responder sending status code", "statusCode", statusCode, "caller", getCaller(ctx))
},
func(err error) {
connectLogger.Error("error caught in handler", "err", err)
connectLogger.Error("error caught in handler", "err", err, "caller", getCaller(ctx))
span.SetStatus(codes.Error, "query error")
if err == nil {
@@ -480,3 +482,12 @@ func getValidDataSourceRef(ctx context.Context, ds *v0alpha1.DataSourceRef, id i
return ds, nil
}
func getCaller(ctx context.Context) string {
authInfo, ok := claims.AuthInfoFrom(ctx)
if !ok {
return "<auth-missing>"
} else {
return strings.Join(authInfo.GetExtra()[authn.ServiceIdentityKey], ",")
}
}
+10 -1
View File
@@ -13,6 +13,7 @@ import (
"time"
"github.com/google/go-cmp/cmp"
claims "github.com/grafana/authlib/types"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/data"
dataapi "github.com/grafana/grafana-plugin-sdk-go/experimental/apis/data/v0alpha1"
@@ -180,7 +181,7 @@ func TestQueryAPI(t *testing.T) {
legacyDatasourceLookup: &mockLegacyDataSourceLookup{},
}
reqCtx := identity.WithRequester(context.Background(), mockUser{})
reqCtx := claims.WithAuthInfo(identity.WithRequester(context.Background(), mockUser{}), &mockAuthInfo{})
req := httptest.NewRequestWithContext(reqCtx, http.MethodPost, "/some-path", bytes.NewReader([]byte(tc.queryJSON)))
req.Header.Set("Content-Type", "application/json")
@@ -439,3 +440,11 @@ func TestMergeHeaders(t *testing.T) {
})
}
}
type mockAuthInfo struct {
claims.AuthInfo
}
func (main mockAuthInfo) GetExtra() map[string][]string {
return nil
}