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