1 // Copyright 2017, OpenCensus Authors
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
26 "go.opencensus.io/exemplar"
28 "go.opencensus.io/stats"
29 "go.opencensus.io/stats/internal"
30 "go.opencensus.io/tag"
33 // View allows users to aggregate the recorded stats.Measurements.
34 // Views need to be passed to the Register function to be before data will be
35 // collected and sent to Exporters.
37 Name string // Name of View. Must be unique. If unset, will default to the name of the Measure.
38 Description string // Description is a human-readable description for this view.
40 // TagKeys are the tag keys describing the grouping of this view.
41 // A single Row will be produced for each combination of associated tag values.
44 // Measure is a stats.Measure to aggregate in this view.
47 // Aggregation is the aggregation function tp apply to the set of Measurements.
48 Aggregation *Aggregation
51 // WithName returns a copy of the View with a new name. This is useful for
52 // renaming views to cope with limitations placed on metric names by various
54 func (v *View) WithName(name string) *View {
60 // same compares two views and returns true if they represent the same aggregation.
61 func (v *View) same(other *View) bool {
68 return reflect.DeepEqual(v.Aggregation, other.Aggregation) &&
69 v.Measure.Name() == other.Measure.Name()
72 // canonicalize canonicalizes v by setting explicit
73 // defaults for Name and Description and sorting the TagKeys
74 func (v *View) canonicalize() error {
76 return fmt.Errorf("cannot register view %q: measure not set", v.Name)
78 if v.Aggregation == nil {
79 return fmt.Errorf("cannot register view %q: aggregation not set", v.Name)
82 v.Name = v.Measure.Name()
84 if v.Description == "" {
85 v.Description = v.Measure.Description()
87 if err := checkViewName(v.Name); err != nil {
90 sort.Slice(v.TagKeys, func(i, j int) bool {
91 return v.TagKeys[i].Name() < v.TagKeys[j].Name()
96 // viewInternal is the internal representation of a View.
97 type viewInternal struct {
98 view *View // view is the canonicalized View definition associated with this view.
99 subscribed uint32 // 1 if someone is subscribed and data need to be exported, use atomic to access
103 func newViewInternal(v *View) (*viewInternal, error) {
104 return &viewInternal{
106 collector: &collector{make(map[string]AggregationData), v.Aggregation},
110 func (v *viewInternal) subscribe() {
111 atomic.StoreUint32(&v.subscribed, 1)
114 func (v *viewInternal) unsubscribe() {
115 atomic.StoreUint32(&v.subscribed, 0)
118 // isSubscribed returns true if the view is exporting
119 // data by subscription.
120 func (v *viewInternal) isSubscribed() bool {
121 return atomic.LoadUint32(&v.subscribed) == 1
124 func (v *viewInternal) clearRows() {
125 v.collector.clearRows()
128 func (v *viewInternal) collectedRows() []*Row {
129 return v.collector.collectedRows(v.view.TagKeys)
132 func (v *viewInternal) addSample(m *tag.Map, e *exemplar.Exemplar) {
133 if !v.isSubscribed() {
136 sig := string(encodeWithKeys(m, v.view.TagKeys))
137 v.collector.addSample(sig, e)
140 // A Data is a set of rows about usage of the single measure associated
141 // with the given view. Each row is specific to a unique set of tags.
148 // Row is the collected value for a specific set of key value pairs a.k.a tags.
154 func (r *Row) String() string {
155 var buffer bytes.Buffer
156 buffer.WriteString("{ ")
157 buffer.WriteString("{ ")
158 for _, t := range r.Tags {
159 buffer.WriteString(fmt.Sprintf("{%v %v}", t.Key.Name(), t.Value))
161 buffer.WriteString(" }")
162 buffer.WriteString(fmt.Sprintf("%v", r.Data))
163 buffer.WriteString(" }")
164 return buffer.String()
167 // Equal returns true if both rows are equal. Tags are expected to be ordered
168 // by the key name. Even both rows have the same tags but the tags appear in
169 // different orders it will return false.
170 func (r *Row) Equal(other *Row) bool {
174 return reflect.DeepEqual(r.Tags, other.Tags) && r.Data.equal(other.Data)
177 func checkViewName(name string) error {
178 if len(name) > internal.MaxNameLength {
179 return fmt.Errorf("view name cannot be larger than %v", internal.MaxNameLength)
181 if !internal.IsPrintable(name) {
182 return fmt.Errorf("view name needs to be an ASCII string")