You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

пре 5 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. # OpenCensus Libraries for Go
  2. [![Build Status][travis-image]][travis-url]
  3. [![Windows Build Status][appveyor-image]][appveyor-url]
  4. [![GoDoc][godoc-image]][godoc-url]
  5. [![Gitter chat][gitter-image]][gitter-url]
  6. OpenCensus Go is a Go implementation of OpenCensus, a toolkit for
  7. collecting application performance and behavior monitoring data.
  8. Currently it consists of three major components: tags, stats and tracing.
  9. ## Installation
  10. ```
  11. $ go get -u go.opencensus.io
  12. ```
  13. The API of this project is still evolving, see: [Deprecation Policy](#deprecation-policy).
  14. The use of vendoring or a dependency management tool is recommended.
  15. ## Prerequisites
  16. OpenCensus Go libraries require Go 1.8 or later.
  17. ## Getting Started
  18. The easiest way to get started using OpenCensus in your application is to use an existing
  19. integration with your RPC framework:
  20. * [net/http](https://godoc.org/go.opencensus.io/plugin/ochttp)
  21. * [gRPC](https://godoc.org/go.opencensus.io/plugin/ocgrpc)
  22. * [database/sql](https://godoc.org/github.com/opencensus-integrations/ocsql)
  23. * [Go kit](https://godoc.org/github.com/go-kit/kit/tracing/opencensus)
  24. * [Groupcache](https://godoc.org/github.com/orijtech/groupcache)
  25. * [Caddy webserver](https://godoc.org/github.com/orijtech/caddy)
  26. * [MongoDB](https://godoc.org/github.com/orijtech/mongo-go-driver)
  27. * [Redis gomodule/redigo](https://godoc.org/github.com/orijtech/redigo)
  28. * [Redis goredis/redis](https://godoc.org/github.com/orijtech/redis)
  29. * [Memcache](https://godoc.org/github.com/orijtech/gomemcache)
  30. If you're using a framework not listed here, you could either implement your own middleware for your
  31. framework or use [custom stats](#stats) and [spans](#spans) directly in your application.
  32. ## Exporters
  33. OpenCensus can export instrumentation data to various backends.
  34. OpenCensus has exporter implementations for the following, users
  35. can implement their own exporters by implementing the exporter interfaces
  36. ([stats](https://godoc.org/go.opencensus.io/stats/view#Exporter),
  37. [trace](https://godoc.org/go.opencensus.io/trace#Exporter)):
  38. * [Prometheus][exporter-prom] for stats
  39. * [OpenZipkin][exporter-zipkin] for traces
  40. * [Stackdriver][exporter-stackdriver] Monitoring for stats and Trace for traces
  41. * [Jaeger][exporter-jaeger] for traces
  42. * [AWS X-Ray][exporter-xray] for traces
  43. * [Datadog][exporter-datadog] for stats and traces
  44. * [Graphite][exporter-graphite] for stats
  45. * [Honeycomb][exporter-honeycomb] for traces
  46. ## Overview
  47. ![OpenCensus Overview](https://i.imgur.com/cf4ElHE.jpg)
  48. In a microservices environment, a user request may go through
  49. multiple services until there is a response. OpenCensus allows
  50. you to instrument your services and collect diagnostics data all
  51. through your services end-to-end.
  52. ## Tags
  53. Tags represent propagated key-value pairs. They are propagated using `context.Context`
  54. in the same process or can be encoded to be transmitted on the wire. Usually, this will
  55. be handled by an integration plugin, e.g. `ocgrpc.ServerHandler` and `ocgrpc.ClientHandler`
  56. for gRPC.
  57. Package `tag` allows adding or modifying tags in the current context.
  58. [embedmd]:# (internal/readme/tags.go new)
  59. ```go
  60. ctx, err = tag.New(ctx,
  61. tag.Insert(osKey, "macOS-10.12.5"),
  62. tag.Upsert(userIDKey, "cde36753ed"),
  63. )
  64. if err != nil {
  65. log.Fatal(err)
  66. }
  67. ```
  68. ## Stats
  69. OpenCensus is a low-overhead framework even if instrumentation is always enabled.
  70. In order to be so, it is optimized to make recording of data points fast
  71. and separate from the data aggregation.
  72. OpenCensus stats collection happens in two stages:
  73. * Definition of measures and recording of data points
  74. * Definition of views and aggregation of the recorded data
  75. ### Recording
  76. Measurements are data points associated with a measure.
  77. Recording implicitly tags the set of Measurements with the tags from the
  78. provided context:
  79. [embedmd]:# (internal/readme/stats.go record)
  80. ```go
  81. stats.Record(ctx, videoSize.M(102478))
  82. ```
  83. ### Views
  84. Views are how Measures are aggregated. You can think of them as queries over the
  85. set of recorded data points (measurements).
  86. Views have two parts: the tags to group by and the aggregation type used.
  87. Currently three types of aggregations are supported:
  88. * CountAggregation is used to count the number of times a sample was recorded.
  89. * DistributionAggregation is used to provide a histogram of the values of the samples.
  90. * SumAggregation is used to sum up all sample values.
  91. [embedmd]:# (internal/readme/stats.go aggs)
  92. ```go
  93. distAgg := view.Distribution(1<<32, 2<<32, 3<<32)
  94. countAgg := view.Count()
  95. sumAgg := view.Sum()
  96. ```
  97. Here we create a view with the DistributionAggregation over our measure.
  98. [embedmd]:# (internal/readme/stats.go view)
  99. ```go
  100. if err := view.Register(&view.View{
  101. Name: "example.com/video_size_distribution",
  102. Description: "distribution of processed video size over time",
  103. Measure: videoSize,
  104. Aggregation: view.Distribution(1<<32, 2<<32, 3<<32),
  105. }); err != nil {
  106. log.Fatalf("Failed to register view: %v", err)
  107. }
  108. ```
  109. Register begins collecting data for the view. Registered views' data will be
  110. exported via the registered exporters.
  111. ## Traces
  112. A distributed trace tracks the progression of a single user request as
  113. it is handled by the services and processes that make up an application.
  114. Each step is called a span in the trace. Spans include metadata about the step,
  115. including especially the time spent in the step, called the span’s latency.
  116. Below you see a trace and several spans underneath it.
  117. ![Traces and spans](https://i.imgur.com/7hZwRVj.png)
  118. ### Spans
  119. Span is the unit step in a trace. Each span has a name, latency, status and
  120. additional metadata.
  121. Below we are starting a span for a cache read and ending it
  122. when we are done:
  123. [embedmd]:# (internal/readme/trace.go startend)
  124. ```go
  125. ctx, span := trace.StartSpan(ctx, "cache.Get")
  126. defer span.End()
  127. // Do work to get from cache.
  128. ```
  129. ### Propagation
  130. Spans can have parents or can be root spans if they don't have any parents.
  131. The current span is propagated in-process and across the network to allow associating
  132. new child spans with the parent.
  133. In the same process, `context.Context` is used to propagate spans.
  134. `trace.StartSpan` creates a new span as a root if the current context
  135. doesn't contain a span. Or, it creates a child of the span that is
  136. already in current context. The returned context can be used to keep
  137. propagating the newly created span in the current context.
  138. [embedmd]:# (internal/readme/trace.go startend)
  139. ```go
  140. ctx, span := trace.StartSpan(ctx, "cache.Get")
  141. defer span.End()
  142. // Do work to get from cache.
  143. ```
  144. Across the network, OpenCensus provides different propagation
  145. methods for different protocols.
  146. * gRPC integrations use the OpenCensus' [binary propagation format](https://godoc.org/go.opencensus.io/trace/propagation).
  147. * HTTP integrations use Zipkin's [B3](https://github.com/openzipkin/b3-propagation)
  148. by default but can be configured to use a custom propagation method by setting another
  149. [propagation.HTTPFormat](https://godoc.org/go.opencensus.io/trace/propagation#HTTPFormat).
  150. ## Execution Tracer
  151. With Go 1.11, OpenCensus Go will support integration with the Go execution tracer.
  152. See [Debugging Latency in Go](https://medium.com/observability/debugging-latency-in-go-1-11-9f97a7910d68)
  153. for an example of their mutual use.
  154. ## Profiles
  155. OpenCensus tags can be applied as profiler labels
  156. for users who are on Go 1.9 and above.
  157. [embedmd]:# (internal/readme/tags.go profiler)
  158. ```go
  159. ctx, err = tag.New(ctx,
  160. tag.Insert(osKey, "macOS-10.12.5"),
  161. tag.Insert(userIDKey, "fff0989878"),
  162. )
  163. if err != nil {
  164. log.Fatal(err)
  165. }
  166. tag.Do(ctx, func(ctx context.Context) {
  167. // Do work.
  168. // When profiling is on, samples will be
  169. // recorded with the key/values from the tag map.
  170. })
  171. ```
  172. A screenshot of the CPU profile from the program above:
  173. ![CPU profile](https://i.imgur.com/jBKjlkw.png)
  174. ## Deprecation Policy
  175. Before version 1.0.0, the following deprecation policy will be observed:
  176. No backwards-incompatible changes will be made except for the removal of symbols that have
  177. been marked as *Deprecated* for at least one minor release (e.g. 0.9.0 to 0.10.0). A release
  178. removing the *Deprecated* functionality will be made no sooner than 28 days after the first
  179. release in which the functionality was marked *Deprecated*.
  180. [travis-image]: https://travis-ci.org/census-instrumentation/opencensus-go.svg?branch=master
  181. [travis-url]: https://travis-ci.org/census-instrumentation/opencensus-go
  182. [appveyor-image]: https://ci.appveyor.com/api/projects/status/vgtt29ps1783ig38?svg=true
  183. [appveyor-url]: https://ci.appveyor.com/project/opencensusgoteam/opencensus-go/branch/master
  184. [godoc-image]: https://godoc.org/go.opencensus.io?status.svg
  185. [godoc-url]: https://godoc.org/go.opencensus.io
  186. [gitter-image]: https://badges.gitter.im/census-instrumentation/lobby.svg
  187. [gitter-url]: https://gitter.im/census-instrumentation/lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge
  188. [new-ex]: https://godoc.org/go.opencensus.io/tag#example-NewMap
  189. [new-replace-ex]: https://godoc.org/go.opencensus.io/tag#example-NewMap--Replace
  190. [exporter-prom]: https://godoc.org/go.opencensus.io/exporter/prometheus
  191. [exporter-stackdriver]: https://godoc.org/contrib.go.opencensus.io/exporter/stackdriver
  192. [exporter-zipkin]: https://godoc.org/go.opencensus.io/exporter/zipkin
  193. [exporter-jaeger]: https://godoc.org/go.opencensus.io/exporter/jaeger
  194. [exporter-xray]: https://github.com/census-ecosystem/opencensus-go-exporter-aws
  195. [exporter-datadog]: https://github.com/DataDog/opencensus-go-exporter-datadog
  196. [exporter-graphite]: https://github.com/census-ecosystem/opencensus-go-exporter-graphite
  197. [exporter-honeycomb]: https://github.com/honeycombio/opencensus-exporter