metrics_correctness_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package metrics
  4. import (
  5. "fmt"
  6. "log"
  7. "testing"
  8. "github.com/stretchr/testify/require"
  9. "go.opentelemetry.io/collector/pdata/pmetric"
  10. "github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/goldendataset"
  11. "github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/metricstestutil"
  12. "github.com/open-telemetry/opentelemetry-collector-contrib/testbed/correctnesstests"
  13. "github.com/open-telemetry/opentelemetry-collector-contrib/testbed/testbed"
  14. )
  15. // tests with the prefix "TestHarness_" get run in the "correctnesstests-metrics" ci job
  16. func TestHarness_MetricsGoldenData(t *testing.T) {
  17. tests, err := correctnesstests.LoadPictOutputPipelineDefs(
  18. "testdata/generated_pict_pairs_metrics_pipeline.txt",
  19. )
  20. require.NoError(t, err)
  21. res := results{}
  22. res.Init("results")
  23. for _, test := range tests {
  24. test.TestName = fmt.Sprintf("%s-%s", test.Receiver, test.Exporter)
  25. test.DataSender = correctnesstests.ConstructMetricsSender(t, test.Receiver)
  26. test.DataReceiver = correctnesstests.ConstructReceiver(t, test.Exporter)
  27. t.Run(test.TestName, func(t *testing.T) {
  28. r := testWithMetricsGoldenDataset(
  29. t,
  30. test.DataSender.(testbed.MetricDataSender),
  31. test.DataReceiver,
  32. )
  33. res.Add("", r)
  34. })
  35. }
  36. res.Save()
  37. }
  38. func testWithMetricsGoldenDataset(
  39. t *testing.T,
  40. sender testbed.MetricDataSender,
  41. receiver testbed.DataReceiver,
  42. ) result {
  43. mds := getTestMetrics(t)
  44. accumulator := newDiffAccumulator()
  45. h := newTestHarness(
  46. t,
  47. newMetricSupplier(mds),
  48. newMetricsReceivedIndex(mds),
  49. sender,
  50. accumulator,
  51. )
  52. tc := newCorrectnessTestCase(t, sender, receiver, h)
  53. tc.startTestbedReceiver()
  54. tc.startCollector()
  55. tc.startTestbedSender()
  56. tc.sendFirstMetric()
  57. tc.waitForAllMetrics()
  58. tc.stopTestbedReceiver()
  59. tc.stopCollector()
  60. r := result{
  61. testName: t.Name(),
  62. testResult: "PASS",
  63. numDiffs: accumulator.numDiffs,
  64. }
  65. if accumulator.numDiffs > 0 {
  66. r.testResult = "FAIL"
  67. t.Fail()
  68. }
  69. return r
  70. }
  71. func getTestMetrics(t *testing.T) []pmetric.Metrics {
  72. const file = "../../../internal/coreinternal/goldendataset/testdata/generated_pict_pairs_metrics.txt"
  73. mds, err := goldendataset.GenerateMetrics(file)
  74. require.NoError(t, err)
  75. return mds
  76. }
  77. type diffAccumulator struct {
  78. numDiffs int
  79. }
  80. var _ diffConsumer = (*diffAccumulator)(nil)
  81. func newDiffAccumulator() *diffAccumulator {
  82. return &diffAccumulator{}
  83. }
  84. func (d *diffAccumulator) accept(metricName string, diffs []*metricstestutil.MetricDiff) {
  85. if len(diffs) > 0 {
  86. d.numDiffs++
  87. log.Printf("Found diffs for [%v]\n%v", metricName, diffs)
  88. }
  89. }