Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

70 linhas
1.8 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. //
  15. package stats
  16. import (
  17. "context"
  18. "go.opencensus.io/exemplar"
  19. "go.opencensus.io/stats/internal"
  20. "go.opencensus.io/tag"
  21. )
  22. func init() {
  23. internal.SubscriptionReporter = func(measure string) {
  24. mu.Lock()
  25. measures[measure].subscribe()
  26. mu.Unlock()
  27. }
  28. }
  29. // Record records one or multiple measurements with the same context at once.
  30. // If there are any tags in the context, measurements will be tagged with them.
  31. func Record(ctx context.Context, ms ...Measurement) {
  32. recorder := internal.DefaultRecorder
  33. if recorder == nil {
  34. return
  35. }
  36. if len(ms) == 0 {
  37. return
  38. }
  39. record := false
  40. for _, m := range ms {
  41. if m.desc.subscribed() {
  42. record = true
  43. break
  44. }
  45. }
  46. if !record {
  47. return
  48. }
  49. recorder(tag.FromContext(ctx), ms, exemplar.AttachmentsFromContext(ctx))
  50. }
  51. // RecordWithTags records one or multiple measurements at once.
  52. //
  53. // Measurements will be tagged with the tags in the context mutated by the mutators.
  54. // RecordWithTags is useful if you want to record with tag mutations but don't want
  55. // to propagate the mutations in the context.
  56. func RecordWithTags(ctx context.Context, mutators []tag.Mutator, ms ...Measurement) error {
  57. ctx, err := tag.New(ctx, mutators...)
  58. if err != nil {
  59. return err
  60. }
  61. Record(ctx, ms...)
  62. return nil
  63. }