3 * Copyright 2015 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
29 "golang.org/x/net/trace"
32 // EnableTracing controls whether to trace RPCs using the golang.org/x/net/trace package.
33 // This should only be set before any RPCs are sent or received by this program.
34 var EnableTracing = true
36 // methodFamily returns the trace family for the given method.
37 // It turns "/pkg.Service/GetFoo" into "pkg.Service".
38 func methodFamily(m string) string {
39 m = strings.TrimPrefix(m, "/") // remove leading slash
40 if i := strings.Index(m, "/"); i >= 0 {
41 m = m[:i] // remove everything from second slash
43 if i := strings.LastIndex(m, "."); i >= 0 {
44 m = m[i+1:] // cut down to last dotted component
49 // traceInfo contains tracing information for an RPC.
50 type traceInfo struct {
55 // firstLine is the first line of an RPC trace.
56 type firstLine struct {
57 client bool // whether this is a client (outgoing) RPC
59 deadline time.Duration // may be zero
62 func (f *firstLine) String() string {
64 io.WriteString(&line, "RPC: ")
66 io.WriteString(&line, "to")
68 io.WriteString(&line, "from")
70 fmt.Fprintf(&line, " %v deadline:", f.remoteAddr)
72 fmt.Fprint(&line, f.deadline)
74 io.WriteString(&line, "none")
79 // payload represents an RPC request or response payload.
81 sent bool // whether this is an outgoing payload
82 msg interface{} // e.g. a proto.Message
83 // TODO(dsymonds): add stringifying info to codec, and limit how much we hold here?
86 func (p payload) String() string {
88 return fmt.Sprintf("sent: %v", p.msg)
90 return fmt.Sprintf("recv: %v", p.msg)
93 type fmtStringer struct {
98 func (f *fmtStringer) String() string {
99 return fmt.Sprintf(f.format, f.a...)
104 func (s stringer) String() string { return string(s) }