Skip to content

Commit f550a53

Browse files
committed
feat(logic): implement telemetry for predicate execution duration metrics
1 parent d0fd0c7 commit f550a53

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

x/logic/keeper/interpreter.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ func (k Keeper) newInterpreter(ctx context.Context, params types.Params) (*prolo
106106
whitelistBlacklistHookFn(whitelistPredicates, blacklistPredicates),
107107
gasMeterHookFn(sdkctx, params.GetGasPolicy()),
108108
telemetryPredicateCallCounterHookFn(),
109+
telemetryPredicateDurationHookFn(),
109110
),
110111
interpreter.WithPredicates(ctx, interpreter.RegistryNames),
111112
interpreter.WithBootstrap(ctx, util.NonZeroOrDefault(interpreterParams.GetBootstrap(), bootstrap.Bootstrap())),

x/logic/keeper/metrics.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package keeper
22

33
import (
44
"fmt"
5+
"time"
56

67
"github.com/axone-protocol/prolog/engine"
78
"github.com/hashicorp/go-metrics"
@@ -45,6 +46,30 @@ func telemetryPredicateCallCounterHookFn() engine.HookFunc {
4546
}
4647
}
4748

49+
func telemetryPredicateDurationHookFn() engine.HookFunc {
50+
var predicate string
51+
var start time.Time
52+
return func(opcode engine.Opcode, operand engine.Term, _ *engine.Env) error {
53+
if opcode != engine.OpCall {
54+
if predicate != "" {
55+
telemetry.MeasureSince(start, append(metricsKeys, predicate)...)
56+
predicate = ""
57+
start = time.Time{}
58+
}
59+
return nil
60+
}
61+
62+
var ok bool
63+
if predicate, ok = stringifyOperand(operand); !ok {
64+
return nil
65+
}
66+
67+
start = telemetry.Now()
68+
69+
return nil
70+
}
71+
}
72+
4873
// stringifyOperand returns the string representation of the operand if it implements fmt.Stringer.
4974
// It returns an empty string and false if the operand does not have a string representation.
5075
func stringifyOperand(operand engine.Term) (string, bool) {

0 commit comments

Comments
 (0)