From c4a9091751aa273b1865359532eaa14fccedb73c Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Thu, 24 Oct 2024 15:24:24 +0200 Subject: [PATCH] feat(balance-worker): add new metrics with prefixed name Signed-off-by: Mark Sagi-Kazar --- .../balanceworker/entitlementhandler.go | 8 +++ .../entitlement/balanceworker/recalculate.go | 50 +++++++++++++++++-- openmeter/entitlement/balanceworker/worker.go | 12 +++++ 3 files changed, 66 insertions(+), 4 deletions(-) diff --git a/openmeter/entitlement/balanceworker/entitlementhandler.go b/openmeter/entitlement/balanceworker/entitlementhandler.go index 59f212f80..aaa4ab0fc 100644 --- a/openmeter/entitlement/balanceworker/entitlementhandler.go +++ b/openmeter/entitlement/balanceworker/entitlementhandler.go @@ -100,6 +100,14 @@ func (w *Worker) createSnapshotEvent(ctx context.Context, entitlementEntity *ent return nil, fmt.Errorf("failed to get entitlement value: %w", err) } + w.metricRecalculationTime.Record( + ctx, + time.Since(calculationStart).Milliseconds(), + metric.WithAttributes( + attribute.String(metricAttributeKeyEntitltementType, string(entitlementEntity.EntitlementType)), + ), + ) + w.metricRecalculationTimeOld.Record(ctx, time.Since(calculationStart).Milliseconds(), metric.WithAttributes( attribute.String(metricAttributeKeyEntitltementType, string(entitlementEntity.EntitlementType)), )) diff --git a/openmeter/entitlement/balanceworker/recalculate.go b/openmeter/entitlement/balanceworker/recalculate.go index d77135c09..bd25d0dfd 100644 --- a/openmeter/entitlement/balanceworker/recalculate.go +++ b/openmeter/entitlement/balanceworker/recalculate.go @@ -29,6 +29,10 @@ const ( defaultLRUCacheSize = 10_000 + metricNameRecalculationTime = "openmeter.balance_worker.entitlement_recalculation_time_ms" + metricNameRecalculationJobCalculationTime = "openmeter.balance_worker.entitlement_recalculation_job_calculation_time_ms" + + // TODO: remove old metrics once not used anymore metricNameRecalculationTimeOld = "balance_worker.entitlement_recalculation_time_ms" metricNameRecalculationJobCalculationTimeOld = "balance_worker.entitlement_recalculation_job_calculation_time_ms" @@ -64,6 +68,10 @@ type Recalculator struct { featureCache *lru.Cache[string, feature.Feature] subjectCache *lru.Cache[string, models.Subject] + metricRecalculationTime metric.Int64Histogram + metricRecalculationJobRecalculationTime metric.Int64Histogram + + // TODO: remove old metrics once not used anymore metricRecalculationTimeOld metric.Int64Histogram metricRecalculationJobRecalculationTimeOld metric.Int64Histogram } @@ -83,6 +91,22 @@ func NewRecalculator(opts RecalculatorOptions) (*Recalculator, error) { return nil, fmt.Errorf("failed to create subject ID cache: %w", err) } + metricRecalculationTime, err := opts.Meter.Int64Histogram( + metricNameRecalculationTime, + metric.WithDescription("Entitlement recalculation time"), + ) + if err != nil { + return nil, fmt.Errorf("failed to create recalculation time histogram: %w", err) + } + + metricRecalculationJobRecalculationTime, err := opts.Meter.Int64Histogram( + metricNameRecalculationJobCalculationTime, + metric.WithDescription("Time takes to recalculate the entitlements including the necessary data fetches"), + ) + if err != nil { + return nil, fmt.Errorf("failed to create recalculation time histogram: %w", err) + } + metricRecalculationTimeOld, err := opts.Meter.Int64Histogram( metricNameRecalculationTimeOld, metric.WithDescription("Entitlement recalculation time"), @@ -100,10 +124,12 @@ func NewRecalculator(opts RecalculatorOptions) (*Recalculator, error) { } return &Recalculator{ - opts: opts, - featureCache: featureCache, - subjectCache: subjectCache, - metricRecalculationTimeOld: metricRecalculationTimeOld, + opts: opts, + featureCache: featureCache, + subjectCache: subjectCache, + metricRecalculationTime: metricRecalculationTime, + metricRecalculationJobRecalculationTime: metricRecalculationJobRecalculationTime, + metricRecalculationTimeOld: metricRecalculationTimeOld, metricRecalculationJobRecalculationTimeOld: metricRecalculationJobRecalculationTimeOld, }, nil } @@ -136,6 +162,14 @@ func (r *Recalculator) processEntitlements(ctx context.Context, entitlements []e errs = errors.Join(errs, fmt.Errorf("error sending event for entitlement [id=%s]: %w", ent.ID, err)) } + r.metricRecalculationJobRecalculationTime.Record( + ctx, + time.Since(start).Milliseconds(), + metric.WithAttributes( + attribute.String(metricAttributeKeyEntitltementType, string(ent.EntitlementType)), + ), + ) + r.metricRecalculationJobRecalculationTimeOld.Record(ctx, time.Since(start).Milliseconds(), metric.WithAttributes( @@ -203,6 +237,14 @@ func (r *Recalculator) sendEntitlementUpdatedEvent(ctx context.Context, ent enti return fmt.Errorf("failed to get entitlement value: %w", err) } + r.metricRecalculationTime.Record( + ctx, + time.Since(calculatedAt).Milliseconds(), + metric.WithAttributes( + attribute.String(metricAttributeKeyEntitltementType, string(ent.EntitlementType)), + ), + ) + r.metricRecalculationTimeOld.Record(ctx, time.Since(calculatedAt).Milliseconds(), metric.WithAttributes( diff --git a/openmeter/entitlement/balanceworker/worker.go b/openmeter/entitlement/balanceworker/worker.go index 994bc4913..6516c1c10 100644 --- a/openmeter/entitlement/balanceworker/worker.go +++ b/openmeter/entitlement/balanceworker/worker.go @@ -66,6 +66,9 @@ type Worker struct { highWatermarkCache *lru.Cache[string, highWatermarkCacheEntry] + metricRecalculationTime metric.Int64Histogram + + // TODO: remove old metrics once not used anymore metricRecalculationTimeOld metric.Int64Histogram } @@ -75,6 +78,14 @@ func New(opts WorkerOptions) (*Worker, error) { return nil, fmt.Errorf("failed to create high watermark cache: %w", err) } + metricRecalculationTime, err := opts.Router.MetricMeter.Int64Histogram( + metricNameRecalculationTime, + metric.WithDescription("Entitlement recalculation time"), + ) + if err != nil { + return nil, fmt.Errorf("failed to create recalculation time histogram: %w", err) + } + metricRecalculationTimeOld, err := opts.Router.MetricMeter.Int64Histogram( metricNameRecalculationTimeOld, metric.WithDescription("Entitlement recalculation time"), @@ -89,6 +100,7 @@ func New(opts WorkerOptions) (*Worker, error) { repo: opts.Repo, highWatermarkCache: highWatermarkCache, + metricRecalculationTime: metricRecalculationTime, metricRecalculationTimeOld: metricRecalculationTimeOld, }