Add MFolderIDsAPICount metric to count FolderIDs in api package (#80866)

* Add MFolderIDsAPICount metric to cound FolderIDs in api package

* Change counter to counter vector with method names as string values
This commit is contained in:
idafurjes 2024-01-24 12:39:11 +01:00 committed by GitHub
parent 5576731332
commit 6b4eaa0d18
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 42 additions and 3 deletions

View File

@ -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()
}
}

View File

@ -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)

View File

@ -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,

View File

@ -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

View File

@ -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

View File

@ -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()
}
}

View File

@ -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,
)
}