/
helper.go
167 lines (145 loc) · 4.74 KB
/
helper.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
155
156
157
158
159
160
161
162
163
164
165
166
167
// Package tracer provides convenience wrapping functionality for tracing feature using OpenTelemetry.
package tracer
import (
"context"
"fmt"
"os"
"github.com/pkg/errors"
"github.com/xwc1125/xwc1125-pkg/utils/iputil/ipv4"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/baggage"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/semconv/v1.4.0"
"go.opentelemetry.io/otel/trace"
)
var (
// defaultTextMapPropagator is the default propagator for context propagation between peers.
defaultTextMapPropagator = propagation.NewCompositeTextMapPropagator(
propagation.TraceContext{},
propagation.Baggage{},
)
)
var (
intranetIps, _ = ipv4.GetIntranetIps()
hostname, _ = os.Hostname()
tracingMaxContentLogSize = 512 * 1024 // Max log size for request and response body, especially for HTTP/RPC request.
)
func init() {
CheckSetDefaultTextMapPropagator()
}
// CheckSetDefaultTextMapPropagator sets the default TextMapPropagator if it is not set previously.
func CheckSetDefaultTextMapPropagator() {
p := otel.GetTextMapPropagator()
if len(p.Fields()) == 0 {
otel.SetTextMapPropagator(GetDefaultTextMapPropagator())
}
}
// GetDefaultTextMapPropagator returns the default propagator for context propagation between peers.
func GetDefaultTextMapPropagator() propagation.TextMapPropagator {
return defaultTextMapPropagator
}
// Ctx create ctx with trace
func Ctx() context.Context {
ctx, span := NewSpan(context.Background(), "gctx.WithCtx")
defer span.End()
return ctx
}
// GetTraceID retrieves and returns TraceId from context.
// It returns an empty string is tracing feature is not activated.
func GetTraceID(ctx context.Context) string {
if ctx == nil {
return ""
}
traceID := trace.SpanContextFromContext(ctx).TraceID()
if traceID.IsValid() {
return traceID.String()
}
return ""
}
// GetSpanID retrieves and returns SpanId from context.
// It returns an empty string is tracing feature is not activated.
func GetSpanID(ctx context.Context) string {
if ctx == nil {
return ""
}
spanID := trace.SpanContextFromContext(ctx).SpanID()
if spanID.IsValid() {
return spanID.String()
}
return ""
}
// WithTraceID injects custom trace id into context to propagate.
func WithTraceID(ctx context.Context, traceID string) (context.Context, error) {
generatedTraceID, err := trace.TraceIDFromHex(traceID)
if err != nil {
return ctx, errors.Wrapf(
err,
`invalid custom traceID "%s", a traceID string should be composed with [0-9a-z] and fixed length 32`,
traceID,
)
}
sc := trace.SpanContextFromContext(ctx)
if !sc.HasTraceID() {
var span trace.Span
ctx, span = NewSpan(ctx, "gtrace.WithTraceID")
defer span.End()
sc = trace.SpanContextFromContext(ctx)
}
ctx = trace.ContextWithRemoteSpanContext(ctx, trace.NewSpanContext(trace.SpanContextConfig{
TraceID: generatedTraceID,
SpanID: sc.SpanID(),
TraceFlags: sc.TraceFlags(),
TraceState: sc.TraceState(),
Remote: sc.IsRemote(),
}))
return ctx, nil
}
func GetTraceParent(ctx context.Context) string {
sc := trace.SpanContextFromContext(ctx)
if !sc.IsValid() {
return ""
}
if ts := sc.TraceState().String(); ts != "" {
return ts
}
// Clear all flags other than the trace-context supported sampling bit.
flags := sc.TraceFlags() & trace.FlagsSampled
h := fmt.Sprintf("%.2x-%s-%s-%s",
0,
sc.TraceID(),
sc.SpanID(),
flags)
return h
}
// MaxContentLogSize returns the max log size for request and response body, especially for HTTP/RPC request.
func MaxContentLogSize() int {
return tracingMaxContentLogSize
}
// CommonLabels returns common used attribute labels:
// net.host.ip, host.name.
func CommonLabels() []attribute.KeyValue {
return []attribute.KeyValue{
// attribute.String(“hostname”, hostname),
semconv.NetHostIPKey.String(intranetIps),
semconv.HostNameKey.String(hostname),
}
}
// SetBaggageValue is a convenient function for adding one key-value pair to baggage.
// Note that it uses attribute.Any to set the key-value pair.
func SetBaggageValue(ctx context.Context, key string, value interface{}) context.Context {
return NewBaggage(ctx).SetValue(key, value)
}
// SetBaggageMap is a convenient function for adding map key-value pairs to baggage.
// Note that it uses attribute.Any to set the key-value pair.
func SetBaggageMap(ctx context.Context, data map[string]interface{}) context.Context {
return NewBaggage(ctx).SetMap(data)
}
// GetBaggageMap retrieves and returns the baggage values as map.
func GetBaggageMap(ctx context.Context) []baggage.Member {
return NewBaggage(ctx).GetMap()
}
// GetBaggageVar retrieves value and returns a *gvar.Var for specified key from baggage.
func GetBaggageVar(ctx context.Context, key string) string {
return NewBaggage(ctx).GetVar(key)
}