stats.go 10 KB

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