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