diff options
Diffstat (limited to 'vendor/google.golang.org/grpc/trace.go')
-rw-r--r-- | vendor/google.golang.org/grpc/trace.go | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/vendor/google.golang.org/grpc/trace.go b/vendor/google.golang.org/grpc/trace.go new file mode 100644 index 0000000..b419c9e --- /dev/null +++ b/vendor/google.golang.org/grpc/trace.go | |||
@@ -0,0 +1,104 @@ | |||
1 | /* | ||
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 | |||
19 | package grpc | ||
20 | |||
21 | import ( | ||
22 | "bytes" | ||
23 | "fmt" | ||
24 | "io" | ||
25 | "net" | ||
26 | "strings" | ||
27 | "time" | ||
28 | |||
29 | "golang.org/x/net/trace" | ||
30 | ) | ||
31 | |||
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 | ||
35 | |||
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 | ||
42 | } | ||
43 | if i := strings.LastIndex(m, "."); i >= 0 { | ||
44 | m = m[i+1:] // cut down to last dotted component | ||
45 | } | ||
46 | return m | ||
47 | } | ||
48 | |||
49 | // traceInfo contains tracing information for an RPC. | ||
50 | type traceInfo struct { | ||
51 | tr trace.Trace | ||
52 | firstLine firstLine | ||
53 | } | ||
54 | |||
55 | // firstLine is the first line of an RPC trace. | ||
56 | type firstLine struct { | ||
57 | client bool // whether this is a client (outgoing) RPC | ||
58 | remoteAddr net.Addr | ||
59 | deadline time.Duration // may be zero | ||
60 | } | ||
61 | |||
62 | func (f *firstLine) String() string { | ||
63 | var line bytes.Buffer | ||
64 | io.WriteString(&line, "RPC: ") | ||
65 | if f.client { | ||
66 | io.WriteString(&line, "to") | ||
67 | } else { | ||
68 | io.WriteString(&line, "from") | ||
69 | } | ||
70 | fmt.Fprintf(&line, " %v deadline:", f.remoteAddr) | ||
71 | if f.deadline != 0 { | ||
72 | fmt.Fprint(&line, f.deadline) | ||
73 | } else { | ||
74 | io.WriteString(&line, "none") | ||
75 | } | ||
76 | return line.String() | ||
77 | } | ||
78 | |||
79 | // payload represents an RPC request or response payload. | ||
80 | type payload struct { | ||
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? | ||
84 | } | ||
85 | |||
86 | func (p payload) String() string { | ||
87 | if p.sent { | ||
88 | return fmt.Sprintf("sent: %v", p.msg) | ||
89 | } | ||
90 | return fmt.Sprintf("recv: %v", p.msg) | ||
91 | } | ||
92 | |||
93 | type fmtStringer struct { | ||
94 | format string | ||
95 | a []interface{} | ||
96 | } | ||
97 | |||
98 | func (f *fmtStringer) String() string { | ||
99 | return fmt.Sprintf(f.format, f.a...) | ||
100 | } | ||
101 | |||
102 | type stringer string | ||
103 | |||
104 | func (s stringer) String() string { return string(s) } | ||