-
Notifications
You must be signed in to change notification settings - Fork 8
/
utils.go
92 lines (81 loc) · 2.02 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package tencentcloud_cls_sdk_go
import (
"errors"
"fmt"
"hash/crc64"
"math"
"net"
"strings"
"go.uber.org/atomic"
"google.golang.org/protobuf/proto"
)
// NewCLSLog ...
func NewCLSLog(logTime int64, addLogMap map[string]string) *Log {
var content = make([]*Log_Content, 0)
for key, value := range addLogMap {
content = append(content, &Log_Content{
Key: proto.String(key),
Value: proto.String(value),
})
}
return &Log{
Time: proto.Int64(logTime),
Contents: content,
}
}
func GetTimeMs(t int64) int64 {
return t / 1000 / 1000
}
func GetLogSizeCalculate(log *Log) (int, error) {
sizeInBytes := 4
logContent := log.GetContents()
count := len(logContent)
for i := 0; i < count; i++ {
if len(*logContent[i].Value) > 1*1024*1024 {
return 0, fmt.Errorf("content value can not be than 1M")
}
sizeInBytes += len(*logContent[i].Value)
sizeInBytes += len(*logContent[i].Key)
}
return sizeInBytes, nil
}
func GetLogListSize(logList []*Log) (int, error) {
sizeInBytes := 0
for _, log := range logList {
sz, err := GetLogSizeCalculate(log)
if err != nil {
return 0, err
}
sizeInBytes += sz
}
return sizeInBytes, nil
}
// GetLocalIP get local IP with string format
func GetLocalIP() (string, error) {
addrs, err := net.InterfaceAddrs()
if err != nil {
return "", err
}
for _, address := range addrs {
// 检查ip地址判断是否回环地址
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return ipnet.IP.String(), nil
}
}
}
return "", errors.New("can not find local ip")
}
// GenerateProducerHash ...
func GenerateProducerHash(instanceID string) string {
table := crc64.MakeTable(crc64.ECMA)
hash := crc64.Checksum([]byte(instanceID), table)
hashString := fmt.Sprintf("%016x", hash)
return strings.ToUpper(hashString)
}
func generatePackageId(producerHash string, batchId *atomic.Int64) string {
if batchId.Load() >= math.MaxInt64 {
batchId.Store(0)
}
return strings.ToUpper(fmt.Sprintf("%s-%x", producerHash, batchId.Inc()))
}