Socket
Socket
Sign inDemoInstall

github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest


Version published
Created
Source

pdatatest

This module provides a test helpers for comparing metric, log and traces. The main functions are:

  • pmetrictest.CompareMetrics
  • plogtest.CompareLogs
  • ptracetest.CompareTraces

These functions compare the actual result with the expected result and return an error if they are not equal. The error contains a detailed description of the differences. The module also provides several options to customize the comparison by ignoring certain fields, attributes, or slices order. The module also provides helper functions for comparing other embedded pdata types such as pmetric.ResourceMetrics, pmetric.ScopeMetrics, plog.LogRecord, ptrace.Span, etc.

Typical Usage

func TestMetricsScraper(t *testing.T) {
	scraper := newScraper(componenttest.NewNopReceiverCreateSettings(), createDefaultConfig().(*Config))
	require.NoError(t, scraper.start(context.Background(), componenttest.NewNopHost()))
	actualMetrics, err := require.NoError(t, scraper.scrape(context.Background()))
	require.NoError(t, err)

	expectedFile, err := readMetrics(filepath.Join("testdata", "expected.json"))
	require.NoError(err)

	require.NoError(t, pmetrictest.CompareMetrics(expectedMetrics, actualMetrics, pmetrictest.IgnoreStartTimestamp(), 
		pmetrictest.IgnoreTimestamp()))
}
func TestLogsReceiver(t *testing.T) {
	sink := &consumertest.LogsSink{}
	rcvr := newLogsReceiver(createDefaultConfig().(*Config), zap.NewNop(), sink)
	rcvr.client = defaultMockClient()
	require.NoError(t,  rcvr.Start(context.Background(), componenttest.NewNopHost()))
	require.Eventually(t, func() bool {
		return sink.LogRecordCount() > 0
	}, 2*time.Second, 10*time.Millisecond)
	err = rcvr.Shutdown(context.Background())
	require.NoError(t, err)
	actualLogs := sink.AllLogs()[0]

	expectedLogs, err := readLogs(filepath.Join("testdata", "logs", "expected.json"))
	require.NoError(t, err)

	require.NoError(t, pmetrictest.CompareLogs(expectedLogs, actualLogs))
}
func TestTraceProcessor(t *testing.T) {
	nextTrace := new(consumertest.TracesSink)
	tp, err := newTracesProcessor(NewFactory().CreateDefaultConfig(), nextTrace)
	traces := generateTraces()
	tp.ConsumeTraces(ctx, traces)
	actualTraces := nextTrace.AllTraces()[0]

	expectedTraces, err := readTraces(filepath.Join("testdata", "traces", "expected.json"))
	require.NoError(t, err)
	
	require.NoError(t, ptracetest.CompareTraces(expectedTraces, actualTraces, ptracetest.IgnoreStartTimestamp(), 
		ptracetest.IgnoreEndTimestamp()))
}

FAQs

Package last updated on 04 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc