stats.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. // Copyright 2018, OpenCensus Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package ochttp
  15. import (
  16. "go.opencensus.io/stats"
  17. "go.opencensus.io/stats/view"
  18. "go.opencensus.io/tag"
  19. )
  20. // The following client HTTP measures are supported for use in custom views.
  21. var (
  22. // Deprecated: Use a Count aggregation over one of the other client measures to achieve the same effect.
  23. ClientRequestCount = stats.Int64("opencensus.io/http/client/request_count", "Number of HTTP requests started", stats.UnitDimensionless)
  24. // Deprecated: Use ClientSentBytes.
  25. ClientRequestBytes = stats.Int64("opencensus.io/http/client/request_bytes", "HTTP request body size if set as ContentLength (uncompressed)", stats.UnitBytes)
  26. // Deprecated: Use ClientReceivedBytes.
  27. ClientResponseBytes = stats.Int64("opencensus.io/http/client/response_bytes", "HTTP response body size (uncompressed)", stats.UnitBytes)
  28. // Deprecated: Use ClientRoundtripLatency.
  29. ClientLatency = stats.Float64("opencensus.io/http/client/latency", "End-to-end latency", stats.UnitMilliseconds)
  30. )
  31. // Client measures supported for use in custom views.
  32. var (
  33. ClientSentBytes = stats.Int64(
  34. "opencensus.io/http/client/sent_bytes",
  35. "Total bytes sent in request body (not including headers)",
  36. stats.UnitBytes,
  37. )
  38. ClientReceivedBytes = stats.Int64(
  39. "opencensus.io/http/client/received_bytes",
  40. "Total bytes received in response bodies (not including headers but including error responses with bodies)",
  41. stats.UnitBytes,
  42. )
  43. ClientRoundtripLatency = stats.Float64(
  44. "opencensus.io/http/client/roundtrip_latency",
  45. "Time between first byte of request headers sent to last byte of response received, or terminal error",
  46. stats.UnitMilliseconds,
  47. )
  48. )
  49. // The following server HTTP measures are supported for use in custom views:
  50. var (
  51. ServerRequestCount = stats.Int64("opencensus.io/http/server/request_count", "Number of HTTP requests started", stats.UnitDimensionless)
  52. ServerRequestBytes = stats.Int64("opencensus.io/http/server/request_bytes", "HTTP request body size if set as ContentLength (uncompressed)", stats.UnitBytes)
  53. ServerResponseBytes = stats.Int64("opencensus.io/http/server/response_bytes", "HTTP response body size (uncompressed)", stats.UnitBytes)
  54. ServerLatency = stats.Float64("opencensus.io/http/server/latency", "End-to-end latency", stats.UnitMilliseconds)
  55. )
  56. // The following tags are applied to stats recorded by this package. Host, Path
  57. // and Method are applied to all measures. StatusCode is not applied to
  58. // ClientRequestCount or ServerRequestCount, since it is recorded before the status is known.
  59. var (
  60. // Host is the value of the HTTP Host header.
  61. //
  62. // The value of this tag can be controlled by the HTTP client, so you need
  63. // to watch out for potentially generating high-cardinality labels in your
  64. // metrics backend if you use this tag in views.
  65. Host, _ = tag.NewKey("http.host")
  66. // StatusCode is the numeric HTTP response status code,
  67. // or "error" if a transport error occurred and no status code was read.
  68. StatusCode, _ = tag.NewKey("http.status")
  69. // Path is the URL path (not including query string) in the request.
  70. //
  71. // The value of this tag can be controlled by the HTTP client, so you need
  72. // to watch out for potentially generating high-cardinality labels in your
  73. // metrics backend if you use this tag in views.
  74. Path, _ = tag.NewKey("http.path")
  75. // Method is the HTTP method of the request, capitalized (GET, POST, etc.).
  76. Method, _ = tag.NewKey("http.method")
  77. // KeyServerRoute is a low cardinality string representing the logical
  78. // handler of the request. This is usually the pattern registered on the a
  79. // ServeMux (or similar string).
  80. KeyServerRoute, _ = tag.NewKey("http_server_route")
  81. )
  82. // Client tag keys.
  83. var (
  84. // KeyClientMethod is the HTTP method, capitalized (i.e. GET, POST, PUT, DELETE, etc.).
  85. KeyClientMethod, _ = tag.NewKey("http_client_method")
  86. // KeyClientPath is the URL path (not including query string).
  87. KeyClientPath, _ = tag.NewKey("http_client_path")
  88. // KeyClientStatus is the HTTP status code as an integer (e.g. 200, 404, 500.), or "error" if no response status line was received.
  89. KeyClientStatus, _ = tag.NewKey("http_client_status")
  90. // KeyClientHost is the value of the request Host header.
  91. KeyClientHost, _ = tag.NewKey("http_client_host")
  92. )
  93. // Default distributions used by views in this package.
  94. var (
  95. DefaultSizeDistribution = view.Distribution(0, 1024, 2048, 4096, 16384, 65536, 262144, 1048576, 4194304, 16777216, 67108864, 268435456, 1073741824, 4294967296)
  96. DefaultLatencyDistribution = view.Distribution(0, 1, 2, 3, 4, 5, 6, 8, 10, 13, 16, 20, 25, 30, 40, 50, 65, 80, 100, 130, 160, 200, 250, 300, 400, 500, 650, 800, 1000, 2000, 5000, 10000, 20000, 50000, 100000)
  97. )
  98. // Package ochttp provides some convenience views.
  99. // You still need to register these views for data to actually be collected.
  100. var (
  101. ClientSentBytesDistribution = &view.View{
  102. Name: "opencensus.io/http/client/sent_bytes",
  103. Measure: ClientSentBytes,
  104. Aggregation: DefaultSizeDistribution,
  105. Description: "Total bytes sent in request body (not including headers), by HTTP method and response status",
  106. TagKeys: []tag.Key{KeyClientMethod, KeyClientStatus},
  107. }
  108. ClientReceivedBytesDistribution = &view.View{
  109. Name: "opencensus.io/http/client/received_bytes",
  110. Measure: ClientReceivedBytes,
  111. Aggregation: DefaultSizeDistribution,
  112. Description: "Total bytes received in response bodies (not including headers but including error responses with bodies), by HTTP method and response status",
  113. TagKeys: []tag.Key{KeyClientMethod, KeyClientStatus},
  114. }
  115. ClientRoundtripLatencyDistribution = &view.View{
  116. Name: "opencensus.io/http/client/roundtrip_latency",
  117. Measure: ClientRoundtripLatency,
  118. Aggregation: DefaultLatencyDistribution,
  119. Description: "End-to-end latency, by HTTP method and response status",
  120. TagKeys: []tag.Key{KeyClientMethod, KeyClientStatus},
  121. }
  122. ClientCompletedCount = &view.View{
  123. Name: "opencensus.io/http/client/completed_count",
  124. Measure: ClientRoundtripLatency,
  125. Aggregation: view.Count(),
  126. Description: "Count of completed requests, by HTTP method and response status",
  127. TagKeys: []tag.Key{KeyClientMethod, KeyClientStatus},
  128. }
  129. )
  130. var (
  131. // Deprecated: No direct replacement, but see ClientCompletedCount.
  132. ClientRequestCountView = &view.View{
  133. Name: "opencensus.io/http/client/request_count",
  134. Description: "Count of HTTP requests started",
  135. Measure: ClientRequestCount,
  136. Aggregation: view.Count(),
  137. }
  138. // Deprecated: Use ClientSentBytesDistribution.
  139. ClientRequestBytesView = &view.View{
  140. Name: "opencensus.io/http/client/request_bytes",
  141. Description: "Size distribution of HTTP request body",
  142. Measure: ClientSentBytes,
  143. Aggregation: DefaultSizeDistribution,
  144. }
  145. // Deprecated: Use ClientReceivedBytesDistribution.
  146. ClientResponseBytesView = &view.View{
  147. Name: "opencensus.io/http/client/response_bytes",
  148. Description: "Size distribution of HTTP response body",
  149. Measure: ClientReceivedBytes,
  150. Aggregation: DefaultSizeDistribution,
  151. }
  152. // Deprecated: Use ClientRoundtripLatencyDistribution.
  153. ClientLatencyView = &view.View{
  154. Name: "opencensus.io/http/client/latency",
  155. Description: "Latency distribution of HTTP requests",
  156. Measure: ClientRoundtripLatency,
  157. Aggregation: DefaultLatencyDistribution,
  158. }
  159. // Deprecated: Use ClientCompletedCount.
  160. ClientRequestCountByMethod = &view.View{
  161. Name: "opencensus.io/http/client/request_count_by_method",
  162. Description: "Client request count by HTTP method",
  163. TagKeys: []tag.Key{Method},
  164. Measure: ClientSentBytes,
  165. Aggregation: view.Count(),
  166. }
  167. // Deprecated: Use ClientCompletedCount.
  168. ClientResponseCountByStatusCode = &view.View{
  169. Name: "opencensus.io/http/client/response_count_by_status_code",
  170. Description: "Client response count by status code",
  171. TagKeys: []tag.Key{StatusCode},
  172. Measure: ClientRoundtripLatency,
  173. Aggregation: view.Count(),
  174. }
  175. )
  176. var (
  177. ServerRequestCountView = &view.View{
  178. Name: "opencensus.io/http/server/request_count",
  179. Description: "Count of HTTP requests started",
  180. Measure: ServerRequestCount,
  181. Aggregation: view.Count(),
  182. }
  183. ServerRequestBytesView = &view.View{
  184. Name: "opencensus.io/http/server/request_bytes",
  185. Description: "Size distribution of HTTP request body",
  186. Measure: ServerRequestBytes,
  187. Aggregation: DefaultSizeDistribution,
  188. }
  189. ServerResponseBytesView = &view.View{
  190. Name: "opencensus.io/http/server/response_bytes",
  191. Description: "Size distribution of HTTP response body",
  192. Measure: ServerResponseBytes,
  193. Aggregation: DefaultSizeDistribution,
  194. }
  195. ServerLatencyView = &view.View{
  196. Name: "opencensus.io/http/server/latency",
  197. Description: "Latency distribution of HTTP requests",
  198. Measure: ServerLatency,
  199. Aggregation: DefaultLatencyDistribution,
  200. }
  201. ServerRequestCountByMethod = &view.View{
  202. Name: "opencensus.io/http/server/request_count_by_method",
  203. Description: "Server request count by HTTP method",
  204. TagKeys: []tag.Key{Method},
  205. Measure: ServerRequestCount,
  206. Aggregation: view.Count(),
  207. }
  208. ServerResponseCountByStatusCode = &view.View{
  209. Name: "opencensus.io/http/server/response_count_by_status_code",
  210. Description: "Server response count by status code",
  211. TagKeys: []tag.Key{StatusCode},
  212. Measure: ServerLatency,
  213. Aggregation: view.Count(),
  214. }
  215. )
  216. // DefaultClientViews are the default client views provided by this package.
  217. // Deprecated: No replacement. Register the views you would like individually.
  218. var DefaultClientViews = []*view.View{
  219. ClientRequestCountView,
  220. ClientRequestBytesView,
  221. ClientResponseBytesView,
  222. ClientLatencyView,
  223. ClientRequestCountByMethod,
  224. ClientResponseCountByStatusCode,
  225. }
  226. // DefaultServerViews are the default server views provided by this package.
  227. // Deprecated: No replacement. Register the views you would like individually.
  228. var DefaultServerViews = []*view.View{
  229. ServerRequestCountView,
  230. ServerRequestBytesView,
  231. ServerResponseBytesView,
  232. ServerLatencyView,
  233. ServerRequestCountByMethod,
  234. ServerResponseCountByStatusCode,
  235. }