Skip to content

Commit

Permalink
feat(balance-worker): add new metrics with prefixed name
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Sagi-Kazar <[email protected]>
  • Loading branch information
sagikazarmark committed Oct 24, 2024
1 parent b61af8e commit c4a9091
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 4 deletions.
8 changes: 8 additions & 0 deletions openmeter/entitlement/balanceworker/entitlementhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
))
Expand Down
50 changes: 46 additions & 4 deletions openmeter/entitlement/balanceworker/recalculate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
}
Expand All @@ -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"),
Expand All @@ -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
}
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
12 changes: 12 additions & 0 deletions openmeter/entitlement/balanceworker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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"),
Expand All @@ -89,6 +100,7 @@ func New(opts WorkerOptions) (*Worker, error) {
repo: opts.Repo,
highWatermarkCache: highWatermarkCache,

metricRecalculationTime: metricRecalculationTime,
metricRecalculationTimeOld: metricRecalculationTimeOld,
}

Expand Down

0 comments on commit c4a9091

Please sign in to comment.