stats.go 11 KB

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