You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
1.5 KiB
69 lines
1.5 KiB
6 years ago
|
package trace
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"encoding/binary"
|
||
|
"math/rand"
|
||
|
"time"
|
||
|
|
||
|
"github.com/bilibili/Kratos/pkg/conf/env"
|
||
|
"github.com/bilibili/Kratos/pkg/net/ip"
|
||
|
"github.com/bilibili/Kratos/pkg/net/metadata"
|
||
|
|
||
|
"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 = "Kratos/pkg/net/trace.trace"
|
||
|
|
||
|
// FromContext returns the trace bound to the context, if any.
|
||
|
func FromContext(ctx context.Context) (t Trace, ok bool) {
|
||
|
if v := metadata.Value(ctx, metadata.Trace); v != nil {
|
||
|
t, ok = v.(Trace)
|
||
|
return
|
||
|
}
|
||
|
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 {
|
||
|
if md, ok := metadata.FromContext(ctx); ok {
|
||
|
md[metadata.Trace] = t
|
||
|
return ctx
|
||
|
}
|
||
|
return context.WithValue(ctx, _ctxkey, t)
|
||
|
}
|