example_test.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2017, 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. //
  15. package tag_test
  16. import (
  17. "context"
  18. "log"
  19. "go.opencensus.io/tag"
  20. )
  21. var (
  22. tagMap *tag.Map
  23. ctx context.Context
  24. key tag.Key
  25. )
  26. func ExampleNewKey() {
  27. // Get a key to represent user OS.
  28. key, err := tag.NewKey("example.com/keys/user-os")
  29. if err != nil {
  30. log.Fatal(err)
  31. }
  32. _ = key // use key
  33. }
  34. func ExampleNew() {
  35. osKey, err := tag.NewKey("example.com/keys/user-os")
  36. if err != nil {
  37. log.Fatal(err)
  38. }
  39. userIDKey, err := tag.NewKey("example.com/keys/user-id")
  40. if err != nil {
  41. log.Fatal(err)
  42. }
  43. ctx, err := tag.New(ctx,
  44. tag.Insert(osKey, "macOS-10.12.5"),
  45. tag.Upsert(userIDKey, "cde36753ed"),
  46. )
  47. if err != nil {
  48. log.Fatal(err)
  49. }
  50. _ = ctx // use context
  51. }
  52. func ExampleNew_replace() {
  53. ctx, err := tag.New(ctx,
  54. tag.Insert(key, "macOS-10.12.5"),
  55. tag.Upsert(key, "macOS-10.12.7"),
  56. )
  57. if err != nil {
  58. log.Fatal(err)
  59. }
  60. _ = ctx // use context
  61. }
  62. func ExampleNewContext() {
  63. // Propagate the tag map in the current context.
  64. ctx := tag.NewContext(context.Background(), tagMap)
  65. _ = ctx // use context
  66. }
  67. func ExampleFromContext() {
  68. tagMap := tag.FromContext(ctx)
  69. _ = tagMap // use the tag map
  70. }
  71. func ExampleDo() {
  72. ctx, err := tag.New(ctx,
  73. tag.Insert(key, "macOS-10.12.5"),
  74. tag.Upsert(key, "macOS-10.12.7"),
  75. )
  76. if err != nil {
  77. log.Fatal(err)
  78. }
  79. tag.Do(ctx, func(ctx context.Context) {
  80. _ = ctx // use context
  81. })
  82. }