record.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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/metric/metricdata"
  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. type recordOptions struct {
  30. attachments metricdata.Attachments
  31. mutators []tag.Mutator
  32. measurements []Measurement
  33. }
  34. // WithAttachments applies provided exemplar attachments.
  35. func WithAttachments(attachments metricdata.Attachments) Options {
  36. return func(ro *recordOptions) {
  37. ro.attachments = attachments
  38. }
  39. }
  40. // WithTags applies provided tag mutators.
  41. func WithTags(mutators ...tag.Mutator) Options {
  42. return func(ro *recordOptions) {
  43. ro.mutators = mutators
  44. }
  45. }
  46. // WithMeasurements applies provided measurements.
  47. func WithMeasurements(measurements ...Measurement) Options {
  48. return func(ro *recordOptions) {
  49. ro.measurements = measurements
  50. }
  51. }
  52. // Options apply changes to recordOptions.
  53. type Options func(*recordOptions)
  54. func createRecordOption(ros ...Options) *recordOptions {
  55. o := &recordOptions{}
  56. for _, ro := range ros {
  57. ro(o)
  58. }
  59. return o
  60. }
  61. // Record records one or multiple measurements with the same context at once.
  62. // If there are any tags in the context, measurements will be tagged with them.
  63. func Record(ctx context.Context, ms ...Measurement) {
  64. RecordWithOptions(ctx, WithMeasurements(ms...))
  65. }
  66. // RecordWithTags records one or multiple measurements at once.
  67. //
  68. // Measurements will be tagged with the tags in the context mutated by the mutators.
  69. // RecordWithTags is useful if you want to record with tag mutations but don't want
  70. // to propagate the mutations in the context.
  71. func RecordWithTags(ctx context.Context, mutators []tag.Mutator, ms ...Measurement) error {
  72. return RecordWithOptions(ctx, WithTags(mutators...), WithMeasurements(ms...))
  73. }
  74. // RecordWithOptions records measurements from the given options (if any) against context
  75. // and tags and attachments in the options (if any).
  76. // If there are any tags in the context, measurements will be tagged with them.
  77. func RecordWithOptions(ctx context.Context, ros ...Options) error {
  78. o := createRecordOption(ros...)
  79. if len(o.measurements) == 0 {
  80. return nil
  81. }
  82. recorder := internal.DefaultRecorder
  83. if recorder == nil {
  84. return nil
  85. }
  86. record := false
  87. for _, m := range o.measurements {
  88. if m.desc.subscribed() {
  89. record = true
  90. break
  91. }
  92. }
  93. if !record {
  94. return nil
  95. }
  96. if len(o.mutators) > 0 {
  97. var err error
  98. if ctx, err = tag.New(ctx, o.mutators...); err != nil {
  99. return err
  100. }
  101. }
  102. recorder(tag.FromContext(ctx), o.measurements, o.attachments)
  103. return nil
  104. }