-
Notifications
You must be signed in to change notification settings - Fork 0
/
trace.go
45 lines (38 loc) · 1.08 KB
/
trace.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
package middleware
import (
"github.com/fighthorse/sampleBookReader/domain/component/log"
"github.com/gin-gonic/gin"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"github.com/uber/jaeger-client-go"
)
func Trace(c *gin.Context) {
// 每个请求到来时增加trace监控
ctx := c.Request.Context()
r := c.Request
url := r.RequestURI
if FilterUrl(url) {
return
}
tracer := opentracing.GlobalTracer()
parentCtx, err := tracer.Extract(opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(r.Header))
var span opentracing.Span
if err != nil {
span = tracer.StartSpan(r.URL.Path)
} else {
span = tracer.StartSpan(r.URL.Path, opentracing.ChildOf(parentCtx))
}
// set request ID for context
spanCtx, ok := span.Context().(jaeger.SpanContext)
if ok {
ctx = log.WithTraceId(ctx, spanCtx.TraceID().String())
}
ext.HTTPMethod.Set(span, r.Method)
ext.HTTPUrl.Set(span, r.URL.String())
ext.SpanKindRPCServer.Set(span)
ctx = opentracing.ContextWithSpan(ctx, span)
defer span.Finish()
// reload
c.Request = r.WithContext(ctx)
c.Next()
}