stats.go 11 KB

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