diff --git a/core/authenticate/service.go b/core/authenticate/service.go index c32a08457..cd49e4a7c 100644 --- a/core/authenticate/service.go +++ b/core/authenticate/service.go @@ -23,6 +23,7 @@ import ( frontiersession "github.com/raystack/frontier/core/authenticate/session" "github.com/raystack/frontier/core/serviceuser" "github.com/raystack/frontier/internal/bootstrap/schema" + "github.com/raystack/frontier/internal/metrics" "github.com/raystack/frontier/pkg/errors" "github.com/lestrrat-go/jwx/v2/jwk" @@ -739,6 +740,11 @@ func (s Service) getOrCreateUser(ctx context.Context, email, title string) (user } func (s Service) GetPrincipal(ctx context.Context, assertions ...ClientAssertion) (Principal, error) { + if metrics.ServiceOprLatency != nil { + promCollect := metrics.ServiceOprLatency("authenticate", "GetPrincipal") + defer promCollect() + } + var currentPrincipal Principal if len(assertions) == 0 { // check all assertions diff --git a/core/organization/service.go b/core/organization/service.go index a81b491e8..17bd9c00c 100644 --- a/core/organization/service.go +++ b/core/organization/service.go @@ -18,6 +18,7 @@ import ( "github.com/raystack/frontier/core/relation" "github.com/raystack/frontier/core/user" "github.com/raystack/frontier/internal/bootstrap/schema" + "github.com/raystack/frontier/internal/metrics" ) type Repository interface { @@ -229,6 +230,11 @@ func (s Service) Update(ctx context.Context, org Organization) (Organization, er } func (s Service) ListByUser(ctx context.Context, principal authenticate.Principal, filter Filter) ([]Organization, error) { + if metrics.ServiceOprLatency != nil { + promCollect := metrics.ServiceOprLatency("organization", "ListByUser") + defer promCollect() + } + subjectIDs, err := s.relationService.LookupResources(ctx, relation.Relation{ Object: relation.Object{ Namespace: schema.OrganizationNamespace, diff --git a/internal/metrics/metrics.go b/internal/metrics/metrics.go index 6745137c4..92d48ee41 100644 --- a/internal/metrics/metrics.go +++ b/internal/metrics/metrics.go @@ -9,6 +9,7 @@ import ( func Init() { initStripe() initDB() + initService() } type HistogramFunc func(labelValue ...string) func() diff --git a/internal/metrics/service.go b/internal/metrics/service.go new file mode 100644 index 000000000..b119cc108 --- /dev/null +++ b/internal/metrics/service.go @@ -0,0 +1,16 @@ +package metrics + +import ( + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" +) + +var ServiceOprLatency HistogramFunc + +func initService() { + ServiceOprLatency = createMeasureTime(promauto.NewHistogramVec(prometheus.HistogramOpts{ + Name: "service_operation_latency", + Help: "Time taken for service operation to complete", + Buckets: []float64{.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10, 30, 60}, + }, []string{"service", "operation"})) +}