blob: 7a552a9b7871199ec44b6f3251f6e76da01af577 [file] [log] [blame]
khenaidoo5fc5cea2021-08-11 17:39:16 -04001/*
2 *
3 * Copyright 2016 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 stats is for collecting and reporting various network and RPC stats.
20// This package is for monitoring purpose only. All fields are read-only.
21// All APIs are experimental.
22package stats // import "google.golang.org/grpc/stats"
23
24import (
25 "context"
26 "net"
27 "time"
28
29 "google.golang.org/grpc/metadata"
30)
31
32// RPCStats contains stats information about RPCs.
33type RPCStats interface {
34 isRPCStats()
35 // IsClient returns true if this RPCStats is from client side.
36 IsClient() bool
37}
38
39// Begin contains stats when an RPC attempt begins.
40// FailFast is only valid if this Begin is from client side.
41type Begin struct {
42 // Client is true if this Begin is from client side.
43 Client bool
44 // BeginTime is the time when the RPC attempt begins.
45 BeginTime time.Time
46 // FailFast indicates if this RPC is failfast.
47 FailFast bool
48 // IsClientStream indicates whether the RPC is a client streaming RPC.
49 IsClientStream bool
50 // IsServerStream indicates whether the RPC is a server streaming RPC.
51 IsServerStream bool
52 // IsTransparentRetryAttempt indicates whether this attempt was initiated
53 // due to transparently retrying a previous attempt.
54 IsTransparentRetryAttempt bool
55}
56
57// IsClient indicates if the stats information is from client side.
58func (s *Begin) IsClient() bool { return s.Client }
59
60func (s *Begin) isRPCStats() {}
61
62// InPayload contains the information for an incoming payload.
63type InPayload struct {
64 // Client is true if this InPayload is from client side.
65 Client bool
66 // Payload is the payload with original type.
67 Payload interface{}
68 // Data is the serialized message payload.
69 Data []byte
Akash Kankanala761955c2024-02-21 19:32:20 +053070
71 // Length is the size of the uncompressed payload data. Does not include any
72 // framing (gRPC or HTTP/2).
khenaidoo5fc5cea2021-08-11 17:39:16 -040073 Length int
Akash Kankanala761955c2024-02-21 19:32:20 +053074 // CompressedLength is the size of the compressed payload data. Does not
75 // include any framing (gRPC or HTTP/2). Same as Length if compression not
76 // enabled.
77 CompressedLength int
78 // WireLength is the size of the compressed payload data plus gRPC framing.
79 // Does not include HTTP/2 framing.
khenaidoo5fc5cea2021-08-11 17:39:16 -040080 WireLength int
Akash Kankanala761955c2024-02-21 19:32:20 +053081
khenaidoo5fc5cea2021-08-11 17:39:16 -040082 // RecvTime is the time when the payload is received.
83 RecvTime time.Time
84}
85
86// IsClient indicates if the stats information is from client side.
87func (s *InPayload) IsClient() bool { return s.Client }
88
89func (s *InPayload) isRPCStats() {}
90
91// InHeader contains stats when a header is received.
92type InHeader struct {
93 // Client is true if this InHeader is from client side.
94 Client bool
95 // WireLength is the wire length of header.
96 WireLength int
97 // Compression is the compression algorithm used for the RPC.
98 Compression string
99 // Header contains the header metadata received.
100 Header metadata.MD
101
102 // The following fields are valid only if Client is false.
103 // FullMethod is the full RPC method string, i.e., /package.service/method.
104 FullMethod string
105 // RemoteAddr is the remote address of the corresponding connection.
106 RemoteAddr net.Addr
107 // LocalAddr is the local address of the corresponding connection.
108 LocalAddr net.Addr
109}
110
111// IsClient indicates if the stats information is from client side.
112func (s *InHeader) IsClient() bool { return s.Client }
113
114func (s *InHeader) isRPCStats() {}
115
116// InTrailer contains stats when a trailer is received.
117type InTrailer struct {
118 // Client is true if this InTrailer is from client side.
119 Client bool
120 // WireLength is the wire length of trailer.
121 WireLength int
122 // Trailer contains the trailer metadata received from the server. This
123 // field is only valid if this InTrailer is from the client side.
124 Trailer metadata.MD
125}
126
127// IsClient indicates if the stats information is from client side.
128func (s *InTrailer) IsClient() bool { return s.Client }
129
130func (s *InTrailer) isRPCStats() {}
131
132// OutPayload contains the information for an outgoing payload.
133type OutPayload struct {
134 // Client is true if this OutPayload is from client side.
135 Client bool
136 // Payload is the payload with original type.
137 Payload interface{}
138 // Data is the serialized message payload.
139 Data []byte
Akash Kankanala761955c2024-02-21 19:32:20 +0530140 // Length is the size of the uncompressed payload data. Does not include any
141 // framing (gRPC or HTTP/2).
khenaidoo5fc5cea2021-08-11 17:39:16 -0400142 Length int
Akash Kankanala761955c2024-02-21 19:32:20 +0530143 // CompressedLength is the size of the compressed payload data. Does not
144 // include any framing (gRPC or HTTP/2). Same as Length if compression not
145 // enabled.
146 CompressedLength int
147 // WireLength is the size of the compressed payload data plus gRPC framing.
148 // Does not include HTTP/2 framing.
khenaidoo5fc5cea2021-08-11 17:39:16 -0400149 WireLength int
150 // SentTime is the time when the payload is sent.
151 SentTime time.Time
152}
153
154// IsClient indicates if this stats information is from client side.
155func (s *OutPayload) IsClient() bool { return s.Client }
156
157func (s *OutPayload) isRPCStats() {}
158
159// OutHeader contains stats when a header is sent.
160type OutHeader struct {
161 // Client is true if this OutHeader is from client side.
162 Client bool
163 // Compression is the compression algorithm used for the RPC.
164 Compression string
165 // Header contains the header metadata sent.
166 Header metadata.MD
167
168 // The following fields are valid only if Client is true.
169 // FullMethod is the full RPC method string, i.e., /package.service/method.
170 FullMethod string
171 // RemoteAddr is the remote address of the corresponding connection.
172 RemoteAddr net.Addr
173 // LocalAddr is the local address of the corresponding connection.
174 LocalAddr net.Addr
175}
176
177// IsClient indicates if this stats information is from client side.
178func (s *OutHeader) IsClient() bool { return s.Client }
179
180func (s *OutHeader) isRPCStats() {}
181
182// OutTrailer contains stats when a trailer is sent.
183type OutTrailer struct {
184 // Client is true if this OutTrailer is from client side.
185 Client bool
186 // WireLength is the wire length of trailer.
187 //
188 // Deprecated: This field is never set. The length is not known when this message is
189 // emitted because the trailer fields are compressed with hpack after that.
190 WireLength int
191 // Trailer contains the trailer metadata sent to the client. This
192 // field is only valid if this OutTrailer is from the server side.
193 Trailer metadata.MD
194}
195
196// IsClient indicates if this stats information is from client side.
197func (s *OutTrailer) IsClient() bool { return s.Client }
198
199func (s *OutTrailer) isRPCStats() {}
200
201// End contains stats when an RPC ends.
202type End struct {
203 // Client is true if this End is from client side.
204 Client bool
205 // BeginTime is the time when the RPC began.
206 BeginTime time.Time
207 // EndTime is the time when the RPC ends.
208 EndTime time.Time
209 // Trailer contains the trailer metadata received from the server. This
210 // field is only valid if this End is from the client side.
211 // Deprecated: use Trailer in InTrailer instead.
212 Trailer metadata.MD
213 // Error is the error the RPC ended with. It is an error generated from
214 // status.Status and can be converted back to status.Status using
215 // status.FromError if non-nil.
216 Error error
217}
218
219// IsClient indicates if this is from client side.
220func (s *End) IsClient() bool { return s.Client }
221
222func (s *End) isRPCStats() {}
223
224// ConnStats contains stats information about connections.
225type ConnStats interface {
226 isConnStats()
227 // IsClient returns true if this ConnStats is from client side.
228 IsClient() bool
229}
230
231// ConnBegin contains the stats of a connection when it is established.
232type ConnBegin struct {
233 // Client is true if this ConnBegin is from client side.
234 Client bool
235}
236
237// IsClient indicates if this is from client side.
238func (s *ConnBegin) IsClient() bool { return s.Client }
239
240func (s *ConnBegin) isConnStats() {}
241
242// ConnEnd contains the stats of a connection when it ends.
243type ConnEnd struct {
244 // Client is true if this ConnEnd is from client side.
245 Client bool
246}
247
248// IsClient indicates if this is from client side.
249func (s *ConnEnd) IsClient() bool { return s.Client }
250
251func (s *ConnEnd) isConnStats() {}
252
253type incomingTagsKey struct{}
254type outgoingTagsKey struct{}
255
256// SetTags attaches stats tagging data to the context, which will be sent in
257// the outgoing RPC with the header grpc-tags-bin. Subsequent calls to
258// SetTags will overwrite the values from earlier calls.
259//
260// NOTE: this is provided only for backward compatibility with existing clients
261// and will likely be removed in an upcoming release. New uses should transmit
262// this type of data using metadata with a different, non-reserved (i.e. does
263// not begin with "grpc-") header name.
264func SetTags(ctx context.Context, b []byte) context.Context {
265 return context.WithValue(ctx, outgoingTagsKey{}, b)
266}
267
268// Tags returns the tags from the context for the inbound RPC.
269//
270// NOTE: this is provided only for backward compatibility with existing clients
271// and will likely be removed in an upcoming release. New uses should transmit
272// this type of data using metadata with a different, non-reserved (i.e. does
273// not begin with "grpc-") header name.
274func Tags(ctx context.Context) []byte {
275 b, _ := ctx.Value(incomingTagsKey{}).([]byte)
276 return b
277}
278
279// SetIncomingTags attaches stats tagging data to the context, to be read by
280// the application (not sent in outgoing RPCs).
281//
282// This is intended for gRPC-internal use ONLY.
283func SetIncomingTags(ctx context.Context, b []byte) context.Context {
284 return context.WithValue(ctx, incomingTagsKey{}, b)
285}
286
287// OutgoingTags returns the tags from the context for the outbound RPC.
288//
289// This is intended for gRPC-internal use ONLY.
290func OutgoingTags(ctx context.Context) []byte {
291 b, _ := ctx.Value(outgoingTagsKey{}).([]byte)
292 return b
293}
294
295type incomingTraceKey struct{}
296type outgoingTraceKey struct{}
297
298// SetTrace attaches stats tagging data to the context, which will be sent in
299// the outgoing RPC with the header grpc-trace-bin. Subsequent calls to
300// SetTrace will overwrite the values from earlier calls.
301//
302// NOTE: this is provided only for backward compatibility with existing clients
303// and will likely be removed in an upcoming release. New uses should transmit
304// this type of data using metadata with a different, non-reserved (i.e. does
305// not begin with "grpc-") header name.
306func SetTrace(ctx context.Context, b []byte) context.Context {
307 return context.WithValue(ctx, outgoingTraceKey{}, b)
308}
309
310// Trace returns the trace from the context for the inbound RPC.
311//
312// NOTE: this is provided only for backward compatibility with existing clients
313// and will likely be removed in an upcoming release. New uses should transmit
314// this type of data using metadata with a different, non-reserved (i.e. does
315// not begin with "grpc-") header name.
316func Trace(ctx context.Context) []byte {
317 b, _ := ctx.Value(incomingTraceKey{}).([]byte)
318 return b
319}
320
321// SetIncomingTrace attaches stats tagging data to the context, to be read by
322// the application (not sent in outgoing RPCs). It is intended for
323// gRPC-internal use.
324func SetIncomingTrace(ctx context.Context, b []byte) context.Context {
325 return context.WithValue(ctx, incomingTraceKey{}, b)
326}
327
328// OutgoingTrace returns the trace from the context for the outbound RPC. It is
329// intended for gRPC-internal use.
330func OutgoingTrace(ctx context.Context) []byte {
331 b, _ := ctx.Value(outgoingTraceKey{}).([]byte)
332 return b
333}