aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/go.opencensus.io/stats/view
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/go.opencensus.io/stats/view')
-rw-r--r--vendor/go.opencensus.io/stats/view/aggregation.go120
-rw-r--r--vendor/go.opencensus.io/stats/view/aggregation_data.go235
-rw-r--r--vendor/go.opencensus.io/stats/view/collector.go87
-rw-r--r--vendor/go.opencensus.io/stats/view/doc.go47
-rw-r--r--vendor/go.opencensus.io/stats/view/export.go58
-rw-r--r--vendor/go.opencensus.io/stats/view/view.go185
-rw-r--r--vendor/go.opencensus.io/stats/view/worker.go229
-rw-r--r--vendor/go.opencensus.io/stats/view/worker_commands.go183
8 files changed, 1144 insertions, 0 deletions
diff --git a/vendor/go.opencensus.io/stats/view/aggregation.go b/vendor/go.opencensus.io/stats/view/aggregation.go
new file mode 100644
index 000000000..b7f169b4a
--- /dev/null
+++ b/vendor/go.opencensus.io/stats/view/aggregation.go
@@ -0,0 +1,120 @@
+// Copyright 2017, OpenCensus Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+package view
+
+// AggType represents the type of aggregation function used on a View.
+type AggType int
+
+// All available aggregation types.
+const (
+ AggTypeNone AggType = iota // no aggregation; reserved for future use.
+ AggTypeCount // the count aggregation, see Count.
+ AggTypeSum // the sum aggregation, see Sum.
+ AggTypeDistribution // the distribution aggregation, see Distribution.
+ AggTypeLastValue // the last value aggregation, see LastValue.
+)
+
+func (t AggType) String() string {
+ return aggTypeName[t]
+}
+
+var aggTypeName = map[AggType]string{
+ AggTypeNone: "None",
+ AggTypeCount: "Count",
+ AggTypeSum: "Sum",
+ AggTypeDistribution: "Distribution",
+ AggTypeLastValue: "LastValue",
+}
+
+// Aggregation represents a data aggregation method. Use one of the functions:
+// Count, Sum, or Distribution to construct an Aggregation.
+type Aggregation struct {
+ Type AggType // Type is the AggType of this Aggregation.
+ Buckets []float64 // Buckets are the bucket endpoints if this Aggregation represents a distribution, see Distribution.
+
+ newData func() AggregationData
+}
+
+var (
+ aggCount = &Aggregation{
+ Type: AggTypeCount,
+ newData: func() AggregationData {
+ return &CountData{}
+ },
+ }
+ aggSum = &Aggregation{
+ Type: AggTypeSum,
+ newData: func() AggregationData {
+ return &SumData{}
+ },
+ }
+)
+
+// Count indicates that data collected and aggregated
+// with this method will be turned into a count value.
+// For example, total number of accepted requests can be
+// aggregated by using Count.
+func Count() *Aggregation {
+ return aggCount
+}
+
+// Sum indicates that data collected and aggregated
+// with this method will be summed up.
+// For example, accumulated request bytes can be aggregated by using
+// Sum.
+func Sum() *Aggregation {
+ return aggSum
+}
+
+// Distribution indicates that the desired aggregation is
+// a histogram distribution.
+//
+// An distribution aggregation may contain a histogram of the values in the
+// population. The bucket boundaries for that histogram are described
+// by the bounds. This defines len(bounds)+1 buckets.
+//
+// If len(bounds) >= 2 then the boundaries for bucket index i are:
+//
+// [-infinity, bounds[i]) for i = 0
+// [bounds[i-1], bounds[i]) for 0 < i < length
+// [bounds[i-1], +infinity) for i = length
+//
+// If len(bounds) is 0 then there is no histogram associated with the
+// distribution. There will be a single bucket with boundaries
+// (-infinity, +infinity).
+//
+// If len(bounds) is 1 then there is no finite buckets, and that single
+// element is the common boundary of the overflow and underflow buckets.
+func Distribution(bounds ...float64) *Aggregation {
+ return &Aggregation{
+ Type: AggTypeDistribution,
+ Buckets: bounds,
+ newData: func() AggregationData {
+ return newDistributionData(bounds)
+ },
+ }
+}
+
+// LastValue only reports the last value recorded using this
+// aggregation. All other measurements will be dropped.
+func LastValue() *Aggregation {
+ return &Aggregation{
+ Type: AggTypeLastValue,
+ newData: func() AggregationData {
+ return &LastValueData{}
+ },
+ }
+}
diff --git a/vendor/go.opencensus.io/stats/view/aggregation_data.go b/vendor/go.opencensus.io/stats/view/aggregation_data.go
new file mode 100644
index 000000000..960b94601
--- /dev/null
+++ b/vendor/go.opencensus.io/stats/view/aggregation_data.go
@@ -0,0 +1,235 @@
+// Copyright 2017, OpenCensus Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+package view
+
+import (
+ "math"
+
+ "go.opencensus.io/exemplar"
+)
+
+// AggregationData represents an aggregated value from a collection.
+// They are reported on the view data during exporting.
+// Mosts users won't directly access aggregration data.
+type AggregationData interface {
+ isAggregationData() bool
+ addSample(e *exemplar.Exemplar)
+ clone() AggregationData
+ equal(other AggregationData) bool
+}
+
+const epsilon = 1e-9
+
+// CountData is the aggregated data for the Count aggregation.
+// A count aggregation processes data and counts the recordings.
+//
+// Most users won't directly access count data.
+type CountData struct {
+ Value int64
+}
+
+func (a *CountData) isAggregationData() bool { return true }
+
+func (a *CountData) addSample(_ *exemplar.Exemplar) {
+ a.Value = a.Value + 1
+}
+
+func (a *CountData) clone() AggregationData {
+ return &CountData{Value: a.Value}
+}
+
+func (a *CountData) equal(other AggregationData) bool {
+ a2, ok := other.(*CountData)
+ if !ok {
+ return false
+ }
+
+ return a.Value == a2.Value
+}
+
+// SumData is the aggregated data for the Sum aggregation.
+// A sum aggregation processes data and sums up the recordings.
+//
+// Most users won't directly access sum data.
+type SumData struct {
+ Value float64
+}
+
+func (a *SumData) isAggregationData() bool { return true }
+
+func (a *SumData) addSample(e *exemplar.Exemplar) {
+ a.Value += e.Value
+}
+
+func (a *SumData) clone() AggregationData {
+ return &SumData{Value: a.Value}
+}
+
+func (a *SumData) equal(other AggregationData) bool {
+ a2, ok := other.(*SumData)
+ if !ok {
+ return false
+ }
+ return math.Pow(a.Value-a2.Value, 2) < epsilon
+}
+
+// DistributionData is the aggregated data for the
+// Distribution aggregation.
+//
+// Most users won't directly access distribution data.
+//
+// For a distribution with N bounds, the associated DistributionData will have
+// N+1 buckets.
+type DistributionData struct {
+ Count int64 // number of data points aggregated
+ Min float64 // minimum value in the distribution
+ Max float64 // max value in the distribution
+ Mean float64 // mean of the distribution
+ SumOfSquaredDev float64 // sum of the squared deviation from the mean
+ CountPerBucket []int64 // number of occurrences per bucket
+ // ExemplarsPerBucket is slice the same length as CountPerBucket containing
+ // an exemplar for the associated bucket, or nil.
+ ExemplarsPerBucket []*exemplar.Exemplar
+ bounds []float64 // histogram distribution of the values
+}
+
+func newDistributionData(bounds []float64) *DistributionData {
+ bucketCount := len(bounds) + 1
+ return &DistributionData{
+ CountPerBucket: make([]int64, bucketCount),
+ ExemplarsPerBucket: make([]*exemplar.Exemplar, bucketCount),
+ bounds: bounds,
+ Min: math.MaxFloat64,
+ Max: math.SmallestNonzeroFloat64,
+ }
+}
+
+// Sum returns the sum of all samples collected.
+func (a *DistributionData) Sum() float64 { return a.Mean * float64(a.Count) }
+
+func (a *DistributionData) variance() float64 {
+ if a.Count <= 1 {
+ return 0
+ }
+ return a.SumOfSquaredDev / float64(a.Count-1)
+}
+
+func (a *DistributionData) isAggregationData() bool { return true }
+
+func (a *DistributionData) addSample(e *exemplar.Exemplar) {
+ f := e.Value
+ if f < a.Min {
+ a.Min = f
+ }
+ if f > a.Max {
+ a.Max = f
+ }
+ a.Count++
+ a.addToBucket(e)
+
+ if a.Count == 1 {
+ a.Mean = f
+ return
+ }
+
+ oldMean := a.Mean
+ a.Mean = a.Mean + (f-a.Mean)/float64(a.Count)
+ a.SumOfSquaredDev = a.SumOfSquaredDev + (f-oldMean)*(f-a.Mean)
+}
+
+func (a *DistributionData) addToBucket(e *exemplar.Exemplar) {
+ var count *int64
+ var ex **exemplar.Exemplar
+ for i, b := range a.bounds {
+ if e.Value < b {
+ count = &a.CountPerBucket[i]
+ ex = &a.ExemplarsPerBucket[i]
+ break
+ }
+ }
+ if count == nil {
+ count = &a.CountPerBucket[len(a.bounds)]
+ ex = &a.ExemplarsPerBucket[len(a.bounds)]
+ }
+ *count++
+ *ex = maybeRetainExemplar(*ex, e)
+}
+
+func maybeRetainExemplar(old, cur *exemplar.Exemplar) *exemplar.Exemplar {
+ if old == nil {
+ return cur
+ }
+
+ // Heuristic to pick the "better" exemplar: first keep the one with a
+ // sampled trace attachment, if neither have a trace attachment, pick the
+ // one with more attachments.
+ _, haveTraceID := cur.Attachments[exemplar.KeyTraceID]
+ if haveTraceID || len(cur.Attachments) >= len(old.Attachments) {
+ return cur
+ }
+ return old
+}
+
+func (a *DistributionData) clone() AggregationData {
+ c := *a
+ c.CountPerBucket = append([]int64(nil), a.CountPerBucket...)
+ c.ExemplarsPerBucket = append([]*exemplar.Exemplar(nil), a.ExemplarsPerBucket...)
+ return &c
+}
+
+func (a *DistributionData) equal(other AggregationData) bool {
+ a2, ok := other.(*DistributionData)
+ if !ok {
+ return false
+ }
+ if a2 == nil {
+ return false
+ }
+ if len(a.CountPerBucket) != len(a2.CountPerBucket) {
+ return false
+ }
+ for i := range a.CountPerBucket {
+ if a.CountPerBucket[i] != a2.CountPerBucket[i] {
+ return false
+ }
+ }
+ return a.Count == a2.Count && a.Min == a2.Min && a.Max == a2.Max && math.Pow(a.Mean-a2.Mean, 2) < epsilon && math.Pow(a.variance()-a2.variance(), 2) < epsilon
+}
+
+// LastValueData returns the last value recorded for LastValue aggregation.
+type LastValueData struct {
+ Value float64
+}
+
+func (l *LastValueData) isAggregationData() bool {
+ return true
+}
+
+func (l *LastValueData) addSample(e *exemplar.Exemplar) {
+ l.Value = e.Value
+}
+
+func (l *LastValueData) clone() AggregationData {
+ return &LastValueData{l.Value}
+}
+
+func (l *LastValueData) equal(other AggregationData) bool {
+ a2, ok := other.(*LastValueData)
+ if !ok {
+ return false
+ }
+ return l.Value == a2.Value
+}
diff --git a/vendor/go.opencensus.io/stats/view/collector.go b/vendor/go.opencensus.io/stats/view/collector.go
new file mode 100644
index 000000000..32415d485
--- /dev/null
+++ b/vendor/go.opencensus.io/stats/view/collector.go
@@ -0,0 +1,87 @@
+// Copyright 2017, OpenCensus Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+package view
+
+import (
+ "sort"
+
+ "go.opencensus.io/exemplar"
+
+ "go.opencensus.io/internal/tagencoding"
+ "go.opencensus.io/tag"
+)
+
+type collector struct {
+ // signatures holds the aggregations values for each unique tag signature
+ // (values for all keys) to its aggregator.
+ signatures map[string]AggregationData
+ // Aggregation is the description of the aggregation to perform for this
+ // view.
+ a *Aggregation
+}
+
+func (c *collector) addSample(s string, e *exemplar.Exemplar) {
+ aggregator, ok := c.signatures[s]
+ if !ok {
+ aggregator = c.a.newData()
+ c.signatures[s] = aggregator
+ }
+ aggregator.addSample(e)
+}
+
+// collectRows returns a snapshot of the collected Row values.
+func (c *collector) collectedRows(keys []tag.Key) []*Row {
+ rows := make([]*Row, 0, len(c.signatures))
+ for sig, aggregator := range c.signatures {
+ tags := decodeTags([]byte(sig), keys)
+ row := &Row{Tags: tags, Data: aggregator.clone()}
+ rows = append(rows, row)
+ }
+ return rows
+}
+
+func (c *collector) clearRows() {
+ c.signatures = make(map[string]AggregationData)
+}
+
+// encodeWithKeys encodes the map by using values
+// only associated with the keys provided.
+func encodeWithKeys(m *tag.Map, keys []tag.Key) []byte {
+ vb := &tagencoding.Values{
+ Buffer: make([]byte, len(keys)),
+ }
+ for _, k := range keys {
+ v, _ := m.Value(k)
+ vb.WriteValue([]byte(v))
+ }
+ return vb.Bytes()
+}
+
+// decodeTags decodes tags from the buffer and
+// orders them by the keys.
+func decodeTags(buf []byte, keys []tag.Key) []tag.Tag {
+ vb := &tagencoding.Values{Buffer: buf}
+ var tags []tag.Tag
+ for _, k := range keys {
+ v := vb.ReadValue()
+ if v != nil {
+ tags = append(tags, tag.Tag{Key: k, Value: string(v)})
+ }
+ }
+ vb.ReadIndex = 0
+ sort.Slice(tags, func(i, j int) bool { return tags[i].Key.Name() < tags[j].Key.Name() })
+ return tags
+}
diff --git a/vendor/go.opencensus.io/stats/view/doc.go b/vendor/go.opencensus.io/stats/view/doc.go
new file mode 100644
index 000000000..dced225c3
--- /dev/null
+++ b/vendor/go.opencensus.io/stats/view/doc.go
@@ -0,0 +1,47 @@
+// Copyright 2017, OpenCensus Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+// Package view contains support for collecting and exposing aggregates over stats.
+//
+// In order to collect measurements, views need to be defined and registered.
+// A view allows recorded measurements to be filtered and aggregated.
+//
+// All recorded measurements can be grouped by a list of tags.
+//
+// OpenCensus provides several aggregation methods: Count, Distribution and Sum.
+//
+// Count only counts the number of measurement points recorded.
+// Distribution provides statistical summary of the aggregated data by counting
+// how many recorded measurements fall into each bucket.
+// Sum adds up the measurement values.
+// LastValue just keeps track of the most recently recorded measurement value.
+// All aggregations are cumulative.
+//
+// Views can be registerd and unregistered at any time during program execution.
+//
+// Libraries can define views but it is recommended that in most cases registering
+// views be left up to applications.
+//
+// Exporting
+//
+// Collected and aggregated data can be exported to a metric collection
+// backend by registering its exporter.
+//
+// Multiple exporters can be registered to upload the data to various
+// different back ends.
+package view // import "go.opencensus.io/stats/view"
+
+// TODO(acetechnologist): Add a link to the language independent OpenCensus
+// spec when it is available.
diff --git a/vendor/go.opencensus.io/stats/view/export.go b/vendor/go.opencensus.io/stats/view/export.go
new file mode 100644
index 000000000..7cb59718f
--- /dev/null
+++ b/vendor/go.opencensus.io/stats/view/export.go
@@ -0,0 +1,58 @@
+// Copyright 2017, OpenCensus Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package view
+
+import "sync"
+
+var (
+ exportersMu sync.RWMutex // guards exporters
+ exporters = make(map[Exporter]struct{})
+)
+
+// Exporter exports the collected records as view data.
+//
+// The ExportView method should return quickly; if an
+// Exporter takes a significant amount of time to
+// process a Data, that work should be done on another goroutine.
+//
+// It is safe to assume that ExportView will not be called concurrently from
+// multiple goroutines.
+//
+// The Data should not be modified.
+type Exporter interface {
+ ExportView(viewData *Data)
+}
+
+// RegisterExporter registers an exporter.
+// Collected data will be reported via all the
+// registered exporters. Once you no longer
+// want data to be exported, invoke UnregisterExporter
+// with the previously registered exporter.
+//
+// Binaries can register exporters, libraries shouldn't register exporters.
+func RegisterExporter(e Exporter) {
+ exportersMu.Lock()
+ defer exportersMu.Unlock()
+
+ exporters[e] = struct{}{}
+}
+
+// UnregisterExporter unregisters an exporter.
+func UnregisterExporter(e Exporter) {
+ exportersMu.Lock()
+ defer exportersMu.Unlock()
+
+ delete(exporters, e)
+}
diff --git a/vendor/go.opencensus.io/stats/view/view.go b/vendor/go.opencensus.io/stats/view/view.go
new file mode 100644
index 000000000..c2a08af67
--- /dev/null
+++ b/vendor/go.opencensus.io/stats/view/view.go
@@ -0,0 +1,185 @@
+// Copyright 2017, OpenCensus Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+package view
+
+import (
+ "bytes"
+ "fmt"
+ "reflect"
+ "sort"
+ "sync/atomic"
+ "time"
+
+ "go.opencensus.io/exemplar"
+
+ "go.opencensus.io/stats"
+ "go.opencensus.io/stats/internal"
+ "go.opencensus.io/tag"
+)
+
+// View allows users to aggregate the recorded stats.Measurements.
+// Views need to be passed to the Register function to be before data will be
+// collected and sent to Exporters.
+type View struct {
+ Name string // Name of View. Must be unique. If unset, will default to the name of the Measure.
+ Description string // Description is a human-readable description for this view.
+
+ // TagKeys are the tag keys describing the grouping of this view.
+ // A single Row will be produced for each combination of associated tag values.
+ TagKeys []tag.Key
+
+ // Measure is a stats.Measure to aggregate in this view.
+ Measure stats.Measure
+
+ // Aggregation is the aggregation function tp apply to the set of Measurements.
+ Aggregation *Aggregation
+}
+
+// WithName returns a copy of the View with a new name. This is useful for
+// renaming views to cope with limitations placed on metric names by various
+// backends.
+func (v *View) WithName(name string) *View {
+ vNew := *v
+ vNew.Name = name
+ return &vNew
+}
+
+// same compares two views and returns true if they represent the same aggregation.
+func (v *View) same(other *View) bool {
+ if v == other {
+ return true
+ }
+ if v == nil {
+ return false
+ }
+ return reflect.DeepEqual(v.Aggregation, other.Aggregation) &&
+ v.Measure.Name() == other.Measure.Name()
+}
+
+// canonicalize canonicalizes v by setting explicit
+// defaults for Name and Description and sorting the TagKeys
+func (v *View) canonicalize() error {
+ if v.Measure == nil {
+ return fmt.Errorf("cannot register view %q: measure not set", v.Name)
+ }
+ if v.Aggregation == nil {
+ return fmt.Errorf("cannot register view %q: aggregation not set", v.Name)
+ }
+ if v.Name == "" {
+ v.Name = v.Measure.Name()
+ }
+ if v.Description == "" {
+ v.Description = v.Measure.Description()
+ }
+ if err := checkViewName(v.Name); err != nil {
+ return err
+ }
+ sort.Slice(v.TagKeys, func(i, j int) bool {
+ return v.TagKeys[i].Name() < v.TagKeys[j].Name()
+ })
+ return nil
+}
+
+// viewInternal is the internal representation of a View.
+type viewInternal struct {
+ view *View // view is the canonicalized View definition associated with this view.
+ subscribed uint32 // 1 if someone is subscribed and data need to be exported, use atomic to access
+ collector *collector
+}
+
+func newViewInternal(v *View) (*viewInternal, error) {
+ return &viewInternal{
+ view: v,
+ collector: &collector{make(map[string]AggregationData), v.Aggregation},
+ }, nil
+}
+
+func (v *viewInternal) subscribe() {
+ atomic.StoreUint32(&v.subscribed, 1)
+}
+
+func (v *viewInternal) unsubscribe() {
+ atomic.StoreUint32(&v.subscribed, 0)
+}
+
+// isSubscribed returns true if the view is exporting
+// data by subscription.
+func (v *viewInternal) isSubscribed() bool {
+ return atomic.LoadUint32(&v.subscribed) == 1
+}
+
+func (v *viewInternal) clearRows() {
+ v.collector.clearRows()
+}
+
+func (v *viewInternal) collectedRows() []*Row {
+ return v.collector.collectedRows(v.view.TagKeys)
+}
+
+func (v *viewInternal) addSample(m *tag.Map, e *exemplar.Exemplar) {
+ if !v.isSubscribed() {
+ return
+ }
+ sig := string(encodeWithKeys(m, v.view.TagKeys))
+ v.collector.addSample(sig, e)
+}
+
+// A Data is a set of rows about usage of the single measure associated
+// with the given view. Each row is specific to a unique set of tags.
+type Data struct {
+ View *View
+ Start, End time.Time
+ Rows []*Row
+}
+
+// Row is the collected value for a specific set of key value pairs a.k.a tags.
+type Row struct {
+ Tags []tag.Tag
+ Data AggregationData
+}
+
+func (r *Row) String() string {
+ var buffer bytes.Buffer
+ buffer.WriteString("{ ")
+ buffer.WriteString("{ ")
+ for _, t := range r.Tags {
+ buffer.WriteString(fmt.Sprintf("{%v %v}", t.Key.Name(), t.Value))
+ }
+ buffer.WriteString(" }")
+ buffer.WriteString(fmt.Sprintf("%v", r.Data))
+ buffer.WriteString(" }")
+ return buffer.String()
+}
+
+// Equal returns true if both rows are equal. Tags are expected to be ordered
+// by the key name. Even both rows have the same tags but the tags appear in
+// different orders it will return false.
+func (r *Row) Equal(other *Row) bool {
+ if r == other {
+ return true
+ }
+ return reflect.DeepEqual(r.Tags, other.Tags) && r.Data.equal(other.Data)
+}
+
+func checkViewName(name string) error {
+ if len(name) > internal.MaxNameLength {
+ return fmt.Errorf("view name cannot be larger than %v", internal.MaxNameLength)
+ }
+ if !internal.IsPrintable(name) {
+ return fmt.Errorf("view name needs to be an ASCII string")
+ }
+ return nil
+}
diff --git a/vendor/go.opencensus.io/stats/view/worker.go b/vendor/go.opencensus.io/stats/view/worker.go
new file mode 100644
index 000000000..63b0ee3cc
--- /dev/null
+++ b/vendor/go.opencensus.io/stats/view/worker.go
@@ -0,0 +1,229 @@
+// Copyright 2017, OpenCensus Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+package view
+
+import (
+ "fmt"
+ "time"
+
+ "go.opencensus.io/stats"
+ "go.opencensus.io/stats/internal"
+ "go.opencensus.io/tag"
+)
+
+func init() {
+ defaultWorker = newWorker()
+ go defaultWorker.start()
+ internal.DefaultRecorder = record
+}
+
+type measureRef struct {
+ measure string
+ views map[*viewInternal]struct{}
+}
+
+type worker struct {
+ measures map[string]*measureRef
+ views map[string]*viewInternal
+ startTimes map[*viewInternal]time.Time
+
+ timer *time.Ticker
+ c chan command
+ quit, done chan bool
+}
+
+var defaultWorker *worker
+
+var defaultReportingDuration = 10 * time.Second
+
+// Find returns a registered view associated with this name.
+// If no registered view is found, nil is returned.
+func Find(name string) (v *View) {
+ req := &getViewByNameReq{
+ name: name,
+ c: make(chan *getViewByNameResp),
+ }
+ defaultWorker.c <- req
+ resp := <-req.c
+ return resp.v
+}
+
+// Register begins collecting data for the given views.
+// Once a view is registered, it reports data to the registered exporters.
+func Register(views ...*View) error {
+ for _, v := range views {
+ if err := v.canonicalize(); err != nil {
+ return err
+ }
+ }
+ req := &registerViewReq{
+ views: views,
+ err: make(chan error),
+ }
+ defaultWorker.c <- req
+ return <-req.err
+}
+
+// Unregister the given views. Data will not longer be exported for these views
+// after Unregister returns.
+// It is not necessary to unregister from views you expect to collect for the
+// duration of your program execution.
+func Unregister(views ...*View) {
+ names := make([]string, len(views))
+ for i := range views {
+ names[i] = views[i].Name
+ }
+ req := &unregisterFromViewReq{
+ views: names,
+ done: make(chan struct{}),
+ }
+ defaultWorker.c <- req
+ <-req.done
+}
+
+// RetrieveData gets a snapshot of the data collected for the the view registered
+// with the given name. It is intended for testing only.
+func RetrieveData(viewName string) ([]*Row, error) {
+ req := &retrieveDataReq{
+ now: time.Now(),
+ v: viewName,
+ c: make(chan *retrieveDataResp),
+ }
+ defaultWorker.c <- req
+ resp := <-req.c
+ return resp.rows, resp.err
+}
+
+func record(tags *tag.Map, ms interface{}, attachments map[string]string) {
+ req := &recordReq{
+ tm: tags,
+ ms: ms.([]stats.Measurement),
+ attachments: attachments,
+ t: time.Now(),
+ }
+ defaultWorker.c <- req
+}
+
+// SetReportingPeriod sets the interval between reporting aggregated views in
+// the program. If duration is less than or equal to zero, it enables the
+// default behavior.
+//
+// Note: each exporter makes different promises about what the lowest supported
+// duration is. For example, the Stackdriver exporter recommends a value no
+// lower than 1 minute. Consult each exporter per your needs.
+func SetReportingPeriod(d time.Duration) {
+ // TODO(acetechnologist): ensure that the duration d is more than a certain
+ // value. e.g. 1s
+ req := &setReportingPeriodReq{
+ d: d,
+ c: make(chan bool),
+ }
+ defaultWorker.c <- req
+ <-req.c // don't return until the timer is set to the new duration.
+}
+
+func newWorker() *worker {
+ return &worker{
+ measures: make(map[string]*measureRef),
+ views: make(map[string]*viewInternal),
+ startTimes: make(map[*viewInternal]time.Time),
+ timer: time.NewTicker(defaultReportingDuration),
+ c: make(chan command, 1024),
+ quit: make(chan bool),
+ done: make(chan bool),
+ }
+}
+
+func (w *worker) start() {
+ for {
+ select {
+ case cmd := <-w.c:
+ cmd.handleCommand(w)
+ case <-w.timer.C:
+ w.reportUsage(time.Now())
+ case <-w.quit:
+ w.timer.Stop()
+ close(w.c)
+ w.done <- true
+ return
+ }
+ }
+}
+
+func (w *worker) stop() {
+ w.quit <- true
+ <-w.done
+}
+
+func (w *worker) getMeasureRef(name string) *measureRef {
+ if mr, ok := w.measures[name]; ok {
+ return mr
+ }
+ mr := &measureRef{
+ measure: name,
+ views: make(map[*viewInternal]struct{}),
+ }
+ w.measures[name] = mr
+ return mr
+}
+
+func (w *worker) tryRegisterView(v *View) (*viewInternal, error) {
+ vi, err := newViewInternal(v)
+ if err != nil {
+ return nil, err
+ }
+ if x, ok := w.views[vi.view.Name]; ok {
+ if !x.view.same(vi.view) {
+ return nil, fmt.Errorf("cannot register view %q; a different view with the same name is already registered", v.Name)
+ }
+
+ // the view is already registered so there is nothing to do and the
+ // command is considered successful.
+ return x, nil
+ }
+ w.views[vi.view.Name] = vi
+ ref := w.getMeasureRef(vi.view.Measure.Name())
+ ref.views[vi] = struct{}{}
+ return vi, nil
+}
+
+func (w *worker) reportView(v *viewInternal, now time.Time) {
+ if !v.isSubscribed() {
+ return
+ }
+ rows := v.collectedRows()
+ _, ok := w.startTimes[v]
+ if !ok {
+ w.startTimes[v] = now
+ }
+ viewData := &Data{
+ View: v.view,
+ Start: w.startTimes[v],
+ End: time.Now(),
+ Rows: rows,
+ }
+ exportersMu.Lock()
+ for e := range exporters {
+ e.ExportView(viewData)
+ }
+ exportersMu.Unlock()
+}
+
+func (w *worker) reportUsage(now time.Time) {
+ for _, v := range w.views {
+ w.reportView(v, now)
+ }
+}
diff --git a/vendor/go.opencensus.io/stats/view/worker_commands.go b/vendor/go.opencensus.io/stats/view/worker_commands.go
new file mode 100644
index 000000000..b38f26f42
--- /dev/null
+++ b/vendor/go.opencensus.io/stats/view/worker_commands.go
@@ -0,0 +1,183 @@
+// Copyright 2017, OpenCensus Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+package view
+
+import (
+ "errors"
+ "fmt"
+ "strings"
+ "time"
+
+ "go.opencensus.io/exemplar"
+
+ "go.opencensus.io/stats"
+ "go.opencensus.io/stats/internal"
+ "go.opencensus.io/tag"
+)
+
+type command interface {
+ handleCommand(w *worker)
+}
+
+// getViewByNameReq is the command to get a view given its name.
+type getViewByNameReq struct {
+ name string
+ c chan *getViewByNameResp
+}
+
+type getViewByNameResp struct {
+ v *View
+}
+
+func (cmd *getViewByNameReq) handleCommand(w *worker) {
+ v := w.views[cmd.name]
+ if v == nil {
+ cmd.c <- &getViewByNameResp{nil}
+ return
+ }
+ cmd.c <- &getViewByNameResp{v.view}
+}
+
+// registerViewReq is the command to register a view.
+type registerViewReq struct {
+ views []*View
+ err chan error
+}
+
+func (cmd *registerViewReq) handleCommand(w *worker) {
+ var errstr []string
+ for _, view := range cmd.views {
+ vi, err := w.tryRegisterView(view)
+ if err != nil {
+ errstr = append(errstr, fmt.Sprintf("%s: %v", view.Name, err))
+ continue
+ }
+ internal.SubscriptionReporter(view.Measure.Name())
+ vi.subscribe()
+ }
+ if len(errstr) > 0 {
+ cmd.err <- errors.New(strings.Join(errstr, "\n"))
+ } else {
+ cmd.err <- nil
+ }
+}
+
+// unregisterFromViewReq is the command to unregister to a view. Has no
+// impact on the data collection for client that are pulling data from the
+// library.
+type unregisterFromViewReq struct {
+ views []string
+ done chan struct{}
+}
+
+func (cmd *unregisterFromViewReq) handleCommand(w *worker) {
+ for _, name := range cmd.views {
+ vi, ok := w.views[name]
+ if !ok {
+ continue
+ }
+
+ // Report pending data for this view before removing it.
+ w.reportView(vi, time.Now())
+
+ vi.unsubscribe()
+ if !vi.isSubscribed() {
+ // this was the last subscription and view is not collecting anymore.
+ // The collected data can be cleared.
+ vi.clearRows()
+ }
+ delete(w.views, name)
+ }
+ cmd.done <- struct{}{}
+}
+
+// retrieveDataReq is the command to retrieve data for a view.
+type retrieveDataReq struct {
+ now time.Time
+ v string
+ c chan *retrieveDataResp
+}
+
+type retrieveDataResp struct {
+ rows []*Row
+ err error
+}
+
+func (cmd *retrieveDataReq) handleCommand(w *worker) {
+ vi, ok := w.views[cmd.v]
+ if !ok {
+ cmd.c <- &retrieveDataResp{
+ nil,
+ fmt.Errorf("cannot retrieve data; view %q is not registered", cmd.v),
+ }
+ return
+ }
+
+ if !vi.isSubscribed() {
+ cmd.c <- &retrieveDataResp{
+ nil,
+ fmt.Errorf("cannot retrieve data; view %q has no subscriptions or collection is not forcibly started", cmd.v),
+ }
+ return
+ }
+ cmd.c <- &retrieveDataResp{
+ vi.collectedRows(),
+ nil,
+ }
+}
+
+// recordReq is the command to record data related to multiple measures
+// at once.
+type recordReq struct {
+ tm *tag.Map
+ ms []stats.Measurement
+ attachments map[string]string
+ t time.Time
+}
+
+func (cmd *recordReq) handleCommand(w *worker) {
+ for _, m := range cmd.ms {
+ if (m == stats.Measurement{}) { // not registered
+ continue
+ }
+ ref := w.getMeasureRef(m.Measure().Name())
+ for v := range ref.views {
+ e := &exemplar.Exemplar{
+ Value: m.Value(),
+ Timestamp: cmd.t,
+ Attachments: cmd.attachments,
+ }
+ v.addSample(cmd.tm, e)
+ }
+ }
+}
+
+// setReportingPeriodReq is the command to modify the duration between
+// reporting the collected data to the registered clients.
+type setReportingPeriodReq struct {
+ d time.Duration
+ c chan bool
+}
+
+func (cmd *setReportingPeriodReq) handleCommand(w *worker) {
+ w.timer.Stop()
+ if cmd.d <= 0 {
+ w.timer = time.NewTicker(defaultReportingDuration)
+ } else {
+ w.timer = time.NewTicker(cmd.d)
+ }
+ cmd.c <- true
+}