Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OSS-62: Prefix metric names in balance-worker #1747

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cmd/jobs/entitlement/recalculatesnapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ func NewRecalculateBalanceSnapshotsCommand() *cobra.Command {

logger := slog.Default()

metricMeter := otel.GetMeterProvider().Meter(otelNameRecalculateBalanceSnapshot)
meter := otel.GetMeterProvider().Meter(otelNameRecalculateBalanceSnapshot)

entitlementConnectors, err := initEntitlements(
cmd.Context(),
conf,
logger,
metricMeter,
meter,
otelNameRecalculateBalanceSnapshot,
)
if err != nil {
Expand All @@ -44,7 +44,7 @@ func NewRecalculateBalanceSnapshotsCommand() *cobra.Command {
recalculator, err := balanceworker.NewRecalculator(balanceworker.RecalculatorOptions{
Entitlement: entitlementConnectors.Registry,
EventBus: entitlementConnectors.EventBus,
MetricMeter: metricMeter,
Meter: meter,
})
if err != nil {
return err
Expand Down
10 changes: 7 additions & 3 deletions openmeter/entitlement/balanceworker/entitlementhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,13 @@ 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.metricRecalculationTime.Record(
ctx,
time.Since(calculationStart).Milliseconds(),
metric.WithAttributes(
attribute.String(metricAttributeKeyEntitltementType, string(entitlementEntity.EntitlementType)),
),
)

mappedValues, err := entitlementdriver.MapEntitlementValueToAPI(value)
if err != nil {
Expand Down
24 changes: 14 additions & 10 deletions openmeter/entitlement/balanceworker/recalculate.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ const (

defaultLRUCacheSize = 10_000

metricNameRecalculationTime = "balance_worker.entitlement_recalculation_time_ms"
metricNameRecalculationJobCalculationTime = "balance_worker.entitlement_recalculation_job_calculation_time_ms"
metricNameRecalculationTime = "openmeter.balance_worker.entitlement_recalculation_time_ms"
metricNameRecalculationJobCalculationTime = "openmeter.balance_worker.entitlement_recalculation_job_calculation_time_ms"

metricAttributeKeyEntitltementType = "entitlement_type"
)
Expand All @@ -39,7 +39,7 @@ type RecalculatorOptions struct {
Entitlement *registry.Entitlement
SubjectResolver SubjectResolver
EventBus eventbus.Publisher
MetricMeter metric.Meter
Meter metric.Meter
}

func (o RecalculatorOptions) Validate() error {
Expand All @@ -51,7 +51,7 @@ func (o RecalculatorOptions) Validate() error {
return errors.New("missing event bus")
}

if o.MetricMeter == nil {
if o.Meter == nil {
return errors.New("missing metric meter")
}

Expand Down Expand Up @@ -83,15 +83,15 @@ func NewRecalculator(opts RecalculatorOptions) (*Recalculator, error) {
return nil, fmt.Errorf("failed to create subject ID cache: %w", err)
}

metricRecalculationTime, err := opts.MetricMeter.Int64Histogram(
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.MetricMeter.Int64Histogram(
metricRecalculationJobRecalculationTime, err := opts.Meter.Int64Histogram(
metricNameRecalculationJobCalculationTime,
metric.WithDescription("Time takes to recalculate the entitlements including the necessary data fetches"),
)
Expand Down Expand Up @@ -136,11 +136,13 @@ 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,
r.metricRecalculationJobRecalculationTime.Record(
ctx,
time.Since(start).Milliseconds(),
metric.WithAttributes(
attribute.String(metricAttributeKeyEntitltementType, string(ent.EntitlementType)),
))
),
)
}

return errs
Expand Down Expand Up @@ -203,11 +205,13 @@ func (r *Recalculator) sendEntitlementUpdatedEvent(ctx context.Context, ent enti
return fmt.Errorf("failed to get entitlement value: %w", err)
}

r.metricRecalculationTime.Record(ctx,
r.metricRecalculationTime.Record(
ctx,
time.Since(calculatedAt).Milliseconds(),
metric.WithAttributes(
attribute.String(metricAttributeKeyEntitltementType, string(ent.EntitlementType)),
))
),
)

mappedValues, err := entitlementdriver.MapEntitlementValueToAPI(value)
if err != nil {
Expand Down
Loading