github.com/weedge/lib@v0.0.0-20230424045628-a36dcc1d90e4/log/tapper/unit.go (about)

     1  package tapper
     2  
     3  import (
     4  	"strconv"
     5  	"strings"
     6  	"sync"
     7  	"time"
     8  )
     9  
    10  type logUnit struct {
    11  	key   string
    12  	value string
    13  }
    14  
    15  type LogUnits struct {
    16  	cmd          string
    17  	begTime      int64
    18  	units        []logUnit
    19  	timeCostUnit []logUnit
    20  	lock         sync.Mutex
    21  }
    22  
    23  func (p *LogUnits) SetCmd(cmd string) {
    24  	p.cmd = cmd
    25  }
    26  
    27  func (p *LogUnits) SetBeginTime() {
    28  	p.begTime = time.Now().UnixNano()
    29  }
    30  
    31  // add log unit like bd odp addNotice
    32  func (p *LogUnits) AddLogUnit(k string, v string) {
    33  	p.units = append(p.units, logUnit{key: k, value: v})
    34  }
    35  
    36  func (p *LogUnits) AddTimeCost(k string, tNs int64) {
    37  	p.timeCostUnit = append(p.timeCostUnit, logUnit{key: k, value: strconv.FormatInt(tNs/1000000, 10)})
    38  }
    39  
    40  // multi go routine safe add
    41  func (p *LogUnits) AddLogUnitThreadSafe(k string, v string) {
    42  	p.lock.Lock()
    43  	p.units = append(p.units, logUnit{key: k, value: v})
    44  	p.lock.Unlock()
    45  }
    46  
    47  // multi go routine 版本
    48  func (p *LogUnits) AddTimeCostThreadSafe(k string, tNs int64) {
    49  	p.lock.Lock()
    50  	p.timeCostUnit = append(p.timeCostUnit, logUnit{key: k, value: strconv.FormatInt(tNs/1000000, 10)})
    51  	p.lock.Unlock()
    52  }
    53  
    54  func (p *LogUnits) String() string {
    55  	endTime := time.Now().UnixNano()
    56  	//var str string
    57  	var buf strings.Builder
    58  	buf.WriteString(p.cmd)
    59  	buf.WriteString("||cost=")
    60  	buf.WriteString(strconv.FormatInt((endTime-p.begTime)/int64(time.Millisecond), 10))
    61  	for idx := range p.units {
    62  		buf.WriteString(" ")
    63  		buf.WriteString(p.units[idx].key)
    64  		buf.WriteString("[")
    65  		buf.WriteString(p.units[idx].value)
    66  		buf.WriteString("]")
    67  	}
    68  	for idx := range p.timeCostUnit {
    69  		buf.WriteString("||")
    70  		buf.WriteString(p.timeCostUnit[idx].key)
    71  		buf.WriteString("=")
    72  		buf.WriteString(p.timeCostUnit[idx].value)
    73  	}
    74  	return buf.String()
    75  }
    76  
    77  func (p *LogUnits) SerializeTimeCost() string {
    78  	var buf strings.Builder
    79  	buf.WriteString("[")
    80  	for idx := range p.timeCostUnit {
    81  		buf.WriteString("{\"")
    82  		buf.WriteString(p.timeCostUnit[idx].key)
    83  		buf.WriteString("\":")
    84  		buf.WriteString(p.timeCostUnit[idx].value)
    85  		buf.WriteString("}")
    86  		if idx < len(p.timeCostUnit)-1 {
    87  			buf.WriteString(",")
    88  		}
    89  	}
    90  	buf.WriteString("]")
    91  	return buf.String()
    92  }
    93  
    94  func (p *LogUnits) MergeLogUnit(o *LogUnits, prefix string) {
    95  	p.lock.Lock()
    96  	for i := range o.units {
    97  		unit := o.units[i]
    98  		p.units = append(p.units, logUnit{key: prefix + unit.key, value: unit.value})
    99  	}
   100  
   101  	for i := range o.timeCostUnit {
   102  		timeCostUnit := o.timeCostUnit[i]
   103  		p.timeCostUnit = append(p.timeCostUnit, logUnit{key: prefix + timeCostUnit.key, value: timeCostUnit.value})
   104  	}
   105  	p.lock.Unlock()
   106  }