diff --git a/pkg/api/alerting.go b/pkg/api/alerting.go index 6cc3a5c91ee..3deb8f84474 100644 --- a/pkg/api/alerting.go +++ b/pkg/api/alerting.go @@ -9,6 +9,7 @@ import ( "github.com/grafana/grafana/pkg/api/dtos" "github.com/grafana/grafana/pkg/api/response" + "github.com/grafana/grafana/pkg/infra/metrics" "github.com/grafana/grafana/pkg/services/alerting" alertmodels "github.com/grafana/grafana/pkg/services/alerting/models" contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model" @@ -101,6 +102,7 @@ func (hs *HTTPServer) GetAlerts(c *contextmodel.ReqContext) response.Response { folderID, err := strconv.ParseInt(id, 10, 64) if err == nil { folderIDs = append(folderIDs, folderID) + metrics.MFolderIDsAPICount.WithLabelValues(metrics.GetAlerts).Inc() } } diff --git a/pkg/api/dashboard.go b/pkg/api/dashboard.go index 6e38f441f16..9c9fef5bf1a 100644 --- a/pkg/api/dashboard.go +++ b/pkg/api/dashboard.go @@ -172,12 +172,13 @@ func (hs *HTTPServer) GetDashboard(c *contextmodel.ReqContext) response.Response AnnotationsPermissions: annotationPermissions, PublicDashboardEnabled: publicDashboardEnabled, } - + metrics.MFolderIDsAPICount.WithLabelValues(metrics.GetDashboard).Inc() // lookup folder title // nolint:staticcheck if dash.FolderID > 0 { // nolint:staticcheck query := dashboards.GetDashboardQuery{ID: dash.FolderID, OrgID: c.SignedInUser.GetOrgID()} + metrics.MFolderIDsAPICount.WithLabelValues(metrics.GetDashboard).Inc() queryResult, err := hs.DashboardService.GetDashboard(c.Req.Context(), &query) if err != nil { if errors.Is(err, dashboards.ErrFolderNotFound) { @@ -974,6 +975,7 @@ func (hs *HTTPServer) RestoreDashboardVersion(c *contextmodel.ReqContext) respon saveCmd.Message = fmt.Sprintf("Restored from version %d", version.Version) // nolint:staticcheck saveCmd.FolderID = dash.FolderID + metrics.MFolderIDsAPICount.WithLabelValues(metrics.RestoreDashboardVersion).Inc() saveCmd.FolderUID = dash.FolderUID return hs.postDashboard(c, saveCmd) diff --git a/pkg/api/dashboard_permission.go b/pkg/api/dashboard_permission.go index 88e6cb826ab..dcd3469c9e4 100644 --- a/pkg/api/dashboard_permission.go +++ b/pkg/api/dashboard_permission.go @@ -8,6 +8,7 @@ import ( "github.com/grafana/grafana/pkg/api/dtos" "github.com/grafana/grafana/pkg/api/response" + "github.com/grafana/grafana/pkg/infra/metrics" "github.com/grafana/grafana/pkg/services/accesscontrol" "github.com/grafana/grafana/pkg/services/auth/identity" contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model" @@ -193,6 +194,7 @@ func (hs *HTTPServer) getDashboardACL(ctx context.Context, user identity.Request permission := dashboardPermissionMap[hs.dashboardPermissionsService.MapActions(p)] + metrics.MFolderIDsAPICount.WithLabelValues(metrics.GetDashboardACL).Inc() acl = append(acl, &dashboards.DashboardACLInfoDTO{ OrgID: dashboard.OrgID, DashboardID: dashboard.ID, diff --git a/pkg/api/folder.go b/pkg/api/folder.go index 2d1d69ec8f4..cdcd42419e7 100644 --- a/pkg/api/folder.go +++ b/pkg/api/folder.go @@ -9,6 +9,7 @@ import ( "github.com/grafana/grafana/pkg/api/apierrors" "github.com/grafana/grafana/pkg/api/dtos" "github.com/grafana/grafana/pkg/api/response" + "github.com/grafana/grafana/pkg/infra/metrics" "github.com/grafana/grafana/pkg/services/accesscontrol" "github.com/grafana/grafana/pkg/services/auth/identity" contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model" @@ -64,6 +65,7 @@ func (hs *HTTPServer) GetFolders(c *contextmodel.ReqContext) response.Response { Title: f.Title, ParentUID: f.ParentUID, }) + metrics.MFolderIDsAPICount.WithLabelValues("GetFolders").Inc() } return response.JSON(http.StatusOK, hits) @@ -122,6 +124,7 @@ func (hs *HTTPServer) GetFolderByID(c *contextmodel.ReqContext) response.Respons if err != nil { return response.Error(http.StatusBadRequest, "id is invalid", err) } + metrics.MFolderIDsAPICount.WithLabelValues("GetFolderByID").Inc() // nolint:staticcheck folder, err := hs.folderService.Get(c.Req.Context(), &folder.GetFolderQuery{ID: &id, OrgID: c.SignedInUser.GetOrgID(), SignedInUser: c.SignedInUser}) if err != nil { @@ -369,7 +372,7 @@ func (hs *HTTPServer) newToFolderDto(c *contextmodel.ReqContext, f *folder.Folde }, nil } } - + metrics.MFolderIDsAPICount.WithLabelValues("newToFolderDto").Inc() return dtos.Folder{ ID: f.ID, // nolint:staticcheck UID: f.UID, @@ -469,6 +472,7 @@ func (hs *HTTPServer) searchFolders(c *contextmodel.ReqContext) ([]dtos.FolderSe UID: hit.UID, Title: hit.Title, }) + metrics.MFolderIDsAPICount.WithLabelValues("searchFolders").Inc() } return folderHits, nil diff --git a/pkg/api/folder_permission.go b/pkg/api/folder_permission.go index 08f6f76ad21..1b322cd3b04 100644 --- a/pkg/api/folder_permission.go +++ b/pkg/api/folder_permission.go @@ -8,6 +8,7 @@ import ( "github.com/grafana/grafana/pkg/api/apierrors" "github.com/grafana/grafana/pkg/api/dtos" "github.com/grafana/grafana/pkg/api/response" + "github.com/grafana/grafana/pkg/infra/metrics" "github.com/grafana/grafana/pkg/services/auth/identity" contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model" "github.com/grafana/grafana/pkg/services/dashboards" @@ -45,7 +46,7 @@ func (hs *HTTPServer) GetFolderPermissionList(c *contextmodel.ReqContext) respon if perm.UserID > 0 && dtos.IsHiddenUser(perm.UserLogin, c.SignedInUser, hs.Cfg) { continue } - + metrics.MFolderIDsAPICount.WithLabelValues(metrics.GetFolderPermissionList).Inc() // nolint:staticcheck perm.FolderID = folder.ID perm.DashboardID = 0 @@ -103,6 +104,7 @@ func (hs *HTTPServer) UpdateFolderPermissions(c *contextmodel.ReqContext) respon Created: time.Now(), Updated: time.Now(), }) + metrics.MFolderIDsAPICount.WithLabelValues(metrics.UpdateFolderPermissions).Inc() } acl, err := hs.getFolderACL(c.Req.Context(), c.SignedInUser, folder) @@ -166,6 +168,7 @@ func (hs *HTTPServer) getFolderACL(ctx context.Context, user identity.Requester, IsFolder: true, Inherited: false, }) + metrics.MFolderIDsAPICount.WithLabelValues(metrics.GetFolderPermissionList).Inc() } return acl, nil diff --git a/pkg/api/search.go b/pkg/api/search.go index 0ff77b123f9..87f48c4c8cf 100644 --- a/pkg/api/search.go +++ b/pkg/api/search.go @@ -57,6 +57,7 @@ func (hs *HTTPServer) Search(c *contextmodel.ReqContext) response.Response { folderID, err := strconv.ParseInt(id, 10, 64) if err == nil { folderIDs = append(folderIDs, folderID) + metrics.MFolderIDsAPICount.WithLabelValues(metrics.Search).Inc() } } diff --git a/pkg/infra/metrics/metrics.go b/pkg/infra/metrics/metrics.go index 4dd89888b0c..f66456f1c50 100644 --- a/pkg/infra/metrics/metrics.go +++ b/pkg/infra/metrics/metrics.go @@ -113,6 +113,9 @@ var ( // MPublicDashboardDatasourceQuerySuccess is a metric counter for successful queries labelled by datasource MPublicDashboardDatasourceQuerySuccess *prometheus.CounterVec + + // MFolderIDsAPICount is a metric counter for folder ids count in the api package + MFolderIDsAPICount *prometheus.CounterVec ) // Timers @@ -216,9 +219,24 @@ var ( MStatTotalCorrelations prometheus.Gauge ) +const ( + GetAlerts string = "GetAlerts" + GetDashboard string = "GetDashboard" + RestoreDashboardVersion string = "RestoreDashboardVersion" + GetFolderByID string = "GetFolderByID" + GetFolderDescendantCounts string = "GetFolderDescendantCounts" + SearchFolders string = "searchFolders" + GetFolderPermissionList string = "GetFolderPermissionList" + UpdateFolderPermissions string = "UpdateFolderPermissions" + GetFolderACL string = "getFolderACL" + Search string = "Search" + GetDashboardACL string = "getDashboardACL" +) + 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} MInstanceStart = prometheus.NewCounter(prometheus.CounterOpts{ Name: "instance_start_total", @@ -456,6 +474,12 @@ func init() { Namespace: ExporterName, }, []string{"datasource", "status"}, map[string][]string{"status": pubdash.QueryResultStatuses}) + MFolderIDsAPICount = metricutil.NewCounterVecStartingAtZero(prometheus.CounterOpts{ + Name: "folder_id_api_count", + Help: "counter for folder id usage in api package", + Namespace: ExporterName, + }, []string{"method"}, map[string][]string{"method": apiFolderIDMethods}) + MStatTotalDashboards = prometheus.NewGauge(prometheus.GaugeOpts{ Name: "stat_totals_dashboard", Help: "total amount of dashboards", @@ -747,5 +771,6 @@ func initMetricVars(reg prometheus.Registerer) { MPublicDashboardRequestCount, MPublicDashboardDatasourceQuerySuccess, MStatTotalCorrelations, + MFolderIDsAPICount, ) }