generated from okp4/template-oss
-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #810 from axone-protocol/feat/telemetry
Feat/telemetry
- Loading branch information
Showing
3 changed files
with
146 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
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 | ||
} | ||
|
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package keeper | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/axone-protocol/prolog/engine" | ||
|
||
. "github.com/smartystreets/goconvey/convey" | ||
) | ||
|
||
func TestStringifyOperand(t *testing.T) { | ||
Convey("Given various inputs to stringifyOperand", t, func() { | ||
testCases := []struct { | ||
description string | ||
input engine.Term | ||
expected string | ||
ok bool | ||
}{ | ||
{ | ||
description: "an operand implementing fmt.Stringer", | ||
input: engine.NewAtom("foo"), | ||
expected: "foo", | ||
ok: true, | ||
}, | ||
{ | ||
description: "an operand not implementing fmt.Stringer", | ||
input: engine.NewAtom("foo").Apply(engine.NewAtom("bar")), | ||
expected: "", | ||
ok: false, | ||
}, | ||
{ | ||
description: "the nil operand", | ||
input: nil, | ||
expected: "", | ||
ok: false, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
Convey(fmt.Sprintf("When input is %s", tc.description), func() { | ||
result, ok := stringifyOperand(tc.input) | ||
|
||
Convey("Then the result should match the expected output", func() { | ||
So(result, ShouldEqual, tc.expected) | ||
So(ok, ShouldEqual, tc.ok) | ||
}) | ||
}) | ||
} | ||
}) | ||
} |