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. 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 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 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} 53 54// IsClient indicates if the stats information is from client side. 55func (s *Begin) IsClient() bool { return s.Client } 56 57func (s *Begin) isRPCStats() {} 58 59// InPayload contains the information for an incoming payload. 60type InPayload struct { 61 // Client is true if this InPayload is from client side. 62 Client bool 63 // Payload is the payload with original type. 64 Payload interface{} 65 // Data is the serialized message payload. 66 Data []byte 67 // Length is the length of uncompressed data. 68 Length int 69 // WireLength is the length of data on wire (compressed, signed, encrypted). 70 WireLength int 71 // RecvTime is the time when the payload is received. 72 RecvTime time.Time 73} 74 75// IsClient indicates if the stats information is from client side. 76func (s *InPayload) IsClient() bool { return s.Client } 77 78func (s *InPayload) isRPCStats() {} 79 80// InHeader contains stats when a header is received. 81type InHeader struct { 82 // Client is true if this InHeader is from client side. 83 Client bool 84 // WireLength is the wire length of header. 85 WireLength int 86 // Compression is the compression algorithm used for the RPC. 87 Compression string 88 // Header contains the header metadata received. 89 Header metadata.MD 90 91 // The following fields are valid only if Client is false. 92 // FullMethod is the full RPC method string, i.e., /package.service/method. 93 FullMethod string 94 // RemoteAddr is the remote address of the corresponding connection. 95 RemoteAddr net.Addr 96 // LocalAddr is the local address of the corresponding connection. 97 LocalAddr net.Addr 98} 99 100// IsClient indicates if the stats information is from client side. 101func (s *InHeader) IsClient() bool { return s.Client } 102 103func (s *InHeader) isRPCStats() {} 104 105// InTrailer contains stats when a trailer is received. 106type InTrailer struct { 107 // Client is true if this InTrailer is from client side. 108 Client bool 109 // WireLength is the wire length of trailer. 110 WireLength int 111 // Trailer contains the trailer metadata received from the server. This 112 // field is only valid if this InTrailer is from the client side. 113 Trailer metadata.MD 114} 115 116// IsClient indicates if the stats information is from client side. 117func (s *InTrailer) IsClient() bool { return s.Client } 118 119func (s *InTrailer) isRPCStats() {} 120 121// OutPayload contains the information for an outgoing payload. 122type OutPayload struct { 123 // Client is true if this OutPayload is from client side. 124 Client bool 125 // Payload is the payload with original type. 126 Payload interface{} 127 // Data is the serialized message payload. 128 Data []byte 129 // Length is the length of uncompressed data. 130 Length int 131 // WireLength is the length of data on wire (compressed, signed, encrypted). 132 WireLength int 133 // SentTime is the time when the payload is sent. 134 SentTime time.Time 135} 136 137// IsClient indicates if this stats information is from client side. 138func (s *OutPayload) IsClient() bool { return s.Client } 139 140func (s *OutPayload) isRPCStats() {} 141 142// OutHeader contains stats when a header is sent. 143type OutHeader struct { 144 // Client is true if this OutHeader is from client side. 145 Client bool 146 // Compression is the compression algorithm used for the RPC. 147 Compression string 148 // Header contains the header metadata sent. 149 Header metadata.MD 150 151 // The following fields are valid only if Client is true. 152 // FullMethod is the full RPC method string, i.e., /package.service/method. 153 FullMethod string 154 // RemoteAddr is the remote address of the corresponding connection. 155 RemoteAddr net.Addr 156 // LocalAddr is the local address of the corresponding connection. 157 LocalAddr net.Addr 158} 159 160// IsClient indicates if this stats information is from client side. 161func (s *OutHeader) IsClient() bool { return s.Client } 162 163func (s *OutHeader) isRPCStats() {} 164 165// OutTrailer contains stats when a trailer is sent. 166type OutTrailer struct { 167 // Client is true if this OutTrailer is from client side. 168 Client bool 169 // WireLength is the wire length of trailer. 170 // 171 // Deprecated: This field is never set. The length is not known when this message is 172 // emitted because the trailer fields are compressed with hpack after that. 173 WireLength int 174 // Trailer contains the trailer metadata sent to the client. This 175 // field is only valid if this OutTrailer is from the server side. 176 Trailer metadata.MD 177} 178 179// IsClient indicates if this stats information is from client side. 180func (s *OutTrailer) IsClient() bool { return s.Client } 181 182func (s *OutTrailer) isRPCStats() {} 183 184// End contains stats when an RPC ends. 185type End struct { 186 // Client is true if this End is from client side. 187 Client bool 188 // BeginTime is the time when the RPC began. 189 BeginTime time.Time 190 // EndTime is the time when the RPC ends. 191 EndTime time.Time 192 // Trailer contains the trailer metadata received from the server. This 193 // field is only valid if this End is from the client side. 194 // Deprecated: use Trailer in InTrailer instead. 195 Trailer metadata.MD 196 // Error is the error the RPC ended with. It is an error generated from 197 // status.Status and can be converted back to status.Status using 198 // status.FromError if non-nil. 199 Error error 200} 201 202// IsClient indicates if this is from client side. 203func (s *End) IsClient() bool { return s.Client } 204 205func (s *End) isRPCStats() {} 206 207// ConnStats contains stats information about connections. 208type ConnStats interface { 209 isConnStats() 210 // IsClient returns true if this ConnStats is from client side. 211 IsClient() bool 212} 213 214// ConnBegin contains the stats of a connection when it is established. 215type ConnBegin struct { 216 // Client is true if this ConnBegin is from client side. 217 Client bool 218} 219 220// IsClient indicates if this is from client side. 221func (s *ConnBegin) IsClient() bool { return s.Client } 222 223func (s *ConnBegin) isConnStats() {} 224 225// ConnEnd contains the stats of a connection when it ends. 226type ConnEnd struct { 227 // Client is true if this ConnEnd is from client side. 228 Client bool 229} 230 231// IsClient indicates if this is from client side. 232func (s *ConnEnd) IsClient() bool { return s.Client } 233 234func (s *ConnEnd) isConnStats() {} 235 236type incomingTagsKey struct{} 237type outgoingTagsKey struct{} 238 239// SetTags attaches stats tagging data to the context, which will be sent in 240// the outgoing RPC with the header grpc-tags-bin. Subsequent calls to 241// SetTags will overwrite the values from earlier calls. 242// 243// NOTE: this is provided only for backward compatibility with existing clients 244// and will likely be removed in an upcoming release. New uses should transmit 245// this type of data using metadata with a different, non-reserved (i.e. does 246// not begin with "grpc-") header name. 247func SetTags(ctx context.Context, b []byte) context.Context { 248 return context.WithValue(ctx, outgoingTagsKey{}, b) 249} 250 251// Tags returns the tags from the context for the inbound RPC. 252// 253// NOTE: this is provided only for backward compatibility with existing clients 254// and will likely be removed in an upcoming release. New uses should transmit 255// this type of data using metadata with a different, non-reserved (i.e. does 256// not begin with "grpc-") header name. 257func Tags(ctx context.Context) []byte { 258 b, _ := ctx.Value(incomingTagsKey{}).([]byte) 259 return b 260} 261 262// SetIncomingTags attaches stats tagging data to the context, to be read by 263// the application (not sent in outgoing RPCs). 264// 265// This is intended for gRPC-internal use ONLY. 266func SetIncomingTags(ctx context.Context, b []byte) context.Context { 267 return context.WithValue(ctx, incomingTagsKey{}, b) 268} 269 270// OutgoingTags returns the tags from the context for the outbound RPC. 271// 272// This is intended for gRPC-internal use ONLY. 273func OutgoingTags(ctx context.Context) []byte { 274 b, _ := ctx.Value(outgoingTagsKey{}).([]byte) 275 return b 276} 277 278type incomingTraceKey struct{} 279type outgoingTraceKey struct{} 280 281// SetTrace attaches stats tagging data to the context, which will be sent in 282// the outgoing RPC with the header grpc-trace-bin. Subsequent calls to 283// SetTrace will overwrite the values from earlier calls. 284// 285// NOTE: this is provided only for backward compatibility with existing clients 286// and will likely be removed in an upcoming release. New uses should transmit 287// this type of data using metadata with a different, non-reserved (i.e. does 288// not begin with "grpc-") header name. 289func SetTrace(ctx context.Context, b []byte) context.Context { 290 return context.WithValue(ctx, outgoingTraceKey{}, b) 291} 292 293// Trace returns the trace from the context for the inbound RPC. 294// 295// NOTE: this is provided only for backward compatibility with existing clients 296// and will likely be removed in an upcoming release. New uses should transmit 297// this type of data using metadata with a different, non-reserved (i.e. does 298// not begin with "grpc-") header name. 299func Trace(ctx context.Context) []byte { 300 b, _ := ctx.Value(incomingTraceKey{}).([]byte) 301 return b 302} 303 304// SetIncomingTrace attaches stats tagging data to the context, to be read by 305// the application (not sent in outgoing RPCs). It is intended for 306// gRPC-internal use. 307func SetIncomingTrace(ctx context.Context, b []byte) context.Context { 308 return context.WithValue(ctx, incomingTraceKey{}, b) 309} 310 311// OutgoingTrace returns the trace from the context for the outbound RPC. It is 312// intended for gRPC-internal use. 313func OutgoingTrace(ctx context.Context) []byte { 314 b, _ := ctx.Value(outgoingTraceKey{}).([]byte) 315 return b 316} 317