/
utils.go
115 lines (99 loc) · 2.48 KB
/
utils.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
package tracer
import (
"context"
"encoding/json"
"fmt"
"runtime"
"strconv"
"strings"
"github.com/golangid/candi/candihelper"
"github.com/golangid/candi/config/env"
"github.com/golangid/candi/logger"
opentracing "github.com/opentracing/opentracing-go"
ext "github.com/opentracing/opentracing-go/ext"
otlog "github.com/opentracing/opentracing-go/log"
)
// WithTracerFunc functional with Tracer instance in function params
func WithTracerFunc(ctx context.Context, operationName string, fn func(context.Context, Tracer)) {
t, ctx := StartTraceWithContext(ctx, operationName)
defer t.Finish()
fn(ctx, t)
}
func toValue(v any) (res any) {
var str string
switch val := v.(type) {
case error:
if val != nil {
str = val.Error()
}
case string:
str = val
case bool, int8, int16, int32, int, int64, float32, float64:
return v
case []byte:
str = candihelper.ByteToString(val)
default:
b, _ := json.Marshal(val)
str = candihelper.ByteToString(b)
}
if len(str) >= int(env.BaseEnv().JaegerMaxPacketSize) {
return fmt.Sprintf("<<Overflow, cannot show data. Size is = %d bytes, JAEGER_MAX_PACKET_SIZE = %d bytes>>",
len(str),
env.BaseEnv().JaegerMaxPacketSize)
}
return logger.MaskLog(str)
}
// SetError global func
// TODO: separate in each tracer platform
func SetError(ctx context.Context, err error) {
span := opentracing.SpanFromContext(ctx)
if span == nil || err == nil {
return
}
ext.Error.Set(span, true)
span.SetTag("error.message", err.Error())
stackTrace := make([]byte, 1024)
for {
n := runtime.Stack(stackTrace, false)
if n < len(stackTrace) {
stackTrace = stackTrace[:n]
break
}
stackTrace = make([]byte, 2*len(stackTrace))
}
span.LogFields(otlog.String("stacktrace", string(stackTrace)))
}
// Log trace
func Log(ctx context.Context, key string, value interface{}) {
span := opentracing.SpanFromContext(ctx)
if span == nil {
return
}
span.LogKV(key, toValue(value))
}
// LogEvent trace
func LogEvent(ctx context.Context, event string, payload ...interface{}) {
span := opentracing.SpanFromContext(ctx)
if span == nil {
return
}
if payload != nil {
for _, p := range payload {
if e, ok := p.(error); ok && e != nil {
ext.Error.Set(span, true)
}
span.LogKV(event, toValue(p))
}
} else {
span.LogKV(event)
}
}
func parseCaller(pc uintptr, file string, line int, ok bool) (caller string) {
if !ok {
return
}
if strings.HasSuffix(file, "candi/tracer/jaeger.go") {
return
}
return file + ":" + strconv.Itoa(line)
}