3 * Copyright 2015, Google Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 "golang.org/x/net/trace"
47 // EnableTracing controls whether to trace RPCs using the golang.org/x/net/trace package.
48 // This should only be set before any RPCs are sent or received by this program.
49 var EnableTracing = true
51 // methodFamily returns the trace family for the given method.
52 // It turns "/pkg.Service/GetFoo" into "pkg.Service".
53 func methodFamily(m string) string {
54 m = strings.TrimPrefix(m, "/") // remove leading slash
55 if i := strings.Index(m, "/"); i >= 0 {
56 m = m[:i] // remove everything from second slash
58 if i := strings.LastIndex(m, "."); i >= 0 {
59 m = m[i+1:] // cut down to last dotted component
64 // traceInfo contains tracing information for an RPC.
65 type traceInfo struct {
70 // firstLine is the first line of an RPC trace.
71 type firstLine struct {
72 client bool // whether this is a client (outgoing) RPC
74 deadline time.Duration // may be zero
77 func (f *firstLine) String() string {
79 io.WriteString(&line, "RPC: ")
81 io.WriteString(&line, "to")
83 io.WriteString(&line, "from")
85 fmt.Fprintf(&line, " %v deadline:", f.remoteAddr)
87 fmt.Fprint(&line, f.deadline)
89 io.WriteString(&line, "none")
94 // payload represents an RPC request or response payload.
96 sent bool // whether this is an outgoing payload
97 msg interface{} // e.g. a proto.Message
98 // TODO(dsymonds): add stringifying info to codec, and limit how much we hold here?
101 func (p payload) String() string {
103 return fmt.Sprintf("sent: %v", p.msg)
105 return fmt.Sprintf("recv: %v", p.msg)
108 type fmtStringer struct {
113 func (f *fmtStringer) String() string {
114 return fmt.Sprintf(f.format, f.a...)
119 func (s stringer) String() string { return string(s) }