Skip to content

Commit 7e5544a

Browse files
authored
Add MFolderIDsServiceCount to count folderIDs in services pkg (grafana#81237)
1 parent 0d66ad6 commit 7e5544a

File tree

25 files changed

+129
-3
lines changed

25 files changed

+129
-3
lines changed

pkg/infra/metrics/metrics.go

+22
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ var (
116116

117117
// MFolderIDsAPICount is a metric counter for folder ids count in the api package
118118
MFolderIDsAPICount *prometheus.CounterVec
119+
120+
// MFolderIDsServicesCount is a metric counter for folder ids count in the services package
121+
MFolderIDsServiceCount *prometheus.CounterVec
119122
)
120123

121124
// Timers
@@ -220,6 +223,7 @@ var (
220223
)
221224

222225
const (
226+
// FolderID API
223227
GetAlerts string = "GetAlerts"
224228
GetDashboard string = "GetDashboard"
225229
RestoreDashboardVersion string = "RestoreDashboardVersion"
@@ -231,12 +235,23 @@ const (
231235
GetFolderACL string = "getFolderACL"
232236
Search string = "Search"
233237
GetDashboardACL string = "getDashboardACL"
238+
// FolderID services
239+
Folder string = "folder"
240+
Dashboard string = "dashboards"
241+
LibraryElements string = "libraryelements"
242+
LibraryPanels string = "librarypanels"
243+
NGAlerts string = "ngalert"
244+
Provisioning string = "provisioning"
245+
PublicDashboards string = "publicdashboards"
246+
AccessControl string = "accesscontrol"
247+
Guardian string = "guardian"
234248
)
235249

236250
func init() {
237251
httpStatusCodes := []string{"200", "404", "500", "unknown"}
238252
objectiveMap := map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001}
239253
apiFolderIDMethods := []string{GetAlerts, GetDashboard, RestoreDashboardVersion, GetFolderByID, GetFolderDescendantCounts, SearchFolders, GetFolderPermissionList, UpdateFolderPermissions, GetFolderACL, Search, GetDashboardACL}
254+
serviceFolderIDMethods := []string{Folder, Dashboard, LibraryElements, LibraryPanels, NGAlerts, Provisioning, PublicDashboards, AccessControl, Guardian, Search}
240255

241256
MInstanceStart = prometheus.NewCounter(prometheus.CounterOpts{
242257
Name: "instance_start_total",
@@ -480,6 +495,12 @@ func init() {
480495
Namespace: ExporterName,
481496
}, []string{"method"}, map[string][]string{"method": apiFolderIDMethods})
482497

498+
MFolderIDsServiceCount = metricutil.NewCounterVecStartingAtZero(prometheus.CounterOpts{
499+
Name: "folder_id_service_count",
500+
Help: "counter for folder id usage in service package",
501+
Namespace: ExporterName,
502+
}, []string{"method"}, map[string][]string{"method": serviceFolderIDMethods})
503+
483504
MStatTotalDashboards = prometheus.NewGauge(prometheus.GaugeOpts{
484505
Name: "stat_totals_dashboard",
485506
Help: "total amount of dashboards",
@@ -772,5 +793,6 @@ func initMetricVars(reg prometheus.Registerer) {
772793
MPublicDashboardDatasourceQuerySuccess,
773794
MStatTotalCorrelations,
774795
MFolderIDsAPICount,
796+
MFolderIDsServiceCount,
775797
)
776798
}

pkg/services/accesscontrol/ossaccesscontrol/permissions_services.go

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/grafana/grafana/pkg/api/routing"
1010
"github.com/grafana/grafana/pkg/infra/db"
11+
"github.com/grafana/grafana/pkg/infra/metrics"
1112
"github.com/grafana/grafana/pkg/services/accesscontrol"
1213
"github.com/grafana/grafana/pkg/services/accesscontrol/resourcepermissions"
1314
"github.com/grafana/grafana/pkg/services/auth/identity"
@@ -151,6 +152,7 @@ func ProvideDashboardPermissions(
151152
if err != nil {
152153
return nil, err
153154
}
155+
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.AccessControl).Inc()
154156
// nolint:staticcheck
155157
if dashboard.FolderID > 0 {
156158
query := &dashboards.GetDashboardQuery{ID: dashboard.FolderID, OrgID: orgID}

pkg/services/dashboardimport/service/service.go

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/grafana/grafana/pkg/api/routing"
77
"github.com/grafana/grafana/pkg/components/simplejson"
8+
"github.com/grafana/grafana/pkg/infra/metrics"
89
"github.com/grafana/grafana/pkg/services/accesscontrol"
910
"github.com/grafana/grafana/pkg/services/auth/identity"
1011
"github.com/grafana/grafana/pkg/services/dashboardimport"
@@ -83,6 +84,7 @@ func (s *ImportDashboardService) ImportDashboard(ctx context.Context, req *dashb
8384
generatedDash.Del("__inputs")
8485
generatedDash.Del("__requires")
8586

87+
metrics.MFolderIDsServiceCount.WithLabelValues("dashboardimport").Inc()
8688
// here we need to get FolderId from FolderUID if it present in the request, if both exist, FolderUID would overwrite FolderID
8789
if req.FolderUid != "" {
8890
folder, err := s.folderService.Get(ctx, &folder.GetFolderQuery{
@@ -137,6 +139,7 @@ func (s *ImportDashboardService) ImportDashboard(ctx context.Context, req *dashb
137139
return nil, err
138140
}
139141

142+
metrics.MFolderIDsServiceCount.WithLabelValues("dashboardimport").Inc()
140143
// nolint:staticcheck
141144
err = s.libraryPanelService.ImportLibraryPanelsForDashboard(ctx, req.User, libraryElements, generatedDash.Get("panels").MustArray(), req.FolderId)
142145
if err != nil {
@@ -149,6 +152,7 @@ func (s *ImportDashboardService) ImportDashboard(ctx context.Context, req *dashb
149152
}
150153

151154
revision := savedDashboard.Data.Get("revision").MustInt64(0)
155+
metrics.MFolderIDsServiceCount.WithLabelValues("dashboardimport").Inc()
152156
return &dashboardimport.ImportDashboardResponse{
153157
UID: savedDashboard.UID,
154158
PluginId: req.PluginId,

pkg/services/dashboards/accesscontrol.go

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"strings"
66

7+
"github.com/grafana/grafana/pkg/infra/metrics"
78
ac "github.com/grafana/grafana/pkg/services/accesscontrol"
89
"github.com/grafana/grafana/pkg/services/folder"
910
)
@@ -168,11 +169,13 @@ func NewDashboardUIDScopeResolver(folderDB folder.FolderStore, ds DashboardServi
168169

169170
func resolveDashboardScope(ctx context.Context, folderDB folder.FolderStore, orgID int64, dashboard *Dashboard, folderSvc folder.Service) ([]string, error) {
170171
var folderUID string
172+
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Dashboard).Inc()
171173
// nolint:staticcheck
172174
if dashboard.FolderID < 0 {
173175
return []string{ScopeDashboardsProvider.GetResourceScopeUID(dashboard.UID)}, nil
174176
}
175177

178+
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Dashboard).Inc()
176179
// nolint:staticcheck
177180
if dashboard.FolderID == 0 {
178181
folderUID = ac.GeneralFolderUID

pkg/services/dashboards/database/database.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ func getExistingDashboardByTitleAndFolder(sess *db.Session, dash *dashboards.Das
367367
return isParentFolderChanged, dashboards.ErrDashboardFolderWithSameNameAsDashboard
368368
}
369369

370+
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Dashboard).Inc()
370371
// nolint:staticcheck
371372
if !dash.IsFolder && (dash.FolderID != existing.FolderID || dash.ID == 0) {
372373
isParentFolderChanged = true
@@ -818,6 +819,7 @@ func (d *dashboardStore) deleteAlertDefinition(dashboardId int64, sess *db.Sessi
818819
func (d *dashboardStore) GetDashboard(ctx context.Context, query *dashboards.GetDashboardQuery) (*dashboards.Dashboard, error) {
819820
var queryResult *dashboards.Dashboard
820821
err := d.store.WithDbSession(ctx, func(sess *db.Session) error {
822+
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Dashboard).Inc()
821823
// nolint:staticcheck
822824
if query.ID == 0 && len(query.UID) == 0 && (query.Title == nil || (query.FolderID == nil && query.FolderUID == "")) {
823825
return dashboards.ErrDashboardIdentifierNotSet
@@ -837,6 +839,7 @@ func (d *dashboardStore) GetDashboard(ctx context.Context, query *dashboards.Get
837839
// nolint:staticcheck
838840
dashboard.FolderID = *query.FolderID
839841
mustCols = append(mustCols, "folder_id")
842+
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Dashboard).Inc()
840843
}
841844

842845
has, err := sess.MustCols(mustCols...).Get(&dashboard)
@@ -940,7 +943,7 @@ func (d *dashboardStore) FindDashboards(ctx context.Context, query *dashboards.F
940943
if len(query.Type) > 0 {
941944
filters = append(filters, searchstore.TypeFilter{Dialect: d.store.GetDialect(), Type: query.Type})
942945
}
943-
946+
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Dashboard).Inc()
944947
// nolint:staticcheck
945948
if len(query.FolderIds) > 0 {
946949
filters = append(filters, searchstore.FolderFilter{IDs: query.FolderIds})
@@ -1013,6 +1016,7 @@ func (d *dashboardStore) CountDashboardsInFolder(
10131016
var count int64
10141017
var err error
10151018
err = d.store.WithDbSession(ctx, func(sess *db.Session) error {
1019+
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Dashboard).Inc()
10161020
// nolint:staticcheck
10171021
session := sess.In("folder_id", req.FolderID).In("org_id", req.OrgID).
10181022
In("is_folder", d.store.GetDialect().BooleanStr(false))

pkg/services/dashboards/models.go

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"time"
66

77
"github.com/grafana/grafana/pkg/components/simplejson"
8+
"github.com/grafana/grafana/pkg/infra/metrics"
89
"github.com/grafana/grafana/pkg/infra/slugify"
910
"github.com/grafana/grafana/pkg/models"
1011
"github.com/grafana/grafana/pkg/services/auth/identity"
@@ -136,6 +137,7 @@ func (cmd *SaveDashboardCommand) GetDashboardModel() *Dashboard {
136137
dash.OrgID = cmd.OrgID
137138
dash.PluginID = cmd.PluginID
138139
dash.IsFolder = cmd.IsFolder
140+
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Dashboard).Inc()
139141
// nolint:staticcheck
140142
dash.FolderID = cmd.FolderID
141143
dash.FolderUID = cmd.FolderUID
@@ -326,6 +328,7 @@ type CountDashboardsInFolderRequest struct {
326328
}
327329

328330
func FromDashboard(dash *Dashboard) *folder.Folder {
331+
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Dashboard).Inc()
329332
return &folder.Folder{
330333
ID: dash.ID, // nolint:staticcheck
331334
UID: dash.UID,

pkg/services/dashboards/service/dashboard_service.go

+11
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/grafana/grafana-plugin-sdk-go/backend/gtime"
1414

1515
"github.com/grafana/grafana/pkg/infra/log"
16+
"github.com/grafana/grafana/pkg/infra/metrics"
1617
"github.com/grafana/grafana/pkg/services/accesscontrol"
1718
"github.com/grafana/grafana/pkg/services/alerting"
1819
"github.com/grafana/grafana/pkg/services/auth/identity"
@@ -114,6 +115,7 @@ func (dr *DashboardServiceImpl) BuildSaveDashboardCommand(ctx context.Context, d
114115
return nil, dashboards.ErrDashboardTitleEmpty
115116
}
116117

118+
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Dashboard).Inc()
117119
// nolint:staticcheck
118120
if dash.IsFolder && dash.FolderID > 0 {
119121
return nil, dashboards.ErrDashboardFolderCannotHaveParent
@@ -146,9 +148,11 @@ func (dr *DashboardServiceImpl) BuildSaveDashboardCommand(ctx context.Context, d
146148
if err != nil {
147149
return nil, err
148150
}
151+
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Dashboard).Inc()
149152
// nolint:staticcheck
150153
dash.FolderID = folder.ID
151154
} else if dash.FolderID != 0 { // nolint:staticcheck
155+
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Dashboard).Inc()
152156
// nolint:staticcheck
153157
folder, err := dr.folderStore.GetFolderByID(ctx, dash.OrgID, dash.FolderID)
154158
if err != nil {
@@ -168,6 +172,7 @@ func (dr *DashboardServiceImpl) BuildSaveDashboardCommand(ctx context.Context, d
168172
if err != nil {
169173
return nil, err
170174
}
175+
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Dashboard).Inc()
171176
// nolint:staticcheck
172177
if canSave, err := guardian.CanCreate(dash.FolderID, dash.IsFolder); err != nil || !canSave {
173178
if err != nil {
@@ -194,6 +199,7 @@ func (dr *DashboardServiceImpl) BuildSaveDashboardCommand(ctx context.Context, d
194199
}
195200

196201
if dash.ID == 0 {
202+
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Dashboard).Inc()
197203
// nolint:staticcheck
198204
if canCreate, err := guard.CanCreate(dash.FolderID, dash.IsFolder); err != nil || !canCreate {
199205
if err != nil {
@@ -215,6 +221,7 @@ func (dr *DashboardServiceImpl) BuildSaveDashboardCommand(ctx context.Context, d
215221
return nil, err
216222
}
217223

224+
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Dashboard).Inc()
218225
cmd := &dashboards.SaveDashboardCommand{
219226
Dashboard: dash.Data,
220227
Message: dto.Message,
@@ -260,6 +267,7 @@ func getGuardianForSavePermissionCheck(ctx context.Context, d *dashboards.Dashbo
260267

261268
if newDashboard {
262269
// if it's a new dashboard/folder check the parent folder permissions
270+
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Dashboard).Inc()
263271
// nolint:staticcheck
264272
guard, err := guardian.New(ctx, d.FolderID, d.OrgID, user)
265273
if err != nil {
@@ -473,6 +481,7 @@ func (dr *DashboardServiceImpl) GetDashboardsByPluginID(ctx context.Context, que
473481
}
474482

475483
func (dr *DashboardServiceImpl) setDefaultPermissions(ctx context.Context, dto *dashboards.SaveDashboardDTO, dash *dashboards.Dashboard, provisioned bool) {
484+
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Dashboard).Inc()
476485
// nolint:staticcheck
477486
inFolder := dash.FolderID > 0
478487
var permissions []accesscontrol.SetResourcePermissionCommand
@@ -686,6 +695,7 @@ func makeQueryResult(query *dashboards.FindPersistedDashboardsQuery, res []dashb
686695
for _, item := range res {
687696
hit, exists := hits[item.ID]
688697
if !exists {
698+
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Dashboard).Inc()
689699
hit = &model.Hit{
690700
ID: item.ID,
691701
UID: item.UID,
@@ -729,6 +739,7 @@ func (dr DashboardServiceImpl) CountInFolder(ctx context.Context, orgID int64, f
729739
return 0, err
730740
}
731741

742+
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Dashboard).Inc()
732743
// nolint:staticcheck
733744
return dr.dashboardStore.CountDashboardsInFolder(ctx, &dashboards.CountDashboardsInFolderRequest{FolderID: folder.ID, OrgID: orgID})
734745
}

pkg/services/folder/folderimpl/dashboard_folder_store.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"strings"
66

77
"github.com/grafana/grafana/pkg/infra/db"
8+
"github.com/grafana/grafana/pkg/infra/metrics"
89
"github.com/grafana/grafana/pkg/services/dashboards"
910
"github.com/grafana/grafana/pkg/services/folder"
1011
)
@@ -26,6 +27,7 @@ func (d *DashboardFolderStoreImpl) GetFolderByTitle(ctx context.Context, orgID i
2627

2728
// there is a unique constraint on org_id, folder_uid, title
2829
// there are no nested folders so the parent folder id is always 0
30+
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Folder).Inc()
2931
// nolint:staticcheck
3032
dashboard := dashboards.Dashboard{OrgID: orgID, FolderID: 0, Title: title}
3133
err := d.store.WithTransactionalDbSession(ctx, func(sess *db.Session) error {
@@ -50,6 +52,7 @@ func (d *DashboardFolderStoreImpl) GetFolderByTitle(ctx context.Context, orgID i
5052
}
5153

5254
func (d *DashboardFolderStoreImpl) GetFolderByID(ctx context.Context, orgID int64, id int64) (*folder.Folder, error) {
55+
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Folder).Inc()
5356
// nolint:staticcheck
5457
dashboard := dashboards.Dashboard{OrgID: orgID, FolderID: 0, ID: id}
5558
err := d.store.WithTransactionalDbSession(ctx, func(sess *db.Session) error {
@@ -74,7 +77,7 @@ func (d *DashboardFolderStoreImpl) GetFolderByUID(ctx context.Context, orgID int
7477
if uid == "" {
7578
return nil, dashboards.ErrDashboardIdentifierNotSet
7679
}
77-
80+
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Folder).Inc()
7881
// nolint:staticcheck
7982
dashboard := dashboards.Dashboard{OrgID: orgID, FolderID: 0, UID: uid}
8083
err := d.store.WithTransactionalDbSession(ctx, func(sess *db.Session) error {

0 commit comments

Comments
 (0)