github.com/qiniu/go-sdk/v7@v7.27.0/internal/uplog/block_uplog.go (about)

     1  package uplog
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"runtime"
     7  	"sync/atomic"
     8  	"time"
     9  
    10  	"github.com/qiniu/go-sdk/v7/conf"
    11  )
    12  
    13  type blockUplog struct {
    14  	LogType           LogType      `json:"log_type,omitempty"`
    15  	UpApiVersion      UpApiVersion `json:"up_api_version,omitempty"`
    16  	TotalElapsedTime  uint64       `json:"total_elapsed_time,omitempty"`
    17  	RequestsCount     uint64       `json:"requests_count,omitempty"`
    18  	BytesSent         uint64       `json:"bytes_sent,omitempty"`
    19  	BytesReceived     uint64       `json:"bytes_received,omitempty"`
    20  	RecoveredFrom     uint64       `json:"recovered_from,omitempty"`
    21  	FileSize          uint64       `json:"file_size,omitempty"`
    22  	APIType           APIType      `json:"api_type,omitempty"`
    23  	UpTime            int64        `json:"up_time,omitempty"`
    24  	TargetBucket      string       `json:"target_bucket,omitempty"`
    25  	TargetKey         string       `json:"target_key,omitempty"`
    26  	OSName            string       `json:"os_name,omitempty"`
    27  	OSVersion         string       `json:"os_version,omitempty"`
    28  	OSArch            string       `json:"os_arch,omitempty"`
    29  	SDKName           string       `json:"sdk_name,omitempty"`
    30  	SDKVersion        string       `json:"sdk_version,omitempty"`
    31  	HTTPClient        string       `json:"http_client,omitempty"`
    32  	HTTPClientVersion string       `json:"http_client_version,omitempty"`
    33  	ErrorType         ErrorType    `json:"error_type,omitempty"`
    34  	ErrorDescription  string       `json:"error_description,omitempty"`
    35  	PerceptiveSpeed   uint64       `json:"perceptive_speed,omitempty"`
    36  }
    37  
    38  func WithBlock(ctx context.Context, upApiVersion UpApiVersion, fileSize, recoveredFrom uint64, targetBucket, targetKey, upToken string, handle func(context.Context) error) error {
    39  	if !IsUplogEnabled() {
    40  		return handle(ctx)
    41  	}
    42  
    43  	uplog := blockUplog{
    44  		LogType:           LogTypeBlock,
    45  		UpApiVersion:      upApiVersion,
    46  		FileSize:          fileSize,
    47  		RecoveredFrom:     recoveredFrom,
    48  		TargetBucket:      truncate(targetBucket, maxFieldValueLength),
    49  		TargetKey:         truncate(targetKey, maxFieldValueLength),
    50  		APIType:           APITypeKodo,
    51  		OSName:            truncate(runtime.GOOS, maxFieldValueLength),
    52  		OSVersion:         truncate(getOsVersion(), maxFieldValueLength),
    53  		OSArch:            truncate(runtime.GOARCH, maxFieldValueLength),
    54  		SDKName:           "go",
    55  		SDKVersion:        truncate(conf.Version, maxFieldValueLength),
    56  		HTTPClient:        truncate(getHttpClientName(), maxFieldValueLength),
    57  		HTTPClientVersion: truncate(conf.Version, maxFieldValueLength),
    58  	}
    59  	bytesSentTotal, ctx := withBytesSentTotalContext(ctx, true)
    60  	bytesReceivedTotal, ctx := withBytesReceivedTotalContext(ctx, true)
    61  	requestsCount, ctx := withRequestsCountContext(ctx, true)
    62  	beginAt := time.Now()
    63  	err := handle(ctx)
    64  	uplog.TotalElapsedTime = getElapsedTime(beginAt)
    65  	if err != nil {
    66  		uplog.ErrorType = detectErrorType(err)
    67  		uplog.ErrorDescription = truncate(err.Error(), maxFieldValueLength)
    68  	}
    69  	uplog.BytesSent = atomic.LoadUint64(bytesSentTotal)
    70  	uplog.BytesReceived = atomic.LoadUint64(bytesReceivedTotal)
    71  	uplog.RequestsCount = atomic.LoadUint64(requestsCount)
    72  	if uplog.TotalElapsedTime > 0 {
    73  		if uplog.BytesSent > uplog.BytesReceived {
    74  			uplog.PerceptiveSpeed = uplog.BytesSent * 1000 / uplog.TotalElapsedTime
    75  		} else {
    76  			uplog.PerceptiveSpeed = uplog.BytesReceived * 1000 / uplog.TotalElapsedTime
    77  		}
    78  	}
    79  	if uplogBytes, jsonError := json.Marshal(uplog); jsonError == nil {
    80  		uplogChan <- uplogSerializedEntry{serializedUplog: uplogBytes, getUpToken: func() (string, error) {
    81  			return upToken, nil
    82  		}}
    83  	}
    84  	return err
    85  }