Skip to content

Commit

Permalink
chore: add RecordBlockInterval (in seconds)
Browse files Browse the repository at this point in the history
  • Loading branch information
albttx committed May 6, 2024
1 parent 2781d08 commit 447fc06
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 14 deletions.
11 changes: 11 additions & 0 deletions tm2/pkg/bft/consensus/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -1377,6 +1377,8 @@ func (cs *ConsensusState) finalizeCommit(height int64) {

fail.Fail() // XXX

cs.recordMetrics(block)

// NewHeightStep!
cs.updateToState(stateCopy)

Expand All @@ -1392,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
25 changes: 17 additions & 8 deletions tm2/pkg/telemetry/collector.go
Original file line number Diff line number Diff line change
@@ -1,30 +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) {
switch {
case promCollector != nil:
if promCollector != nil && !reflect.ValueOf(promCollector).IsNil() {
promCollector.RecordBroadcastTxTimer(data)
fallthrough
case opentlmCollector != nil:
}
if opentlmCollector != nil && !reflect.ValueOf(opentlmCollector).IsNil() {
opentlmCollector.RecordBroadcastTxTimer(data)
}
}

func RecordBuildBlockTimer(data time.Duration) {
switch {
case promCollector != nil:
if promCollector != nil && !reflect.ValueOf(promCollector).IsNil() {
promCollector.RecordBuildBlockTimer(data)
fallthrough
case opentlmCollector != nil:
}
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)
}
}
6 changes: 4 additions & 2 deletions tm2/pkg/telemetry/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
var (
isConfigSet atomic.Bool

promCollector Collector
opentlmCollector Collector
promCollector Collector = nil
opentlmCollector Collector = nil
)

type Config struct {
Expand Down Expand Up @@ -53,6 +53,8 @@ func Init(c Config) error {
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
}
}
Expand Down
17 changes: 15 additions & 2 deletions tm2/pkg/telemetry/opentlm/metrics/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ import (

// Collector is complient with the telemetry.Collector interface
type Collector struct {
broadcastTxTimer metric.Int64Histogram
buildBlockTimer metric.Int64Histogram
broadcastTxTimer metric.Int64Histogram
buildBlockTimer metric.Int64Histogram
blockIntervalSeconds metric.Int64Histogram
}

// Collector is complient with the telemetry.Collector interface
Expand All @@ -35,6 +36,10 @@ func (c *Collector) RecordBuildBlockTimer(data time.Duration) {
c.buildBlockTimer.Record(context.Background(), data.Milliseconds())
}

func (c *Collector) RecordBlockIntervalSeconds(data time.Duration) {
c.blockIntervalSeconds.Record(context.Background(), data.Milliseconds())
}

func Init(config *config.Config) (*Collector, error) {
if config.ExporterEndpoint == "" {
return nil, exporter.ErrEndpointNotSet
Expand Down Expand Up @@ -83,5 +88,13 @@ func Init(config *config.Config) (*Collector, error) {
return nil, err
}

if collector.blockIntervalSeconds, err = meter.Int64Histogram(
"block_interval_seconds",
metric.WithDescription("block interval duration"),
metric.WithUnit("s"),
); err != nil {
return nil, err
}

return collector, nil
}
9 changes: 7 additions & 2 deletions tm2/pkg/telemetry/prom/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (

// Collector is complient with the telemetry.Collector interface
type Collector struct {
broadcastTxTimer prometheus.Histogram
buildBlockTimer prometheus.Histogram
broadcastTxTimer prometheus.Histogram
buildBlockTimer prometheus.Histogram
blockIntervalSeconds prometheus.Histogram
}

func (c *Collector) RecordBroadcastTxTimer(data time.Duration) {
Expand All @@ -19,3 +20,7 @@ func (c *Collector) RecordBroadcastTxTimer(data time.Duration) {
func (c *Collector) RecordBuildBlockTimer(data time.Duration) {
c.buildBlockTimer.Observe(float64(data.Milliseconds()))
}

func (c *Collector) RecordBlockIntervalSeconds(data time.Duration) {
c.blockIntervalSeconds.Observe(data.Seconds())
}
8 changes: 8 additions & 0 deletions tm2/pkg/telemetry/prom/prom.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,17 @@ func Init(cfg *Config) (*Collector, error) {
Help: "block build duration",
})

c.blockIntervalSeconds = prometheus.NewHistogram(
prometheus.HistogramOpts{
Namespace: cfg.Namespace,
Name: "block_interval_seconds",
Help: "block interval in seconds",
})

prometheus.MustRegister(
c.broadcastTxTimer,
c.buildBlockTimer,
c.blockIntervalSeconds,
)

go func() {
Expand Down

0 comments on commit 447fc06

Please sign in to comment.