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

Add telemetry #463

Merged
merged 1 commit into from
Mar 19, 2021
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
2 changes: 1 addition & 1 deletion contrib/local/02-contracts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
echo "-----------------------"
echo "## Add new CosmWasm contract"
RESP=$(wasmd tx wasm store "$DIR/../../x/wasm/internal/keeper/testdata/hackatom.wasm" \
--from validator --gas 1000000 -y --chain-id=testing --node=http://localhost:26657 -b block)
--from validator --gas 1500000 -y --chain-id=testing --node=http://localhost:26657 -b block)

CODE_ID=$(echo "$RESP" | jq -r '.logs[0].events[0].attributes[-1].value')
echo "* Code id: $CODE_ID"
Expand Down
8 changes: 5 additions & 3 deletions contrib/local/03-grpc-queries.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ echo "-----------------------"
COSMOS_SDK_DIR=${COSMOS_SDK_DIR:-$(go list -f "{{ .Dir }}" -m github.com/cosmos/cosmos-sdk)}

echo "### List all codes"
grpcurl -plaintext -import-path $COSMOS_SDK_DIR/third_party/proto -import-path $COSMOS_SDK_DIR/proto -import-path . -proto ./x/wasm/internal/types/query.proto \
localhost:9090 cosmwasm.wasm.v1beta1.Query/Codes | jq
RESP=$(grpcurl -plaintext -import-path $COSMOS_SDK_DIR/third_party/proto -import-path $COSMOS_SDK_DIR/proto -import-path . -proto ./x/wasm/internal/types/query.proto \
localhost:9090 cosmwasm.wasm.v1beta1.Query/Codes)
echo "$RESP" | jq

CODE_ID=$(echo "$RESP" | jq -r '.codeInfos[-1].codeId')
echo "### List contract by code"
RESP=$(grpcurl -plaintext -import-path $COSMOS_SDK_DIR/third_party/proto -import-path $COSMOS_SDK_DIR/proto -import-path . -proto ./x/wasm/internal/types/query.proto \
-d '{"codeId":2}' localhost:9090 cosmwasm.wasm.v1beta1.Query/ContractsByCode )
-d "{\"codeId\": $CODE_ID}" localhost:9090 cosmwasm.wasm.v1beta1.Query/ContractsByCode )
echo $RESP | jq

echo "### Show history for contract"
Expand Down
47 changes: 47 additions & 0 deletions contrib/prometheus/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Setup
Enable prometheus metrics in wasmd:

* Edit `$HOME/config/app.toml`
```toml
[telemetry]

# Enabled enables the application telemetry functionality. When enabled,
# an in-memory sink is also enabled by default. Operators may also enabled
# other sinks such as Prometheus.
enabled =true
# ...

# PrometheusRetentionTime, when positive, enables a Prometheus metrics sink.
prometheus-retention-time = 15
```

`retention-time` must be >0 (see prometheus scrape config)



* Edit `$HOME/config/config.toml`
```toml
[instrumentation]

# When true, Prometheus metrics are served under /metrics on
# PrometheusListenAddr.
# Check out the documentation for the list of available metrics.
prometheus = true
```
# Local testing
## Run Prometheus
alpe marked this conversation as resolved.
Show resolved Hide resolved
```sh
# port 9090 is used by wasmd already
docker run -it -v $(pwd)/contrib/prometheus:/prometheus -p9091:9090 prom/prometheus --config.file=/prometheus/prometheus.yaml
```
* Open [console](http://localhost:9091) and find `wasm_`service metrics

## Run Grafana

```shell
docker run -it -p 3000:3000 grafana/grafana
```
* Add Prometheus data source
`http://host.docker.internal:9091`
### Labels
* `wasm_contract_create` = nanosec
11 changes: 11 additions & 0 deletions contrib/prometheus/prometheus.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
global:
scrape_interval: 15s # By default, scrape targets every 15 seconds.
evaluation_interval: 15s # By default, scrape targets every 15 seconds.
rule_files:

scrape_configs:
- job_name: wasmd

scrape_interval: 5s
static_configs:
- targets: ['host.docker.internal:26660']
13 changes: 10 additions & 3 deletions x/wasm/internal/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@ import (
"bytes"
"encoding/binary"
"fmt"
abci "github.com/tendermint/tendermint/abci/types"
"path/filepath"

"github.com/CosmWasm/wasmd/x/wasm/internal/types"
wasmvm "github.com/CosmWasm/wasmvm"
wasmvmtypes "github.com/CosmWasm/wasmvm/types"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store/prefix"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/libs/log"
"path/filepath"
"time"
)

// GasMultiplier is how many cosmwasm gas points = 1 sdk gas point
Expand Down Expand Up @@ -227,6 +228,7 @@ func (k Keeper) Instantiate(ctx sdk.Context, codeID uint64, creator, admin sdk.A
}

func (k Keeper) instantiate(ctx sdk.Context, codeID uint64, creator, admin sdk.AccAddress, initMsg []byte, label string, deposit sdk.Coins, authZ AuthorizationPolicy) (sdk.AccAddress, []byte, error) {
defer telemetry.MeasureSince(time.Now(), "wasm", "contract", "instantiate")
if !k.IsPinnedCode(ctx, codeID) {
ctx.GasMeter().ConsumeGas(InstanceCost, "Loading CosmWasm module: instantiate")
}
Expand Down Expand Up @@ -321,6 +323,7 @@ func (k Keeper) instantiate(ctx sdk.Context, codeID uint64, creator, admin sdk.A

// Execute executes the contract instance
func (k Keeper) Execute(ctx sdk.Context, contractAddress sdk.AccAddress, caller sdk.AccAddress, msg []byte, coins sdk.Coins) (*sdk.Result, error) {
defer telemetry.MeasureSince(time.Now(), "wasm", "contract", "execute")
contractInfo, codeInfo, prefixStore, err := k.contractInstance(ctx, contractAddress)
if err != nil {
return nil, err
Expand Down Expand Up @@ -370,6 +373,7 @@ func (k Keeper) Migrate(ctx sdk.Context, contractAddress sdk.AccAddress, caller
}

func (k Keeper) migrate(ctx sdk.Context, contractAddress sdk.AccAddress, caller sdk.AccAddress, newCodeID uint64, msg []byte, authZ AuthorizationPolicy) (*sdk.Result, error) {
defer telemetry.MeasureSince(time.Now(), "wasm", "contract", "migrate")
if !k.IsPinnedCode(ctx, newCodeID) {
ctx.GasMeter().ConsumeGas(InstanceCost, "Loading CosmWasm module: migrate")
}
Expand Down Expand Up @@ -443,6 +447,7 @@ func (k Keeper) migrate(ctx sdk.Context, contractAddress sdk.AccAddress, caller
// another native Go module directly. Thus, the keeper doesn't place any access controls on it, that is the
// responsibility or the app developer (who passes the wasm.Keeper in app.go)
func (k Keeper) Sudo(ctx sdk.Context, contractAddress sdk.AccAddress, msg []byte) (*sdk.Result, error) {
defer telemetry.MeasureSince(time.Now(), "wasm", "contract", "sudo")
Copy link
Member

Choose a reason for hiding this comment

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

Nice, so we get a count and time used for all the keeper calls.

If we have recursive contract calls (A calls B calls C), we will get 3 prometheus events, right? (A + B + C, B + C, and C). Is it possible to annotate that these are nested somehow?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

These are very broad metrics that measure an operation but not link them with the contract itself. With recursive calls we would get 3 events. I had chosen to not add contract labels as this would be a growing dataset. But I agree that some sort of tracing makes a lot of sense. We should look into https://opentelemetry.io/ and friends instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

pinned could be a good category label in a new iteration

contractInfo, codeInfo, prefixStore, err := k.contractInstance(ctx, contractAddress)
if err != nil {
return nil, err
Expand Down Expand Up @@ -577,6 +582,7 @@ func (k Keeper) GetContractHistory(ctx sdk.Context, contractAddr sdk.AccAddress)

// QuerySmart queries the smart contract itself.
func (k Keeper) QuerySmart(ctx sdk.Context, contractAddr sdk.AccAddress, req []byte) ([]byte, error) {
defer telemetry.MeasureSince(time.Now(), "wasm", "contract", "query-smart")
contractInfo, codeInfo, prefixStore, err := k.contractInstance(ctx, contractAddr)
if err != nil {
return nil, err
Expand All @@ -599,6 +605,7 @@ func (k Keeper) QuerySmart(ctx sdk.Context, contractAddr sdk.AccAddress, req []b

// QueryRaw returns the contract's state for give key. Returns `nil` when key is `nil`.
func (k Keeper) QueryRaw(ctx sdk.Context, contractAddress sdk.AccAddress, key []byte) []byte {
defer telemetry.MeasureSince(time.Now(), "wasm", "contract", "query-raw")
if key == nil {
return nil
}
Expand Down
11 changes: 11 additions & 0 deletions x/wasm/internal/keeper/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package keeper
import (
"github.com/CosmWasm/wasmd/x/wasm/internal/types"
wasmvmtypes "github.com/CosmWasm/wasmvm/types"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"time"
)

// OnOpenChannel calls the contract to participate in the IBC channel handshake step.
Expand All @@ -17,6 +19,8 @@ func (k Keeper) OnOpenChannel(
contractAddr sdk.AccAddress,
channel wasmvmtypes.IBCChannel,
) error {
defer telemetry.MeasureSince(time.Now(), "wasm", "contract", "ibc-open-channel")

_, codeInfo, prefixStore, err := k.contractInstance(ctx, contractAddr)
if err != nil {
return err
Expand Down Expand Up @@ -47,6 +51,7 @@ func (k Keeper) OnConnectChannel(
contractAddr sdk.AccAddress,
channel wasmvmtypes.IBCChannel,
) error {
defer telemetry.MeasureSince(time.Now(), "wasm", "contract", "ibc-connect-channel")
contractInfo, codeInfo, prefixStore, err := k.contractInstance(ctx, contractAddr)
if err != nil {
return err
Expand Down Expand Up @@ -83,6 +88,8 @@ func (k Keeper) OnCloseChannel(
contractAddr sdk.AccAddress,
channel wasmvmtypes.IBCChannel,
) error {
defer telemetry.MeasureSince(time.Now(), "wasm", "contract", "ibc-close-channel")

contractInfo, codeInfo, prefixStore, err := k.contractInstance(ctx, contractAddr)
if err != nil {
return err
Expand Down Expand Up @@ -119,6 +126,7 @@ func (k Keeper) OnRecvPacket(
contractAddr sdk.AccAddress,
packet wasmvmtypes.IBCPacket,
) ([]byte, error) {
defer telemetry.MeasureSince(time.Now(), "wasm", "contract", "ibc-recv-packet")
contractInfo, codeInfo, prefixStore, err := k.contractInstance(ctx, contractAddr)
if err != nil {
return nil, err
Expand Down Expand Up @@ -156,6 +164,7 @@ func (k Keeper) OnAckPacket(
contractAddr sdk.AccAddress,
acknowledgement wasmvmtypes.IBCAcknowledgement,
) error {
defer telemetry.MeasureSince(time.Now(), "wasm", "contract", "ibc-ack-packet")
contractInfo, codeInfo, prefixStore, err := k.contractInstance(ctx, contractAddr)
if err != nil {
return err
Expand Down Expand Up @@ -189,6 +198,8 @@ func (k Keeper) OnTimeoutPacket(
contractAddr sdk.AccAddress,
packet wasmvmtypes.IBCPacket,
) error {
defer telemetry.MeasureSince(time.Now(), "wasm", "contract", "ibc-timeout-packet")

contractInfo, codeInfo, prefixStore, err := k.contractInstance(ctx, contractAddr)
if err != nil {
return err
Expand Down