-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
132 lines (110 loc) · 2.74 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
/*
* Copyright 2024 hopeio. All rights reserved.
* Licensed under the MIT License that can be found in the LICENSE file.
* @Created by jyb
*/
package context
import (
"context"
"github.com/google/uuid"
"go.opentelemetry.io/otel/trace"
"time"
)
type Context struct {
ctx context.Context
rootSpan trace.Span
traceID string
}
func New(ctx context.Context) *Context {
var traceId string
var rootSpan trace.Span
if ctx != nil {
span := trace.SpanFromContext(ctx)
if spanContext := span.SpanContext(); spanContext.IsValid() {
traceId = spanContext.TraceID().String()
rootSpan = span
}
} else {
ctx = context.Background()
}
if traceId == "" || rootSpan == nil {
ctx, rootSpan = StartSpan(ctx, "")
if spanContext := rootSpan.SpanContext(); spanContext.IsValid() {
traceId = spanContext.TraceID().String()
} else {
traceId = uuid.New().String()
}
}
return &Context{
ctx: ctx,
rootSpan: rootSpan,
traceID: traceId,
}
}
func (c *Context) TraceID() string {
return c.traceID
}
type ctxKey struct{}
func (c *Context) Wrapper() context.Context {
return context.WithValue(c.ctx, ctxKey{}, c)
}
func WrapperKey() ctxKey {
return ctxKey{}
}
func FromContextValue(ctx context.Context) *Context {
if ctx == nil {
return New(nil)
}
ctxi := ctx.Value(ctxKey{})
c, ok := ctxi.(*Context)
if !ok {
c = New(ctx)
}
c.ctx = ctx
return c
}
func (c *Context) Base() context.Context {
return c.ctx
}
func (c *Context) SetBase(ctx context.Context) {
c.ctx = ctx
}
func (c *Context) RootSpan() trace.Span {
return c.rootSpan
}
func (c *Context) WithTimeout(timeout time.Duration) context.CancelFunc {
var cancel context.CancelFunc
c.ctx, cancel = context.WithTimeout(c.ctx, timeout)
return cancel
}
func (c *Context) WithTimeoutCause(timeout time.Duration, cause error) context.CancelFunc {
var cancel context.CancelFunc
c.ctx, cancel = context.WithTimeoutCause(c.ctx, timeout, cause)
return cancel
}
func (c *Context) WithCancel() context.CancelFunc {
var cancel context.CancelFunc
c.ctx, cancel = context.WithCancel(c.ctx)
return cancel
}
func (c *Context) WithoutCancel() {
c.ctx = context.WithoutCancel(c.ctx)
}
func (c *Context) WithValue(key, val any) {
c.ctx = context.WithValue(c.ctx, key, val)
}
func (c *Context) WithCancelCause() context.CancelCauseFunc {
var cancel context.CancelCauseFunc
c.ctx, cancel = context.WithCancelCause(c.ctx)
return cancel
}
func (c *Context) WithDeadline(d time.Time) context.CancelFunc {
var cancel context.CancelFunc
c.ctx, cancel = context.WithDeadline(c.ctx, d)
return cancel
}
func (c *Context) WithDeadlineCause(d time.Time, cause error) context.CancelFunc {
var cancel context.CancelFunc
c.ctx, cancel = context.WithDeadlineCause(c.ctx, d, cause)
return cancel
}