/
plugin.go
93 lines (67 loc) · 2.49 KB
/
plugin.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
package http
import (
"context"
"net/http"
semconv "go.opentelemetry.io/otel/semconv/v1.10.0"
"go.opentelemetry.io/otel/trace"
lc "github.com/air-go/rpc/library/context"
"github.com/air-go/rpc/library/logger"
jaeger "github.com/air-go/rpc/library/opentracing/http"
libraryOtel "github.com/air-go/rpc/library/otel"
"github.com/air-go/rpc/server/http/middleware/timeout"
)
type BeforeRequestPlugin interface {
Handle(ctx context.Context, req *http.Request) (context.Context, error)
Name() string
}
type AfterRequestPlugin interface {
Handle(ctx context.Context, req *http.Request, resp *http.Response) (context.Context, error)
Name() string
}
type OpentracingBeforePlugin struct{}
var _ BeforeRequestPlugin = (*OpentracingBeforePlugin)(nil)
func (*OpentracingBeforePlugin) Handle(ctx context.Context, req *http.Request) (context.Context, error) {
return ctx, jaeger.InjectHTTP(ctx, req)
}
func (*OpentracingBeforePlugin) Name() string {
return "OpentracingBeforePlugin"
}
type OpentelemetryBeforePlugin struct{}
var _ BeforeRequestPlugin = (*OpentelemetryBeforePlugin)(nil)
func (*OpentelemetryBeforePlugin) Handle(ctx context.Context, req *http.Request) (context.Context, error) {
if !libraryOtel.CheckHasTraceID(ctx) {
return ctx, nil
}
logID := lc.ValueLogID(ctx)
req.Header.Set(logger.LogHeader, logID)
libraryOtel.InjectHTTPBaggage(ctx, req.Header)
path := req.URL.Path
opts := []trace.SpanStartOption{
trace.WithAttributes(semconv.NetAttributesFromHTTPRequest("tcp", req)...),
trace.WithAttributes(semconv.EndUserAttributesFromHTTPRequest(req)...),
trace.WithAttributes(semconv.HTTPServerAttributesFromHTTPRequest("rpc-example", path, req)...),
trace.WithSpanKind(trace.SpanKindClient),
}
ctx, span := libraryOtel.Tracer().Start(ctx, path, opts...)
defer span.End()
return ctx, nil
}
func (*OpentelemetryBeforePlugin) Name() string {
return "OpentelemetryBeforePlugin"
}
type LogBeforePlugin struct{}
var _ BeforeRequestPlugin = (*LogBeforePlugin)(nil)
func (*LogBeforePlugin) Handle(ctx context.Context, req *http.Request) (context.Context, error) {
return ctx, logger.SetLogID(ctx, req.Header)
}
func (*LogBeforePlugin) Name() string {
return "LogBeforePlugin"
}
type TimeoutBeforePlugin struct{}
var _ BeforeRequestPlugin = (*TimeoutBeforePlugin)(nil)
func (*TimeoutBeforePlugin) Handle(ctx context.Context, req *http.Request) (context.Context, error) {
return ctx, timeout.SetHeader(ctx, req.Header)
}
func (*TimeoutBeforePlugin) Name() string {
return "TimeoutBeforePlugin"
}