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.
 
 
 

78 lines
2.0 KiB

  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. }