/
util.go
executable file
·59 lines (47 loc) · 1.24 KB
/
util.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
package trace
import (
"context"
"encoding/binary"
"math/rand"
"time"
"github.com/gs02048/micro/pkg/conf/env"
"github.com/gs02048/micro/pkg/net/ip"
"github.com/pkg/errors"
)
var _hostHash byte
func init() {
rand.Seed(time.Now().UnixNano())
_hostHash = byte(oneAtTimeHash(env.Hostname))
}
func extendTag() (tags []Tag) {
tags = append(tags,
TagString("region", env.Region),
TagString("zone", env.Zone),
TagString("hostname", env.Hostname),
TagString("ip", ip.InternalIP()),
)
return
}
func genID() uint64 {
var b [8]byte
// i think this code will not survive to 2106-02-07
binary.BigEndian.PutUint32(b[4:], uint32(time.Now().Unix())>>8)
b[4] = _hostHash
binary.BigEndian.PutUint32(b[:4], uint32(rand.Int31()))
return binary.BigEndian.Uint64(b[:])
}
type stackTracer interface {
StackTrace() errors.StackTrace
}
type ctxKey string
var _ctxkey ctxKey = "micro/pkg/net/trace.trace"
// FromContext returns the trace bound to the context, if any.
func FromContext(ctx context.Context) (t Trace, ok bool) {
t, ok = ctx.Value(_ctxkey).(Trace)
return
}
// NewContext new a trace context.
// NOTE: This method is not thread safe.
func NewContext(ctx context.Context, t Trace) context.Context {
return context.WithValue(ctx, _ctxkey, t)
}