Code refactoring for bpa operator
[icn.git] / cmd / bpa-operator / vendor / go.opencensus.io / stats / view / view_to_metric.go
1 // Copyright 2019, 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 view
17
18 import (
19         "time"
20
21         "go.opencensus.io/metric/metricdata"
22         "go.opencensus.io/stats"
23 )
24
25 func getUnit(unit string) metricdata.Unit {
26         switch unit {
27         case "1":
28                 return metricdata.UnitDimensionless
29         case "ms":
30                 return metricdata.UnitMilliseconds
31         case "By":
32                 return metricdata.UnitBytes
33         }
34         return metricdata.UnitDimensionless
35 }
36
37 func getType(v *View) metricdata.Type {
38         m := v.Measure
39         agg := v.Aggregation
40
41         switch agg.Type {
42         case AggTypeSum:
43                 switch m.(type) {
44                 case *stats.Int64Measure:
45                         return metricdata.TypeCumulativeInt64
46                 case *stats.Float64Measure:
47                         return metricdata.TypeCumulativeFloat64
48                 default:
49                         panic("unexpected measure type")
50                 }
51         case AggTypeDistribution:
52                 return metricdata.TypeCumulativeDistribution
53         case AggTypeLastValue:
54                 switch m.(type) {
55                 case *stats.Int64Measure:
56                         return metricdata.TypeGaugeInt64
57                 case *stats.Float64Measure:
58                         return metricdata.TypeGaugeFloat64
59                 default:
60                         panic("unexpected measure type")
61                 }
62         case AggTypeCount:
63                 switch m.(type) {
64                 case *stats.Int64Measure:
65                         return metricdata.TypeCumulativeInt64
66                 case *stats.Float64Measure:
67                         return metricdata.TypeCumulativeInt64
68                 default:
69                         panic("unexpected measure type")
70                 }
71         default:
72                 panic("unexpected aggregation type")
73         }
74 }
75
76 func getLableKeys(v *View) []string {
77         labelKeys := []string{}
78         for _, k := range v.TagKeys {
79                 labelKeys = append(labelKeys, k.Name())
80         }
81         return labelKeys
82 }
83
84 func viewToMetricDescriptor(v *View) *metricdata.Descriptor {
85         return &metricdata.Descriptor{
86                 Name:        v.Name,
87                 Description: v.Description,
88                 Unit:        getUnit(v.Measure.Unit()),
89                 Type:        getType(v),
90                 LabelKeys:   getLableKeys(v),
91         }
92 }
93
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))
98         }
99         return labelValues
100 }
101
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,
107         }
108 }
109
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{}
114         }
115
116         rows := v.collectedRows()
117         if len(rows) == 0 {
118                 return nil
119         }
120
121         ts := []*metricdata.TimeSeries{}
122         for _, row := range rows {
123                 ts = append(ts, rowToTimeseries(v, row, now, startTime))
124         }
125
126         m := &metricdata.Metric{
127                 Descriptor: *v.metricDescriptor,
128                 TimeSeries: ts,
129         }
130         return m
131 }