-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrace.go
47 lines (43 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
46
47
package notifier
import (
"context"
"github.com/carousell/go-logging"
stdopentracing "github.com/opentracing/opentracing-go"
"github.com/pborman/uuid"
"strings"
)
const (
tracerID = "tracerId"
)
// SetTraceId updates the traceID based on context values
func SetTraceId(ctx context.Context) context.Context {
if GetTraceId(ctx) != "" {
return ctx
}
var traceID string
if span := stdopentracing.SpanFromContext(ctx); span != nil {
traceID = span.BaggageItem("trace")
}
// if no trace id then create one
if strings.TrimSpace(traceID) == "" {
traceID = uuid.NewUUID().String()
}
ctx = logging.AddToLogContext(ctx, "trace", traceID)
return AddToOptions(ctx, tracerID, traceID)
}
// GetTraceId fetches traceID from context
func GetTraceId(ctx context.Context) string {
if o := FromContext(ctx); o != nil {
if data, found := o.Get(tracerID); found {
return data.(string)
}
}
if logCtx := logging.FromContext(ctx); logCtx != nil {
if data, found := logCtx["trace"]; found {
traceID := data.(string)
AddToOptions(ctx, tracerID, traceID)
return traceID
}
}
return ""
}