mirror of
https://github.com/grafana/grafana.git
synced 2025-02-14 17:43:35 -06:00
* Plugin: Remove external service on plugin removal * Early exit no service account * Add log * WIP * Cable OAuth2Server client removal * Move function lower * Add function to test removal * Add test to RemoveExternalService * Test RemoveExtSvcAccount * remove apostrophy in comment * Add cfg to plugin installer to check features * Add feature flag check in the service registration service * Comments * Move metrics Inc * Initialize map * Reorder * Initialize mutex as well * Add HasExternalService as suggested * WIP: CleanUpOrphanedExternalServices * Commit suggestion Co-authored-by: linoman <2051016+linoman@users.noreply.github.com> * Nit on test. Co-authored-by: linoman <2051016+linoman@users.noreply.github.com> * oauthserver return names * Name is not Slug * Use plugin ID not slug * Add background job * remove negation on feature check * Add test to the CleanUp function * Test GetExternalServiceNames * rename test * Add test for ExtSvcAccountsService_GetExternalServiceNames * Add a todo * Add todo * Option based on mix * Rewrite a bit the comment * Opinionated choice use slugs instead of names everywhere * Nit. * Comments and re-ordering * Comment * Add log * Add context --------- Co-authored-by: linoman <2051016+linoman@users.noreply.github.com>
63 lines
1.7 KiB
Go
63 lines
1.7 KiB
Go
package extsvcaccounts
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
|
"github.com/grafana/grafana/pkg/services/extsvcauth"
|
|
"github.com/grafana/grafana/pkg/services/serviceaccounts"
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
)
|
|
|
|
type metrics struct {
|
|
storedCount prometheus.GaugeFunc
|
|
savedCount prometheus.Counter
|
|
deletedCount prometheus.Counter
|
|
}
|
|
|
|
func newMetrics(reg prometheus.Registerer, saSvc serviceaccounts.Service, logger log.Logger) *metrics {
|
|
var m metrics
|
|
|
|
m.storedCount = prometheus.NewGaugeFunc(
|
|
prometheus.GaugeOpts{
|
|
Namespace: metricsNamespace,
|
|
Name: "extsvc_total",
|
|
Help: "Number of external service accounts in store",
|
|
},
|
|
func() float64 {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
res, err := saSvc.SearchOrgServiceAccounts(ctx, &serviceaccounts.SearchOrgServiceAccountsQuery{
|
|
OrgID: extsvcauth.TmpOrgID,
|
|
Filter: serviceaccounts.FilterOnlyExternal,
|
|
CountOnly: true,
|
|
SignedInUser: extsvcuser,
|
|
})
|
|
if err != nil {
|
|
logger.Error("Could not compute extsvc_total metric", "error", err)
|
|
return 0.0
|
|
}
|
|
return float64(res.TotalCount)
|
|
},
|
|
)
|
|
m.savedCount = prometheus.NewCounter(prometheus.CounterOpts{
|
|
Namespace: metricsNamespace,
|
|
Name: "extsvc_saved_total",
|
|
Help: "Number of external service accounts saved since start up.",
|
|
})
|
|
m.deletedCount = prometheus.NewCounter(prometheus.CounterOpts{
|
|
Namespace: metricsNamespace,
|
|
Name: "extsvc_deleted_total",
|
|
Help: "Number of external service accounts deleted since start up.",
|
|
})
|
|
|
|
if reg != nil {
|
|
reg.MustRegister(m.storedCount)
|
|
reg.MustRegister(m.savedCount)
|
|
reg.MustRegister(m.deletedCount)
|
|
}
|
|
|
|
return &m
|
|
}
|