-
Notifications
You must be signed in to change notification settings - Fork 9
/
span.go
91 lines (81 loc) · 3.56 KB
/
span.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
package ginopentracing
import (
"net/http"
"runtime"
opentracing "github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
)
// StartSpan will start a new span with no parent span.
func StartSpan(operationName, method, path string) opentracing.Span {
return StartSpanWithParent(nil, operationName, method, path)
}
// StartDBSpanWithParent - start a DB operation span
func StartDBSpanWithParent(parent opentracing.SpanContext, operationName, dbInstance, dbType, dbStatement string) opentracing.Span {
options := []opentracing.StartSpanOption{opentracing.Tag{Key: ext.SpanKindRPCServer.Key, Value: ext.SpanKindRPCServer.Value}}
if len(dbInstance) > 0 {
options = append(options, opentracing.Tag{Key: string(ext.DBInstance), Value: dbInstance})
}
if len(dbType) > 0 {
options = append(options, opentracing.Tag{Key: string(ext.DBType), Value: dbType})
}
if len(dbStatement) > 0 {
options = append(options, opentracing.Tag{Key: string(ext.DBStatement), Value: dbStatement})
}
if parent != nil {
options = append(options, opentracing.ChildOf(parent))
}
return opentracing.StartSpan(operationName, options...)
}
// StartSpanWithParent will start a new span with a parent span.
// example:
// span:= StartSpanWithParent(c.Get("tracing-context"),
func StartSpanWithParent(parent opentracing.SpanContext, operationName, method, path string) opentracing.Span {
options := []opentracing.StartSpanOption{
opentracing.Tag{Key: ext.SpanKindRPCServer.Key, Value: ext.SpanKindRPCServer.Value},
opentracing.Tag{Key: string(ext.HTTPMethod), Value: method},
opentracing.Tag{Key: string(ext.HTTPUrl), Value: path},
opentracing.Tag{Key: "current-goroutines", Value: runtime.NumGoroutine()},
}
if parent != nil {
options = append(options, opentracing.ChildOf(parent))
}
return opentracing.StartSpan(operationName, options...)
}
// StartSpanWithHeader will look in the headers to look for a parent span before starting the new span.
// example:
// func handleGet(c *gin.Context) {
// span := StartSpanWithHeader(&c.Request.Header, "api-request", method, path)
// defer span.Finish()
// c.Set("tracing-context", span) // add the span to the context so it can be used for the duration of the request.
// bosePersonID := c.Param("bosePersonID")
// span.SetTag("bosePersonID", bosePersonID)
//
func StartSpanWithHeader(header *http.Header, operationName, method, path string) opentracing.Span {
var wireContext opentracing.SpanContext
if header != nil {
wireContext, _ = opentracing.GlobalTracer().Extract(opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(*header))
}
return StartSpanWithParent(wireContext, operationName, method, path)
}
// InjectTraceID injects the span ID into the provided HTTP header object, so that the
// current span will be propogated downstream to the server responding to an HTTP request.
// Specifying the span ID in this way will allow the tracing system to connect spans
// between servers.
//
// Usage:
// // resty example
// r := resty.R()
// injectTraceID(span, r.Header)
// resp, err := r.Get(fmt.Sprintf("http://localhost:8000/users/%s", bosePersonID))
//
// // galapagos_clients example
// c := galapagos_clients.GetHTTPClient()
// req, err := http.NewRequest("GET", fmt.Sprintf("http://localhost:8000/users/%s", bosePersonID))
// injectTraceID(span, req.Header)
// c.Do(req)
func InjectTraceID(ctx opentracing.SpanContext, header http.Header) {
opentracing.GlobalTracer().Inject(
ctx,
opentracing.HTTPHeaders,
opentracing.HTTPHeadersCarrier(header))
}