example_test.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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_test
  15. import (
  16. "log"
  17. "net/http"
  18. "go.opencensus.io/plugin/ochttp"
  19. "go.opencensus.io/plugin/ochttp/propagation/b3"
  20. "go.opencensus.io/stats/view"
  21. "go.opencensus.io/tag"
  22. )
  23. func ExampleTransport() {
  24. // import (
  25. // "go.opencensus.io/plugin/ochttp"
  26. // "go.opencensus.io/stats/view"
  27. // )
  28. if err := view.Register(
  29. // Register a few default views.
  30. ochttp.ClientSentBytesDistribution,
  31. ochttp.ClientReceivedBytesDistribution,
  32. ochttp.ClientRoundtripLatencyDistribution,
  33. // Register a custom view.
  34. &view.View{
  35. Name: "httpclient_latency_by_path",
  36. TagKeys: []tag.Key{ochttp.KeyClientPath},
  37. Measure: ochttp.ClientRoundtripLatency,
  38. Aggregation: ochttp.DefaultLatencyDistribution,
  39. },
  40. ); err != nil {
  41. log.Fatal(err)
  42. }
  43. client := &http.Client{
  44. Transport: &ochttp.Transport{},
  45. }
  46. // Use client to perform requests.
  47. _ = client
  48. }
  49. var usersHandler http.Handler
  50. func ExampleHandler() {
  51. // import "go.opencensus.io/plugin/ochttp"
  52. http.Handle("/users", ochttp.WithRouteTag(usersHandler, "/users"))
  53. // If no handler is specified, the default mux is used.
  54. log.Fatal(http.ListenAndServe("localhost:8080", &ochttp.Handler{}))
  55. }
  56. func ExampleHandler_mux() {
  57. // import "go.opencensus.io/plugin/ochttp"
  58. mux := http.NewServeMux()
  59. mux.Handle("/users", ochttp.WithRouteTag(usersHandler, "/users"))
  60. log.Fatal(http.ListenAndServe("localhost:8080", &ochttp.Handler{
  61. Handler: mux,
  62. Propagation: &b3.HTTPFormat{},
  63. }))
  64. }