google.golang.org/grpc@v1.72.2/stats/stats.go (about) 1 /* 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. 22 package stats // import "google.golang.org/grpc/stats" 23 24 import ( 25 "context" 26 "net" 27 "time" 28 29 "google.golang.org/grpc/metadata" 30 ) 31 32 // RPCStats contains stats information about RPCs. 33 type RPCStats interface { 34 isRPCStats() 35 // IsClient returns true if this RPCStats is from client side. 36 IsClient() bool 37 } 38 39 // Begin contains stats for the start of an RPC attempt. 40 // 41 // - Server-side: Triggered after `InHeader`, as headers are processed 42 // before the RPC lifecycle begins. 43 // - Client-side: The first stats event recorded. 44 // 45 // FailFast is only valid if this Begin is from client side. 46 type Begin struct { 47 // Client is true if this Begin is from client side. 48 Client bool 49 // BeginTime is the time when the RPC attempt begins. 50 BeginTime time.Time 51 // FailFast indicates if this RPC is failfast. 52 FailFast bool 53 // IsClientStream indicates whether the RPC is a client streaming RPC. 54 IsClientStream bool 55 // IsServerStream indicates whether the RPC is a server streaming RPC. 56 IsServerStream bool 57 // IsTransparentRetryAttempt indicates whether this attempt was initiated 58 // due to transparently retrying a previous attempt. 59 IsTransparentRetryAttempt bool 60 } 61 62 // IsClient indicates if the stats information is from client side. 63 func (s *Begin) IsClient() bool { return s.Client } 64 65 func (s *Begin) isRPCStats() {} 66 67 // PickerUpdated indicates that the LB policy provided a new picker while the 68 // RPC was waiting for one. 69 type PickerUpdated struct{} 70 71 // IsClient indicates if the stats information is from client side. Only Client 72 // Side interfaces with a Picker, thus always returns true. 73 func (*PickerUpdated) IsClient() bool { return true } 74 75 func (*PickerUpdated) isRPCStats() {} 76 77 // InPayload contains stats about an incoming payload. 78 type InPayload struct { 79 // Client is true if this InPayload is from client side. 80 Client bool 81 // Payload is the payload with original type. This may be modified after 82 // the call to HandleRPC which provides the InPayload returns and must be 83 // copied if needed later. 84 Payload any 85 86 // Length is the size of the uncompressed payload data. Does not include any 87 // framing (gRPC or HTTP/2). 88 Length int 89 // CompressedLength is the size of the compressed payload data. Does not 90 // include any framing (gRPC or HTTP/2). Same as Length if compression not 91 // enabled. 92 CompressedLength int 93 // WireLength is the size of the compressed payload data plus gRPC framing. 94 // Does not include HTTP/2 framing. 95 WireLength int 96 97 // RecvTime is the time when the payload is received. 98 RecvTime time.Time 99 } 100 101 // IsClient indicates if the stats information is from client side. 102 func (s *InPayload) IsClient() bool { return s.Client } 103 104 func (s *InPayload) isRPCStats() {} 105 106 // InHeader contains stats about header reception. 107 // 108 // - Server-side: The first stats event after the RPC request is received. 109 type InHeader struct { 110 // Client is true if this InHeader is from client side. 111 Client bool 112 // WireLength is the wire length of header. 113 WireLength int 114 // Compression is the compression algorithm used for the RPC. 115 Compression string 116 // Header contains the header metadata received. 117 Header metadata.MD 118 119 // The following fields are valid only if Client is false. 120 // FullMethod is the full RPC method string, i.e., /package.service/method. 121 FullMethod string 122 // RemoteAddr is the remote address of the corresponding connection. 123 RemoteAddr net.Addr 124 // LocalAddr is the local address of the corresponding connection. 125 LocalAddr net.Addr 126 } 127 128 // IsClient indicates if the stats information is from client side. 129 func (s *InHeader) IsClient() bool { return s.Client } 130 131 func (s *InHeader) isRPCStats() {} 132 133 // InTrailer contains stats about trailer reception. 134 type InTrailer struct { 135 // Client is true if this InTrailer is from client side. 136 Client bool 137 // WireLength is the wire length of trailer. 138 WireLength int 139 // Trailer contains the trailer metadata received from the server. This 140 // field is only valid if this InTrailer is from the client side. 141 Trailer metadata.MD 142 } 143 144 // IsClient indicates if the stats information is from client side. 145 func (s *InTrailer) IsClient() bool { return s.Client } 146 147 func (s *InTrailer) isRPCStats() {} 148 149 // OutPayload contains stats about an outgoing payload. 150 type OutPayload struct { 151 // Client is true if this OutPayload is from client side. 152 Client bool 153 // Payload is the payload with original type. This may be modified after 154 // the call to HandleRPC which provides the OutPayload returns and must be 155 // copied if needed later. 156 Payload any 157 // Length is the size of the uncompressed payload data. Does not include any 158 // framing (gRPC or HTTP/2). 159 Length int 160 // CompressedLength is the size of the compressed payload data. Does not 161 // include any framing (gRPC or HTTP/2). Same as Length if compression not 162 // enabled. 163 CompressedLength int 164 // WireLength is the size of the compressed payload data plus gRPC framing. 165 // Does not include HTTP/2 framing. 166 WireLength int 167 // SentTime is the time when the payload is sent. 168 SentTime time.Time 169 } 170 171 // IsClient indicates if this stats information is from client side. 172 func (s *OutPayload) IsClient() bool { return s.Client } 173 174 func (s *OutPayload) isRPCStats() {} 175 176 // OutHeader contains stats about header transmission. 177 // 178 // - Client-side: Only occurs after 'Begin', as headers are always the first 179 // thing sent on a stream. 180 type OutHeader struct { 181 // Client is true if this OutHeader is from client side. 182 Client bool 183 // Compression is the compression algorithm used for the RPC. 184 Compression string 185 // Header contains the header metadata sent. 186 Header metadata.MD 187 188 // The following fields are valid only if Client is true. 189 // FullMethod is the full RPC method string, i.e., /package.service/method. 190 FullMethod string 191 // RemoteAddr is the remote address of the corresponding connection. 192 RemoteAddr net.Addr 193 // LocalAddr is the local address of the corresponding connection. 194 LocalAddr net.Addr 195 } 196 197 // IsClient indicates if this stats information is from client side. 198 func (s *OutHeader) IsClient() bool { return s.Client } 199 200 func (s *OutHeader) isRPCStats() {} 201 202 // OutTrailer contains stats about trailer transmission. 203 type OutTrailer struct { 204 // Client is true if this OutTrailer is from client side. 205 Client bool 206 // WireLength is the wire length of trailer. 207 // 208 // Deprecated: This field is never set. The length is not known when this 209 // message is emitted because the trailer fields are compressed with hpack 210 // after that. 211 WireLength int 212 // Trailer contains the trailer metadata sent to the client. This 213 // field is only valid if this OutTrailer is from the server side. 214 Trailer metadata.MD 215 } 216 217 // IsClient indicates if this stats information is from client side. 218 func (s *OutTrailer) IsClient() bool { return s.Client } 219 220 func (s *OutTrailer) isRPCStats() {} 221 222 // End contains stats about RPC completion. 223 type End struct { 224 // Client is true if this End is from client side. 225 Client bool 226 // BeginTime is the time when the RPC began. 227 BeginTime time.Time 228 // EndTime is the time when the RPC ends. 229 EndTime time.Time 230 // Trailer contains the trailer metadata received from the server. This 231 // field is only valid if this End is from the client side. 232 // Deprecated: use Trailer in InTrailer instead. 233 Trailer metadata.MD 234 // Error is the error the RPC ended with. It is an error generated from 235 // status.Status and can be converted back to status.Status using 236 // status.FromError if non-nil. 237 Error error 238 } 239 240 // IsClient indicates if this is from client side. 241 func (s *End) IsClient() bool { return s.Client } 242 243 func (s *End) isRPCStats() {} 244 245 // ConnStats contains stats information about connections. 246 type ConnStats interface { 247 isConnStats() 248 // IsClient returns true if this ConnStats is from client side. 249 IsClient() bool 250 } 251 252 // ConnBegin contains stats about connection establishment. 253 type ConnBegin struct { 254 // Client is true if this ConnBegin is from client side. 255 Client bool 256 } 257 258 // IsClient indicates if this is from client side. 259 func (s *ConnBegin) IsClient() bool { return s.Client } 260 261 func (s *ConnBegin) isConnStats() {} 262 263 // ConnEnd contains stats about connection termination. 264 type ConnEnd struct { 265 // Client is true if this ConnEnd is from client side. 266 Client bool 267 } 268 269 // IsClient indicates if this is from client side. 270 func (s *ConnEnd) IsClient() bool { return s.Client } 271 272 func (s *ConnEnd) isConnStats() {} 273 274 // SetTags attaches stats tagging data to the context, which will be sent in 275 // the outgoing RPC with the header grpc-tags-bin. Subsequent calls to 276 // SetTags will overwrite the values from earlier calls. 277 // 278 // Deprecated: set the `grpc-tags-bin` header in the metadata instead. 279 func SetTags(ctx context.Context, b []byte) context.Context { 280 return metadata.AppendToOutgoingContext(ctx, "grpc-tags-bin", string(b)) 281 } 282 283 // Tags returns the tags from the context for the inbound RPC. 284 // 285 // Deprecated: obtain the `grpc-tags-bin` header from metadata instead. 286 func Tags(ctx context.Context) []byte { 287 traceValues := metadata.ValueFromIncomingContext(ctx, "grpc-tags-bin") 288 if len(traceValues) == 0 { 289 return nil 290 } 291 return []byte(traceValues[len(traceValues)-1]) 292 } 293 294 // SetTrace attaches stats tagging data to the context, which will be sent in 295 // the outgoing RPC with the header grpc-trace-bin. Subsequent calls to 296 // SetTrace will overwrite the values from earlier calls. 297 // 298 // Deprecated: set the `grpc-trace-bin` header in the metadata instead. 299 func SetTrace(ctx context.Context, b []byte) context.Context { 300 return metadata.AppendToOutgoingContext(ctx, "grpc-trace-bin", string(b)) 301 } 302 303 // Trace returns the trace from the context for the inbound RPC. 304 // 305 // Deprecated: obtain the `grpc-trace-bin` header from metadata instead. 306 func Trace(ctx context.Context) []byte { 307 traceValues := metadata.ValueFromIncomingContext(ctx, "grpc-trace-bin") 308 if len(traceValues) == 0 { 309 return nil 310 } 311 return []byte(traceValues[len(traceValues)-1]) 312 }