/
trace.go
59 lines (49 loc) · 1.64 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
48
49
50
51
52
53
54
55
56
57
58
59
package serverplugin
import (
"context"
"net"
"reflect"
"runtime"
"github.com/werunclub/rpcx/v4/protocol"
"golang.org/x/net/trace"
)
type TracePlugin struct {
}
func (p *TracePlugin) Register(name string, rcvr interface{}, metadata string) error {
tr := trace.New("rpcx.Server", "Register")
defer tr.Finish()
tr.LazyPrintf("register %s: %T", name, rcvr)
return nil
}
func (p *TracePlugin) RegisterFunction(name string, fn interface{}, metadata string) error {
tr := trace.New("rpcx.Server", "RegisterFunction")
defer tr.Finish()
tr.LazyPrintf("register %s: %T", name, GetFunctionName(fn))
return nil
}
func GetFunctionName(i interface{}) string {
return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
}
func (p *TracePlugin) PostConnAccept(conn net.Conn) (net.Conn, bool) {
tr := trace.New("rpcx.Server", "Accept")
defer tr.Finish()
tr.LazyPrintf("accept conn %s", conn.RemoteAddr().String())
return conn, true
}
func (p *TracePlugin) PostReadRequest(ctx context.Context, r *protocol.Message, e error) error {
tr := trace.New("rpcx.Server", "ReadRequest")
defer tr.Finish()
tr.LazyPrintf("read request %s.%s, seq: %d", r.ServicePath, r.ServiceMethod, r.Seq())
return nil
}
func (p *TracePlugin) PostWriteResponse(ctx context.Context, req *protocol.Message, res *protocol.Message, err error) error {
tr := trace.New("rpcx.Server", "WriteResponse")
defer tr.Finish()
if err == nil {
tr.LazyPrintf("succeed to call %s.%s, seq: %d", req.ServicePath, req.ServiceMethod, req.Seq())
} else {
tr.LazyPrintf("failed to call %s.%s, seq: %d : %v", req.Seq, req.ServicePath, req.ServiceMethod, req.Seq(), err)
tr.SetError()
}
return nil
}