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

general fixes + addition of 'single' request to fetch info #39

Open
wants to merge 31 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
d1c26a4
feat: add proposals metrics
arifintahu Nov 2, 2022
56ce1c3
feat: add delegator and upgrade metrics
arifintahu Nov 16, 2022
d1ef52f
fix: fix validator rank
arifintahu Nov 21, 2022
468798a
fix: update validators missed blocks
arifintahu Nov 21, 2022
0d561be
chore(ci): dockerized :whale:
albttx Jan 23, 2023
b2d0309
feat: general add token price
albttx Jan 26, 2023
7277006
fix validator ranking
shifty11 Feb 25, 2022
3bb017e
feat: add height upgrade plan
arifintahu Feb 15, 2023
d79469e
Merge pull request #5 from s16rv/feature/add-height-upgrade
egunawan85 Feb 20, 2023
f802b0f
fix: use BigInt()
albttx Feb 25, 2023
2582466
chore: refactoring and add latest_block_height
albttx Mar 3, 2023
5982b5e
feat: add estimated height on upgrade metrics
arifintahu Mar 14, 2023
6df085d
fix: remove lint
arifintahu Mar 14, 2023
14bb131
Merge pull request #6 from s16rv/feature/upgrade-plan-improvement
egunawan85 Mar 14, 2023
c0f741f
build: add goarch
arifintahu Mar 14, 2023
fc9b4a1
feat: add kujira oracle
PFC-developer May 28, 2023
a438ef2
feat: return the number of active proposals
albttx Jun 14, 2023
c265c41
Update README.md
AlexToTheMoon Aug 23, 2023
a86d315
fix(ci): upgrade goreleaser
albttx Sep 8, 2023
f4221ae
fix(ci): upgrade goreleaser config file
albttx Sep 8, 2023
78d1ddd
Merge pull request #1 from AlexToTheMoon/master
PFC-developer Oct 2, 2023
5195f5a
Merge remote-tracking branch 'nysa/master'
PFC-developer Oct 2, 2023
ea205e4
feat: merge patches from nysa-network.
PFC-developer Oct 3, 2023
3b42781
Merge remote-tracking branch 's16rv/main'
PFC-developer Oct 3, 2023
5806846
feat: merge patches from s16rv.
PFC-developer Oct 3, 2023
311f304
upgraded SDK
PFC-developer Oct 3, 2023
10ce1a2
upgraded SDK
PFC-developer Oct 3, 2023
63487c4
chore: don't use simapp directly
PFC-developer Oct 3, 2023
82ea269
fix: attempt docker push
PFC-developer Oct 6, 2023
f218198
feat: add a /metrics request handler
PFC-developer Oct 10, 2023
d957562
feat: add a /metrics request handler
PFC-developer Oct 10, 2023
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
9 changes: 0 additions & 9 deletions .github/workflows/actions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,3 @@ jobs:
- run: go version
- run: go mod download
- run: go vet
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
args: --timeout 300s
39 changes: 39 additions & 0 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: docker

on:
push:
branches:
- 'master'
tags:
- 'v*'

jobs:
docker:
runs-on: ubuntu-latest
steps:

- name: Checkout
uses: actions/checkout@v3

- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Docker meta
id: meta
uses: docker/metadata-action@v4
with:
images: ghcr.io/${{ github.repository }}
tags: |
type=raw,value=latest
type=semver,pattern=v{{version}}
- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
13 changes: 5 additions & 8 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: goreleaser
on:
push:
tags:
- '*'
- "*"

permissions:
contents: write
Expand All @@ -12,15 +12,12 @@ jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: '1.16.5'
-
name: Run GoReleaser
- uses: actions/checkout@v4
- uses: actions/setup-go@v4
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
with:
version: latest
args: release --rm-dist
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
main

dist/

/.idea
3 changes: 3 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ builds:
- linux
- windows
- darwin
goarch:
- 386
- amd64
archives:
- replacements:
darwin: Darwin
Expand Down
14 changes: 14 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM golang:1.19-alpine AS builder

COPY . /app

WORKDIR /app

RUN go build -o cosmos-exporter


FROM alpine

COPY --from=builder /app/cosmos-exporter /usr/local/bin/cosmos-exporter

ENTRYPOINT [ "/usr/local/bin/cosmos-exporter" ]
32 changes: 28 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,29 @@

cosmos-exporter is a Prometheus scraper that fetches the data from a full node of a Cosmos-based blockchain via gRPC.

There are two modes to run this in 'single' mode, and 'detailed' mode

# Single mode
the aim of single mode is that all the configuration happens when you run exporter itself, not in the prometheus configuration.
so you specify the validators, wallets, and so on the command line, and simply configure the prometheus to do a single call to /metrics
I find it easier to do this (configure the binary for each chain) than to do it in prometheus.

## Enabling single mode
when starting cosmos-exporter you can pass **single** to enable it.

by default, it will work the same as /metrics/general

### single mode parameters
* single - enable single metric mode. If this is not enabled, it will ignore the other parameters
* params - also include the details of the chain parameters
* validators - include basic information for validators listed. (basic is mainly operational things I use to alert on)
* oracle - oracle misses (for kujira only)
* upgrades - upcoming chain upgrades
* proposals - active proposals (/metrics/proposals includes the last N proposals)
* wallets - includes balance of ''denom'' coin. (/metrics/wallets includes all balances)

# Detailed mode
This mode can still be used alongside 'single' mode as well.
## What can I use it for?

You can run a full node, run cosmos-exporter on the same host, set up Prometheus to scrape the data from it (see below for instructions), then set up Grafana to visualize the data coming from the exporter and probably add some alerting. Here are some examples of Grafana dashboards we created for ourselves:
Expand All @@ -23,7 +46,7 @@ tar xvfz cosmos-exporter-*
./cosmos-exporter
```

That's not really interesting, what you probably want to do is to have it running in the background. For that, first of all, we have to copy the file to the system apps folder:
That isn't really fascinating, what you probably want to do is to have it running in the background. For that, first of all, we have to copy the file to the system apps folder:

```sh
sudo cp ./cosmos-exporter /usr/bin
Expand Down Expand Up @@ -71,7 +94,7 @@ If you need to, you can also see the logs of the process:
sudo journalctl -u cosmos-exporter -f --output cat
```

## How can I scrape data from it?
## How can I scrape data from it? (original)

Here's the example of the Prometheus config you can use for scraping data:

Expand Down Expand Up @@ -117,7 +140,7 @@ scrape-configs:

Then restart Prometheus and you're good to go!

All of the metrics provided by cosmos-exporter have the following prefixes:
All the metrics provided by cosmos-exporter have the following prefixes:
- `cosmos_validator_*` - metrics related to a single validator
- `cosmos_validators_*` - metrics related to a validator set
- `cosmos_wallet_*` - metrics related to a single wallet
Expand All @@ -128,7 +151,7 @@ It queries the full node via gRPC and returns it in the format Prometheus can co

## How can I configure it?

You can pass the artuments to the executable file to configure it. Here is the parameters list:
You can pass the arguments to the executable file to configure it. Here is the parameters list:

- `--bech-prefix` - the global prefix for addresses. Defaults to `persistence`
- `--denom` - the currency, for example, `uatom` for Cosmos. Defaults to `uxprt`
Expand Down Expand Up @@ -169,3 +192,4 @@ In theory, it should work on a Cosmos-based blockchains with cosmos-sdk >= 0.40.
## How can I contribute?

Bug reports and feature requests are always welcome! If you want to contribute, feel free to open issues or PRs.

93 changes: 93 additions & 0 deletions delegator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package main

import (
"context"
"net/http"
"sync"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
querytypes "github.com/cosmos/cosmos-sdk/types/query"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/google/uuid"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

func (s *service) DelegatorHandler(w http.ResponseWriter, r *http.Request) {
requestStart := time.Now()

sublogger := log.With().
Str("request-id", uuid.New().String()).
Logger()

validatorAddress := r.URL.Query().Get("validator_address")
valAddress, err := sdk.ValAddressFromBech32(validatorAddress)
if err != nil {
sublogger.Error().
Str("validator_address", validatorAddress).
Err(err).
Msg("Could not get validator address")
return
}

delegatorTotalGauge := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "cosmos_validator_delegator_total",
Help: "Number of delegators in validator",
ConstLabels: ConstLabels,
},
[]string{"validator_address"},
)

registry := prometheus.NewRegistry()
registry.MustRegister(delegatorTotalGauge)

var wg sync.WaitGroup

wg.Add(1)
go func() {
defer wg.Done()
sublogger.Debug().
Str("validator_address", validatorAddress).
Msg("Started querying delegator")
queryStart := time.Now()

stakingClient := stakingtypes.NewQueryClient(s.grpcConn)
delegatorRes, err := stakingClient.ValidatorDelegations(
context.Background(),
&stakingtypes.QueryValidatorDelegationsRequest{
ValidatorAddr: valAddress.String(),
Pagination: &querytypes.PageRequest{
Limit: Limit,
},
},
)
if err != nil {
sublogger.Error().
Str("validator_address", validatorAddress).
Err(err).
Msg("Could not get delegator")
return
}

sublogger.Debug().
Str("validator_address", validatorAddress).
Float64("request-time", time.Since(queryStart).Seconds()).
Msg("Finished querying delegators")

delegatorTotalGauge.With(prometheus.Labels{
"validator_address": validatorAddress,
}).Set(float64(len(delegatorRes.DelegationResponses)))
}()

wg.Wait()

h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
h.ServeHTTP(w, r)
sublogger.Info().
Str("method", "GET").
Str("endpoint", "/metrics/delegator?validator_address="+validatorAddress).
Float64("request-time", time.Since(requestStart).Seconds()).
Msg("Request processed")
}
Loading