3 * Copyright 2016 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.
19 //go:generate protoc --go_out=plugins=grpc:. grpc_testing/test.proto
21 // Package stats is for collecting and reporting various network and RPC stats.
22 // This package is for monitoring purpose only. All fields are read-only.
23 // All APIs are experimental.
24 package stats // import "google.golang.org/grpc/stats"
30 "golang.org/x/net/context"
33 // RPCStats contains stats information about RPCs.
34 type RPCStats interface {
36 // IsClient returns true if this RPCStats is from client side.
40 // Begin contains stats when an RPC begins.
41 // FailFast is only valid if this Begin is from client side.
43 // Client is true if this Begin is from client side.
45 // BeginTime is the time when the RPC begins.
47 // FailFast indicates if this RPC is failfast.
51 // IsClient indicates if the stats information is from client side.
52 func (s *Begin) IsClient() bool { return s.Client }
54 func (s *Begin) isRPCStats() {}
56 // InPayload contains the information for an incoming payload.
57 type InPayload struct {
58 // Client is true if this InPayload is from client side.
60 // Payload is the payload with original type.
62 // Data is the serialized message payload.
64 // Length is the length of uncompressed data.
66 // WireLength is the length of data on wire (compressed, signed, encrypted).
68 // RecvTime is the time when the payload is received.
72 // IsClient indicates if the stats information is from client side.
73 func (s *InPayload) IsClient() bool { return s.Client }
75 func (s *InPayload) isRPCStats() {}
77 // InHeader contains stats when a header is received.
78 type InHeader struct {
79 // Client is true if this InHeader is from client side.
81 // WireLength is the wire length of header.
84 // The following fields are valid only if Client is false.
85 // FullMethod is the full RPC method string, i.e., /package.service/method.
87 // RemoteAddr is the remote address of the corresponding connection.
89 // LocalAddr is the local address of the corresponding connection.
91 // Compression is the compression algorithm used for the RPC.
95 // IsClient indicates if the stats information is from client side.
96 func (s *InHeader) IsClient() bool { return s.Client }
98 func (s *InHeader) isRPCStats() {}
100 // InTrailer contains stats when a trailer is received.
101 type InTrailer struct {
102 // Client is true if this InTrailer is from client side.
104 // WireLength is the wire length of trailer.
108 // IsClient indicates if the stats information is from client side.
109 func (s *InTrailer) IsClient() bool { return s.Client }
111 func (s *InTrailer) isRPCStats() {}
113 // OutPayload contains the information for an outgoing payload.
114 type OutPayload struct {
115 // Client is true if this OutPayload is from client side.
117 // Payload is the payload with original type.
119 // Data is the serialized message payload.
121 // Length is the length of uncompressed data.
123 // WireLength is the length of data on wire (compressed, signed, encrypted).
125 // SentTime is the time when the payload is sent.
129 // IsClient indicates if this stats information is from client side.
130 func (s *OutPayload) IsClient() bool { return s.Client }
132 func (s *OutPayload) isRPCStats() {}
134 // OutHeader contains stats when a header is sent.
135 type OutHeader struct {
136 // Client is true if this OutHeader is from client side.
139 // The following fields are valid only if Client is true.
140 // FullMethod is the full RPC method string, i.e., /package.service/method.
142 // RemoteAddr is the remote address of the corresponding connection.
144 // LocalAddr is the local address of the corresponding connection.
146 // Compression is the compression algorithm used for the RPC.
150 // IsClient indicates if this stats information is from client side.
151 func (s *OutHeader) IsClient() bool { return s.Client }
153 func (s *OutHeader) isRPCStats() {}
155 // OutTrailer contains stats when a trailer is sent.
156 type OutTrailer struct {
157 // Client is true if this OutTrailer is from client side.
159 // WireLength is the wire length of trailer.
163 // IsClient indicates if this stats information is from client side.
164 func (s *OutTrailer) IsClient() bool { return s.Client }
166 func (s *OutTrailer) isRPCStats() {}
168 // End contains stats when an RPC ends.
170 // Client is true if this End is from client side.
172 // EndTime is the time when the RPC ends.
174 // Error is the error the RPC ended with. It is an error generated from
175 // status.Status and can be converted back to status.Status using
176 // status.FromError if non-nil.
180 // IsClient indicates if this is from client side.
181 func (s *End) IsClient() bool { return s.Client }
183 func (s *End) isRPCStats() {}
185 // ConnStats contains stats information about connections.
186 type ConnStats interface {
188 // IsClient returns true if this ConnStats is from client side.
192 // ConnBegin contains the stats of a connection when it is established.
193 type ConnBegin struct {
194 // Client is true if this ConnBegin is from client side.
198 // IsClient indicates if this is from client side.
199 func (s *ConnBegin) IsClient() bool { return s.Client }
201 func (s *ConnBegin) isConnStats() {}
203 // ConnEnd contains the stats of a connection when it ends.
204 type ConnEnd struct {
205 // Client is true if this ConnEnd is from client side.
209 // IsClient indicates if this is from client side.
210 func (s *ConnEnd) IsClient() bool { return s.Client }
212 func (s *ConnEnd) isConnStats() {}
214 type incomingTagsKey struct{}
215 type outgoingTagsKey struct{}
217 // SetTags attaches stats tagging data to the context, which will be sent in
218 // the outgoing RPC with the header grpc-tags-bin. Subsequent calls to
219 // SetTags will overwrite the values from earlier calls.
221 // NOTE: this is provided only for backward compatibility with existing clients
222 // and will likely be removed in an upcoming release. New uses should transmit
223 // this type of data using metadata with a different, non-reserved (i.e. does
224 // not begin with "grpc-") header name.
225 func SetTags(ctx context.Context, b []byte) context.Context {
226 return context.WithValue(ctx, outgoingTagsKey{}, b)
229 // Tags returns the tags from the context for the inbound RPC.
231 // NOTE: this is provided only for backward compatibility with existing clients
232 // and will likely be removed in an upcoming release. New uses should transmit
233 // this type of data using metadata with a different, non-reserved (i.e. does
234 // not begin with "grpc-") header name.
235 func Tags(ctx context.Context) []byte {
236 b, _ := ctx.Value(incomingTagsKey{}).([]byte)
240 // SetIncomingTags attaches stats tagging data to the context, to be read by
241 // the application (not sent in outgoing RPCs).
243 // This is intended for gRPC-internal use ONLY.
244 func SetIncomingTags(ctx context.Context, b []byte) context.Context {
245 return context.WithValue(ctx, incomingTagsKey{}, b)
248 // OutgoingTags returns the tags from the context for the outbound RPC.
250 // This is intended for gRPC-internal use ONLY.
251 func OutgoingTags(ctx context.Context) []byte {
252 b, _ := ctx.Value(outgoingTagsKey{}).([]byte)
256 type incomingTraceKey struct{}
257 type outgoingTraceKey struct{}
259 // SetTrace attaches stats tagging data to the context, which will be sent in
260 // the outgoing RPC with the header grpc-trace-bin. Subsequent calls to
261 // SetTrace will overwrite the values from earlier calls.
263 // NOTE: this is provided only for backward compatibility with existing clients
264 // and will likely be removed in an upcoming release. New uses should transmit
265 // this type of data using metadata with a different, non-reserved (i.e. does
266 // not begin with "grpc-") header name.
267 func SetTrace(ctx context.Context, b []byte) context.Context {
268 return context.WithValue(ctx, outgoingTraceKey{}, b)
271 // Trace returns the trace from the context for the inbound RPC.
273 // NOTE: this is provided only for backward compatibility with existing clients
274 // and will likely be removed in an upcoming release. New uses should transmit
275 // this type of data using metadata with a different, non-reserved (i.e. does
276 // not begin with "grpc-") header name.
277 func Trace(ctx context.Context) []byte {
278 b, _ := ctx.Value(incomingTraceKey{}).([]byte)
282 // SetIncomingTrace attaches stats tagging data to the context, to be read by
283 // the application (not sent in outgoing RPCs). It is intended for
284 // gRPC-internal use.
285 func SetIncomingTrace(ctx context.Context, b []byte) context.Context {
286 return context.WithValue(ctx, incomingTraceKey{}, b)
289 // OutgoingTrace returns the trace from the context for the outbound RPC. It is
290 // intended for gRPC-internal use.
291 func OutgoingTrace(ctx context.Context) []byte {
292 b, _ := ctx.Value(outgoingTraceKey{}).([]byte)