Add MFolderIDsServiceCount to count folderIDs in services pkg (#81237)

This commit is contained in:
idafurjes 2024-01-25 11:10:35 +01:00 committed by GitHub
parent 0d66ad68f8
commit 7e5544ab21
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
25 changed files with 129 additions and 3 deletions

View File

@ -116,6 +116,9 @@ var (
// MFolderIDsAPICount is a metric counter for folder ids count in the api package
MFolderIDsAPICount *prometheus.CounterVec
// MFolderIDsServicesCount is a metric counter for folder ids count in the services package
MFolderIDsServiceCount *prometheus.CounterVec
)
// Timers
@ -220,6 +223,7 @@ var (
)
const (
// FolderID API
GetAlerts string = "GetAlerts"
GetDashboard string = "GetDashboard"
RestoreDashboardVersion string = "RestoreDashboardVersion"
@ -231,12 +235,23 @@ const (
GetFolderACL string = "getFolderACL"
Search string = "Search"
GetDashboardACL string = "getDashboardACL"
// FolderID services
Folder string = "folder"
Dashboard string = "dashboards"
LibraryElements string = "libraryelements"
LibraryPanels string = "librarypanels"
NGAlerts string = "ngalert"
Provisioning string = "provisioning"
PublicDashboards string = "publicdashboards"
AccessControl string = "accesscontrol"
Guardian string = "guardian"
)
func init() {
httpStatusCodes := []string{"200", "404", "500", "unknown"}
objectiveMap := map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001}
apiFolderIDMethods := []string{GetAlerts, GetDashboard, RestoreDashboardVersion, GetFolderByID, GetFolderDescendantCounts, SearchFolders, GetFolderPermissionList, UpdateFolderPermissions, GetFolderACL, Search, GetDashboardACL}
serviceFolderIDMethods := []string{Folder, Dashboard, LibraryElements, LibraryPanels, NGAlerts, Provisioning, PublicDashboards, AccessControl, Guardian, Search}
MInstanceStart = prometheus.NewCounter(prometheus.CounterOpts{
Name: "instance_start_total",
@ -480,6 +495,12 @@ func init() {
Namespace: ExporterName,
}, []string{"method"}, map[string][]string{"method": apiFolderIDMethods})
MFolderIDsServiceCount = metricutil.NewCounterVecStartingAtZero(prometheus.CounterOpts{
Name: "folder_id_service_count",
Help: "counter for folder id usage in service package",
Namespace: ExporterName,
}, []string{"method"}, map[string][]string{"method": serviceFolderIDMethods})
MStatTotalDashboards = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "stat_totals_dashboard",
Help: "total amount of dashboards",
@ -772,5 +793,6 @@ func initMetricVars(reg prometheus.Registerer) {
MPublicDashboardDatasourceQuerySuccess,
MStatTotalCorrelations,
MFolderIDsAPICount,
MFolderIDsServiceCount,
)
}

View File

@ -8,6 +8,7 @@ import (
"github.com/grafana/grafana/pkg/api/routing"
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/infra/metrics"
"github.com/grafana/grafana/pkg/services/accesscontrol"
"github.com/grafana/grafana/pkg/services/accesscontrol/resourcepermissions"
"github.com/grafana/grafana/pkg/services/auth/identity"
@ -151,6 +152,7 @@ func ProvideDashboardPermissions(
if err != nil {
return nil, err
}
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.AccessControl).Inc()
// nolint:staticcheck
if dashboard.FolderID > 0 {
query := &dashboards.GetDashboardQuery{ID: dashboard.FolderID, OrgID: orgID}

View File

@ -5,6 +5,7 @@ import (
"github.com/grafana/grafana/pkg/api/routing"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/infra/metrics"
"github.com/grafana/grafana/pkg/services/accesscontrol"
"github.com/grafana/grafana/pkg/services/auth/identity"
"github.com/grafana/grafana/pkg/services/dashboardimport"
@ -83,6 +84,7 @@ func (s *ImportDashboardService) ImportDashboard(ctx context.Context, req *dashb
generatedDash.Del("__inputs")
generatedDash.Del("__requires")
metrics.MFolderIDsServiceCount.WithLabelValues("dashboardimport").Inc()
// here we need to get FolderId from FolderUID if it present in the request, if both exist, FolderUID would overwrite FolderID
if req.FolderUid != "" {
folder, err := s.folderService.Get(ctx, &folder.GetFolderQuery{
@ -137,6 +139,7 @@ func (s *ImportDashboardService) ImportDashboard(ctx context.Context, req *dashb
return nil, err
}
metrics.MFolderIDsServiceCount.WithLabelValues("dashboardimport").Inc()
// nolint:staticcheck
err = s.libraryPanelService.ImportLibraryPanelsForDashboard(ctx, req.User, libraryElements, generatedDash.Get("panels").MustArray(), req.FolderId)
if err != nil {
@ -149,6 +152,7 @@ func (s *ImportDashboardService) ImportDashboard(ctx context.Context, req *dashb
}
revision := savedDashboard.Data.Get("revision").MustInt64(0)
metrics.MFolderIDsServiceCount.WithLabelValues("dashboardimport").Inc()
return &dashboardimport.ImportDashboardResponse{
UID: savedDashboard.UID,
PluginId: req.PluginId,

View File

@ -4,6 +4,7 @@ import (
"context"
"strings"
"github.com/grafana/grafana/pkg/infra/metrics"
ac "github.com/grafana/grafana/pkg/services/accesscontrol"
"github.com/grafana/grafana/pkg/services/folder"
)
@ -168,11 +169,13 @@ func NewDashboardUIDScopeResolver(folderDB folder.FolderStore, ds DashboardServi
func resolveDashboardScope(ctx context.Context, folderDB folder.FolderStore, orgID int64, dashboard *Dashboard, folderSvc folder.Service) ([]string, error) {
var folderUID string
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Dashboard).Inc()
// nolint:staticcheck
if dashboard.FolderID < 0 {
return []string{ScopeDashboardsProvider.GetResourceScopeUID(dashboard.UID)}, nil
}
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Dashboard).Inc()
// nolint:staticcheck
if dashboard.FolderID == 0 {
folderUID = ac.GeneralFolderUID

View File

@ -367,6 +367,7 @@ func getExistingDashboardByTitleAndFolder(sess *db.Session, dash *dashboards.Das
return isParentFolderChanged, dashboards.ErrDashboardFolderWithSameNameAsDashboard
}
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Dashboard).Inc()
// nolint:staticcheck
if !dash.IsFolder && (dash.FolderID != existing.FolderID || dash.ID == 0) {
isParentFolderChanged = true
@ -818,6 +819,7 @@ func (d *dashboardStore) deleteAlertDefinition(dashboardId int64, sess *db.Sessi
func (d *dashboardStore) GetDashboard(ctx context.Context, query *dashboards.GetDashboardQuery) (*dashboards.Dashboard, error) {
var queryResult *dashboards.Dashboard
err := d.store.WithDbSession(ctx, func(sess *db.Session) error {
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Dashboard).Inc()
// nolint:staticcheck
if query.ID == 0 && len(query.UID) == 0 && (query.Title == nil || (query.FolderID == nil && query.FolderUID == "")) {
return dashboards.ErrDashboardIdentifierNotSet
@ -837,6 +839,7 @@ func (d *dashboardStore) GetDashboard(ctx context.Context, query *dashboards.Get
// nolint:staticcheck
dashboard.FolderID = *query.FolderID
mustCols = append(mustCols, "folder_id")
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Dashboard).Inc()
}
has, err := sess.MustCols(mustCols...).Get(&dashboard)
@ -940,7 +943,7 @@ func (d *dashboardStore) FindDashboards(ctx context.Context, query *dashboards.F
if len(query.Type) > 0 {
filters = append(filters, searchstore.TypeFilter{Dialect: d.store.GetDialect(), Type: query.Type})
}
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Dashboard).Inc()
// nolint:staticcheck
if len(query.FolderIds) > 0 {
filters = append(filters, searchstore.FolderFilter{IDs: query.FolderIds})
@ -1013,6 +1016,7 @@ func (d *dashboardStore) CountDashboardsInFolder(
var count int64
var err error
err = d.store.WithDbSession(ctx, func(sess *db.Session) error {
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Dashboard).Inc()
// nolint:staticcheck
session := sess.In("folder_id", req.FolderID).In("org_id", req.OrgID).
In("is_folder", d.store.GetDialect().BooleanStr(false))

View File

@ -5,6 +5,7 @@ import (
"time"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/infra/metrics"
"github.com/grafana/grafana/pkg/infra/slugify"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/auth/identity"
@ -136,6 +137,7 @@ func (cmd *SaveDashboardCommand) GetDashboardModel() *Dashboard {
dash.OrgID = cmd.OrgID
dash.PluginID = cmd.PluginID
dash.IsFolder = cmd.IsFolder
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Dashboard).Inc()
// nolint:staticcheck
dash.FolderID = cmd.FolderID
dash.FolderUID = cmd.FolderUID
@ -326,6 +328,7 @@ type CountDashboardsInFolderRequest struct {
}
func FromDashboard(dash *Dashboard) *folder.Folder {
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Dashboard).Inc()
return &folder.Folder{
ID: dash.ID, // nolint:staticcheck
UID: dash.UID,

View File

@ -13,6 +13,7 @@ import (
"github.com/grafana/grafana-plugin-sdk-go/backend/gtime"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/metrics"
"github.com/grafana/grafana/pkg/services/accesscontrol"
"github.com/grafana/grafana/pkg/services/alerting"
"github.com/grafana/grafana/pkg/services/auth/identity"
@ -114,6 +115,7 @@ func (dr *DashboardServiceImpl) BuildSaveDashboardCommand(ctx context.Context, d
return nil, dashboards.ErrDashboardTitleEmpty
}
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Dashboard).Inc()
// nolint:staticcheck
if dash.IsFolder && dash.FolderID > 0 {
return nil, dashboards.ErrDashboardFolderCannotHaveParent
@ -146,9 +148,11 @@ func (dr *DashboardServiceImpl) BuildSaveDashboardCommand(ctx context.Context, d
if err != nil {
return nil, err
}
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Dashboard).Inc()
// nolint:staticcheck
dash.FolderID = folder.ID
} else if dash.FolderID != 0 { // nolint:staticcheck
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Dashboard).Inc()
// nolint:staticcheck
folder, err := dr.folderStore.GetFolderByID(ctx, dash.OrgID, dash.FolderID)
if err != nil {
@ -168,6 +172,7 @@ func (dr *DashboardServiceImpl) BuildSaveDashboardCommand(ctx context.Context, d
if err != nil {
return nil, err
}
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Dashboard).Inc()
// nolint:staticcheck
if canSave, err := guardian.CanCreate(dash.FolderID, dash.IsFolder); err != nil || !canSave {
if err != nil {
@ -194,6 +199,7 @@ func (dr *DashboardServiceImpl) BuildSaveDashboardCommand(ctx context.Context, d
}
if dash.ID == 0 {
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Dashboard).Inc()
// nolint:staticcheck
if canCreate, err := guard.CanCreate(dash.FolderID, dash.IsFolder); err != nil || !canCreate {
if err != nil {
@ -215,6 +221,7 @@ func (dr *DashboardServiceImpl) BuildSaveDashboardCommand(ctx context.Context, d
return nil, err
}
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Dashboard).Inc()
cmd := &dashboards.SaveDashboardCommand{
Dashboard: dash.Data,
Message: dto.Message,
@ -260,6 +267,7 @@ func getGuardianForSavePermissionCheck(ctx context.Context, d *dashboards.Dashbo
if newDashboard {
// if it's a new dashboard/folder check the parent folder permissions
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Dashboard).Inc()
// nolint:staticcheck
guard, err := guardian.New(ctx, d.FolderID, d.OrgID, user)
if err != nil {
@ -473,6 +481,7 @@ func (dr *DashboardServiceImpl) GetDashboardsByPluginID(ctx context.Context, que
}
func (dr *DashboardServiceImpl) setDefaultPermissions(ctx context.Context, dto *dashboards.SaveDashboardDTO, dash *dashboards.Dashboard, provisioned bool) {
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Dashboard).Inc()
// nolint:staticcheck
inFolder := dash.FolderID > 0
var permissions []accesscontrol.SetResourcePermissionCommand
@ -686,6 +695,7 @@ func makeQueryResult(query *dashboards.FindPersistedDashboardsQuery, res []dashb
for _, item := range res {
hit, exists := hits[item.ID]
if !exists {
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Dashboard).Inc()
hit = &model.Hit{
ID: item.ID,
UID: item.UID,
@ -729,6 +739,7 @@ func (dr DashboardServiceImpl) CountInFolder(ctx context.Context, orgID int64, f
return 0, err
}
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Dashboard).Inc()
// nolint:staticcheck
return dr.dashboardStore.CountDashboardsInFolder(ctx, &dashboards.CountDashboardsInFolderRequest{FolderID: folder.ID, OrgID: orgID})
}

View File

@ -5,6 +5,7 @@ import (
"strings"
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/infra/metrics"
"github.com/grafana/grafana/pkg/services/dashboards"
"github.com/grafana/grafana/pkg/services/folder"
)
@ -26,6 +27,7 @@ func (d *DashboardFolderStoreImpl) GetFolderByTitle(ctx context.Context, orgID i
// there is a unique constraint on org_id, folder_uid, title
// there are no nested folders so the parent folder id is always 0
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Folder).Inc()
// nolint:staticcheck
dashboard := dashboards.Dashboard{OrgID: orgID, FolderID: 0, Title: title}
err := d.store.WithTransactionalDbSession(ctx, func(sess *db.Session) error {
@ -50,6 +52,7 @@ func (d *DashboardFolderStoreImpl) GetFolderByTitle(ctx context.Context, orgID i
}
func (d *DashboardFolderStoreImpl) GetFolderByID(ctx context.Context, orgID int64, id int64) (*folder.Folder, error) {
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Folder).Inc()
// nolint:staticcheck
dashboard := dashboards.Dashboard{OrgID: orgID, FolderID: 0, ID: id}
err := d.store.WithTransactionalDbSession(ctx, func(sess *db.Session) error {
@ -74,7 +77,7 @@ func (d *DashboardFolderStoreImpl) GetFolderByUID(ctx context.Context, orgID int
if uid == "" {
return nil, dashboards.ErrDashboardIdentifierNotSet
}
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Folder).Inc()
// nolint:staticcheck
dashboard := dashboards.Dashboard{OrgID: orgID, FolderID: 0, UID: uid}
err := d.store.WithTransactionalDbSession(ctx, func(sess *db.Session) error {

View File

@ -17,6 +17,7 @@ import (
"github.com/grafana/grafana/pkg/events"
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/metrics"
"github.com/grafana/grafana/pkg/services/accesscontrol"
"github.com/grafana/grafana/pkg/services/auth/identity"
"github.com/grafana/grafana/pkg/services/dashboards"
@ -171,6 +172,7 @@ func (s *Service) Get(ctx context.Context, q *folder.GetFolderQuery) (*folder.Fo
}
// nolint:staticcheck
case q.ID != nil:
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Folder).Inc()
dashFolder, err = s.getFolderByID(ctx, *q.ID, q.OrgID)
if err != nil {
return nil, err
@ -206,7 +208,7 @@ func (s *Service) Get(ctx context.Context, q *folder.GetFolderQuery) (*folder.Fo
if !s.features.IsEnabled(ctx, featuremgmt.FlagNestedFolders) {
return dashFolder, nil
}
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Folder).Inc()
// nolint:staticcheck
if q.ID != nil {
q.ID = nil
@ -219,6 +221,7 @@ func (s *Service) Get(ctx context.Context, q *folder.GetFolderQuery) (*folder.Fo
}
// always expose the dashboard store sequential ID
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Folder).Inc()
// nolint:staticcheck
f.ID = dashFolder.ID
f.Version = dashFolder.Version
@ -287,6 +290,7 @@ func (s *Service) GetChildren(ctx context.Context, q *folder.GetChildrenQuery) (
}
// always expose the dashboard store sequential ID
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Folder).Inc()
// nolint:staticcheck
f.ID = dashFolder.ID
}
@ -336,6 +340,7 @@ func (s *Service) getRootFolders(ctx context.Context, q *folder.GetChildrenQuery
s.log.Error("failed to fetch folder by UID from dashboard store", "orgID", f.OrgID, "uid", f.UID)
}
// always expose the dashboard store sequential ID
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Folder).Inc()
// nolint:staticcheck
f.ID = dashFolder.ID
@ -591,6 +596,7 @@ func (s *Service) Update(ctx context.Context, cmd *folder.UpdateFolderCommand) (
if cmd.NewTitle != nil {
namespace, id := cmd.SignedInUser.GetNamespacedID()
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Folder).Inc()
if err := s.bus.Publish(context.Background(), &events.FolderTitleUpdated{
Timestamp: foldr.Updated,
Title: foldr.Title,
@ -616,6 +622,7 @@ func (s *Service) Update(ctx context.Context, cmd *folder.UpdateFolderCommand) (
}
// always expose the dashboard store sequential ID
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Folder).Inc()
// nolint:staticcheck
foldr.ID = dashFolder.ID
foldr.Version = dashFolder.Version
@ -788,6 +795,7 @@ func (s *Service) deleteChildrenInFolder(ctx context.Context, orgID int64, folde
}
func (s *Service) legacyDelete(ctx context.Context, cmd *folder.DeleteFolderCommand, dashFolder *folder.Folder) error {
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Folder).Inc()
// nolint:staticcheck
deleteCmd := dashboards.DeleteDashboardCommand{OrgID: cmd.OrgID, ID: dashFolder.ID, ForceDeleteFolderRules: cmd.ForceDeleteRules}
@ -1020,6 +1028,7 @@ func (s *Service) buildSaveDashboardCommand(ctx context.Context, dto *dashboards
}
if dash.ID == 0 {
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Folder).Inc()
// nolint:staticcheck
if canCreate, err := guard.CanCreate(dash.FolderID, dash.IsFolder); err != nil || !canCreate {
if err != nil {
@ -1047,6 +1056,7 @@ func (s *Service) buildSaveDashboardCommand(ctx context.Context, dto *dashboards
}
}
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Folder).Inc()
cmd := &dashboards.SaveDashboardCommand{
Dashboard: dash.Data,
Message: dto.Message,
@ -1073,6 +1083,7 @@ func getGuardianForSavePermissionCheck(ctx context.Context, d *dashboards.Dashbo
if newDashboard {
// if it's a new dashboard/folder check the parent folder permissions
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Folder).Inc()
// nolint:staticcheck
guard, err := guardian.New(ctx, d.FolderID, d.OrgID, user)
if err != nil {

View File

@ -11,6 +11,7 @@ import (
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/metrics"
"github.com/grafana/grafana/pkg/services/dashboards"
"github.com/grafana/grafana/pkg/services/folder"
"github.com/grafana/grafana/pkg/services/sqlstore/migrator"
@ -70,6 +71,7 @@ func (ss *sqlStore) Create(ctx context.Context, cmd folder.CreateFolderCommand)
return err
}
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Folder).Inc()
foldr, err = ss.Get(ctx, folder.GetFolderQuery{
ID: &lastInsertedID, // nolint:staticcheck
})
@ -170,6 +172,7 @@ func (ss *sqlStore) Get(ctx context.Context, q folder.GetFolderQuery) (*folder.F
exists, err = sess.SQL("SELECT * FROM folder WHERE uid = ? AND org_id = ?", q.UID, q.OrgID).Get(foldr)
// nolint:staticcheck
case q.ID != nil:
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Folder).Inc()
exists, err = sess.SQL("SELECT * FROM folder WHERE id = ?", q.ID).Get(foldr)
case q.Title != nil:
s := strings.Builder{}

View File

@ -4,6 +4,7 @@ import (
"fmt"
"time"
"github.com/grafana/grafana/pkg/infra/metrics"
"github.com/grafana/grafana/pkg/infra/slugify"
"github.com/grafana/grafana/pkg/services/auth/identity"
"github.com/grafana/grafana/pkg/setting"
@ -62,6 +63,7 @@ var SharedWithMeFolder = Folder{
}
func (f *Folder) IsGeneral() bool {
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Folder).Inc()
// nolint:staticcheck
return f.ID == GeneralFolder.ID && f.Title == GeneralFolder.Title
}

View File

@ -4,6 +4,7 @@ import (
"context"
"slices"
"github.com/grafana/grafana/pkg/infra/metrics"
"github.com/grafana/grafana/pkg/services/auth/identity"
"github.com/grafana/grafana/pkg/services/dashboards"
"github.com/grafana/grafana/pkg/services/folder"
@ -117,6 +118,7 @@ func MockDashboardGuardian(mock *FakeDashboardGuardian) {
NewByFolder = func(_ context.Context, f *folder.Folder, orgId int64, user identity.Requester) (DashboardGuardian, error) {
mock.OrgID = orgId
mock.DashUID = f.UID
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Guardian).Inc()
// nolint:staticcheck
mock.DashID = f.ID
mock.User = user

View File

@ -6,6 +6,7 @@ import (
"github.com/grafana/grafana/pkg/api/response"
"github.com/grafana/grafana/pkg/api/routing"
"github.com/grafana/grafana/pkg/infra/metrics"
ac "github.com/grafana/grafana/pkg/services/accesscontrol"
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
"github.com/grafana/grafana/pkg/services/dashboards"
@ -62,6 +63,7 @@ func (l *LibraryElementService) createHandler(c *contextmodel.ReqContext) respon
if cmd.FolderUID != nil {
if *cmd.FolderUID == "" {
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.LibraryElements).Inc()
// nolint:staticcheck
cmd.FolderID = 0
generalFolderUID := ac.GeneralFolderUID
@ -71,6 +73,7 @@ func (l *LibraryElementService) createHandler(c *contextmodel.ReqContext) respon
if err != nil || folder == nil {
return response.ErrOrFallback(http.StatusBadRequest, "failed to get folder", err)
}
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.LibraryElements).Inc()
// nolint:staticcheck
cmd.FolderID = folder.ID
}
@ -81,8 +84,10 @@ func (l *LibraryElementService) createHandler(c *contextmodel.ReqContext) respon
return toLibraryElementError(err, "Failed to create library element")
}
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.LibraryElements).Inc()
// nolint:staticcheck
if element.FolderID != 0 {
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.LibraryElements).Inc()
// nolint:staticcheck
folder, err := l.folderService.Get(c.Req.Context(), &folder.GetFolderQuery{OrgID: c.SignedInUser.GetOrgID(), ID: &element.FolderID, SignedInUser: c.SignedInUser})
if err != nil {
@ -210,6 +215,7 @@ func (l *LibraryElementService) patchHandler(c *contextmodel.ReqContext) respons
if cmd.FolderUID != nil {
if *cmd.FolderUID == "" {
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.LibraryElements).Inc()
// nolint:staticcheck
cmd.FolderID = 0
} else {
@ -217,6 +223,7 @@ func (l *LibraryElementService) patchHandler(c *contextmodel.ReqContext) respons
if err != nil || folder == nil {
return response.Error(http.StatusBadRequest, "failed to get folder", err)
}
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.LibraryElements).Inc()
// nolint:staticcheck
cmd.FolderID = folder.ID
}
@ -227,8 +234,10 @@ func (l *LibraryElementService) patchHandler(c *contextmodel.ReqContext) respons
return toLibraryElementError(err, "Failed to update library element")
}
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.LibraryElements).Inc()
// nolint:staticcheck
if element.FolderID != 0 {
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.LibraryElements).Inc()
// nolint:staticcheck
folder, err := l.folderService.Get(c.Req.Context(), &folder.GetFolderQuery{OrgID: c.SignedInUser.GetOrgID(), ID: &element.FolderID, SignedInUser: c.SignedInUser})
if err != nil {

View File

@ -10,6 +10,7 @@ import (
"github.com/grafana/grafana/pkg/api/dtos"
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/infra/metrics"
"github.com/grafana/grafana/pkg/kinds/librarypanel"
ac "github.com/grafana/grafana/pkg/services/accesscontrol"
"github.com/grafana/grafana/pkg/services/auth/identity"
@ -146,6 +147,7 @@ func (l *LibraryElementService) createLibraryElement(c context.Context, signedIn
}
}
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.LibraryElements).Inc()
element := model.LibraryElement{
OrgID: signedInUser.GetOrgID(),
FolderID: cmd.FolderID, // nolint:staticcheck
@ -176,6 +178,7 @@ func (l *LibraryElementService) createLibraryElement(c context.Context, signedIn
return err
}
} else {
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.LibraryElements).Inc()
// nolint:staticcheck
if err := l.requireEditPermissionsOnFolder(c, signedInUser, cmd.FolderID); err != nil {
return err
@ -190,6 +193,7 @@ func (l *LibraryElementService) createLibraryElement(c context.Context, signedIn
return nil
})
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.LibraryElements).Inc()
dto := model.LibraryElementDTO{
ID: element.ID,
OrgID: element.OrgID,
@ -229,6 +233,7 @@ func (l *LibraryElementService) deleteLibraryElement(c context.Context, signedIn
if err != nil {
return err
}
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.LibraryElements).Inc()
// nolint:staticcheck
if err := l.requireEditPermissionsOnFolder(c, signedInUser, element.FolderID); err != nil {
return err
@ -280,6 +285,7 @@ func (l *LibraryElementService) getLibraryElements(c context.Context, store db.D
builder.Write(", ? as folder_name ", cmd.FolderName)
builder.Write(", '' as folder_uid ")
builder.Write(getFromLibraryElementDTOWithMeta(store.GetDialect()))
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.LibraryElements).Inc()
// nolint:staticcheck
writeParamSelectorSQL(&builder, append(params, Pair{"folder_id", cmd.FolderID})...)
builder.Write(" UNION ")
@ -314,6 +320,7 @@ func (l *LibraryElementService) getLibraryElements(c context.Context, store db.D
}
}
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.LibraryElements).Inc()
leDtos[i] = model.LibraryElementDTO{
ID: libraryElement.ID,
OrgID: libraryElement.OrgID,
@ -434,6 +441,7 @@ func (l *LibraryElementService) getAllLibraryElements(c context.Context, signedI
return err
}
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.LibraryElements).Inc()
retDTOs := make([]model.LibraryElementDTO, 0)
for _, element := range elements {
retDTOs = append(retDTOs, model.LibraryElementDTO{
@ -526,6 +534,7 @@ func (l *LibraryElementService) handleFolderIDPatches(ctx context.Context, eleme
if err := l.requireEditPermissionsOnFolder(ctx, user, fromFolderID); err != nil {
return err
}
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.LibraryElements).Inc()
// nolint:staticcheck
elementToPatch.FolderID = toFolderID
@ -573,6 +582,7 @@ func (l *LibraryElementService) patchLibraryElement(c context.Context, signedInU
}
}
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.LibraryElements).Inc()
var libraryElement = model.LibraryElement{
ID: elementInDB.ID,
OrgID: signedInUser.GetOrgID(),
@ -596,6 +606,7 @@ func (l *LibraryElementService) patchLibraryElement(c context.Context, signedInU
if cmd.Model == nil {
libraryElement.Model = elementInDB.Model
}
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.LibraryElements).Inc()
// nolint:staticcheck
if err := l.handleFolderIDPatches(c, &libraryElement, elementInDB.FolderID, cmd.FolderID, signedInUser); err != nil {
return err
@ -612,6 +623,7 @@ func (l *LibraryElementService) patchLibraryElement(c context.Context, signedInU
return model.ErrLibraryElementNotFound
}
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.LibraryElements).Inc()
dto = model.LibraryElementDTO{
ID: libraryElement.ID,
OrgID: libraryElement.OrgID,
@ -711,6 +723,7 @@ func (l *LibraryElementService) getElementsForDashboardID(c context.Context, das
return err
}
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.LibraryElements).Inc()
for _, element := range libraryElements {
libraryElementMap[element.UID] = model.LibraryElementDTO{
ID: element.ID,
@ -761,6 +774,7 @@ func (l *LibraryElementService) connectElementsToDashboardID(c context.Context,
if err != nil {
return err
}
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.LibraryElements).Inc()
// nolint:staticcheck
if err := l.requireViewPermissionsOnFolder(c, signedInUser, element.FolderID); err != nil {
return err

View File

@ -7,6 +7,7 @@ import (
"strings"
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/infra/metrics"
"github.com/grafana/grafana/pkg/services/libraryelements/model"
)
@ -84,6 +85,7 @@ func parseFolderFilter(query model.SearchLibraryElementsQuery) FolderFilter {
hasFolderFilter := len(strings.TrimSpace(query.FolderFilter)) > 0
hasFolderFilterUID := len(strings.TrimSpace(query.FolderFilterUIDs)) > 0
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.LibraryElements).Inc()
result := FolderFilter{
includeGeneralFolder: true,
folderIDs: folderIDs, // nolint:staticcheck
@ -99,6 +101,7 @@ func parseFolderFilter(query model.SearchLibraryElementsQuery) FolderFilter {
if hasFolderFilter {
result.includeGeneralFolder = false
folderIDs = strings.Split(query.FolderFilter, ",")
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.LibraryElements).Inc()
// nolint:staticcheck
result.folderIDs = folderIDs
for _, filter := range folderIDs {
@ -132,6 +135,7 @@ func parseFolderFilter(query model.SearchLibraryElementsQuery) FolderFilter {
func (f *FolderFilter) writeFolderFilterSQL(includeGeneral bool, builder *db.SQLBuilder) error {
var sql bytes.Buffer
params := make([]any, 0)
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.LibraryElements).Inc()
// nolint:staticcheck
for _, filter := range f.folderIDs {
folderID, err := strconv.ParseInt(filter, 10, 64)

View File

@ -9,6 +9,7 @@ import (
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/metrics"
"github.com/grafana/grafana/pkg/services/auth/identity"
"github.com/grafana/grafana/pkg/services/dashboards"
"github.com/grafana/grafana/pkg/services/folder"
@ -163,6 +164,7 @@ func importLibraryPanelsRecursively(c context.Context, service libraryelements.S
return err
}
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.LibraryPanels).Inc()
var cmd = model.CreateLibraryElementCommand{
FolderID: folderID, // nolint:staticcheck
Name: name,
@ -193,6 +195,7 @@ func (lps LibraryPanelService) CountInFolder(ctx context.Context, orgID int64, f
if err != nil {
return err
}
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.LibraryPanels).Inc()
// nolint:staticcheck
q := sess.Table("library_element").Where("org_id = ?", u.GetOrgID()).
Where("folder_id = ?", folder.ID).Where("kind = ?", int64(model.PanelElement))

View File

@ -10,6 +10,7 @@ import (
"strings"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/metrics"
"github.com/grafana/grafana/pkg/services/accesscontrol"
"github.com/grafana/grafana/pkg/services/auth/identity"
"github.com/grafana/grafana/pkg/services/dashboards"
@ -65,6 +66,7 @@ type migrationFolder struct {
func (sync *sync) migratedFolder(ctx context.Context, l log.Logger, dashboardUID string, folderID int64) (*migrationFolder, error) {
dashFolder, err := sync.getFolder(ctx, folderID)
if err != nil {
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.NGAlerts).Inc()
// nolint:staticcheck
l.Warn("Failed to find folder for dashboard", "missingFolderId", folderID, "error", err)
}
@ -86,6 +88,7 @@ func (sync *sync) migratedFolder(ctx context.Context, l log.Logger, dashboardUID
} else if folderID <= 0 && strings.HasPrefix(migratedFolder.Title, generalAlertingFolderTitle) {
du.warning = "dashboard alerts moved to general alerting folder during upgrade: general folder not supported"
} else if migratedFolder.ID != folderID { // nolint:staticcheck
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.NGAlerts).Inc()
du.warning = "dashboard alerts moved to new folder during upgrade: folder permission changes were needed"
}
@ -110,10 +113,12 @@ func (sync *sync) getOrCreateMigratedFolder(ctx context.Context, l log.Logger, d
// Check if the dashboard has custom permissions. If it does, we need to create a new folder for it.
// This folder will be cached for re-use for each dashboard in the folder with the same permissions.
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.NGAlerts).Inc()
// nolint:staticcheck
permissionsToFolder, ok := sync.permissionsMap[parentFolder.ID]
if !ok {
permissionsToFolder = make(map[permissionHash]*folder.Folder)
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.NGAlerts).Inc()
// nolint:staticcheck
sync.permissionsMap[parentFolder.ID] = permissionsToFolder

View File

@ -6,6 +6,7 @@ import (
"fmt"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/metrics"
legacymodels "github.com/grafana/grafana/pkg/services/alerting/models"
"github.com/grafana/grafana/pkg/services/dashboards"
migmodels "github.com/grafana/grafana/pkg/services/ngalert/migration/models"
@ -53,6 +54,7 @@ func (om *OrgMigration) migrateDashboard(ctx context.Context, dashID int64, aler
du := migmodels.NewDashboardUpgrade(dashID)
du.UID = dashboard.UID
du.Title = dashboard.Title
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.NGAlerts).Inc()
// nolint:staticcheck
du.FolderID = dashboard.FolderID

View File

@ -9,6 +9,7 @@ import (
"testing"
"time"
"github.com/grafana/grafana/pkg/infra/metrics"
"github.com/grafana/grafana/pkg/services/auth/identity"
"github.com/grafana/grafana/pkg/services/folder"
"github.com/grafana/grafana/pkg/services/ngalert/models"
@ -67,6 +68,7 @@ mainloop:
}
}
if existing == nil {
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.NGAlerts).Inc()
folders = append(folders, &folder.Folder{
ID: rand.Int63(), // nolint:staticcheck
UID: r.NamespaceUID,

View File

@ -6,6 +6,7 @@ import (
"fmt"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/metrics"
"github.com/grafana/grafana/pkg/services/dashboards"
"github.com/grafana/grafana/pkg/services/folder"
alert_models "github.com/grafana/grafana/pkg/services/ngalert/models"
@ -96,6 +97,7 @@ func (prov *defaultAlertRuleProvisioner) provisionRule(
func (prov *defaultAlertRuleProvisioner) getOrCreateFolderUID(
ctx context.Context, folderName string, orgID int64) (string, error) {
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Provisioning).Inc()
cmd := &dashboards.GetDashboardQuery{
Title: &folderName,
FolderID: util.Pointer(int64(0)), // nolint:staticcheck

View File

@ -13,6 +13,7 @@ import (
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/metrics"
"github.com/grafana/grafana/pkg/services/accesscontrol"
"github.com/grafana/grafana/pkg/services/dashboards"
"github.com/grafana/grafana/pkg/services/folder"
@ -247,6 +248,7 @@ func (fr *FileReader) saveDashboard(ctx context.Context, path string, folderID i
// keeps track of which UIDs and titles we have already provisioned
dash := jsonFile.dashboard
provisioningMetadata.uid = dash.Dashboard.UID
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Provisioning).Inc()
// nolint:staticcheck
provisioningMetadata.identity = dashboardIdentity{title: dash.Dashboard.Title, folderID: dash.Dashboard.FolderID}
@ -268,6 +270,7 @@ func (fr *FileReader) saveDashboard(ctx context.Context, path string, folderID i
}
if upToDate {
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Provisioning).Inc()
// nolint:staticcheck
fr.log.Debug("provisioned dashboard is up to date", "provisioner", fr.Cfg.Name, "file", path, "folderId", dash.Dashboard.FolderID, "folderUid", dash.Dashboard.FolderUID)
return provisioningMetadata, nil
@ -283,6 +286,7 @@ func (fr *FileReader) saveDashboard(ctx context.Context, path string, folderID i
}
if !fr.isDatabaseAccessRestricted() {
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Provisioning).Inc()
// nolint:staticcheck
fr.log.Debug("saving new dashboard", "provisioner", fr.Cfg.Name, "file", path, "folderId", dash.Dashboard.FolderID, "folderUid", dash.Dashboard.FolderUID)
dp := &dashboards.DashboardProvisioning{
@ -296,6 +300,7 @@ func (fr *FileReader) saveDashboard(ctx context.Context, path string, folderID i
return provisioningMetadata, err
}
} else {
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Provisioning).Inc()
// nolint:staticcheck
fr.log.Warn("Not saving new dashboard due to restricted database access", "provisioner", fr.Cfg.Name,
"file", path, "folderId", dash.Dashboard.FolderID)
@ -324,6 +329,7 @@ func (fr *FileReader) getOrCreateFolder(ctx context.Context, cfg *config, servic
return 0, "", ErrFolderNameMissing
}
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Provisioning).Inc()
cmd := &dashboards.GetDashboardQuery{
Title: &folderName,
FolderID: util.Pointer(int64(0)), // nolint:staticcheck
@ -352,6 +358,7 @@ func (fr *FileReader) getOrCreateFolder(ctx context.Context, cfg *config, servic
if err != nil {
return 0, "", err
}
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Provisioning).Inc()
// nolint:staticcheck
return f.ID, f.UID, nil
}

View File

@ -5,6 +5,7 @@ import (
"time"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/infra/metrics"
"github.com/grafana/grafana/pkg/services/dashboards"
"github.com/grafana/grafana/pkg/services/provisioning/values"
)
@ -63,6 +64,7 @@ func createDashboardJSON(data *simplejson.Json, lastModified time.Time, cfg *con
dash.Overwrite = true
dash.OrgID = cfg.OrgID
dash.Dashboard.OrgID = cfg.OrgID
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Provisioning).Inc()
// nolint:staticcheck
dash.Dashboard.FolderID = folderID
dash.Dashboard.FolderUID = folderUID

View File

@ -5,6 +5,7 @@ import (
"time"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/metrics"
)
type duplicate struct {
@ -102,6 +103,7 @@ func (c *duplicateValidator) logWarnings(duplicatesByOrg map[int64]duplicateEntr
for id, usage := range duplicates.Titles {
if usage.Sum > 1 {
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Provisioning).Inc()
// nolint:staticcheck
c.logger.Warn("dashboard title is not unique in folder", "orgId", orgID, "title", id.title, "folderID", id.folderID, "times",
usage.Sum, "providers", keysToSlice(usage.InvolvedReaders))

View File

@ -10,6 +10,7 @@ import (
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana/pkg/api/dtos"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/metrics"
"github.com/grafana/grafana/pkg/services/accesscontrol"
"github.com/grafana/grafana/pkg/services/annotations"
"github.com/grafana/grafana/pkg/services/dashboards"
@ -74,6 +75,7 @@ func (pd *PublicDashboardServiceImpl) GetPublicDashboardForView(ctx context.Cont
return nil, err
}
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.PublicDashboards).Inc()
meta := dtos.DashboardMeta{
Slug: dash.Slug,
Type: dashboards.DashTypeDB,

View File

@ -5,6 +5,7 @@ import (
"sort"
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/infra/metrics"
"github.com/grafana/grafana/pkg/services/dashboards"
"github.com/grafana/grafana/pkg/services/dashboards/dashboardaccess"
"github.com/grafana/grafana/pkg/services/search/model"
@ -79,6 +80,7 @@ func (s *SearchService) SearchHandler(ctx context.Context, query *Query) (model.
}
}
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Search).Inc()
dashboardQuery := dashboards.FindPersistedDashboardsQuery{
Title: query.Title,
SignedInUser: query.SignedInUser,