metrics_test_harness.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package metrics // import "github.com/open-telemetry/opentelemetry-collector-contrib/testbed/correctnesstests/metrics"
  4. import (
  5. "context"
  6. "testing"
  7. "github.com/stretchr/testify/require"
  8. "go.opentelemetry.io/collector/consumer"
  9. "go.opentelemetry.io/collector/pdata/pmetric"
  10. "github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/metricstestutil"
  11. "github.com/open-telemetry/opentelemetry-collector-contrib/testbed/testbed"
  12. )
  13. // testHarness listens for datapoints from the receiver to which it is attached
  14. // and when it receives one, it compares it to the datapoint that was previously
  15. // sent out. It then sends the next datapoint, if there is one.
  16. type testHarness struct {
  17. t *testing.T
  18. metricSupplier *metricSupplier
  19. metricIndex *metricsReceivedIndex
  20. sender testbed.MetricDataSender
  21. currPDM pmetric.Metrics
  22. diffConsumer diffConsumer
  23. outOfMetrics bool
  24. allMetricsReceived chan struct{}
  25. }
  26. type diffConsumer interface {
  27. accept(string, []*metricstestutil.MetricDiff)
  28. }
  29. func newTestHarness(
  30. t *testing.T,
  31. s *metricSupplier,
  32. mi *metricsReceivedIndex,
  33. ds testbed.MetricDataSender,
  34. diffConsumer diffConsumer,
  35. ) *testHarness {
  36. return &testHarness{
  37. t: t,
  38. metricSupplier: s,
  39. metricIndex: mi,
  40. sender: ds,
  41. diffConsumer: diffConsumer,
  42. allMetricsReceived: make(chan struct{}),
  43. }
  44. }
  45. func (h *testHarness) Capabilities() consumer.Capabilities {
  46. return consumer.Capabilities{MutatesData: false}
  47. }
  48. func (h *testHarness) ConsumeMetrics(_ context.Context, pdm pmetric.Metrics) error {
  49. h.compare(pdm)
  50. if h.metricIndex.allReceived() {
  51. close(h.allMetricsReceived)
  52. }
  53. if !h.outOfMetrics {
  54. h.sendNextMetric()
  55. }
  56. return nil
  57. }
  58. func (h *testHarness) compare(pdm pmetric.Metrics) {
  59. pdms := pdm.ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics()
  60. var diffs []*metricstestutil.MetricDiff
  61. for i := 0; i < pdms.Len(); i++ {
  62. pdmRecd := pdms.At(i)
  63. metricName := pdmRecd.Name()
  64. metric, found := h.metricIndex.lookup(metricName)
  65. if !found {
  66. h.diffConsumer.accept(metricName, []*metricstestutil.MetricDiff{{
  67. ExpectedValue: metricName,
  68. Msg: "Metric name not found in index",
  69. }})
  70. }
  71. if !metric.received {
  72. metric.received = true
  73. sent := metric.pdm
  74. pdmExpected := sent.ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(0)
  75. diffs = metricstestutil.DiffMetric(
  76. diffs,
  77. pdmExpected,
  78. pdmRecd,
  79. )
  80. h.diffConsumer.accept(metricName, diffs)
  81. }
  82. }
  83. }
  84. func (h *testHarness) sendNextMetric() {
  85. h.currPDM, h.outOfMetrics = h.metricSupplier.nextMetrics()
  86. if h.outOfMetrics {
  87. return
  88. }
  89. err := h.sender.ConsumeMetrics(context.Background(), h.currPDM)
  90. require.NoError(h.t, err)
  91. }