stats.go 10 KB

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