Skip to content
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

telemetry #4

Merged
merged 2 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
41 changes: 41 additions & 0 deletions gno.land/cmd/gnoland/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
"context"
"flag"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"time"

"github.com/gnolang/gno/gno.land/pkg/gnoland"
vmm "github.com/gnolang/gno/gno.land/pkg/sdk/vm"
gno "github.com/gnolang/gno/gnovm/pkg/gnolang"
"github.com/gnolang/gno/gnovm/pkg/gnomod"
"github.com/gnolang/gno/telemetry"
"github.com/gnolang/gno/tm2/pkg/amino"
abci "github.com/gnolang/gno/tm2/pkg/bft/abci/types"
"github.com/gnolang/gno/tm2/pkg/bft/node"
Expand Down Expand Up @@ -121,10 +124,48 @@
)
}

func initTelemetry(ctx context.Context) error {
var options []telemetry.Option

if os.Getenv("TELEM_METRICS_ENABLED") == "true" {

Check failure on line 130 in gno.land/cmd/gnoland/start.go

View workflow job for this annotation

GitHub Actions / lint

string `true` has 3 occurrences, make it a constant (goconst)
options = append(options, telemetry.WithOptionMetricsEnabled())
}
if os.Getenv("TELEM_TRACES_ENABLED") == "true" {
options = append(options, telemetry.WithOptionTracesEnabled())
}
if portString := os.Getenv("TELEM_PORT"); portString != "" {
port, err := strconv.ParseUint(portString, 10, 64)
if err != nil {
return fmt.Errorf("invalid port: %w", err)
}

options = append(options, telemetry.WithOptionPort(port))
}
if os.Getenv("TELEM_USE_FAKE_METRICS") == "true" {
options = append(options, telemetry.WithOptionFakeMetrics())
}

// The string options can be added by default. Their absence would yield the same result
// as if the option were excluded altogether.
options = append(options, telemetry.WithOptionMeterName(os.Getenv("TELEM_METER_NAME")))
options = append(options, telemetry.WithOptionExporterEndpoint(os.Getenv("TELEM_EXPORTER_ENDPOINT")))
options = append(options, telemetry.WithOptionServiceName(os.Getenv("TELEM_SERVICE_NAME")))

return telemetry.Init(ctx, options...)
}

func execStart(c startCfg, args []string) error {
rootDir := c.rootDir
tmcfg := &c.baseCfg.tmConfig


Check failure on line 161 in gno.land/cmd/gnoland/start.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofmt`-ed with `-s` (gofmt)
// Attempt to initialize telemetry. If the enviroment variables required to initialize

Check failure on line 162 in gno.land/cmd/gnoland/start.go

View workflow job for this annotation

GitHub Actions / lint

`enviroment` is a misspelling of `environment` (misspell)
// telemetry are not set, then the initialization will do nothing.
ctx := context.Background()
if err := initTelemetry(ctx); err != nil {
return fmt.Errorf("error initializing telemetry: %w", err)
}

// create priv validator first.
// need it to generate genesis.json
newPrivValKey := tmcfg.PrivValidatorKeyFile()
Expand Down
1 change: 1 addition & 0 deletions gno.land/pkg/gnoland/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func NewApp(dataRootDir string, skipFailingGenesisTxs bool, logger log.Logger, m
}

cfg.Logger = logger
cfg.SkipFailingGenesisTxs = skipFailingGenesisTxs

return NewAppWithOptions(cfg)
}
Expand Down
48 changes: 48 additions & 0 deletions gno.land/pkg/sdk/vm/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import (
"fmt"
"strings"

"github.com/gnolang/gno/telemetry"
"github.com/gnolang/gno/telemetry/traces"
abci "github.com/gnolang/gno/tm2/pkg/bft/abci/types"
"github.com/gnolang/gno/tm2/pkg/sdk"
"github.com/gnolang/gno/tm2/pkg/sdk/auth"
"github.com/gnolang/gno/tm2/pkg/std"
"go.opentelemetry.io/otel/attribute"
)

type vmHandler struct {
Expand All @@ -22,6 +25,17 @@ func NewHandler(vm *VMKeeper) vmHandler {
}

func (vh vmHandler) Process(ctx sdk.Context, msg std.Msg) sdk.Result {
if telemetry.TracesEnabled() {
// This is the trace's entry point for the VM namespace, so initialize it with the context.
traces.InitNamespace(ctx.Context(), traces.NamespaceVMProcess)
spanEnder := traces.StartSpan(
traces.NamespaceVM,
"vmHandler.Process",
attribute.String("msg.Type", msg.Type()),
)
defer spanEnder.End()
}

switch msg := msg.(type) {
case MsgAddPackage:
return vh.handleMsgAddPackage(ctx, msg)
Expand All @@ -35,6 +49,17 @@ func (vh vmHandler) Process(ctx sdk.Context, msg std.Msg) sdk.Result {

// Handle MsgAddPackage.
func (vh vmHandler) handleMsgAddPackage(ctx sdk.Context, msg MsgAddPackage) sdk.Result {
if telemetry.TracesEnabled() {
spanEnder := traces.StartSpan(
traces.NamespaceVM,
"vmHandler.handleMsgAddPackage",
attribute.String("msg.Creator", msg.Creator.String()),
attribute.String("msg.Package.Path", msg.Package.Path),
attribute.String("msg.Deposit", msg.Deposit.String()),
)
defer spanEnder.End()
}

amount, err := std.ParseCoins("1000000ugnot") // XXX calculate
if err != nil {
return abciResult(err)
Expand All @@ -52,6 +77,18 @@ func (vh vmHandler) handleMsgAddPackage(ctx sdk.Context, msg MsgAddPackage) sdk.

// Handle MsgCall.
func (vh vmHandler) handleMsgCall(ctx sdk.Context, msg MsgCall) (res sdk.Result) {
if telemetry.TracesEnabled() {
spanEnder := traces.StartSpan(
traces.NamespaceVM,
"vmHandler.handleMsgCall",
attribute.String("msg.Caller", msg.Caller.String()),
attribute.String("msg.PkgPath", msg.PkgPath),
attribute.String("msg.Func", msg.Func),
attribute.StringSlice("msg.Args", msg.Args),
)
defer spanEnder.End()
}

amount, err := std.ParseCoins("1000000ugnot") // XXX calculate
if err != nil {
return abciResult(err)
Expand Down Expand Up @@ -91,6 +128,17 @@ const (
)

func (vh vmHandler) Query(ctx sdk.Context, req abci.RequestQuery) (res abci.ResponseQuery) {
if telemetry.TracesEnabled() {
traces.InitNamespace(ctx.Context(), traces.NamespaceVMQuery)
spanEnder := traces.StartSpan(
traces.NamespaceVMQuery,
"vmHandler.Query",
attribute.String("req.Path", req.Path),
attribute.String("req.Data", string(req.Data)),
)
defer spanEnder.End()
}

switch secondPart(req.Path) {
case QueryPackage:
return vh.queryPackage(ctx, req)
Expand Down
29 changes: 29 additions & 0 deletions gno.land/pkg/sdk/vm/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (

gno "github.com/gnolang/gno/gnovm/pkg/gnolang"
"github.com/gnolang/gno/gnovm/stdlibs"
"github.com/gnolang/gno/telemetry"
"github.com/gnolang/gno/telemetry/traces"
"github.com/gnolang/gno/tm2/pkg/errors"
"github.com/gnolang/gno/tm2/pkg/sdk"
"github.com/gnolang/gno/tm2/pkg/sdk/auth"
Expand Down Expand Up @@ -70,6 +72,16 @@ func (vm *VMKeeper) Initialize(ms store.MultiStore) {
if vm.gnoStore != nil {
panic("should not happen")
}

if telemetry.TracesEnabled() {
traces.InitNamespace(nil, traces.NamespaceVMInit)
spanEnder := traces.StartSpan(
traces.NamespaceVMInit,
"VMKeeper.Initialize",
)
defer spanEnder.End()
}

alloc := gno.NewAllocator(maxAllocTx)
baseSDKStore := ms.GetStore(vm.baseKey)
iavlSDKStore := ms.GetStore(vm.iavlKey)
Expand Down Expand Up @@ -97,6 +109,15 @@ func (vm *VMKeeper) getGnoStore(ctx sdk.Context) gno.Store {
if vm.gnoStore == nil {
panic("VMKeeper must first be initialized")
}

if telemetry.TracesEnabled() {
spanEnder := traces.StartSpan(
traces.NamespaceVM,
"VMKeeper.getGnoStore",
)
defer spanEnder.End()
}

switch ctx.Mode() {
case sdk.RunTxModeDeliver:
// swap sdk store of existing gnoStore.
Expand Down Expand Up @@ -195,6 +216,14 @@ func (vm *VMKeeper) AddPackage(ctx sdk.Context, msg MsgAddPackage) error {

// Calls calls a public Gno function (for delivertx).
func (vm *VMKeeper) Call(ctx sdk.Context, msg MsgCall) (res string, err error) {
if telemetry.TracesEnabled() {
spanEnder := traces.StartSpan(
traces.NamespaceVM,
"VMKeeper.Call",
)
defer spanEnder.End()
}

pkgPath := msg.PkgPath // to import
fnc := msg.Func
store := vm.getGnoStore(ctx)
Expand Down
37 changes: 37 additions & 0 deletions gnovm/pkg/gnolang/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ import (
"sync"
"testing"

"github.com/gnolang/gno/telemetry"
"github.com/gnolang/gno/telemetry/traces"
"github.com/gnolang/gno/tm2/pkg/errors"
"github.com/gnolang/gno/tm2/pkg/std"
"go.opentelemetry.io/otel/attribute"
)

//----------------------------------------
Expand Down Expand Up @@ -603,6 +606,14 @@ func (m *Machine) RunMain() {
// Input must not have been preprocessed, that is,
// it should not be the child of any parent.
func (m *Machine) Eval(x Expr) []TypedValue {
if telemetry.TracesEnabled() {
spanEnder := traces.StartSpan(
traces.NamespaceVM,
"Machine.Eval",
)
defer spanEnder.End()
}

if debug {
m.Printf("Machine.Eval(%v)\n", x)
}
Expand Down Expand Up @@ -1019,13 +1030,36 @@ const (
// main run loop.

func (m *Machine) Run() {
var spanEnder *traces.SpanEnder
if telemetry.TracesEnabled() {
// Ensure that spanEnder.End() is called on panic.
defer func() {
if r := recover(); r != nil {
spanEnder.End()
panic(r)
}
}()
}

for {
op := m.PopOp()

if telemetry.TracesEnabled() {
spanEnder.End()

spanEnder = traces.StartSpan(
traces.NamespaceVM,
"Machine.Run",
attribute.String("op", opToStringMap[op]),
)
}

// TODO: this can be optimized manually, even into tiers.
switch op {
/* Control operators */
case OpHalt:
m.incrCPU(OpCPUHalt)
spanEnder.End()
return
case OpNoop:
m.incrCPU(OpCPUNoop)
Expand Down Expand Up @@ -1345,6 +1379,9 @@ func (m *Machine) Run() {
panic(fmt.Sprintf("unexpected opcode %s", op.String()))
}
}

// Uncomment this if this code ever becomes reachable.
// spanEnder.End()
}

//----------------------------------------
Expand Down
Loading
Loading