Skip to content

Commit

Permalink
add additional latency metrics for few service calls
Browse files Browse the repository at this point in the history
Signed-off-by: Kush Sharma <thekushsharma@gmail.com>
  • Loading branch information
kushsharma committed Feb 7, 2025
1 parent 39b21b9 commit 7b51f57
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
6 changes: 6 additions & 0 deletions core/authenticate/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions core/organization/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions internal/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
func Init() {
initStripe()
initDB()
initService()
}

type HistogramFunc func(labelValue ...string) func()
Expand Down
16 changes: 16 additions & 0 deletions internal/metrics/service.go
Original file line number Diff line number Diff line change
@@ -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_method_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"}))
}

0 comments on commit 7b51f57

Please sign in to comment.