chore(unified-storage): add tracing for unified storage folder implementation (#105601)

This commit is contained in:
Mustafa Sencer Özcan
2025-05-19 23:25:08 +03:00
committed by GitHub
parent d57ce72969
commit 91882cf50e
10 changed files with 95 additions and 28 deletions
+39 -1
View File
@@ -5,6 +5,7 @@ import (
"fmt"
"strings"
"go.opentelemetry.io/otel/trace"
apierrors "k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -28,24 +29,31 @@ import (
"github.com/grafana/grafana/pkg/util"
)
const tracePrefix = "folder.unifiedstore."
type FolderUnifiedStoreImpl struct {
log log.Logger
k8sclient client.K8sHandler
userService user.Service
tracer trace.Tracer
}
// sqlStore implements the store interface.
var _ folder.Store = (*FolderUnifiedStoreImpl)(nil)
func ProvideUnifiedStore(k8sHandler client.K8sHandler, userService user.Service) *FolderUnifiedStoreImpl {
func ProvideUnifiedStore(k8sHandler client.K8sHandler, userService user.Service, tracer trace.Tracer) *FolderUnifiedStoreImpl {
return &FolderUnifiedStoreImpl{
k8sclient: k8sHandler,
log: log.New("folder-store"),
userService: userService,
tracer: tracer,
}
}
func (ss *FolderUnifiedStoreImpl) Create(ctx context.Context, cmd folder.CreateFolderCommand) (*folder.Folder, error) {
ctx, span := ss.tracer.Start(ctx, tracePrefix+"Create")
defer span.End()
obj, err := internalfolders.LegacyCreateCommandToUnstructured(&cmd)
if err != nil {
return nil, err
@@ -65,6 +73,9 @@ func (ss *FolderUnifiedStoreImpl) Create(ctx context.Context, cmd folder.CreateF
}
func (ss *FolderUnifiedStoreImpl) Delete(ctx context.Context, UIDs []string, orgID int64) error {
ctx, span := ss.tracer.Start(ctx, tracePrefix+"Delete")
defer span.End()
for _, uid := range UIDs {
err := ss.k8sclient.Delete(ctx, uid, orgID, v1.DeleteOptions{})
if err != nil {
@@ -76,6 +87,9 @@ func (ss *FolderUnifiedStoreImpl) Delete(ctx context.Context, UIDs []string, org
}
func (ss *FolderUnifiedStoreImpl) Update(ctx context.Context, cmd folder.UpdateFolderCommand) (*folder.Folder, error) {
ctx, span := ss.tracer.Start(ctx, tracePrefix+"Update")
defer span.End()
obj, err := ss.k8sclient.Get(ctx, cmd.UID, cmd.OrgID, v1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
@@ -140,6 +154,9 @@ func (ss *FolderUnifiedStoreImpl) Update(ctx context.Context, cmd folder.UpdateF
//
// The full path of C is "A/B\/C".
func (ss *FolderUnifiedStoreImpl) Get(ctx context.Context, q folder.GetFolderQuery) (*folder.Folder, error) {
ctx, span := ss.tracer.Start(ctx, tracePrefix+"Get")
defer span.End()
out, err := ss.k8sclient.Get(ctx, *q.UID, q.OrgID, v1.GetOptions{})
if err != nil && !apierrors.IsNotFound(err) {
return nil, err
@@ -151,6 +168,9 @@ func (ss *FolderUnifiedStoreImpl) Get(ctx context.Context, q folder.GetFolderQue
}
func (ss *FolderUnifiedStoreImpl) GetParents(ctx context.Context, q folder.GetParentsQuery) ([]*folder.Folder, error) {
ctx, span := ss.tracer.Start(ctx, tracePrefix+"GetParents")
defer span.End()
hits := []*folder.Folder{}
parentUID := q.UID
@@ -177,6 +197,9 @@ func (ss *FolderUnifiedStoreImpl) GetParents(ctx context.Context, q folder.GetPa
}
func (ss *FolderUnifiedStoreImpl) GetChildren(ctx context.Context, q folder.GetChildrenQuery) ([]*folder.FolderReference, error) {
ctx, span := ss.tracer.Start(ctx, tracePrefix+"GetChildren")
defer span.End()
// the general folder is saved as an empty string in the database
if q.UID == folder.GeneralFolderUID {
q.UID = ""
@@ -261,6 +284,9 @@ func (ss *FolderUnifiedStoreImpl) GetChildren(ctx context.Context, q folder.GetC
// TODO use a single query to get the height of a folder
func (ss *FolderUnifiedStoreImpl) GetHeight(ctx context.Context, foldrUID string, orgID int64, parentUID *string) (int, error) {
ctx, span := ss.tracer.Start(ctx, tracePrefix+"GetHeight")
defer span.End()
height := -1
queue := []string{foldrUID}
for len(queue) > 0 && height <= folder.MaxNestedFolderDepth {
@@ -319,6 +345,9 @@ func (ss *FolderUnifiedStoreImpl) GetHeight(ctx context.Context, foldrUID string
// The full path UIDs of B is "uid1/uid2".
// The full path UIDs of A is "uid1".
func (ss *FolderUnifiedStoreImpl) GetFolders(ctx context.Context, q folder.GetFoldersFromStoreQuery) ([]*folder.Folder, error) {
ctx, span := ss.tracer.Start(ctx, tracePrefix+"GetFolders")
defer span.End()
opts := v1.ListOptions{}
if q.WithFullpath || q.WithFullpathUIDs {
// only supported in modes 0-2, to keep the alerting queries from causing tons of get folder requests
@@ -371,6 +400,9 @@ func (ss *FolderUnifiedStoreImpl) GetFolders(ctx context.Context, q folder.GetFo
}
func (ss *FolderUnifiedStoreImpl) GetDescendants(ctx context.Context, orgID int64, ancestor_uid string) ([]*folder.Folder, error) {
ctx, span := ss.tracer.Start(ctx, tracePrefix+"GetDescendants")
defer span.End()
out, err := ss.list(ctx, orgID, v1.ListOptions{})
if err != nil {
return nil, err
@@ -421,6 +453,9 @@ func getDescendants(nodes map[string]*folder.Folder, tree map[string]map[string]
}
func (ss *FolderUnifiedStoreImpl) CountFolderContent(ctx context.Context, orgID int64, ancestor_uid string) (folder.DescendantCounts, error) {
ctx, span := ss.tracer.Start(ctx, tracePrefix+"CountFolderContent")
defer span.End()
counts, err := ss.k8sclient.Get(ctx, ancestor_uid, orgID, v1.GetOptions{}, "counts")
if err != nil {
if apierrors.IsNotFound(err) {
@@ -448,6 +483,9 @@ func (ss *FolderUnifiedStoreImpl) CountInOrg(ctx context.Context, orgID int64) (
}
func (ss *FolderUnifiedStoreImpl) list(ctx context.Context, orgID int64, opts v1.ListOptions) (*unstructured.UnstructuredList, error) {
ctx, span := ss.tracer.Start(ctx, tracePrefix+"list")
defer span.End()
var allItems []unstructured.Unstructured
listOpts := opts.DeepCopy()