Skip to content

Commit

Permalink
feat(metrics): Use modular telemetry package with easy configuration (#…
Browse files Browse the repository at this point in the history
…85)

* use modular telemetry package with easy configuration

* format

* nit renaming

* dd config

* Example app
  • Loading branch information
calbera authored May 14, 2024
1 parent 80ff8fe commit 5c99a69
Show file tree
Hide file tree
Showing 13 changed files with 719 additions and 23 deletions.
2 changes: 0 additions & 2 deletions examples/listener/.env

This file was deleted.

18 changes: 18 additions & 0 deletions examples/listener/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package app

import (
"github.com/berachain/offchain-sdk/log"
"github.com/berachain/offchain-sdk/telemetry"

"github.com/berachain/offchain-sdk/baseapp"
coreapp "github.com/berachain/offchain-sdk/core/app"
Expand All @@ -22,6 +23,7 @@ var _ coreapp.App[config.Config] = &ListenerApp{}
// The event is watched by the offchain-sdk and when it is emitted, the execution function is called.
type ListenerApp struct {
*baseapp.BaseApp
metrics telemetry.Metrics
}

// Name implements the `App` interface.
Expand All @@ -35,6 +37,22 @@ func (app *ListenerApp) Setup(
config config.Config,
logger log.Logger,
) {
var err error

// Set up metrics instance
app.metrics, err = telemetry.NewMetrics(&config.Metrics)
if err != nil {
logger.Error("error setting up metrics", "error", err)
return
}

// Spin up Prometheus HTTP server
if config.Metrics.Prometheus.Enabled {
if err = ab.RegisterPrometheusTelemetry(); err != nil {
panic(err)
}
}

// This job is subscribed to the `NumberChanged(uint256)` event.
ab.RegisterJob(
jobs.NewEthSub(
Expand Down
15 changes: 15 additions & 0 deletions examples/listener/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,26 @@ EventName = "NumberChanged(uint256)"
[App.Jobs.Poller]
Interval = "1s"

[App.Metrics]
HealthReportInterval = "5s"

# NOTE: if not enabling Datadog, can omit this section.
[App.Telemetry.Datadog]
Enabled = false
StatsdAddr = ""
Namespace = ""

[App.Metrics.Prometheus]
Enabled = true
Namespace = "example"
Subsystem = "listener_app"

[App.ConnectionPool]
EthHTTPURLs = ["http://localhost:10545"]
EthWSURLs = ["ws://localhost:10546"]
DefaultTimeout = "5s"
HealthCheckInterval = "5s"

# For Prometheus to run, must also expose the HTTP server endpoint.
[Server.HTTP]
Port = 8080
9 changes: 7 additions & 2 deletions examples/listener/config/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package config

import "time"
import (
"time"

"github.com/berachain/offchain-sdk/telemetry"
)

type SubStruct struct {
AddressToListen string
Expand All @@ -17,5 +21,6 @@ type Jobs struct {
}

type Config struct {
Jobs Jobs
Jobs Jobs
Metrics telemetry.Config
}
19 changes: 13 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.21

require (
cosmossdk.io/log v1.3.0
github.com/DataDog/datadog-go/v5 v5.5.0
github.com/alitto/pond v1.8.3
github.com/aws/aws-sdk-go-v2 v1.23.1
github.com/aws/aws-sdk-go-v2/config v1.18.45
Expand All @@ -12,14 +13,16 @@ require (
github.com/berachain/go-utils v0.0.0-20240108175945-0bf7199282f7
github.com/ethereum/go-ethereum v1.13.4
github.com/golangci/golangci-lint v1.54.1
github.com/google/uuid v1.3.0
github.com/hashicorp/golang-lru/v2 v2.0.7
github.com/holiman/uint256 v1.2.3
github.com/huandu/skiplist v1.2.0
github.com/prometheus/client_golang v1.17.0
github.com/rs/zerolog v1.31.0
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.17.0
github.com/stretchr/testify v1.8.4
go-micro.dev/v4 v4.10.2
google.golang.org/grpc v1.62.1
)

require (
Expand Down Expand Up @@ -116,6 +119,7 @@ require (
github.com/golangci/revgrep v0.0.0-20220804021717-745bb2f7c2e6 // indirect
github.com/golangci/unconvert v0.0.0-20180507085042-28b1c447d1f4 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gordonklaus/ineffassign v0.0.0-20230610083614-0e73809eb601 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/gostaticanalysis/analysisutil v0.7.1 // indirect
Expand All @@ -128,7 +132,6 @@ require (
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hexops/gotextdiff v1.0.3 // indirect
github.com/holiman/bloomfilter/v2 v2.0.3 // indirect
github.com/holiman/uint256 v1.2.3 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jgautheron/goconst v1.5.1 // indirect
github.com/jingyugao/rowserrcheck v1.1.1 // indirect
Expand Down Expand Up @@ -157,6 +160,7 @@ require (
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mbilski/exhaustivestruct v1.2.0 // indirect
github.com/mgechev/revive v1.3.2 // indirect
github.com/miekg/dns v1.1.43 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mmcloughlin/addchain v0.4.0 // indirect
Expand All @@ -167,6 +171,7 @@ require (
github.com/nishanths/predeclared v0.2.2 // indirect
github.com/nunnatsa/ginkgolinter v0.13.3 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
Expand Down Expand Up @@ -227,15 +232,17 @@ require (
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/crypto v0.15.0 // indirect
golang.org/x/crypto v0.18.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/exp/typeparams v0.0.0-20230307190834-24139beb5833 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.13.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240123012728-ef4313101c80 // indirect
google.golang.org/protobuf v1.32.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
Loading

0 comments on commit 5c99a69

Please sign in to comment.