metricsutil_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package internal
  4. import (
  5. "go.opentelemetry.io/collector/pdata/pcommon"
  6. "go.opentelemetry.io/collector/pdata/pmetric"
  7. )
  8. type kv struct {
  9. Key, Value string
  10. }
  11. func metrics(metrics ...pmetric.Metric) pmetric.Metrics {
  12. md := pmetric.NewMetrics()
  13. ms := md.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics()
  14. for _, metric := range metrics {
  15. destMetric := ms.AppendEmpty()
  16. metric.CopyTo(destMetric)
  17. }
  18. return md
  19. }
  20. func histogramPointRaw(attributes []*kv, startTimestamp, timestamp pcommon.Timestamp) pmetric.HistogramDataPoint {
  21. hdp := pmetric.NewHistogramDataPoint()
  22. hdp.SetStartTimestamp(startTimestamp)
  23. hdp.SetTimestamp(timestamp)
  24. attrs := hdp.Attributes()
  25. for _, kv := range attributes {
  26. attrs.PutStr(kv.Key, kv.Value)
  27. }
  28. return hdp
  29. }
  30. func histogramPoint(attributes []*kv, startTimestamp, timestamp pcommon.Timestamp, bounds []float64, counts []uint64) pmetric.HistogramDataPoint {
  31. hdp := histogramPointRaw(attributes, startTimestamp, timestamp)
  32. hdp.ExplicitBounds().FromRaw(bounds)
  33. hdp.BucketCounts().FromRaw(counts)
  34. var sum float64
  35. var count uint64
  36. for i, bcount := range counts {
  37. count += bcount
  38. if i > 0 {
  39. sum += float64(bcount) * bounds[i-1]
  40. }
  41. }
  42. hdp.SetCount(count)
  43. hdp.SetSum(sum)
  44. return hdp
  45. }
  46. func histogramPointNoValue(attributes []*kv, startTimestamp, timestamp pcommon.Timestamp) pmetric.HistogramDataPoint {
  47. hdp := histogramPointRaw(attributes, startTimestamp, timestamp)
  48. hdp.SetFlags(pmetric.DefaultDataPointFlags.WithNoRecordedValue(true))
  49. return hdp
  50. }
  51. func histogramMetric(name string, points ...pmetric.HistogramDataPoint) pmetric.Metric {
  52. metric := pmetric.NewMetric()
  53. metric.SetName(name)
  54. histogram := metric.SetEmptyHistogram()
  55. histogram.SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
  56. destPointL := histogram.DataPoints()
  57. // By default the AggregationTemporality is Cumulative until it'll be changed by the caller.
  58. for _, point := range points {
  59. destPoint := destPointL.AppendEmpty()
  60. point.CopyTo(destPoint)
  61. }
  62. return metric
  63. }
  64. func doublePointRaw(attributes []*kv, startTimestamp, timestamp pcommon.Timestamp) pmetric.NumberDataPoint {
  65. ndp := pmetric.NewNumberDataPoint()
  66. ndp.SetStartTimestamp(startTimestamp)
  67. ndp.SetTimestamp(timestamp)
  68. for _, kv := range attributes {
  69. ndp.Attributes().PutStr(kv.Key, kv.Value)
  70. }
  71. return ndp
  72. }
  73. func doublePoint(attributes []*kv, startTimestamp, timestamp pcommon.Timestamp, value float64) pmetric.NumberDataPoint {
  74. ndp := doublePointRaw(attributes, startTimestamp, timestamp)
  75. ndp.SetDoubleValue(value)
  76. return ndp
  77. }
  78. func doublePointNoValue(attributes []*kv, startTimestamp, timestamp pcommon.Timestamp) pmetric.NumberDataPoint {
  79. ndp := doublePointRaw(attributes, startTimestamp, timestamp)
  80. ndp.SetFlags(pmetric.DefaultDataPointFlags.WithNoRecordedValue(true))
  81. return ndp
  82. }
  83. func gaugeMetric(name string, points ...pmetric.NumberDataPoint) pmetric.Metric {
  84. metric := pmetric.NewMetric()
  85. metric.SetName(name)
  86. destPointL := metric.SetEmptyGauge().DataPoints()
  87. for _, point := range points {
  88. destPoint := destPointL.AppendEmpty()
  89. point.CopyTo(destPoint)
  90. }
  91. return metric
  92. }
  93. func sumMetric(name string, points ...pmetric.NumberDataPoint) pmetric.Metric {
  94. metric := pmetric.NewMetric()
  95. metric.SetName(name)
  96. sum := metric.SetEmptySum()
  97. sum.SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
  98. sum.SetIsMonotonic(true)
  99. destPointL := sum.DataPoints()
  100. for _, point := range points {
  101. destPoint := destPointL.AppendEmpty()
  102. point.CopyTo(destPoint)
  103. }
  104. return metric
  105. }
  106. func summaryPointRaw(attributes []*kv, startTimestamp, timestamp pcommon.Timestamp) pmetric.SummaryDataPoint {
  107. sdp := pmetric.NewSummaryDataPoint()
  108. sdp.SetStartTimestamp(startTimestamp)
  109. sdp.SetTimestamp(timestamp)
  110. for _, kv := range attributes {
  111. sdp.Attributes().PutStr(kv.Key, kv.Value)
  112. }
  113. return sdp
  114. }
  115. func summaryPoint(attributes []*kv, startTimestamp, timestamp pcommon.Timestamp, count uint64, sum float64, quantiles, values []float64) pmetric.SummaryDataPoint {
  116. sdp := summaryPointRaw(attributes, startTimestamp, timestamp)
  117. sdp.SetCount(count)
  118. sdp.SetSum(sum)
  119. qvL := sdp.QuantileValues()
  120. for i := 0; i < len(quantiles); i++ {
  121. qvi := qvL.AppendEmpty()
  122. qvi.SetQuantile(quantiles[i])
  123. qvi.SetValue(values[i])
  124. }
  125. return sdp
  126. }
  127. func summaryPointNoValue(attributes []*kv, startTimestamp, timestamp pcommon.Timestamp) pmetric.SummaryDataPoint {
  128. sdp := summaryPointRaw(attributes, startTimestamp, timestamp)
  129. sdp.SetFlags(pmetric.DefaultDataPointFlags.WithNoRecordedValue(true))
  130. return sdp
  131. }
  132. func summaryMetric(name string, points ...pmetric.SummaryDataPoint) pmetric.Metric {
  133. metric := pmetric.NewMetric()
  134. metric.SetName(name)
  135. destPointL := metric.SetEmptySummary().DataPoints()
  136. for _, point := range points {
  137. destPoint := destPointL.AppendEmpty()
  138. point.CopyTo(destPoint)
  139. }
  140. return metric
  141. }