-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlabels.go
154 lines (131 loc) · 3.22 KB
/
labels.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package tlog
import (
"crypto/md5" //nolint
"crypto/sha1" //nolint
"fmt"
"io"
"os"
"os/user"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"
)
// AutoLabels is a list of automatically filled labels
//
// _hostname - local hostname
// _user - current user
// _pid - process pid
// _timezone - local timezone code (UTC, MSK)
// _goversion - go version
// _execmd5 - this binary md5 hash
// _execsha1 - this binary sha1 hash
// _execname - executable base name (project name)
// _randid - random id. May be used to distinguish different runs.
var AutoLabels = map[string]func() interface{}{
"_hostname": func() interface{} { return Hostname() },
"_user": func() interface{} { return User() },
"_os": func() interface{} { return runtime.GOOS },
"_arch": func() interface{} { return runtime.GOARCH },
"_numcpu": func() interface{} { return runtime.NumCPU() },
"_gomaxprocs": func() interface{} { return runtime.GOMAXPROCS(0) },
"_goversion": func() interface{} { return runtime.Version },
"_pid": func() interface{} {
return os.Getpid()
},
"_timezone": func() interface{} {
n, _ := time.Now().Zone()
return n
},
"_execmd5": func() interface{} { return ExecutableMD5() },
"_execsha1": func() interface{} { return ExecutableSHA1() },
"_execname": func() interface{} {
return filepath.Base(os.Args[0])
},
"_randid": func() interface{} {
return MathRandID().StringFull()
},
}
// Hostname returns hostname or err.Error().
func Hostname() string {
h, err := os.Hostname()
if h == "" && err != nil {
h = err.Error()
}
return h
}
// User returns current username or err.Error().
func User() string {
u, err := user.Current()
if u != nil && u.Username != "" {
return u.Username
} else if err != nil {
return err.Error()
}
return ""
}
// ExecutableMD5 returns current process executable md5 hash.
// May be useful to find exact executable later.
func ExecutableMD5() string {
path, err := os.Executable()
if err != nil {
return err.Error()
}
f, err := os.Open(path)
if err != nil {
return err.Error()
}
defer f.Close()
h := md5.New() //nolint
_, err = io.Copy(h, f)
if err != nil {
return err.Error()
}
return fmt.Sprintf("%02x", h.Sum(nil))
}
// ExecutableSHA1 returns current process executable sha1 hash.
// May be useful to find exact executable later.
func ExecutableSHA1() string {
path, err := os.Executable()
if err != nil {
return err.Error()
}
f, err := os.Open(path)
if err != nil {
return err.Error()
}
defer f.Close()
h := sha1.New() //nolint
_, err = io.Copy(h, f)
if err != nil {
return err.Error()
}
return fmt.Sprintf("%02x", h.Sum(nil))
}
// ParseLabels parses comma separated list of labels and fills them with values (See FillLabelsWithDefaults).
func ParseLabels(s string) []interface{} {
l := strings.Split(s, ",")
res := make([]interface{}, 0, len(l)*2)
for _, l := range l {
if l == "" {
continue
}
p := strings.IndexByte(l, '=')
if p == -1 {
if f, ok := AutoLabels[l]; ok {
res = append(res, l, f())
} else {
res = append(res, l, "")
}
continue
}
k, v := l[:p], l[p+1:]
if x, err := strconv.Atoi(v); err == nil {
res = append(res, k, x)
continue
}
res = append(res, k, v)
}
return res
}