stats.go 9.7 KB

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