# README
golden
The package golden provides utilities for reading and writing files with metrics, traces and logs in JSON 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
or golden.WriteLogs
.
When writing a new test:
- Write the test as if the expected file exists.
- Follow the steps below for updating an existing test.
When updating an existing test:
- Add a call to
golden.WriteMetrics
orgolden.WriteLogs
or in the appropriate place. - Run the test once.
- Remove the call to
golden.WriteMetrics
orgolden.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, pmetrictest.CompareMetrics(expectedMetrics, actualMetrics))
}
# Functions
ReadLogs reads a plog.Logs from the specified file.
ReadMetrics reads a pmetric.Metrics from the specified file.
ReadTraces reads a ptrace.Traces from the specified file.
WriteLogs writes a plog.Logs to the specified file.
WriteMetrics writes a pmetric.Metrics to the specified file.
No description provided by the author