Categorygithub.com/open-telemetry/opentelemetry-collector-contrib/internal/comparetest

# README

comparetest

This module provides a mechanism for capturing and comparing expected metric and log results.

Typical Usage

A scraper test typically looks something like this:

func TestScraper(t *testing.T) {
  cfg := createDefaultConfig().(*Config)
  require.NoError(t, component.ValidateConfig(cfg))

  scraper := newScraper(componenttest.NewNopReceiverCreateSettings(), cfg)

  err := scraper.start(context.Background(), componenttest.NewNopHost())
  require.NoError(t, err)

  actualMetrics, err := scraper.scrape(context.Background())
  require.NoError(t, err)

  expectedFile := filepath.Join("testdata", "scraper", "expected.json")
  expectedMetrics, err := golden.ReadMetrics(expectedFile)
  require.NoError(t, err)

  require.NoError(t, comparetest.CompareMetrics(expectedMetrics, actualMetrics))
}
func TestLogsSink(t *testing.T) {
  cfg := createDefaultConfig().(*Config)
  require.NoError(t, component.ValidateConfig(cfg))

  sink := &consumertest.LogsSink{}
  alertRcvr := newLogsReceiver(cfg, zap.NewNop(), sink)
  alertRcvr.client = defaultMockClient()

  err := alertRcvr.Start(context.Background(), componenttest.NewNopHost())
  require.NoError(t, err)

  require.Eventually(t, func() bool {
    return sink.LogRecordCount() > 0
  }, 2*time.Second, 10*time.Millisecond)

  err = alertRcvr.Shutdown(context.Background())
  require.NoError(t, err)

  logs := sink.AllLogs()[0]
  expected, err := readLogs(filepath.Join("testdata", "logs", "expected.json"))
  require.NoError(t, err)
  require.NoError(t, comparetest.CompareLogs(expected, logs))
}

Generating an expected result file

The easiest way to capture the expected result in a file is golden.WriteMetrics or golden.WriteLogs.

When writing a new test:

  1. Write the test as if the expected file exists.
  2. Follow the steps below for updating an existing test.

When updating an existing test:

  1. Add a call to golden.WriteMetrics or golden.WriteLogs or in the appropriate place.
  2. Run the test once.
  3. Remove the call to golden.WriteMetrics or golden.WriteLogs.

NOTE: golden.WriteMetrics will always mark the test as failed. This behavior is necessary to ensure the function is removed after the golden file is written.

func TestScraper(t *testing.T) {
  cfg := createDefaultConfig().(*Config)
  require.NoError(t, component.ValidateConfig(cfg))

  scraper := newScraper(componenttest.NewNopReceiverCreateSettings(), cfg)

  err := scraper.start(context.Background(), componenttest.NewNopHost())
  require.NoError(t, err)

  actualMetrics, err := scraper.scrape(context.Background())
  require.NoError(t, err)

  expectedFile := filepath.Join("testdata", "scraper", "expected.json")

  golden.WriteMetrics(t, expectedFile, actualMetrics) // This line is temporary! TODO remove this!!

  expectedMetrics, err := golden.ReadMetrics(expectedFile)
  require.NoError(t, err)

  require.NoError(t, comparetest.CompareMetrics(expectedMetrics, actualMetrics))
}

# Packages

No description provided by the author

# Functions

CompareLogRecords compares each part of two given LogRecord and returns an error if they don't match.
CompareLogRecordSlices compares each part of two given LogRecordSlices and returns an error if they don't match.
CompareLogs compares each part of two given Logs and returns an error if they don't match.
No description provided by the author
CompareMetricSlices compares each part of two given MetricSlices and returns an error if they don't match.
CompareNumberDataPoints compares each part of two given NumberDataPoints and returns an error if they don't match.
CompareNumberDataPointSlices compares each part of two given NumberDataPointSlices and returns an error if they don't match.
CompareResourceLogs compares each part of two given ResourceLogs and returns an error if they don't match.
No description provided by the author
IgnoreMetricAttributeValue is a MetricsCompareOption that clears value of the metric attribute.
IgnoreMetricValues is a MetricsCompareOption that clears all metric values.
No description provided by the author
IgnoreResourceAttributeValue is a CompareOption that removes a resource attribute from all resources.
IgnoreSubsequentDataPoints is a MetricsCompareOption that ignores data points after the first.

# Interfaces

No description provided by the author
LogsCompareOption can be used to mutate expected and/or actual logs before comparing.
MetricsCompareOption can be used to mutate expected and/or actual metrics before comparing.