forked from tencentyun/tsf-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracing.go
134 lines (123 loc) · 4.66 KB
/
tracing.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
package tsf
import (
"context"
"net/url"
"github.com/hisonsoft/tsf-go/gin"
"github.com/hisonsoft/tsf-go/pkg/meta"
"github.com/hisonsoft/tsf-go/tracing"
"github.com/hisonsoft/tsf-go/util"
"github.com/go-kratos/kratos/v2"
"github.com/go-kratos/kratos/v2/middleware"
"github.com/go-kratos/kratos/v2/transport"
"github.com/go-kratos/kratos/v2/transport/http"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"google.golang.org/grpc/peer"
)
// tracingServer returns a new server middleware for OpenTelemetry.
func tracingServer() middleware.Middleware {
tracer, e := tracing.NewTracer(trace.SpanKindServer)
if e != nil {
panic(e)
}
return func(handler middleware.Handler) middleware.Handler {
return func(ctx context.Context, req interface{}) (reply interface{}, err error) {
if tr, ok := transport.FromServerContext(ctx); ok {
method := "POST"
operation := tr.Operation()
path := tr.Operation()
var remote string
if c, ok := gin.FromGinContext(ctx); ok {
operation = c.Ctx.FullPath()
method = c.Ctx.Request.Method
path = c.Ctx.Request.URL.Path
remote = c.Ctx.Request.RemoteAddr
} else if tr.Kind() == transport.KindHTTP {
if ht, ok := tr.(*http.Transport); ok {
operation = ht.PathTemplate()
method = ht.Request().Method
path = ht.Request().URL.Path
remote = ht.Request().RemoteAddr
}
} else if tr.Kind() == transport.KindGRPC {
if p, ok := peer.FromContext(ctx); ok {
remote = p.Addr.String()
}
}
var span trace.Span
ctx, span = tracer.Start(ctx, tr.Kind().String(), operation, tr.RequestHeader())
span.SetAttributes(attribute.String("localComponent", tr.Kind().String()))
k, _ := kratos.FromContext(ctx)
span.SetAttributes(attribute.String("local.service", k.Name()))
u, _ := url.Parse(tr.Endpoint())
if u != nil {
localIP, localPort := util.ParseAddr(u.Host)
span.SetAttributes(attribute.String("local.ip", localIP))
span.SetAttributes(attribute.Int64("local.port", int64(localPort)))
}
if name, ok := meta.Sys(ctx, meta.SourceKey(meta.ServiceName)).(string); ok {
span.SetAttributes(attribute.String("peer.service", name))
}
remoteIP, remotePort := util.ParseAddr(remote)
span.SetAttributes(attribute.String("peer.ip", remoteIP))
span.SetAttributes(attribute.Int64("peer.port", int64(remotePort)))
span.SetAttributes(attribute.String("http.method", method))
span.SetAttributes(attribute.String("localInterface", operation))
span.SetAttributes(attribute.String("http.path", path))
defer func() { tracer.End(ctx, span, err) }()
}
reply, err = handler(ctx, req)
return
}
}
}
// tracingClient returns a new client middleware for OpenTelemetry.
func tracingClient() middleware.Middleware {
tracer, e := tracing.NewTracer(trace.SpanKindClient)
if e != nil {
panic(e)
}
return func(handler middleware.Handler) middleware.Handler {
return func(ctx context.Context, req interface{}) (reply interface{}, err error) {
if tr, ok := transport.FromClientContext(ctx); ok {
operation := tr.Operation()
path := tr.Operation()
method := "POST"
if tr.Kind() == transport.KindHTTP {
if ht, ok := tr.(*http.Transport); ok {
operation = ht.PathTemplate()
method = ht.Request().Method
path = ht.Request().URL.Path
}
}
var span trace.Span
ctx, span = tracer.Start(ctx, tr.Kind().String(), operation, tr.RequestHeader())
span.SetAttributes(attribute.String("remoteComponent", tr.Kind().String()))
if str, ok := transport.FromServerContext(ctx); ok {
span.SetAttributes(attribute.String("localComponent", str.Kind().String()))
span.SetAttributes(attribute.String("localInterface", str.Operation()))
}
localEndpoint := LocalEndpoint(ctx)
span.SetAttributes(attribute.String("local.service", localEndpoint.Service))
span.SetAttributes(attribute.String("local.ip", localEndpoint.IP))
span.SetAttributes(attribute.Int64("local.port", int64(localEndpoint.Port)))
remoteService, _ := util.ParseTarget(tr.Endpoint())
span.SetAttributes(attribute.String("peer.service", remoteService))
span.SetAttributes(attribute.String("http.method", method))
span.SetAttributes(attribute.String("http.path", path))
defer func() {
if tr.Kind() == transport.KindHTTP {
if ht, ok := tr.(*http.Transport); ok {
remoteIP, remotePort := util.ParseAddr(ht.Request().Host)
span.SetAttributes(attribute.String("peer.ip", remoteIP))
span.SetAttributes(attribute.Int64("peer.port", int64(remotePort)))
}
}
tracer.End(ctx, span, err)
}()
}
reply, err = handler(ctx, req)
return
}
}
}