synapsecns / sanguine

Synapse Monorepo
MIT License
35 stars 25 forks source link

[submitter] extract metrics into a seperate struct #2820

Closed trajan0x closed 6 days ago

greptile-apps[bot] commented 6 days ago

To extract metrics into a separate struct, follow these steps:

  1. Create a new struct for metrics in submitter.go:
// Metrics contains all the metrics-related fields and methods.
type Metrics struct {
    handler metrics.Handler
    meter   metric.Meter
    numPendingGauge metric.Int64ObservableGauge
    nonceGauge metric.Int64ObservableGauge
    gasBalanceGauge metric.Float64ObservableGauge
    numPendingTxes *hashmap.Map[uint32, int]
    currentNonces *hashmap.Map[uint32, uint64]
    currentGasBalances *hashmap.Map[uint32, *big.Int]
}
  1. Move metrics-related fields from txSubmitterImpl to Metrics:
// In txSubmitterImpl struct
metrics Metrics
  1. Update the NewTransactionSubmitter function to initialize the new Metrics struct:
func NewTransactionSubmitter(metricsHandler metrics.Handler, signer signer.Signer, fetcher ClientFetcher, db db.Service, config config.IConfig) TransactionSubmitter {
    return &txSubmitterImpl{
        db:                 db,
        config:             config,
        metrics:            Metrics{
            handler:            metricsHandler,
            meter:              metricsHandler.Meter(meterName),
            numPendingTxes:     hashmap.New[uint32, int](),
            currentNonces:      hashmap.New[uint32, uint64](),
            currentGasBalances: hashmap.New[uint32, *big.Int](),
        },
        signer:             signer,
        fetcher:            fetcher,
        nonceMux:           mapmutex.NewStringerMapMutex(),
        statusMux:          mapmutex.NewStringMapMutex(),
        retryNow:           make(chan bool, 1),
        lastGasBlockCache:  xsync.NewIntegerMapOf[int, *types.Header](),
    }
}
  1. Move metrics-related methods to the Metrics struct:
func (m *Metrics) setupMetrics() (err error) {
    m.numPendingGauge, err = m.meter.Int64ObservableGauge("num_pending_txes")
    if err != nil {
        return fmt.Errorf("could not create num pending txes gauge: %w", err)
    }

    _, err = m.meter.RegisterCallback(m.recordNumPending, m.numPendingGauge)
    if err != nil {
        return fmt.Errorf("could not register callback: %w", err)
    }

    m.nonceGauge, err = m.meter.Int64ObservableGauge("current_nonce")
    if err != nil {
        return fmt.Errorf("could not create nonce gauge: %w", err)
    }

    _, err = m.meter.RegisterCallback(m.recordNonces, m.nonceGauge)
    if err != nil {
        return fmt.Errorf("could not register callback: %w", err)
    }

    m.gasBalanceGauge, err = m.meter.Float64ObservableGauge("gas_balance")
    if err != nil {
        return fmt.Errorf("could not create gas balance gauge: %w", err)
    }

    _, err = m.meter.RegisterCallback(m.recordBalance, m.gasBalanceGauge)
    if err != nil {
        return fmt.Errorf("could not register callback: %w", err)
    }

    return nil
}
  1. Update method calls in txSubmitterImpl to use the new Metrics struct:
func (t *txSubmitterImpl) Start(parentCtx context.Context) (err error) {
    err = t.metrics.setupMetrics()
    if err != nil {
        return fmt.Errorf("could not setup metrics: %w", err)
    }
    // ... rest of the method
}
  1. Update the SubmitTransaction method to use the new Metrics struct:
func (t *txSubmitterImpl) SubmitTransaction(parentCtx context.Context, chainID *big.Int, call ContractCallType) (nonce uint64, err error) {
    ctx, span := t.metrics.handler.Tracer().Start(parentCtx, "submitter.SubmitTransaction", trace.WithAttributes(
        attribute.Stringer("chainID", chainID),
        attribute.String("caller", runtime.FuncForPC(reflect.ValueOf(call).Pointer()).Name()),
    ))
    // ... rest of the method
}

References

/ethergo/submitter/submitter.go /ethergo/submitter/suite_test.go

#### About Greptile This response provides a starting point for your research, not a precise solution. Help us improve! Please leave a ๐Ÿ‘ if this is helpful and ๐Ÿ‘Ž if it is irrelevant. [Ask Greptile](https://app.greptile.com/chat/github/synapsecns/sanguine/master) ยท [Edit Issue Bot Settings](https://app.greptile.com/apps/github)