-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecord_tracing.go
130 lines (114 loc) · 3.42 KB
/
record_tracing.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package golden
import (
"github.com/go-resty/resty/v2"
"github.com/opentracing/basictracer-go"
"github.com/opentracing/opentracing-go"
"github.com/porschemacan/golden/libs"
"strings"
)
var logTagMappingFunc = func(method string, path string) string {
return "_undef"
}
func SetLogTagMapping(mappingFunc func(method string, path string) string) {
logTagMappingFunc = mappingFunc
}
var LogTraceIntegrator = func() func(basictracer.SpanEvent) {
var fields = map[string]interface{}{}
var operationName string
return func(e basictracer.SpanEvent) {
switch t := e.(type) {
case basictracer.EventCreate:
operationName = t.OperationName
case basictracer.EventFinish:
tagName := operationName
items := strings.Split(operationName, "__")
if len(items) >= 2 {
tagName = logTagMappingFunc(items[0], items[1])
}
entry := libs.GetDLog(tagName)
for k, v := range fields {
entry = entry.WithField(k, v)
}
entry.Info(operationName)
case basictracer.EventTag:
fields[t.Key] = t.Value
case basictracer.EventLogFields:
for _, f := range t.Fields {
fields[f.Key()] = f.Value()
}
case basictracer.EventLog:
fields[t.Event] = t.Payload
}
}
}
// record request
func RecordInputSpan(spanContext opentracing.SpanContext, c *HttpContext) {
var traceId uint64
var spanId uint64
if spanContext != nil {
basicSpanContext := spanContext.(basictracer.SpanContext)
traceId = basicSpanContext.TraceID
spanId = basicSpanContext.SpanID
}
libs.GetDLog("_com_request_in").
WithField("traceid", traceId).
WithField("parentSpanid", spanId).
WithField("spanid", c.GetSpanId()).
WithField("method", c.RawRequest().Method).
WithField("uri", c.RawRequest().URL.Path).
WithField("contentlength", c.RawRequest().ContentLength).
Infoln()
}
// record response
func RecordOutputSpan(spanContext opentracing.SpanContext, c *HttpContext) {
basicSpanContext := spanContext.(basictracer.SpanContext)
libs.GetDLog("_com_request_out").
WithField("traceid", basicSpanContext.TraceID).
WithField("spanid", basicSpanContext.SpanID).
WithField("method", c.RawRequest().Method).
WithField("uri", c.RawRequest().URL.Path).
WithField("proc_time", c.GetInt64(PROCTIME)).
WithField("errno", c.GetInt64(ERROR_NO)).
Infoln()
}
func RecordSubCallResponse(c *resty.Client, r *resty.Response) error {
if r.Request.Context() == nil {
return nil
}
span := opentracing.SpanFromContext(r.Request.Context())
if span == nil {
return nil
}
spanContext := span.Context()
if spanContext == nil {
return nil
}
basicSpanContext := spanContext.(basictracer.SpanContext)
procTime := int64(r.Time().Nanoseconds() / 1000000)
keyTag := "_com_http_success"
errno := 0
if r.IsError() {
keyTag = "_com_http_failure"
errno = r.StatusCode()
}
libs.GetDLog(keyTag).
WithField("traceid", basicSpanContext.TraceID).
WithField("spanid", basicSpanContext.SpanID).
WithField("method", r.Request.Method).
WithField("url", r.Request.URL).
WithField("proc_time", procTime).
WithField("errno", errno).
Infoln()
return nil
}
type SpanFinishRecorder struct {
}
func (*SpanFinishRecorder) RecordSpan(span basictracer.RawSpan) {
libs.GetDLog("_undef").
WithField("traceid", span.Context.TraceID).
WithField("parentSpanid", span.ParentSpanID).
WithField("spanid", span.Context.SpanID).
WithField("starttime", span.Start.Unix()).
WithField("duration", int64(span.Duration.Nanoseconds()/1000000)).
Info(span.Context.Baggage)
}