github.com/pingcap/br@v5.3.0-alpha.0.20220125034240-ec59c7b6ce30+incompatible/pkg/rtree/logging.go (about)

     1  // Copyright 2021 PingCAP, Inc. Licensed under Apache-2.0.
     2  
     3  package rtree
     4  
     5  import (
     6  	"fmt"
     7  
     8  	"go.uber.org/zap"
     9  	"go.uber.org/zap/zapcore"
    10  
    11  	"github.com/pingcap/br/pkg/logutil"
    12  	"github.com/pingcap/br/pkg/redact"
    13  )
    14  
    15  // String formats a range to a string.
    16  func (rg Range) String() string {
    17  	return fmt.Sprintf("[%s, %s)", redact.Key(rg.StartKey), redact.Key(rg.EndKey))
    18  }
    19  
    20  // ZapRanges make zap fields for logging Range slice.
    21  func ZapRanges(ranges []Range) zapcore.Field {
    22  	return zap.Object("ranges", rangesMarshaler(ranges))
    23  }
    24  
    25  type rangesMarshaler []Range
    26  
    27  func (rs rangesMarshaler) MarshalLogArray(encoder zapcore.ArrayEncoder) error {
    28  	for _, r := range rs {
    29  		encoder.AppendString(r.String())
    30  	}
    31  	return nil
    32  }
    33  
    34  func (rs rangesMarshaler) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
    35  	total := len(rs)
    36  	encoder.AddInt("total", total)
    37  	elements := make([]string, 0, total)
    38  	for _, r := range rs {
    39  		elements = append(elements, r.String())
    40  	}
    41  	_ = encoder.AddArray("ranges", logutil.AbbreviatedArrayMarshaler(elements))
    42  
    43  	totalKV := uint64(0)
    44  	totalBytes := uint64(0)
    45  	totalSize := uint64(0)
    46  	totalFile := 0
    47  	for _, r := range rs {
    48  		for _, f := range r.Files {
    49  			totalKV += f.GetTotalKvs()
    50  			totalBytes += f.GetTotalBytes()
    51  			totalSize += f.GetSize_()
    52  		}
    53  		totalFile += len(r.Files)
    54  	}
    55  
    56  	encoder.AddInt("totalFiles", totalFile)
    57  	encoder.AddUint64("totalKVs", totalKV)
    58  	encoder.AddUint64("totalBytes", totalBytes)
    59  	encoder.AddUint64("totalSize", totalBytes)
    60  	return nil
    61  }