Skip to content

Commit

Permalink
Track evaluation times in executor (#3882)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmjb authored Jul 12, 2024
1 parent 6a66082 commit 6cce159
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
7 changes: 7 additions & 0 deletions internal/engine/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package engine
import (
"context"
"fmt"
"time"

"github.com/google/uuid"
"github.com/open-feature/go-sdk/openfeature"
Expand Down Expand Up @@ -82,6 +83,10 @@ func (e *executor) EvalEntityEvent(ctx context.Context, inf *entities.EntityInfo
Str("project_id", inf.ProjectID.String())
logger.Msg("entity evaluation - started")

// track the time taken to evaluate each entity
entityStartTime := time.Now()
defer e.metrics.TimeProfileEvaluation(ctx, entityStartTime)

provider, err := e.providerManager.InstantiateFromID(ctx, inf.ProviderID)
if err != nil {
return fmt.Errorf("could not instantiate provider: %w", err)
Expand All @@ -104,6 +109,8 @@ func (e *executor) EvalEntityEvent(ctx context.Context, inf *entities.EntityInfo

err = e.forProjectsInHierarchy(
ctx, inf, func(ctx context.Context, profile *pb.Profile, hierarchy []uuid.UUID) error {
profileStartTime := time.Now()
defer e.metrics.TimeProfileEvaluation(ctx, profileStartTime)
// Get only these rules that are relevant for this entity type
relevant, err := profiles.GetRulesForEntity(profile, inf.Type)
if err != nil {
Expand Down
29 changes: 29 additions & 0 deletions internal/engine/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package engine
import (
"context"
"fmt"
"time"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
Expand All @@ -30,6 +31,8 @@ type ExecutorMetrics struct {
evalCounter metric.Int64Counter
remediationCounter metric.Int64Counter
alertCounter metric.Int64Counter
entityDuration metric.Int64Histogram
profileDuration metric.Int64Histogram
}

// NewExecutorMetrics instantiates the ExecutorMetrics struct.
Expand All @@ -56,10 +59,26 @@ func NewExecutorMetrics(meterFactory meters.MeterFactory) (*ExecutorMetrics, err
return nil, fmt.Errorf("failed to create alert counter: %w", err)
}

profileDuration, err := meter.Int64Histogram("eval.entity-eval-duration",
metric.WithDescription("Time taken to evaluate all profiles against an entity"),
metric.WithUnit("milliseconds"))
if err != nil {
return nil, fmt.Errorf("failed to create profile histogram: %w", err)
}

entityDuration, err := meter.Int64Histogram("eval.profile-eval-duration",
metric.WithDescription("Time taken to evaluate a single profile against an entity"),
metric.WithUnit("milliseconds"))
if err != nil {
return nil, fmt.Errorf("failed to create entity histogram: %w", err)
}

return &ExecutorMetrics{
evalCounter: evalCounter,
remediationCounter: remediationCounter,
alertCounter: alertCounter,
profileDuration: profileDuration,
entityDuration: entityDuration,
}, nil
}

Expand Down Expand Up @@ -94,3 +113,13 @@ func (e *ExecutorMetrics) CountAlertStatus(
attribute.String("status", string(status)),
))
}

// TimeEntityEvaluation records how long it took to evaluate a profile.
func (e *ExecutorMetrics) TimeEntityEvaluation(ctx context.Context, startTime time.Time) {
e.entityDuration.Record(ctx, time.Since(startTime).Milliseconds())
}

// TimeProfileEvaluation records how long it took to evaluate a profile.
func (e *ExecutorMetrics) TimeProfileEvaluation(ctx context.Context, startTime time.Time) {
e.profileDuration.Record(ctx, time.Since(startTime).Milliseconds())
}

0 comments on commit 6cce159

Please sign in to comment.