/
otel.go
169 lines (151 loc) · 4.41 KB
/
otel.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
168
169
package opentelemetry
import (
"context"
"fmt"
"github.com/MrAlias/flow"
"github.com/gin-gonic/gin"
"github.com/zhanjunjie2019/clover/global/confs"
"github.com/zhanjunjie2019/clover/global/consts"
"github.com/zhanjunjie2019/clover/global/uctx"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.9.0"
"go.opentelemetry.io/otel/trace"
"google.golang.org/grpc"
"net/http"
"time"
)
// +ioc:autowire=true
// +ioc:autowire:type=singleton
type OpenTelemetry struct {
tracerProvider *sdktrace.TracerProvider
tracer trace.Tracer
}
func (o *OpenTelemetry) Shutdown() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
if err := o.tracerProvider.Shutdown(ctx); err != nil {
otel.Handle(err)
}
}
func (o *OpenTelemetry) InitProvider() error {
otelConfig := confs.GetGlobalConfig().OtelConfig
if otelConfig.Enabled.Bool() {
svcConf := confs.GetServerConfig().SvcConf
var (
grpcEndpoint = otelConfig.CollectorGrpcEndpoint
svcName = svcConf.SvcName
svcVersion = svcConf.SvcVersion
svcNum = int(svcConf.SvcNum)
ctx = context.Background()
)
traceClient := otlptracegrpc.NewClient(
otlptracegrpc.WithInsecure(),
otlptracegrpc.WithEndpoint(grpcEndpoint),
otlptracegrpc.WithDialOption(grpc.WithBlock()))
ctx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
traceExporter, err := otlptrace.New(ctx, traceClient)
if err != nil {
return fmt.Errorf("failed to create trace exporter: %w", err)
}
res, err := resource.New(ctx,
resource.WithAttributes(
semconv.ServiceNameKey.String(svcName),
semconv.ServiceInstanceIDKey.Int(svcNum),
semconv.ServiceVersionKey.String(svcVersion),
),
)
if err != nil {
return fmt.Errorf("failed to create resource: %w", err)
}
bsp := sdktrace.NewBatchSpanProcessor(traceExporter)
tracerProvider := sdktrace.NewTracerProvider(
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithResource(res),
//sdktrace.WithSpanProcessor(bsp),
flow.WithSpanProcessor(bsp),
)
otel.SetTracerProvider(tracerProvider)
otel.SetTextMapPropagator(propagation.TraceContext{})
if o.tracerProvider != nil {
if err = o.tracerProvider.Shutdown(ctx); err != nil {
otel.Handle(err)
}
}
o.tracerProvider = tracerProvider
o.tracer = otel.Tracer("")
}
return nil
}
func (o *OpenTelemetry) Start(ctx context.Context, spanName string) (context.Context, trace.Span) {
return o.tracer.Start(ctx, spanName)
}
func (o *OpenTelemetry) InvokeFunc(ctx context.Context, spanName string, fn func(context.Context, any) (any, error), args any) (any, error) {
_, ok := ctx.(*gin.Context)
if ok {
ctx = uctx.GetSpanContext(ctx)
}
if o.tracer != nil {
c, span := o.tracer.Start(ctx, spanName)
defer span.End()
return fn(c, args)
} else {
return fn(ctx, args)
}
}
func (o *OpenTelemetry) Inject(ctx context.Context, headers http.Header) {
sc := trace.SpanContextFromContext(ctx)
if !sc.IsValid() {
return
}
headers.Set(consts.TraceIDHeaderKey, sc.TraceID().String())
headers.Set(consts.TraceSpanIDHeaderKey, sc.SpanID().String())
}
func (o *OpenTelemetry) Extract(ctx context.Context, headers http.Header) context.Context {
traceID := headers.Get(consts.TraceIDHeaderKey)
if len(traceID) != 32 {
return ctx
}
spanID := headers.Get(consts.TraceSpanIDHeaderKey)
if len(spanID) != 16 {
return ctx
}
return o.GetCtx(ctx, traceID, spanID)
}
func (o *OpenTelemetry) ExtractByGrpcCtx(ctx context.Context) context.Context {
var traceID, spanID string
ctx, traceID = uctx.GetTraceIDByGrpcCtx(ctx)
if len(traceID) != 32 {
return ctx
}
spanID = uctx.GetTraceSpanIDByGrpcCtx(ctx)
if len(spanID) != 16 {
return ctx
}
return o.GetCtx(ctx, traceID, spanID)
}
func (o *OpenTelemetry) GetCtx(ctx context.Context, traceID, spanID string) context.Context {
var (
err error
scc trace.SpanContextConfig
)
scc.TraceID, err = trace.TraceIDFromHex(traceID)
if err != nil {
return ctx
}
scc.SpanID, err = trace.SpanIDFromHex(spanID)
if err != nil {
return ctx
}
scc.Remote = true
sc := trace.NewSpanContext(scc)
if !sc.IsValid() {
return ctx
}
return trace.ContextWithRemoteSpanContext(ctx, sc)
}