You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

view_to_metric.go 3.8 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. package view
  16. import (
  17. "time"
  18. "go.opencensus.io/metric/metricdata"
  19. "go.opencensus.io/stats"
  20. )
  21. func getUnit(unit string) metricdata.Unit {
  22. switch unit {
  23. case "1":
  24. return metricdata.UnitDimensionless
  25. case "ms":
  26. return metricdata.UnitMilliseconds
  27. case "By":
  28. return metricdata.UnitBytes
  29. }
  30. return metricdata.UnitDimensionless
  31. }
  32. func getType(v *View) metricdata.Type {
  33. m := v.Measure
  34. agg := v.Aggregation
  35. switch agg.Type {
  36. case AggTypeSum:
  37. switch m.(type) {
  38. case *stats.Int64Measure:
  39. return metricdata.TypeCumulativeInt64
  40. case *stats.Float64Measure:
  41. return metricdata.TypeCumulativeFloat64
  42. default:
  43. panic("unexpected measure type")
  44. }
  45. case AggTypeDistribution:
  46. return metricdata.TypeCumulativeDistribution
  47. case AggTypeLastValue:
  48. switch m.(type) {
  49. case *stats.Int64Measure:
  50. return metricdata.TypeGaugeInt64
  51. case *stats.Float64Measure:
  52. return metricdata.TypeGaugeFloat64
  53. default:
  54. panic("unexpected measure type")
  55. }
  56. case AggTypeCount:
  57. switch m.(type) {
  58. case *stats.Int64Measure:
  59. return metricdata.TypeCumulativeInt64
  60. case *stats.Float64Measure:
  61. return metricdata.TypeCumulativeInt64
  62. default:
  63. panic("unexpected measure type")
  64. }
  65. default:
  66. panic("unexpected aggregation type")
  67. }
  68. }
  69. func getLabelKeys(v *View) []metricdata.LabelKey {
  70. labelKeys := []metricdata.LabelKey{}
  71. for _, k := range v.TagKeys {
  72. labelKeys = append(labelKeys, metricdata.LabelKey{Key: k.Name()})
  73. }
  74. return labelKeys
  75. }
  76. func viewToMetricDescriptor(v *View) *metricdata.Descriptor {
  77. return &metricdata.Descriptor{
  78. Name: v.Name,
  79. Description: v.Description,
  80. Unit: convertUnit(v),
  81. Type: getType(v),
  82. LabelKeys: getLabelKeys(v),
  83. }
  84. }
  85. func convertUnit(v *View) metricdata.Unit {
  86. switch v.Aggregation.Type {
  87. case AggTypeCount:
  88. return metricdata.UnitDimensionless
  89. default:
  90. return getUnit(v.Measure.Unit())
  91. }
  92. }
  93. func toLabelValues(row *Row, expectedKeys []metricdata.LabelKey) []metricdata.LabelValue {
  94. labelValues := []metricdata.LabelValue{}
  95. tagMap := make(map[string]string)
  96. for _, tag := range row.Tags {
  97. tagMap[tag.Key.Name()] = tag.Value
  98. }
  99. for _, key := range expectedKeys {
  100. if val, ok := tagMap[key.Key]; ok {
  101. labelValues = append(labelValues, metricdata.NewLabelValue(val))
  102. } else {
  103. labelValues = append(labelValues, metricdata.LabelValue{})
  104. }
  105. }
  106. return labelValues
  107. }
  108. func rowToTimeseries(v *viewInternal, row *Row, now time.Time, startTime time.Time) *metricdata.TimeSeries {
  109. return &metricdata.TimeSeries{
  110. Points: []metricdata.Point{row.Data.toPoint(v.metricDescriptor.Type, now)},
  111. LabelValues: toLabelValues(row, v.metricDescriptor.LabelKeys),
  112. StartTime: startTime,
  113. }
  114. }
  115. func viewToMetric(v *viewInternal, now time.Time, startTime time.Time) *metricdata.Metric {
  116. if v.metricDescriptor.Type == metricdata.TypeGaugeInt64 ||
  117. v.metricDescriptor.Type == metricdata.TypeGaugeFloat64 {
  118. startTime = time.Time{}
  119. }
  120. rows := v.collectedRows()
  121. if len(rows) == 0 {
  122. return nil
  123. }
  124. ts := []*metricdata.TimeSeries{}
  125. for _, row := range rows {
  126. ts = append(ts, rowToTimeseries(v, row, now, startTime))
  127. }
  128. m := &metricdata.Metric{
  129. Descriptor: *v.metricDescriptor,
  130. TimeSeries: ts,
  131. }
  132. return m
  133. }