Skip to content

Feat/telemetry #810

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

Merged
merged 3 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
29 changes: 15 additions & 14 deletions x/logic/keeper/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ func (k Keeper) newInterpreter(ctx context.Context, params types.Params) (*prolo
interpreter.WithHooks(
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 Expand Up @@ -135,23 +137,24 @@ func whitelistBlacklistHookFn(whitelist, blacklist []string) engine.HookFunc {
return nil
}

predicateStringer, ok := operand.(fmt.Stringer)
predicate, ok := stringifyOperand(operand)
if !ok {
return engine.SyntaxError(operand, env)
}

predicate := predicateStringer.String()
if !interpreter.IsRegistered(predicate) {
return nil
}

if interpreter.IsRegistered(predicate) {
if _, found := allowed.Get(predicate); !found {
return engine.PermissionError(
prolog2.AtomOperationExecute,
prolog2.AtomPermissionForbiddenPredicate,
engine.NewAtom(predicate),
env,
)
}
if _, found := allowed.Get(predicate); !found {
return engine.PermissionError(
prolog2.AtomOperationExecute,
prolog2.AtomPermissionForbiddenPredicate,
engine.NewAtom(predicate),
env,
)
}

return nil
}
}
Expand All @@ -166,13 +169,11 @@ func gasMeterHookFn(ctx context.Context, gasPolicy types.GasPolicy) engine.HookF
return nil
}

operandStringer, ok := operand.(fmt.Stringer)
predicate, ok := stringifyOperand(operand)
if !ok {
return engine.SyntaxError(operand, env)
}

predicate := operandStringer.String()

cost := lookupCost(predicate,
lo.CoalesceOrEmpty(gasPolicy.DefaultPredicateCost, defaultPredicateCost),
gasPolicy.PredicateCosts)
Expand Down
80 changes: 80 additions & 0 deletions x/logic/keeper/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package keeper

import (
"fmt"
"time"

"github.com/axone-protocol/prolog/engine"
"github.com/hashicorp/go-metrics"

"github.com/cosmos/cosmos-sdk/telemetry"

"github.com/axone-protocol/axoned/v10/x/logic/interpreter"
"github.com/axone-protocol/axoned/v10/x/logic/types"
)

var metricsKeys = []string{types.ModuleName, "vm", "predicate"}

const (
labelPredicate = "predicate"
)

func telemetryPredicateCallCounterHookFn() engine.HookFunc {
return func(opcode engine.Opcode, operand engine.Term, _ *engine.Env) error {
if opcode != engine.OpCall {
return nil
}

predicate, ok := stringifyOperand(operand)
if !ok {
return nil
}

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

View check run for this annotation

Codecov / codecov/patch

x/logic/keeper/metrics.go#L30-L31

Added lines #L30 - L31 were not covered by tests

if !interpreter.IsRegistered(predicate) {
return nil
}

telemetry.IncrCounterWithLabels(
metricsKeys,
1,
[]metrics.Label{
telemetry.NewLabel(labelPredicate, predicate),
},
)

return nil
}
}

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) {
if stringer, ok := operand.(fmt.Stringer); ok {
return stringer.String(), true
}
return "", false

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

View check run for this annotation

Codecov / codecov/patch

x/logic/keeper/metrics.go#L79

Added line #L79 was not covered by tests
}
Loading