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

Enable pprof HTTP endpoints with CLI flag #107

Merged
merged 3 commits into from
Aug 29, 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
8 changes: 8 additions & 0 deletions cmd/rosetta/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ VERSION:
Required: false,
Value: math.MaxUint32,
}

cliFlagShouldEnablePprofEndpoints = cli.BoolFlag{
Name: "pprof",
Usage: "Whether to enable pprof HTTP endpoints.",
}
)

func getAllCliFlags() []cli.Flag {
Expand Down Expand Up @@ -212,6 +217,7 @@ func getAllCliFlags() []cli.Flag {
cliFlagConfigFileCustomCurrencies,
cliFlagActivationEpochSirius,
cliFlagActivationEpochSpica,
cliFlagShouldEnablePprofEndpoints,
}
}

Expand Down Expand Up @@ -243,6 +249,7 @@ type parsedCliFlags struct {
configFileCustomCurrencies string
activationEpochSirius uint32
activationEpochSpica uint32
shouldEnablePprofEndpoints bool
}

func getParsedCliFlags(ctx *cli.Context) parsedCliFlags {
Expand Down Expand Up @@ -274,5 +281,6 @@ func getParsedCliFlags(ctx *cli.Context) parsedCliFlags {
configFileCustomCurrencies: ctx.GlobalString(cliFlagConfigFileCustomCurrencies.Name),
activationEpochSirius: uint32(ctx.GlobalUint(cliFlagActivationEpochSirius.Name)),
activationEpochSpica: uint32(ctx.GlobalUint(cliFlagActivationEpochSpica.Name)),
shouldEnablePprofEndpoints: ctx.GlobalBool(cliFlagShouldEnablePprofEndpoints.Name),
}
}
4 changes: 4 additions & 0 deletions cmd/rosetta/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ func startRosetta(ctx *cli.Context) error {
return err
}

if cliFlags.shouldEnablePprofEndpoints {
controllers = append(controllers, newPprofController())
}

httpServer, err := createHttpServer(cliFlags.port, controllers...)
if err != nil {
return err
Expand Down
58 changes: 58 additions & 0 deletions cmd/rosetta/pprof.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import (
"net/http"
"net/http/pprof"

"github.com/coinbase/rosetta-sdk-go/server"
)

type pprofController struct {
routes []server.Route
}

// See:
// - https://stackoverflow.com/a/71032595/1475331
// - https://pkg.go.dev/net/http/pprof
func newPprofController() *pprofController {
routes := []server.Route{
{
Method: "GET",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe you can use const http.MethodGet

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

Pattern: "/debug/pprof/",
HandlerFunc: http.HandlerFunc(pprof.Index),
},
{
Method: "GET",
Pattern: "/debug/pprof/cmdline",
HandlerFunc: http.HandlerFunc(pprof.Cmdline),
},
{
Method: "GET",
Pattern: "/debug/pprof/profile",
HandlerFunc: http.HandlerFunc(pprof.Profile),
},
{
Method: "GET",
Pattern: "/debug/pprof/symbol",
HandlerFunc: http.HandlerFunc(pprof.Symbol),
},
{
Method: "GET",
Pattern: "/debug/pprof/trace",
HandlerFunc: http.HandlerFunc(pprof.Trace),
},
{
Method: "GET",
Pattern: "/debug/pprof/{cmd}",
HandlerFunc: http.HandlerFunc(pprof.Index),
},
}

return &pprofController{
routes: routes,
}
}

func (r *pprofController) Routes() server.Routes {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing comment

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

return r.routes
}
30 changes: 26 additions & 4 deletions server/services/accountService.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package services

import (
"context"
"fmt"

"github.com/coinbase/rosetta-sdk-go/server"
"github.com/coinbase/rosetta-sdk-go/types"
"github.com/multiversx/mx-chain-core-go/core"
)

type accountService struct {
Expand All @@ -23,10 +25,30 @@ func NewAccountService(provider NetworkProvider) server.AccountAPIServicer {
}

// AccountBalance implements the /account/balance endpoint.
func (service *accountService) AccountBalance(
_ context.Context,
request *types.AccountBalanceRequest,
) (*types.AccountBalanceResponse, *types.Error) {
func (service *accountService) AccountBalance(_ context.Context, request *types.AccountBalanceRequest) (*types.AccountBalanceResponse, *types.Error) {
stopWatch := core.NewStopWatch()
stopWatch.Start("account")

response, err := service.doGetAccountBalance(request)
if err != nil {
return nil, err
}

stopWatch.Stop("account")
duration := stopWatch.GetMeasurement("account")

if duration > durationAlarmThresholdAccountServiceGetAccountBalance {
log.Debug(fmt.Sprintf("accountService.AccountBalance() took more than %s", durationAlarmThresholdAccountServiceGetAccountBalance),
"duration", duration,
"address", request.AccountIdentifier.Address,
"blockNonce", response.BlockIdentifier.Index,
)
}

return response, nil
}

func (service *accountService) doGetAccountBalance(request *types.AccountBalanceRequest) (*types.AccountBalanceResponse, *types.Error) {
options, err := blockIdentifierToAccountQueryOptions(request.BlockIdentifier)
if err != nil {
return nil, service.errFactory.newErrWithOriginal(ErrUnableToGetAccount, err)
Expand Down
16 changes: 16 additions & 0 deletions server/services/blockService.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package services

import (
"context"
"fmt"
"sync"

"github.com/coinbase/rosetta-sdk-go/server"
"github.com/coinbase/rosetta-sdk-go/types"
"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/data/api"
"github.com/multiversx/mx-chain-rosetta/server/resources"
)
Expand Down Expand Up @@ -37,11 +39,25 @@ func (service *blockService) Block(
_ context.Context,
request *types.BlockRequest,
) (*types.BlockResponse, *types.Error) {
stopWatch := core.NewStopWatch()
stopWatch.Start("block")

response, err := service.doGetBlock(request)
if err != nil {
return nil, err
}

stopWatch.Stop("block")
duration := stopWatch.GetMeasurement("block")

if duration > durationAlarmThresholdBlockServiceGetBlock {
log.Debug(fmt.Sprintf("blockService.Block() took more than %s", durationAlarmThresholdBlockServiceGetBlock),
"duration", duration,
"blockNonce", response.Block.BlockIdentifier.Index,
"blockHash", response.Block.BlockIdentifier.Hash,
)
}

traceBlockResponse(response)
return response, nil
}
Expand Down
35 changes: 19 additions & 16 deletions server/services/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,30 @@ package services
import (
"encoding/hex"
"strings"
"time"

"github.com/multiversx/mx-chain-core-go/core"
)

var (
transactionVersion = 1
transactionProcessingTypeRelayedV1 = "RelayedTx"
transactionProcessingTypeBuiltInFunctionCall = "BuiltInFunctionCall"
transactionProcessingTypeMoveBalance = "MoveBalance"
transactionProcessingTypeContractInvoking = "SCInvoking"
transactionProcessingTypeContractDeployment = "SCDeployment"
amountZero = "0"
builtInFunctionClaimDeveloperRewards = core.BuiltInFunctionClaimDeveloperRewards
builtInFunctionESDTTransfer = core.BuiltInFunctionESDTTransfer
refundGasMessage = "refundedGas"
argumentsSeparator = "@"
sendingValueToNonPayableContractDataPrefix = argumentsSeparator + hex.EncodeToString([]byte("sending value to non payable contract"))
emptyHash = strings.Repeat("0", 64)
nodeVersionForOfflineRosetta = "N / A"
systemContractDeployAddress = "erd1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq6gq4hu"
nativeAsESDTIdentifier = "EGLD-000000"
transactionVersion = 1
transactionProcessingTypeRelayedV1 = "RelayedTx"
transactionProcessingTypeBuiltInFunctionCall = "BuiltInFunctionCall"
transactionProcessingTypeMoveBalance = "MoveBalance"
transactionProcessingTypeContractInvoking = "SCInvoking"
transactionProcessingTypeContractDeployment = "SCDeployment"
amountZero = "0"
builtInFunctionClaimDeveloperRewards = core.BuiltInFunctionClaimDeveloperRewards
builtInFunctionESDTTransfer = core.BuiltInFunctionESDTTransfer
refundGasMessage = "refundedGas"
argumentsSeparator = "@"
sendingValueToNonPayableContractDataPrefix = argumentsSeparator + hex.EncodeToString([]byte("sending value to non payable contract"))
emptyHash = strings.Repeat("0", 64)
nodeVersionForOfflineRosetta = "N / A"
systemContractDeployAddress = "erd1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq6gq4hu"
nativeAsESDTIdentifier = "EGLD-000000"
durationAlarmThresholdBlockServiceGetBlock = time.Duration(250) * time.Millisecond
durationAlarmThresholdAccountServiceGetAccountBalance = time.Duration(250) * time.Millisecond
)

var (
Expand Down
4 changes: 2 additions & 2 deletions server/services/transactionsTransformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (transformer *transactionsTransformer) unsignedTxToRosettaTx(
txsInBlock []*transaction.ApiTransactionResult,
) *types.Transaction {
if transformer.featuresDetector.isSmartContractResultIneffectiveRefund(scr) {
log.Info("unsignedTxToRosettaTx: ineffective refund", "hash", scr.Hash, "block", scr.BlockNonce)
log.Debug("unsignedTxToRosettaTx: ineffective refund", "hash", scr.Hash, "block", scr.BlockNonce)

return &types.Transaction{
TransactionIdentifier: hashToTransactionIdentifier(scr.Hash),
Expand Down Expand Up @@ -444,7 +444,7 @@ func (transformer *transactionsTransformer) addOperationsGivenTransactionEvents(
}

for _, event := range eventsTransferValueOnly {
log.Info("eventTransferValueOnly (effective)", "tx", tx.Hash, "block", tx.BlockNonce)
log.Debug("eventTransferValueOnly (effective)", "tx", tx.Hash, "block", tx.BlockNonce)

operations := []*types.Operation{
{
Expand Down
3 changes: 2 additions & 1 deletion systemtests/check_with_mesh_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,14 @@ def run_rosetta(configuration: Configuration):
f"--observer-actual-shard={configuration.network_shard}",
f"--network-id={configuration.network_id}",
f"--network-name={configuration.network_name}",
f"--handle-contracts",
f"--native-currency={configuration.native_currency}",
f"--config-custom-currencies={configuration.config_file_custom_currencies}",
f"--first-historical-epoch={current_epoch}",
f"--num-historical-epochs={configuration.num_historical_epochs}",
f"--activation-epoch-sirius={configuration.activation_epoch_sirius}",
f"--activation-epoch-spica={configuration.activation_epoch_spica}",
"--handle-contracts",
"--pprof"
]

return subprocess.Popen(command)
Expand Down
Loading