Skip to content

Commit

Permalink
Merge pull request cosmos#463 from CosmWasm/telemetry_387
Browse files Browse the repository at this point in the history
Add telemetry
  • Loading branch information
alpe authored Mar 19, 2021
2 parents e3b5a6e + 75d5fcb commit 63fe568
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 7 deletions.
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
```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 @@ -228,6 +229,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 @@ -322,6 +324,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 @@ -371,6 +374,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 @@ -444,6 +448,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")
contractInfo, codeInfo, prefixStore, err := k.contractInstance(ctx, contractAddress)
if err != nil {
return nil, err
Expand Down Expand Up @@ -578,6 +583,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 @@ -600,6 +606,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

0 comments on commit 63fe568

Please sign in to comment.