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

chore(tm2): add prometheus exporter #2032

Closed
wants to merge 3 commits into from
Closed
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
6 changes: 6 additions & 0 deletions contribs/gnodev/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ require (
require (
dario.cat/mergo v1.0.0 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.3 // indirect
github.com/btcsuite/btcd/btcutil v1.1.5 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cockroachdb/apd/v3 v3.2.1 // indirect
github.com/cosmos/ledger-cosmos-go v0.13.3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
Expand All @@ -46,6 +48,10 @@ require (
github.com/peterbourgon/ff/v3 v3.4.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.19.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.48.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/rivo/uniseg v0.4.3 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/rs/cors v1.10.1 // indirect
Expand Down
12 changes: 12 additions & 0 deletions contribs/gnodev/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions contribs/gnokeykc/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ require (

require (
github.com/alessio/shellescape v1.4.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.3 // indirect
github.com/btcsuite/btcd/btcutil v1.1.5 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cosmos/ledger-cosmos-go v0.13.3 // indirect
github.com/danieljoos/wincred v1.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
Expand All @@ -31,6 +33,10 @@ require (
github.com/peterbourgon/ff/v3 v3.4.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.19.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.48.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/rs/xid v1.5.0 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 // indirect
Expand Down
12 changes: 12 additions & 0 deletions contribs/gnokeykc/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ require (
github.com/pelletier/go-toml v1.9.5
github.com/peterbourgon/ff/v3 v3.4.0
github.com/pmezard/go-difflib v1.0.0
github.com/prometheus/client_golang v1.19.0
github.com/rogpeppe/go-internal v1.12.0
github.com/rs/cors v1.10.1
github.com/rs/xid v1.5.0
Expand All @@ -50,7 +51,9 @@ require (
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/gdamore/encoding v1.0.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
Expand All @@ -61,6 +64,9 @@ require (
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/nxadm/tail v1.4.11 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.48.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/rivo/uniseg v0.4.3 // indirect
github.com/zondax/hid v0.9.2 // indirect
github.com/zondax/ledger-go v0.14.3 // indirect
Expand Down
12 changes: 12 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tm2/pkg/bft/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/gnolang/gno/tm2/pkg/errors"
osm "github.com/gnolang/gno/tm2/pkg/os"
p2p "github.com/gnolang/gno/tm2/pkg/p2p/config"
telemetry "github.com/gnolang/gno/tm2/pkg/telemetry/config"
"github.com/gnolang/gno/tm2/pkg/telemetry"
)

var (
Expand Down
15 changes: 12 additions & 3 deletions tm2/pkg/bft/consensus/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package consensus

import (
"bytes"
"context"
goerrors "errors"
"fmt"
"log/slog"
Expand All @@ -26,7 +25,6 @@ import (
"github.com/gnolang/gno/tm2/pkg/p2p"
"github.com/gnolang/gno/tm2/pkg/service"
"github.com/gnolang/gno/tm2/pkg/telemetry"
"github.com/gnolang/gno/tm2/pkg/telemetry/metrics"
)

// -----------------------------------------------------------------------------
Expand Down Expand Up @@ -1000,7 +998,7 @@ func (cs *ConsensusState) createProposalBlock() (block *types.Block, blockParts
if telemetry.MetricsEnabled() {
startTime := time.Now()
defer func(t time.Time) {
metrics.BuildBlockTimer.Record(context.Background(), time.Since(t).Milliseconds())
telemetry.RecordBuildBlockTimer(time.Since(t))
}(startTime)
}

Expand Down Expand Up @@ -1379,6 +1377,8 @@ func (cs *ConsensusState) finalizeCommit(height int64) {

fail.Fail() // XXX

cs.recordMetrics(block)

// NewHeightStep!
cs.updateToState(stateCopy)

Expand All @@ -1394,6 +1394,15 @@ func (cs *ConsensusState) finalizeCommit(height int64) {
// * cs.StartTime is set to when we will start round0.
}

func (cs *ConsensusState) recordMetrics(block *types.Block) {
if block.Height > 1 {
lastBlockMeta := cs.blockStore.LoadBlockMeta(block.Height - 1)
if lastBlockMeta != nil {
telemetry.RecordBlockIntervalSeconds(block.Time.Sub(lastBlockMeta.Header.Time))
}
}
}

// -----------------------------------------------------------------------------

func (cs *ConsensusState) defaultSetProposal(proposal *types.Proposal) error {
Expand Down
6 changes: 1 addition & 5 deletions tm2/pkg/p2p/switch.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package p2p

import (
"context"
"fmt"
"math"
"sync"
Expand All @@ -14,7 +13,6 @@ import (
"github.com/gnolang/gno/tm2/pkg/random"
"github.com/gnolang/gno/tm2/pkg/service"
"github.com/gnolang/gno/tm2/pkg/telemetry"
"github.com/gnolang/gno/tm2/pkg/telemetry/metrics"
)

const (
Expand Down Expand Up @@ -269,9 +267,7 @@ func (sw *Switch) Broadcast(chID byte, msgBytes []byte) chan bool {
go func() {
wg.Wait()
close(successChan)
if telemetry.MetricsEnabled() {
metrics.BroadcastTxTimer.Record(context.Background(), time.Since(startTime).Milliseconds())
}
telemetry.RecordBroadcastTxTimer(time.Since(startTime))
}()

return successChan
Expand Down
39 changes: 39 additions & 0 deletions tm2/pkg/telemetry/collector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package telemetry

import (
"reflect"
"time"
)

type Collector interface {
RecordBroadcastTxTimer(data time.Duration)
RecordBuildBlockTimer(data time.Duration)
RecordBlockIntervalSeconds(data time.Duration)
}

func RecordBroadcastTxTimer(data time.Duration) {
if promCollector != nil && !reflect.ValueOf(promCollector).IsNil() {
promCollector.RecordBroadcastTxTimer(data)
}
if opentlmCollector != nil && !reflect.ValueOf(opentlmCollector).IsNil() {
opentlmCollector.RecordBroadcastTxTimer(data)
}
}

func RecordBuildBlockTimer(data time.Duration) {
if promCollector != nil && !reflect.ValueOf(promCollector).IsNil() {
promCollector.RecordBuildBlockTimer(data)
}
if opentlmCollector != nil && !reflect.ValueOf(opentlmCollector).IsNil() {
opentlmCollector.RecordBuildBlockTimer(data)
}
}

func RecordBlockIntervalSeconds(data time.Duration) {
if promCollector != nil && !reflect.ValueOf(promCollector).IsNil() {
promCollector.RecordBlockIntervalSeconds(data)
}
if opentlmCollector != nil && !reflect.ValueOf(opentlmCollector).IsNil() {
opentlmCollector.RecordBlockIntervalSeconds(data)
}
}
56 changes: 45 additions & 11 deletions tm2/pkg/telemetry/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,65 @@ package telemetry
import (
"sync/atomic"

"github.com/gnolang/gno/tm2/pkg/telemetry/config"
"github.com/gnolang/gno/tm2/pkg/telemetry/metrics"
opentlmConfig "github.com/gnolang/gno/tm2/pkg/telemetry/opentlm/config"
"github.com/gnolang/gno/tm2/pkg/telemetry/opentlm/metrics"
"github.com/gnolang/gno/tm2/pkg/telemetry/prom"
)

var (
globalConfig config.Config
globalConfigSet atomic.Bool
isConfigSet atomic.Bool

promCollector Collector = nil
opentlmCollector Collector = nil
)

type Config struct {
OpenTelemetry *opentlmConfig.Config `toml:"opentelemetry"`
Prometheus *prom.Config `toml:"prometheus"`
}

func DefaultTelemetryConfig() *Config {
return &Config{
OpenTelemetry: opentlmConfig.DefaultConfig(),
Prometheus: prom.DefaultConfig(),
}
}

func TestTelemetryConfig() *Config {
return DefaultTelemetryConfig()
}

// MetricsEnabled returns true if metrics have been initialized.
func MetricsEnabled() bool {
return globalConfig.MetricsEnabled
return promCollector != nil || opentlmCollector != nil
}

// Init sets the configuration for telemetry to c, and if telemetry is enabled,
// starts tracking.
// Init may only be called once. Multiple calls to Init will panic.
func Init(c config.Config) error {
if !globalConfigSet.CompareAndSwap(false, true) {
func Init(c Config) error {
if !isConfigSet.CompareAndSwap(false, true) {
panic("telemetry configuration has already been set and initialised")
}
globalConfig = c
// Initialize metrics to be collected.
if c.MetricsEnabled {
return metrics.Init(c)

var err error

// Initialize opentelemetry metrics to be collected.
if c.OpenTelemetry.MetricsEnabled {
opentlmCollector, err = metrics.Init(c.OpenTelemetry)
if err != nil {
// NOTE(albttx): When endpoint isn't specify, this error is return
// and create a CONSENSUS fail error.
return err
}
}

// Initialize prometheus metrics to be collected
if c.Prometheus.MetricsEnabled {
promCollector, err = prom.Init(c.Prometheus)
if err != nil {
return err
}
}
return nil
}
Loading
Loading