-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsim.go
66 lines (55 loc) · 1.99 KB
/
sim.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
package elasticapm
import (
"context"
"strings"
"github.com/yyle88/neatjson/neatjsons"
"github.com/yyle88/zaplog"
"go.elastic.co/apm/module/apmhttp/v2"
"go.elastic.co/apm/v2"
"google.golang.org/grpc/metadata"
)
func StartRequestTransaction(name string) *apm.Transaction {
return apm.DefaultTracer().StartTransaction(name, "request")
}
func StartTransaction(name, apmTxnType string) (apmTxn *apm.Transaction) {
return apm.DefaultTracer().StartTransaction(name, apmTxnType)
}
func ContextWithTransaction(ctx context.Context, apmTxn *apm.Transaction) context.Context {
return apm.ContextWithTransaction(ctx, apmTxn)
}
func TransactionFromContext(ctx context.Context) (transaction *apm.Transaction) {
return apm.TransactionFromContext(ctx)
}
func StartApmTxnTraceGrpcOutgoing(ctx context.Context, name, apmTxnType string) (*apm.Transaction, context.Context) {
apmTxn := StartTransaction(name, apmTxnType)
ctx = ContextWithTraceGrpcOutgoing(ctx, apmTxn)
return apmTxn, ctx
}
func ContextWithTraceGrpcOutgoing(ctx context.Context, apmTxn *apm.Transaction) context.Context {
ctx = ContextWithTransaction(ctx, apmTxn)
ctx = ContextWithGrpcOutgoingTrace(ctx, apmTxn.TraceContext())
return ctx
}
func ContextWithGrpcOutgoingTrace(ctx context.Context, apmTraceContext apm.TraceContext) context.Context {
upk := apmhttp.FormatTraceparentHeader(apmTraceContext)
md, ok := metadata.FromOutgoingContext(ctx)
if !ok {
md = metadata.Pairs(strings.ToLower(apmhttp.W3CTraceparentHeader), upk)
} else {
md = md.Copy()
md.Set(strings.ToLower(apmhttp.W3CTraceparentHeader), upk)
}
if stateMessage := apmTraceContext.State.String(); stateMessage != "" {
md.Set(strings.ToLower(apmhttp.TracestateHeader), stateMessage)
}
return metadata.NewOutgoingContext(ctx, md)
}
func SetLog(LOG apm.Logger) {
apm.DefaultTracer().SetLogger(LOG)
}
func Close() {
atc := apm.DefaultTracer()
atc.Flush(nil) //直接带个刷缓冲即可,否则程序快速退出时没数据
atc.Close()
zaplog.SUG.Debug(neatjsons.S(atc.Stats()))
}