storj.io/uplink@v1.13.0/stats.go (about)

     1  // Copyright (C) 2022 Storj Labs, Inc.
     2  // See LICENSE for copying information.
     3  
     4  package uplink
     5  
     6  import (
     7  	"context"
     8  	"time"
     9  
    10  	"github.com/zeebo/errs"
    11  
    12  	"storj.io/common/paths"
    13  	"storj.io/common/rpc"
    14  	"storj.io/common/storj"
    15  )
    16  
    17  const eventErrorMessageLimit = 64
    18  
    19  type operationStats struct {
    20  	start       time.Time
    21  	quicRollout int
    22  	bytes       int64
    23  	working     time.Duration
    24  	failure     []error
    25  	satellite   string
    26  	encPath     paths.Encrypted
    27  }
    28  
    29  func newOperationStats(ctx context.Context, satellite storj.NodeURL) (os operationStats) {
    30  	os.start = time.Now()
    31  	os.quicRollout = rpc.QUICRolloutPercent(ctx)
    32  	os.satellite = satellite.String()
    33  	return os
    34  }
    35  
    36  func (os *operationStats) trackWorking() func() {
    37  	start := time.Now()
    38  	return func() { os.working += time.Since(start) }
    39  }
    40  
    41  func (os *operationStats) flagFailure(err error) {
    42  	if err != nil {
    43  		os.failure = append(os.failure, err)
    44  	}
    45  }
    46  
    47  func (os *operationStats) err() (message string, err error) {
    48  	err = errs.Combine(os.failure...)
    49  	if err != nil {
    50  		message = err.Error()
    51  		if len(message) > eventErrorMessageLimit {
    52  			message = message[:eventErrorMessageLimit]
    53  		}
    54  	}
    55  
    56  	return message, err
    57  }