Skip to content

Commit

Permalink
feat(logic): implement telemetry for predicate execution duration met…
Browse files Browse the repository at this point in the history
…rics
  • Loading branch information
ccamel committed Nov 10, 2024
1 parent d0fd0c7 commit f550a53
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions x/logic/keeper/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func (k Keeper) newInterpreter(ctx context.Context, params types.Params) (*prolo
whitelistBlacklistHookFn(whitelistPredicates, blacklistPredicates),
gasMeterHookFn(sdkctx, params.GetGasPolicy()),
telemetryPredicateCallCounterHookFn(),
telemetryPredicateDurationHookFn(),
),
interpreter.WithPredicates(ctx, interpreter.RegistryNames),
interpreter.WithBootstrap(ctx, util.NonZeroOrDefault(interpreterParams.GetBootstrap(), bootstrap.Bootstrap())),
Expand Down
25 changes: 25 additions & 0 deletions x/logic/keeper/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"fmt"
"time"

"github.com/axone-protocol/prolog/engine"
"github.com/hashicorp/go-metrics"
Expand Down Expand Up @@ -45,6 +46,30 @@ func telemetryPredicateCallCounterHookFn() engine.HookFunc {
}
}

func telemetryPredicateDurationHookFn() engine.HookFunc {
var predicate string
var start time.Time
return func(opcode engine.Opcode, operand engine.Term, _ *engine.Env) error {
if opcode != engine.OpCall {
if predicate != "" {
telemetry.MeasureSince(start, append(metricsKeys, predicate)...)
predicate = ""
start = time.Time{}
}
return nil
}

var ok bool
if predicate, ok = stringifyOperand(operand); !ok {
return nil
}

Check warning on line 65 in x/logic/keeper/metrics.go

View check run for this annotation

Codecov / codecov/patch

x/logic/keeper/metrics.go#L64-L65

Added lines #L64 - L65 were not covered by tests

start = telemetry.Now()

return nil
}
}

// stringifyOperand returns the string representation of the operand if it implements fmt.Stringer.
// It returns an empty string and false if the operand does not have a string representation.
func stringifyOperand(operand engine.Term) (string, bool) {
Expand Down

0 comments on commit f550a53

Please sign in to comment.