view_to_metric.go 3.6 KB

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