github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/consensus/hotstuff/blockproducer/metrics_wrapper.go (about)

     1  package blockproducer
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/onflow/flow-go/model/flow"
     7  	"github.com/onflow/flow-go/module"
     8  )
     9  
    10  // BlockBuilderMetricsWrapper implements the module.Builder interface.
    11  // It wraps a module.Builder instance and measures the time which HotStuff's core logic
    12  // spends in the module.Builder component, i.e. the with generating block payloads.
    13  // The measured time durations are reported as values for the
    14  // PayloadProductionDuration metric.
    15  type BlockBuilderMetricsWrapper struct {
    16  	builder module.Builder
    17  	metrics module.HotstuffMetrics
    18  }
    19  
    20  func NewMetricsWrapper(builder module.Builder, metrics module.HotstuffMetrics) *BlockBuilderMetricsWrapper {
    21  	return &BlockBuilderMetricsWrapper{
    22  		builder: builder,
    23  		metrics: metrics,
    24  	}
    25  }
    26  
    27  func (w BlockBuilderMetricsWrapper) BuildOn(parentID flow.Identifier, setter func(*flow.Header) error, sign func(*flow.Header) error) (*flow.Header, error) {
    28  	processStart := time.Now()
    29  	header, err := w.builder.BuildOn(parentID, setter, sign)
    30  	w.metrics.PayloadProductionDuration(time.Since(processStart))
    31  	return header, err
    32  }