blob: 0a57b9994812b1a9b4d63ef0bb990cc79972d454 [file] [log] [blame]
khenaidooac637102019-01-14 15:44:34 -05001/*
2 *
3 * Copyright 2015 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19package grpc
20
21import (
22 "bytes"
23 "fmt"
24 "io"
25 "net"
26 "strings"
Scott Bakerbeb3cfa2019-10-01 14:44:30 -070027 "sync"
khenaidooac637102019-01-14 15:44:34 -050028 "time"
29
30 "golang.org/x/net/trace"
31)
32
33// EnableTracing controls whether to trace RPCs using the golang.org/x/net/trace package.
34// This should only be set before any RPCs are sent or received by this program.
35var EnableTracing bool
36
37// methodFamily returns the trace family for the given method.
38// It turns "/pkg.Service/GetFoo" into "pkg.Service".
39func methodFamily(m string) string {
40 m = strings.TrimPrefix(m, "/") // remove leading slash
41 if i := strings.Index(m, "/"); i >= 0 {
42 m = m[:i] // remove everything from second slash
43 }
44 if i := strings.LastIndex(m, "."); i >= 0 {
45 m = m[i+1:] // cut down to last dotted component
46 }
47 return m
48}
49
50// traceInfo contains tracing information for an RPC.
51type traceInfo struct {
52 tr trace.Trace
53 firstLine firstLine
54}
55
56// firstLine is the first line of an RPC trace.
Scott Bakerbeb3cfa2019-10-01 14:44:30 -070057// It may be mutated after construction; remoteAddr specifically may change
58// during client-side use.
khenaidooac637102019-01-14 15:44:34 -050059type firstLine struct {
Scott Bakerbeb3cfa2019-10-01 14:44:30 -070060 mu sync.Mutex
khenaidooac637102019-01-14 15:44:34 -050061 client bool // whether this is a client (outgoing) RPC
62 remoteAddr net.Addr
63 deadline time.Duration // may be zero
64}
65
Scott Bakerbeb3cfa2019-10-01 14:44:30 -070066func (f *firstLine) SetRemoteAddr(addr net.Addr) {
67 f.mu.Lock()
68 f.remoteAddr = addr
69 f.mu.Unlock()
70}
71
khenaidooac637102019-01-14 15:44:34 -050072func (f *firstLine) String() string {
Scott Bakerbeb3cfa2019-10-01 14:44:30 -070073 f.mu.Lock()
74 defer f.mu.Unlock()
75
khenaidooac637102019-01-14 15:44:34 -050076 var line bytes.Buffer
77 io.WriteString(&line, "RPC: ")
78 if f.client {
79 io.WriteString(&line, "to")
80 } else {
81 io.WriteString(&line, "from")
82 }
83 fmt.Fprintf(&line, " %v deadline:", f.remoteAddr)
84 if f.deadline != 0 {
85 fmt.Fprint(&line, f.deadline)
86 } else {
87 io.WriteString(&line, "none")
88 }
89 return line.String()
90}
91
92const truncateSize = 100
93
94func truncate(x string, l int) string {
95 if l > len(x) {
96 return x
97 }
98 return x[:l]
99}
100
101// payload represents an RPC request or response payload.
102type payload struct {
103 sent bool // whether this is an outgoing payload
104 msg interface{} // e.g. a proto.Message
105 // TODO(dsymonds): add stringifying info to codec, and limit how much we hold here?
106}
107
108func (p payload) String() string {
109 if p.sent {
110 return truncate(fmt.Sprintf("sent: %v", p.msg), truncateSize)
111 }
112 return truncate(fmt.Sprintf("recv: %v", p.msg), truncateSize)
113}
114
115type fmtStringer struct {
116 format string
117 a []interface{}
118}
119
120func (f *fmtStringer) String() string {
121 return fmt.Sprintf(f.format, f.a...)
122}
123
124type stringer string
125
126func (s stringer) String() string { return string(s) }