stats.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. }
  82. // IsClient indicates if the stats information is from client side.
  83. func (s *InHeader) IsClient() bool { return s.Client }
  84. func (s *InHeader) isRPCStats() {}
  85. // InTrailer contains stats when a trailer is received.
  86. type InTrailer struct {
  87. // Client is true if this InTrailer is from client side.
  88. Client bool
  89. // WireLength is the wire length of trailer.
  90. WireLength int
  91. }
  92. // IsClient indicates if the stats information is from client side.
  93. func (s *InTrailer) IsClient() bool { return s.Client }
  94. func (s *InTrailer) isRPCStats() {}
  95. // OutPayload contains the information for an outgoing payload.
  96. type OutPayload struct {
  97. // Client is true if this OutPayload is from client side.
  98. Client bool
  99. // Payload is the payload with original type.
  100. Payload interface{}
  101. // Data is the serialized message payload.
  102. Data []byte
  103. // Length is the length of uncompressed data.
  104. Length int
  105. // WireLength is the length of data on wire (compressed, signed, encrypted).
  106. WireLength int
  107. // SentTime is the time when the payload is sent.
  108. SentTime time.Time
  109. }
  110. // IsClient indicates if this stats information is from client side.
  111. func (s *OutPayload) IsClient() bool { return s.Client }
  112. func (s *OutPayload) isRPCStats() {}
  113. // OutHeader contains stats when a header is sent.
  114. type OutHeader struct {
  115. // Client is true if this OutHeader is from client side.
  116. Client bool
  117. // The following fields are valid only if Client is true.
  118. // FullMethod is the full RPC method string, i.e., /package.service/method.
  119. FullMethod string
  120. // RemoteAddr is the remote address of the corresponding connection.
  121. RemoteAddr net.Addr
  122. // LocalAddr is the local address of the corresponding connection.
  123. LocalAddr net.Addr
  124. // Compression is the compression algorithm used for the RPC.
  125. Compression string
  126. }
  127. // IsClient indicates if this stats information is from client side.
  128. func (s *OutHeader) IsClient() bool { return s.Client }
  129. func (s *OutHeader) isRPCStats() {}
  130. // OutTrailer contains stats when a trailer is sent.
  131. type OutTrailer struct {
  132. // Client is true if this OutTrailer is from client side.
  133. Client bool
  134. // WireLength is the wire length of trailer.
  135. WireLength int
  136. }
  137. // IsClient indicates if this stats information is from client side.
  138. func (s *OutTrailer) IsClient() bool { return s.Client }
  139. func (s *OutTrailer) isRPCStats() {}
  140. // End contains stats when an RPC ends.
  141. type End struct {
  142. // Client is true if this End is from client side.
  143. Client bool
  144. // BeginTime is the time when the RPC began.
  145. BeginTime time.Time
  146. // EndTime is the time when the RPC ends.
  147. EndTime time.Time
  148. // Trailer contains the trailer metadata received from the server. This
  149. // field is only valid if this End is from the client side.
  150. Trailer metadata.MD
  151. // Error is the error the RPC ended with. It is an error generated from
  152. // status.Status and can be converted back to status.Status using
  153. // status.FromError if non-nil.
  154. Error error
  155. }
  156. // IsClient indicates if this is from client side.
  157. func (s *End) IsClient() bool { return s.Client }
  158. func (s *End) isRPCStats() {}
  159. // ConnStats contains stats information about connections.
  160. type ConnStats interface {
  161. isConnStats()
  162. // IsClient returns true if this ConnStats is from client side.
  163. IsClient() bool
  164. }
  165. // ConnBegin contains the stats of a connection when it is established.
  166. type ConnBegin struct {
  167. // Client is true if this ConnBegin is from client side.
  168. Client bool
  169. }
  170. // IsClient indicates if this is from client side.
  171. func (s *ConnBegin) IsClient() bool { return s.Client }
  172. func (s *ConnBegin) isConnStats() {}
  173. // ConnEnd contains the stats of a connection when it ends.
  174. type ConnEnd struct {
  175. // Client is true if this ConnEnd is from client side.
  176. Client bool
  177. }
  178. // IsClient indicates if this is from client side.
  179. func (s *ConnEnd) IsClient() bool { return s.Client }
  180. func (s *ConnEnd) isConnStats() {}
  181. type incomingTagsKey struct{}
  182. type outgoingTagsKey struct{}
  183. // SetTags attaches stats tagging data to the context, which will be sent in
  184. // the outgoing RPC with the header grpc-tags-bin. Subsequent calls to
  185. // SetTags will overwrite the values from earlier calls.
  186. //
  187. // NOTE: this is provided only for backward compatibility with existing clients
  188. // and will likely be removed in an upcoming release. New uses should transmit
  189. // this type of data using metadata with a different, non-reserved (i.e. does
  190. // not begin with "grpc-") header name.
  191. func SetTags(ctx context.Context, b []byte) context.Context {
  192. return context.WithValue(ctx, outgoingTagsKey{}, b)
  193. }
  194. // Tags returns the tags from the context for the inbound RPC.
  195. //
  196. // NOTE: this is provided only for backward compatibility with existing clients
  197. // and will likely be removed in an upcoming release. New uses should transmit
  198. // this type of data using metadata with a different, non-reserved (i.e. does
  199. // not begin with "grpc-") header name.
  200. func Tags(ctx context.Context) []byte {
  201. b, _ := ctx.Value(incomingTagsKey{}).([]byte)
  202. return b
  203. }
  204. // SetIncomingTags attaches stats tagging data to the context, to be read by
  205. // the application (not sent in outgoing RPCs).
  206. //
  207. // This is intended for gRPC-internal use ONLY.
  208. func SetIncomingTags(ctx context.Context, b []byte) context.Context {
  209. return context.WithValue(ctx, incomingTagsKey{}, b)
  210. }
  211. // OutgoingTags returns the tags from the context for the outbound RPC.
  212. //
  213. // This is intended for gRPC-internal use ONLY.
  214. func OutgoingTags(ctx context.Context) []byte {
  215. b, _ := ctx.Value(outgoingTagsKey{}).([]byte)
  216. return b
  217. }
  218. type incomingTraceKey struct{}
  219. type outgoingTraceKey struct{}
  220. // SetTrace attaches stats tagging data to the context, which will be sent in
  221. // the outgoing RPC with the header grpc-trace-bin. Subsequent calls to
  222. // SetTrace will overwrite the values from earlier calls.
  223. //
  224. // NOTE: this is provided only for backward compatibility with existing clients
  225. // and will likely be removed in an upcoming release. New uses should transmit
  226. // this type of data using metadata with a different, non-reserved (i.e. does
  227. // not begin with "grpc-") header name.
  228. func SetTrace(ctx context.Context, b []byte) context.Context {
  229. return context.WithValue(ctx, outgoingTraceKey{}, b)
  230. }
  231. // Trace returns the trace from the context for the inbound RPC.
  232. //
  233. // NOTE: this is provided only for backward compatibility with existing clients
  234. // and will likely be removed in an upcoming release. New uses should transmit
  235. // this type of data using metadata with a different, non-reserved (i.e. does
  236. // not begin with "grpc-") header name.
  237. func Trace(ctx context.Context) []byte {
  238. b, _ := ctx.Value(incomingTraceKey{}).([]byte)
  239. return b
  240. }
  241. // SetIncomingTrace attaches stats tagging data to the context, to be read by
  242. // the application (not sent in outgoing RPCs). It is intended for
  243. // gRPC-internal use.
  244. func SetIncomingTrace(ctx context.Context, b []byte) context.Context {
  245. return context.WithValue(ctx, incomingTraceKey{}, b)
  246. }
  247. // OutgoingTrace returns the trace from the context for the outbound RPC. It is
  248. // intended for gRPC-internal use.
  249. func OutgoingTrace(ctx context.Context) []byte {
  250. b, _ := ctx.Value(outgoingTraceKey{}).([]byte)
  251. return b
  252. }