Skip to content

Commit

Permalink
feat: Add missed blocks metric (#1)
Browse files Browse the repository at this point in the history
* feat: Add missed blocks metric

* feat: Add ci pipelines

* chore: Refactor name

* feat: Improve README

* Update pkg/grpc/grpc.go

Co-authored-by: Joonas Lehtimäki <[email protected]>

* Update pkg/grpc/grpc.go

Co-authored-by: Joonas Lehtimäki <[email protected]>

* Update pkg/collector/collector.go

Co-authored-by: Joonas Lehtimäki <[email protected]>

* fix: Remove ctx from grpc client struct

* fix: Linter fix

* fix: Use wrapped errors

* fix: Make http server more secure

* fix: Return wrapped errors from grpc package

* fix: Return wrapped error from config package

* fix: Log potential error only during connection close

---------

Co-authored-by: Joonas Lehtimäki <[email protected]>
  • Loading branch information
kayano and jlehtimaki committed Dec 21, 2023
1 parent 83ef538 commit 3ca0ef4
Show file tree
Hide file tree
Showing 16 changed files with 2,233 additions and 4 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/pr-validation.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
name: PR Validation

on:
pull_request:
branches:
- main

jobs:
run-reviewdog:
name: Run ReviewDog
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: ReviewDog - GolangCI Lint
uses: reviewdog/action-golangci-lint@v2
with:
go_version_file: go.mod
tool_name: GolangCI Lint
golangci_lint_flags: --config=.golangci.yaml --timeout=10m
fail_on_error: true
run-tests:
name: Tests
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version-file: go.mod
- name: Tests
run: make test-ci
44 changes: 44 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
name: Release

on:
push:
branches:
- main

jobs:
release:
name: Release
runs-on: ubuntu-latest
steps:
- id: release-please
name: Release Please
uses: google-github-actions/release-please-action@v4
with:
release-type: go

- name: Checkout
uses: actions/checkout@v4
if: steps.release-please.outputs.release_created
with:
fetch-depth: 0

- name: Set up Go
uses: actions/setup-go@v5
if: steps.release-please.outputs.release_created
with:
go-version-file: go.mod

- name: Set up QEMU
uses: docker/setup-qemu-action@v3
if: steps.release-please.outputs.release_created

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
if: steps.release-please.outputs.release_created

- name: Run GoReleaser
if: steps.release-please.outputs.release_created
run: make release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
12 changes: 9 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# vscode
.vscode

# direnv
.envrc

# Test binary, built with `go test -c`
*.test

Expand All @@ -19,3 +22,6 @@

# Go workspace file
go.work

/dist
/build
54 changes: 54 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
output:
sort-results: true

linters:
enable:
- depguard
- gocritic
- gofumpt
- goimports
- revive
- misspell
- unconvert
- unused
- wsl

issues:
max-same-issues: 0
exclude-rules:
- linters:
- gocritic
text: "appendAssign"
- path: _test.go
linters:
- errcheck

linters-settings:
depguard:
rules:
main:
files:
- $all
deny:
- pkg: "github.com/sirupsen/logrus"
desc: Not allowed
- pkg: "github.com/pkg/errors"
desc: Should be replaced by standard lib errors package
- pkg: "sync/atomic"
desc: Use go.uber.org/atomic instead of sync/atomic
- pkg: "io/ioutil"
desc: Use corresponding 'os' or 'io' functions instead.
errcheck:
exclude-functions:
# Don't flag lines such as "io.Copy(io.Discard, resp.Body)".
- io.Copy
# The next two are used in HTTP handlers, any error is handled by the server itself.
- io.WriteString
- (net/http.ResponseWriter).Write
# No need to check for errors on server's shutdown.
- (*net/http.Server).Shutdown
goimports:
local-prefixes: github.com/prometheus/prometheus
gofumpt:
extra-rules: true
57 changes: 57 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
project_name: validator-exporter
before:
hooks:
- go mod tidy
env:
- CGO_ENABLED=0
builds:
- main: "./cmd/{{ .ProjectName }}/main.go"
binary: "{{ .ProjectName }}"
goos:
- linux
- darwin
goarch:
- amd64
- arm64
flags:
- -mod=readonly
- -trimpath
ldflags:
- -s -w
tags:
- netgo

universal_binaries:
- replace: false

dockers:
- use: buildx
goos: linux
goarch: amd64
image_templates:
- "ghcr.io/archway-network/{{ .ProjectName }}:{{ .Version }}-amd64"
build_flag_templates:
- "--pull"
- "--platform=linux/amd64"
- "--label=org.opencontainers.image.created={{.Date}}"
- "--label=org.opencontainers.image.title={{.ProjectName}}"
- "--label=org.opencontainers.image.revision={{.FullCommit}}"
- "--label=org.opencontainers.image.version={{.Version}}"
- use: buildx
goos: linux
goarch: arm64
image_templates:
- "ghcr.io/archway-network/{{ .ProjectName }}:{{ .Version }}-arm64v8"
build_flag_templates:
- "--pull"
- "--platform=linux/arm64"
- "--label=org.opencontainers.image.created={{.Date}}"
- "--label=org.opencontainers.image.title={{.ProjectName}}"
- "--label=org.opencontainers.image.revision={{.FullCommit}}"
- "--label=org.opencontainers.image.version={{.Version}}"
docker_manifests:
- name_template: ghcr.io/archway-network/{{ .ProjectName }}:{{ .Version }}
image_templates:
- ghcr.io/archway-network/{{ .ProjectName }}:{{ .Version }}-amd64
- ghcr.io/archway-network/{{ .ProjectName }}:{{ .Version }}-arm64v8
10 changes: 10 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM gcr.io/distroless/static-debian12:nonroot

COPY validator-exporter /usr/bin/validator-exporter

# metrics server
EXPOSE 8008

ENTRYPOINT [ "/usr/bin/validator-exporter" ]

CMD [ "--help" ]
80 changes: 80 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
BUILD_FOLDER = build
DIST_FOLDER = dist
GORELEASER_VERSION = v1.22.1
DOCKER := $(shell which docker)
PACKAGE_NAME = github.com/archway-network/validator-exporter

.PHONY: all
all: install

.PHONY: install
install: go.sum
go install cmd/validator-exporter/main.go

.PHONY: go.sum
go.sum:
@echo "--> Ensure dependencies have not been modified"
@go mod verify

go-mod-cache: go.sum
@echo "--> Download go modules to local cache"
@go mod download

.PHONY: build
build: go.sum
go build -o build/validator-exporter ./cmd/validator-exporter/main.go

.PHONY: clean
clean:
rm -rf $(BUILD_FOLDER)
rm -rf $(DIST_FOLDER)

.PHONY: test
test:
go test -race ./...

.PHONY: text-cover
test-cover:
go test -race ./... -coverprofile=coverage.out
go tool cover -html=coverage.out
rm coverage.out

.PHONY: test-ci
test-ci:
@go get ./...
@go test ./...

.PHONY: lint
lint:
@golangci-lint run
@go mod verify

.PHONY: update
update:
go get -u -d ./...
@go mod tidy
@go build -o "$(TMPDIR)/validator-exporter" cmd/validator-exporter/main.go
@git diff -- go.mod go.sum


release-dryrun:
$(DOCKER) run \
--rm \
-e GITHUB_TOKEN="$(GITHUB_TOKEN)" \
-v /var/run/docker.sock:/var/run/docker.sock \
-v `pwd`:/go/src/$(PACKAGE_NAME) \
-w /go/src/$(PACKAGE_NAME) \
ghcr.io/goreleaser/goreleaser:$(GORELEASER_VERSION) \
--clean \
--skip=publish,validate \
--snapshot

release:
$(DOCKER) run \
--rm \
-e GITHUB_TOKEN="$(GITHUB_TOKEN)" \
-v /var/run/docker.sock:/var/run/docker.sock \
-v `pwd`:/go/src/$(PACKAGE_NAME) \
-w /go/src/$(PACKAGE_NAME) \
ghcr.io/goreleaser/goreleaser:$(GORELEASER_VERSION) \
--clean
40 changes: 39 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,40 @@
# validator-exporter
Prometheus exporter for validator metrics
Prometheus exporter for cosmos validators metrics.

## Usage
```
Usage of validator-exporter:
-log-level value
Set log level
-p int
Server port (default 8008)
```
### Supported env vars
```
"GRPC_ADDR" envDefault:"grpc.constantine.archway.tech:443"
"GRPC_TLS_ENABLED" envDefault:"true"
"GRPC_TIMEOUT_SECONDS" envDefault:"5"
"PREFIX" envDefault:"archway"
```
### Connecting to archway constantine testnet by default
```
validator-exporter
```
### Connecting to archway mainnet
```
GRPC_ADDR=grpc.mainnet.archway.io:443 validator-exporter
```
### Connecting to localnet
```
GRPC_TLS_ENABLED=false GRPC_ADDR=localhost:9090 validator-exporter --log-level debug
```

## Metrics
```
# HELP cosmos_validator_missed_blocks Returns missed blocks for a validator.
# TYPE cosmos_validator_missed_blocks gauge
cosmos_validator_missed_blocks{moniker="validator_1",valcons="archwayvalcons18le5pevj6sdynyksn77n9z9g8394l3xqk04s3z",valoper="archwayvaloper172zqrqtrwfplwhec44050dhuv66ekcmty4hnfv"} 0
cosmos_validator_missed_blocks{moniker="validator_2",valcons="archwayvalcons1z4q9zpe8l8puwv8aq4dqadkz4zm244pnu72qcd",valoper="archwayvaloper1370vgzkv5l3kylcylwekzjcdt2hjk2k8zrht6c"} 0
cosmos_validator_missed_blocks{moniker="validator_3",valcons="archwayvalcons1ep8hnygqw8gvsdfvyanhcfsmvlrvae4s9hljta",valoper="archwayvaloper1scxt3mgxmw3z2hpf8k4mlssz5qvljmtaplv6nz"} 2
```
Loading

0 comments on commit 3ca0ef4

Please sign in to comment.