generated_metrics_test.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // Code generated by mdatagen. DO NOT EDIT.
  2. package metadata
  3. import (
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "go.opentelemetry.io/collector/pdata/pcommon"
  7. "go.opentelemetry.io/collector/pdata/pmetric"
  8. "go.opentelemetry.io/collector/receiver/receivertest"
  9. "go.uber.org/zap"
  10. "go.uber.org/zap/zaptest/observer"
  11. )
  12. type testConfigCollection int
  13. const (
  14. testSetDefault testConfigCollection = iota
  15. testSetAll
  16. testSetNone
  17. )
  18. func TestMetricsBuilder(t *testing.T) {
  19. tests := []struct {
  20. name string
  21. configSet testConfigCollection
  22. }{
  23. {
  24. name: "default",
  25. configSet: testSetDefault,
  26. },
  27. {
  28. name: "all_set",
  29. configSet: testSetAll,
  30. },
  31. {
  32. name: "none_set",
  33. configSet: testSetNone,
  34. },
  35. }
  36. for _, test := range tests {
  37. t.Run(test.name, func(t *testing.T) {
  38. start := pcommon.Timestamp(1_000_000_000)
  39. ts := pcommon.Timestamp(1_000_001_000)
  40. observedZapCore, observedLogs := observer.New(zap.WarnLevel)
  41. settings := receivertest.NewNopCreateSettings()
  42. settings.Logger = zap.New(observedZapCore)
  43. mb := NewMetricsBuilder(loadMetricsBuilderConfig(t, test.name), settings, WithStartTime(start))
  44. expectedWarnings := 0
  45. assert.Equal(t, expectedWarnings, observedLogs.Len())
  46. defaultMetricsCount := 0
  47. allMetricsCount := 0
  48. defaultMetricsCount++
  49. allMetricsCount++
  50. mb.RecordRabbitmqConsumerCountDataPoint(ts, 1)
  51. defaultMetricsCount++
  52. allMetricsCount++
  53. mb.RecordRabbitmqMessageAcknowledgedDataPoint(ts, 1)
  54. defaultMetricsCount++
  55. allMetricsCount++
  56. mb.RecordRabbitmqMessageCurrentDataPoint(ts, 1, AttributeMessageStateReady)
  57. defaultMetricsCount++
  58. allMetricsCount++
  59. mb.RecordRabbitmqMessageDeliveredDataPoint(ts, 1)
  60. defaultMetricsCount++
  61. allMetricsCount++
  62. mb.RecordRabbitmqMessageDroppedDataPoint(ts, 1)
  63. defaultMetricsCount++
  64. allMetricsCount++
  65. mb.RecordRabbitmqMessagePublishedDataPoint(ts, 1)
  66. rb := mb.NewResourceBuilder()
  67. rb.SetRabbitmqNodeName("rabbitmq.node.name-val")
  68. rb.SetRabbitmqQueueName("rabbitmq.queue.name-val")
  69. rb.SetRabbitmqVhostName("rabbitmq.vhost.name-val")
  70. res := rb.Emit()
  71. metrics := mb.Emit(WithResource(res))
  72. if test.configSet == testSetNone {
  73. assert.Equal(t, 0, metrics.ResourceMetrics().Len())
  74. return
  75. }
  76. assert.Equal(t, 1, metrics.ResourceMetrics().Len())
  77. rm := metrics.ResourceMetrics().At(0)
  78. assert.Equal(t, res, rm.Resource())
  79. assert.Equal(t, 1, rm.ScopeMetrics().Len())
  80. ms := rm.ScopeMetrics().At(0).Metrics()
  81. if test.configSet == testSetDefault {
  82. assert.Equal(t, defaultMetricsCount, ms.Len())
  83. }
  84. if test.configSet == testSetAll {
  85. assert.Equal(t, allMetricsCount, ms.Len())
  86. }
  87. validatedMetrics := make(map[string]bool)
  88. for i := 0; i < ms.Len(); i++ {
  89. switch ms.At(i).Name() {
  90. case "rabbitmq.consumer.count":
  91. assert.False(t, validatedMetrics["rabbitmq.consumer.count"], "Found a duplicate in the metrics slice: rabbitmq.consumer.count")
  92. validatedMetrics["rabbitmq.consumer.count"] = true
  93. assert.Equal(t, pmetric.MetricTypeSum, ms.At(i).Type())
  94. assert.Equal(t, 1, ms.At(i).Sum().DataPoints().Len())
  95. assert.Equal(t, "The number of consumers currently reading from the queue.", ms.At(i).Description())
  96. assert.Equal(t, "{consumers}", ms.At(i).Unit())
  97. assert.Equal(t, false, ms.At(i).Sum().IsMonotonic())
  98. assert.Equal(t, pmetric.AggregationTemporalityCumulative, ms.At(i).Sum().AggregationTemporality())
  99. dp := ms.At(i).Sum().DataPoints().At(0)
  100. assert.Equal(t, start, dp.StartTimestamp())
  101. assert.Equal(t, ts, dp.Timestamp())
  102. assert.Equal(t, pmetric.NumberDataPointValueTypeInt, dp.ValueType())
  103. assert.Equal(t, int64(1), dp.IntValue())
  104. case "rabbitmq.message.acknowledged":
  105. assert.False(t, validatedMetrics["rabbitmq.message.acknowledged"], "Found a duplicate in the metrics slice: rabbitmq.message.acknowledged")
  106. validatedMetrics["rabbitmq.message.acknowledged"] = true
  107. assert.Equal(t, pmetric.MetricTypeSum, ms.At(i).Type())
  108. assert.Equal(t, 1, ms.At(i).Sum().DataPoints().Len())
  109. assert.Equal(t, "The number of messages acknowledged by consumers.", ms.At(i).Description())
  110. assert.Equal(t, "{messages}", ms.At(i).Unit())
  111. assert.Equal(t, true, ms.At(i).Sum().IsMonotonic())
  112. assert.Equal(t, pmetric.AggregationTemporalityCumulative, ms.At(i).Sum().AggregationTemporality())
  113. dp := ms.At(i).Sum().DataPoints().At(0)
  114. assert.Equal(t, start, dp.StartTimestamp())
  115. assert.Equal(t, ts, dp.Timestamp())
  116. assert.Equal(t, pmetric.NumberDataPointValueTypeInt, dp.ValueType())
  117. assert.Equal(t, int64(1), dp.IntValue())
  118. case "rabbitmq.message.current":
  119. assert.False(t, validatedMetrics["rabbitmq.message.current"], "Found a duplicate in the metrics slice: rabbitmq.message.current")
  120. validatedMetrics["rabbitmq.message.current"] = true
  121. assert.Equal(t, pmetric.MetricTypeSum, ms.At(i).Type())
  122. assert.Equal(t, 1, ms.At(i).Sum().DataPoints().Len())
  123. assert.Equal(t, "The total number of messages currently in the queue.", ms.At(i).Description())
  124. assert.Equal(t, "{messages}", ms.At(i).Unit())
  125. assert.Equal(t, false, ms.At(i).Sum().IsMonotonic())
  126. assert.Equal(t, pmetric.AggregationTemporalityCumulative, ms.At(i).Sum().AggregationTemporality())
  127. dp := ms.At(i).Sum().DataPoints().At(0)
  128. assert.Equal(t, start, dp.StartTimestamp())
  129. assert.Equal(t, ts, dp.Timestamp())
  130. assert.Equal(t, pmetric.NumberDataPointValueTypeInt, dp.ValueType())
  131. assert.Equal(t, int64(1), dp.IntValue())
  132. attrVal, ok := dp.Attributes().Get("state")
  133. assert.True(t, ok)
  134. assert.EqualValues(t, "ready", attrVal.Str())
  135. case "rabbitmq.message.delivered":
  136. assert.False(t, validatedMetrics["rabbitmq.message.delivered"], "Found a duplicate in the metrics slice: rabbitmq.message.delivered")
  137. validatedMetrics["rabbitmq.message.delivered"] = true
  138. assert.Equal(t, pmetric.MetricTypeSum, ms.At(i).Type())
  139. assert.Equal(t, 1, ms.At(i).Sum().DataPoints().Len())
  140. assert.Equal(t, "The number of messages delivered to consumers.", ms.At(i).Description())
  141. assert.Equal(t, "{messages}", ms.At(i).Unit())
  142. assert.Equal(t, true, ms.At(i).Sum().IsMonotonic())
  143. assert.Equal(t, pmetric.AggregationTemporalityCumulative, ms.At(i).Sum().AggregationTemporality())
  144. dp := ms.At(i).Sum().DataPoints().At(0)
  145. assert.Equal(t, start, dp.StartTimestamp())
  146. assert.Equal(t, ts, dp.Timestamp())
  147. assert.Equal(t, pmetric.NumberDataPointValueTypeInt, dp.ValueType())
  148. assert.Equal(t, int64(1), dp.IntValue())
  149. case "rabbitmq.message.dropped":
  150. assert.False(t, validatedMetrics["rabbitmq.message.dropped"], "Found a duplicate in the metrics slice: rabbitmq.message.dropped")
  151. validatedMetrics["rabbitmq.message.dropped"] = true
  152. assert.Equal(t, pmetric.MetricTypeSum, ms.At(i).Type())
  153. assert.Equal(t, 1, ms.At(i).Sum().DataPoints().Len())
  154. assert.Equal(t, "The number of messages dropped as unroutable.", ms.At(i).Description())
  155. assert.Equal(t, "{messages}", ms.At(i).Unit())
  156. assert.Equal(t, true, ms.At(i).Sum().IsMonotonic())
  157. assert.Equal(t, pmetric.AggregationTemporalityCumulative, ms.At(i).Sum().AggregationTemporality())
  158. dp := ms.At(i).Sum().DataPoints().At(0)
  159. assert.Equal(t, start, dp.StartTimestamp())
  160. assert.Equal(t, ts, dp.Timestamp())
  161. assert.Equal(t, pmetric.NumberDataPointValueTypeInt, dp.ValueType())
  162. assert.Equal(t, int64(1), dp.IntValue())
  163. case "rabbitmq.message.published":
  164. assert.False(t, validatedMetrics["rabbitmq.message.published"], "Found a duplicate in the metrics slice: rabbitmq.message.published")
  165. validatedMetrics["rabbitmq.message.published"] = true
  166. assert.Equal(t, pmetric.MetricTypeSum, ms.At(i).Type())
  167. assert.Equal(t, 1, ms.At(i).Sum().DataPoints().Len())
  168. assert.Equal(t, "The number of messages published to a queue.", ms.At(i).Description())
  169. assert.Equal(t, "{messages}", ms.At(i).Unit())
  170. assert.Equal(t, true, ms.At(i).Sum().IsMonotonic())
  171. assert.Equal(t, pmetric.AggregationTemporalityCumulative, ms.At(i).Sum().AggregationTemporality())
  172. dp := ms.At(i).Sum().DataPoints().At(0)
  173. assert.Equal(t, start, dp.StartTimestamp())
  174. assert.Equal(t, ts, dp.Timestamp())
  175. assert.Equal(t, pmetric.NumberDataPointValueTypeInt, dp.ValueType())
  176. assert.Equal(t, int64(1), dp.IntValue())
  177. }
  178. }
  179. })
  180. }
  181. }