scrapertest

package module
v0.66.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 23, 2022 License: Apache-2.0 Imports: 5 Imported by: 0

README

scrapertest

This module provides a mechanism for capturing and comparing expected metric 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, scrapertest.CompareMetrics(expectedMetrics, actualMetrics))
}

Generating an expected result file

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

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 in the appropriate place.
  2. Run the test once.
  3. Remove the call to golden.WriteMetrics.
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(expectedFile, actualMetrics)   // This line is temporary! TODO remove this!!

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CompareMetricSlices

func CompareMetricSlices(expected, actual pmetric.MetricSlice) error

CompareMetricSlices compares each part of two given MetricSlices and returns an error if they don't match. The error describes what didn't match. The expected and actual values are clones before options are applied.

func CompareMetrics added in v0.44.0

func CompareMetrics(expected, actual pmetric.Metrics, options ...CompareOption) error

func CompareNumberDataPointSlices

func CompareNumberDataPointSlices(expected, actual pmetric.NumberDataPointSlice) error

CompareNumberDataPointSlices compares each part of two given NumberDataPointSlices and returns an error if they don't match. The error describes what didn't match.

func CompareNumberDataPoints

func CompareNumberDataPoints(expected, actual pmetric.NumberDataPoint) error

CompareNumberDataPoints compares each part of two given NumberDataPoints and returns an error if they don't match. The error describes what didn't match.

func CompareResourceMetrics added in v0.44.0

func CompareResourceMetrics(expected, actual pmetric.ResourceMetrics) error

Types

type CompareOption added in v0.41.0

type CompareOption interface {
	// contains filtered or unexported methods
}

CompareOption is applied by the CompareMetricSlices function to mutates an expected and/or actual result before comparing.

func IgnoreMetricAttributeValue added in v0.44.0

func IgnoreMetricAttributeValue(attributeName string, metricNames ...string) CompareOption

IgnoreMetricAttributeValue is a CompareOption that clears all values

func IgnoreMetricValues added in v0.44.0

func IgnoreMetricValues(metricNames ...string) CompareOption

IgnoreMetricValues is a CompareOption that clears all values

func IgnoreResourceAttributeValue added in v0.54.0

func IgnoreResourceAttributeValue(attributeName string) CompareOption

IgnoreResourceAttributeValue is a CompareOption that removes a resource attribute from all resources

func IgnoreSubsequentDataPoints added in v0.64.0

func IgnoreSubsequentDataPoints(metricNames ...string) CompareOption

IgnoreSubsequentDataPoints is a CompareOption that ignores data points after the first

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL