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

Track evaluation times in executor #3882

Merged
merged 1 commit into from
Jul 12, 2024
Merged
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
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())
}
Loading