Skip to content

Commit 539a593

Browse files
committed
feat(logic): implement telemetry for predicate execution duration metrics
1 parent dd0b163 commit 539a593

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"
@@ -47,6 +48,30 @@ func telemetryPredicateCallCounterHookFn() engine.HookFunc {
4748
}
4849
}
4950

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

0 commit comments

Comments
 (0)