1 // Copyright 2019, 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.
21 "go.opencensus.io/metric/metricdata"
22 "go.opencensus.io/stats"
25 func getUnit(unit string) metricdata.Unit {
28 return metricdata.UnitDimensionless
30 return metricdata.UnitMilliseconds
32 return metricdata.UnitBytes
34 return metricdata.UnitDimensionless
37 func getType(v *View) metricdata.Type {
44 case *stats.Int64Measure:
45 return metricdata.TypeCumulativeInt64
46 case *stats.Float64Measure:
47 return metricdata.TypeCumulativeFloat64
49 panic("unexpected measure type")
51 case AggTypeDistribution:
52 return metricdata.TypeCumulativeDistribution
53 case AggTypeLastValue:
55 case *stats.Int64Measure:
56 return metricdata.TypeGaugeInt64
57 case *stats.Float64Measure:
58 return metricdata.TypeGaugeFloat64
60 panic("unexpected measure type")
64 case *stats.Int64Measure:
65 return metricdata.TypeCumulativeInt64
66 case *stats.Float64Measure:
67 return metricdata.TypeCumulativeInt64
69 panic("unexpected measure type")
72 panic("unexpected aggregation type")
76 func getLableKeys(v *View) []string {
77 labelKeys := []string{}
78 for _, k := range v.TagKeys {
79 labelKeys = append(labelKeys, k.Name())
84 func viewToMetricDescriptor(v *View) *metricdata.Descriptor {
85 return &metricdata.Descriptor{
87 Description: v.Description,
88 Unit: getUnit(v.Measure.Unit()),
90 LabelKeys: getLableKeys(v),
94 func toLabelValues(row *Row) []metricdata.LabelValue {
95 labelValues := []metricdata.LabelValue{}
96 for _, tag := range row.Tags {
97 labelValues = append(labelValues, metricdata.NewLabelValue(tag.Value))
102 func rowToTimeseries(v *viewInternal, row *Row, now time.Time, startTime time.Time) *metricdata.TimeSeries {
103 return &metricdata.TimeSeries{
104 Points: []metricdata.Point{row.Data.toPoint(v.metricDescriptor.Type, now)},
105 LabelValues: toLabelValues(row),
106 StartTime: startTime,
110 func viewToMetric(v *viewInternal, now time.Time, startTime time.Time) *metricdata.Metric {
111 if v.metricDescriptor.Type == metricdata.TypeGaugeInt64 ||
112 v.metricDescriptor.Type == metricdata.TypeGaugeFloat64 {
113 startTime = time.Time{}
116 rows := v.collectedRows()
121 ts := []*metricdata.TimeSeries{}
122 for _, row := range rows {
123 ts = append(ts, rowToTimeseries(v, row, now, startTime))
126 m := &metricdata.Metric{
127 Descriptor: *v.metricDescriptor,