stats.go 12 KB

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