-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
145 lines (123 loc) · 3.2 KB
/
context.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
package context
import (
"context"
"github.com/google/uuid"
"github.com/hopeio/cherry/utils/net/http"
timei "github.com/hopeio/cherry/utils/time"
"go.opentelemetry.io/otel/trace"
"google.golang.org/grpc"
"runtime"
"strings"
"sync"
"time"
)
func GetPool[REQ any]() sync.Pool {
return sync.Pool{New: func() any {
return new(RequestContext[REQ])
}}
}
type RequestContext[REQ any] struct {
ctx context.Context
TraceID string
Token string
AuthID string
AuthInfo
AuthInfoRaw string
*DeviceInfo
http.RequestAt
RequestCtx REQ
grpc.ServerTransportStream
Internal string
Values map[string]any
}
func (c *RequestContext[REQ]) StartSpan(name string, o ...trace.SpanStartOption) (*RequestContext[REQ], trace.Span) {
ctx, span := tracer.Start(c.ctx, name, o...)
c.ctx = ctx
if c.TraceID == "" {
c.TraceID = span.SpanContext().TraceID().String()
}
return c, span
}
func Tracing(ctx context.Context, name string) (context.Context, trace.Span) {
if span := trace.SpanFromContext(ctx); span != nil {
return ctx, span
}
if name == "" {
pc, _, _, _ := runtime.Caller(3)
name = runtime.FuncForPC(pc).Name()
}
return tracer.Start(ctx, name)
}
func methodFamily(m string) string {
m = strings.TrimPrefix(m, "/") // remove leading slash
if i := strings.Index(m, "/"); i >= 0 {
m = m[:i] // remove everything from second slash
}
return m
}
type ctxKey struct{}
func (c *RequestContext[REQ]) ContextWrapper() context.Context {
return context.WithValue(c.ctx, ctxKey{}, c)
}
func RequestContextFromContext[REQ any](ctx context.Context) *RequestContext[REQ] {
if ctx == nil {
return NewRequestContext[REQ](context.Background(), "")
}
ctxi := ctx.Value(ctxKey{})
c, ok := ctxi.(*RequestContext[REQ])
if !ok {
var traceId string
span := trace.SpanFromContext(ctx)
if spanContext := span.SpanContext(); spanContext.IsValid() {
traceId = spanContext.TraceID().String()
}
c = NewRequestContext[REQ](ctx, traceId)
}
if c.ServerTransportStream == nil {
c.ServerTransportStream = grpc.ServerTransportStreamFromContext(ctx)
}
c.ctx = ctx
return c
}
func (c *RequestContext[REQ]) Context() context.Context {
return c.ctx
}
func (c *RequestContext[REQ]) SetContext(ctx context.Context) {
c.ctx = ctx
}
func NewRequestContext[REQ any](ctx context.Context, traceId string) *RequestContext[REQ] {
now := time.Now()
if traceId == "" {
traceId = uuid.New().String()
}
return &RequestContext[REQ]{
ctx: ctx,
TraceID: traceId,
RequestAt: http.RequestAt{
Time: now,
TimeStamp: now.Unix(),
TimeString: now.Format(timei.LayoutTimeMacro),
},
ServerTransportStream: grpc.ServerTransportStreamFromContext(ctx),
Values: map[string]any{},
}
}
func (c *RequestContext[REQ]) reset(ctx context.Context) *RequestContext[REQ] {
span := trace.SpanFromContext(ctx)
now := time.Now()
traceId := span.SpanContext().TraceID().String()
if traceId == "" {
traceId = uuid.New().String()
}
c.ctx = ctx
c.RequestAt.Time = now
c.RequestAt.TimeString = now.Format(timei.LayoutTimeMacro)
c.RequestAt.TimeStamp = now.Unix()
return c
}
func (c *RequestContext[REQ]) Method() string {
if c.ServerTransportStream != nil {
return c.ServerTransportStream.Method()
}
return ""
}