-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracer.go
34 lines (31 loc) · 918 Bytes
/
tracer.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
package middleware
import (
"context"
"github.com/gin-gonic/gin"
"github.com/leon37/blog-server/global"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
)
func Tracing() gin.HandlerFunc {
return func(c *gin.Context) {
var newCtx context.Context
var span opentracing.Span
spanCtx, err := opentracing.GlobalTracer().Extract(
opentracing.HTTPHeaders,
opentracing.HTTPHeadersCarrier(c.Request.Header),
)
if err != nil {
span, newCtx = opentracing.StartSpanFromContextWithTracer(c.Request.Context(), global.Tracer, c.Request.URL.Path)
} else {
span, newCtx = opentracing.StartSpanFromContextWithTracer(
c.Request.Context(),
global.Tracer,
c.Request.URL.Path,
opentracing.ChildOf(spanCtx),
opentracing.Tag{Key: string(ext.Component), Value: "HTTP"})
}
defer span.Finish()
c.Request = c.Request.WithContext(newCtx)
c.Next()
}
}