golden

package module
v0.99.0 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2024 License: Apache-2.0 Imports: 14 Imported by: 2

README

golden

Status
Stability alpha: traces, metrics, logs
Issues Open issues Closed issues
Code Owners @djaglowski, @atoulme

The package golden provides utilities for reading and writing files with metrics, traces and logs in YAML format. The package is expected to be used with pkg/pdatatest module.

Generating an expected result file

The easiest way to capture the expected result in a file is golden.WriteMetrics, golden.WriteTraces 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, golden.WriteTraces or golden.WriteLogs or in the appropriate place.
  2. Run the test once.
  3. Remove the call to golden.WriteMetrics, golden.WriteTraces 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.yaml")

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

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MarshalMetricsYAML

func MarshalMetricsYAML(metrics pmetric.Metrics) ([]byte, error)

MarshalMetricsYAML marshals a pmetric.Metrics to YAML format.

func ReadLogs

func ReadLogs(filePath string) (plog.Logs, error)

ReadLogs reads a plog.Logs from the specified YAML or JSON file.

func ReadMetrics

func ReadMetrics(filePath string) (pmetric.Metrics, error)

ReadMetrics reads a pmetric.Metrics from the specified YAML or JSON file.

func ReadTraces

func ReadTraces(filePath string) (ptrace.Traces, error)

ReadTraces reads a ptrace.Traces from the specified YAML or JSON file.

func WriteLogs

func WriteLogs(t testing.TB, filePath string, logs plog.Logs) error

WriteLogs writes a plog.Logs to the specified file in YAML format.

func WriteMetrics

func WriteMetrics(t testing.TB, filePath string, metrics pmetric.Metrics, opts ...WriteMetricsOption) error

WriteMetrics writes a pmetric.Metrics to the specified file in YAML format.

func WriteTraces

func WriteTraces(t testing.TB, filePath string, traces ptrace.Traces) error

WriteTraces writes a ptrace.Traces to the specified file in YAML format.

Types

type WriteMetricsOption added in v0.95.0

type WriteMetricsOption func(*writeMetricsOptions)

WriteMetricsOption is an option for the WriteMetrics function

func SkipMetricTimestampNormalization added in v0.95.0

func SkipMetricTimestampNormalization() WriteMetricsOption

SkipMetricTimestampNormalization is an option that skips normalizing timestamps before writing metrics to disk.

Jump to

Keyboard shortcuts

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