github.com/onflow/flow-go@v0.33.17/consensus/hotstuff/committees/metrics_wrapper.go (about)

     1  // (c) 2020 Dapper Labs - ALL RIGHTS RESERVED
     2  package committees
     3  
     4  import (
     5  	"time"
     6  
     7  	"github.com/onflow/flow-go/consensus/hotstuff"
     8  	"github.com/onflow/flow-go/model/flow"
     9  	"github.com/onflow/flow-go/module"
    10  )
    11  
    12  // CommitteeMetricsWrapper implements the hotstuff.DynamicCommittee interface.
    13  // It wraps a hotstuff.DynamicCommittee instance and measures the time which the HotStuff's core logic
    14  // spends in the hotstuff.DynamicCommittee component, i.e. the time determining consensus committee
    15  // relations. The measured time durations are reported as values for the
    16  // CommitteeProcessingDuration metric.
    17  type CommitteeMetricsWrapper struct {
    18  	committee hotstuff.DynamicCommittee
    19  	metrics   module.HotstuffMetrics
    20  }
    21  
    22  var _ hotstuff.Replicas = (*CommitteeMetricsWrapper)(nil)
    23  var _ hotstuff.DynamicCommittee = (*CommitteeMetricsWrapper)(nil)
    24  
    25  func NewMetricsWrapper(committee hotstuff.DynamicCommittee, metrics module.HotstuffMetrics) *CommitteeMetricsWrapper {
    26  	return &CommitteeMetricsWrapper{
    27  		committee: committee,
    28  		metrics:   metrics,
    29  	}
    30  }
    31  
    32  func (w CommitteeMetricsWrapper) IdentitiesByBlock(blockID flow.Identifier) (flow.IdentityList, error) {
    33  	processStart := time.Now()
    34  	identities, err := w.committee.IdentitiesByBlock(blockID)
    35  	w.metrics.CommitteeProcessingDuration(time.Since(processStart))
    36  	return identities, err
    37  }
    38  
    39  func (w CommitteeMetricsWrapper) IdentityByBlock(blockID flow.Identifier, participantID flow.Identifier) (*flow.Identity, error) {
    40  	processStart := time.Now()
    41  	identity, err := w.committee.IdentityByBlock(blockID, participantID)
    42  	w.metrics.CommitteeProcessingDuration(time.Since(processStart))
    43  	return identity, err
    44  }
    45  
    46  func (w CommitteeMetricsWrapper) IdentitiesByEpoch(view uint64) (flow.IdentityList, error) {
    47  	processStart := time.Now()
    48  	identities, err := w.committee.IdentitiesByEpoch(view)
    49  	w.metrics.CommitteeProcessingDuration(time.Since(processStart))
    50  	return identities, err
    51  }
    52  
    53  func (w CommitteeMetricsWrapper) IdentityByEpoch(view uint64, participantID flow.Identifier) (*flow.Identity, error) {
    54  	processStart := time.Now()
    55  	identity, err := w.committee.IdentityByEpoch(view, participantID)
    56  	w.metrics.CommitteeProcessingDuration(time.Since(processStart))
    57  	return identity, err
    58  }
    59  
    60  func (w CommitteeMetricsWrapper) LeaderForView(view uint64) (flow.Identifier, error) {
    61  	processStart := time.Now()
    62  	id, err := w.committee.LeaderForView(view)
    63  	w.metrics.CommitteeProcessingDuration(time.Since(processStart))
    64  	return id, err
    65  }
    66  
    67  func (w CommitteeMetricsWrapper) QuorumThresholdForView(view uint64) (uint64, error) {
    68  	processStart := time.Now()
    69  	id, err := w.committee.QuorumThresholdForView(view)
    70  	w.metrics.CommitteeProcessingDuration(time.Since(processStart))
    71  	return id, err
    72  }
    73  
    74  func (w CommitteeMetricsWrapper) TimeoutThresholdForView(view uint64) (uint64, error) {
    75  	processStart := time.Now()
    76  	id, err := w.committee.TimeoutThresholdForView(view)
    77  	w.metrics.CommitteeProcessingDuration(time.Since(processStart))
    78  	return id, err
    79  }
    80  
    81  func (w CommitteeMetricsWrapper) Self() flow.Identifier {
    82  	processStart := time.Now()
    83  	id := w.committee.Self()
    84  	w.metrics.CommitteeProcessingDuration(time.Since(processStart))
    85  	return id
    86  }
    87  
    88  func (w CommitteeMetricsWrapper) DKG(view uint64) (hotstuff.DKG, error) {
    89  	processStart := time.Now()
    90  	dkg, err := w.committee.DKG(view)
    91  	w.metrics.CommitteeProcessingDuration(time.Since(processStart))
    92  	return dkg, err
    93  }