testutil

package module
v0.63.3 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2025 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Overview

Package testutil includes OTLP-related test utilities.

Index

Constants

View Source
const (
	ValidateAPIKeyEndpoint = "/api/v1/validate" // nolint G101
	MetricV1Endpoint       = "/api/v1/series"
	MetricV2Endpoint       = "/api/v2/series"
	SketchesMetricEndpoint = "/api/beta/sketches"
	MetadataEndpoint       = "/intake"
	TraceEndpoint          = "/api/v0.2/traces"
	APMStatsEndpoint       = "/api/v0.2/stats"
)

#nosec G101 -- This is a false positive, these are API endpoints rather than credentials

Variables

View Source
var (
	// TestLogTime is the default time used for tests.
	TestLogTime = time.Date(2020, 2, 11, 20, 26, 13, 789, time.UTC)
	// TestLogTimestamp is the default timestamp used for tests.
	TestLogTimestamp = pcommon.NewTimestampFromTime(TestLogTime)
)
View Source
var (

	// TestMetrics metrics for tests.
	TestMetrics = newMetricsWithAttributeMap(testAttributes)
	// TestTraces traces for tests.
	TestTraces = newTracesWithAttributeMap(testAttributes)
)
View Source
var StatsPayloads = []*pb.ClientStatsPayload{
	{
		Hostname:         "host",
		Env:              "prod",
		Version:          "v1.2",
		Lang:             "go",
		TracerVersion:    "v44",
		RuntimeID:        "123jkl",
		Sequence:         2,
		AgentAggregation: "blah",
		Service:          "mysql",
		ContainerID:      "abcdef123456",
		Tags:             []string{"a:b", "c:d"},
		Stats: []*pb.ClientStatsBucket{
			{
				Start:    10,
				Duration: 1,
				Stats: []*pb.ClientGroupedStats{
					{
						Service:        "kafka",
						Name:           "queue.add",
						Resource:       "append",
						HTTPStatusCode: 220,
						Type:           "queue",
						Hits:           15,
						Errors:         3,
						Duration:       143,
						OkSummary:      testSketchBytes(1, 2, 3),
						ErrorSummary:   testSketchBytes(4, 5, 6),
						TopLevelHits:   5,
					},
				},
			},
		},
	},
	{
		Hostname:         "host2",
		Env:              "prod2",
		Version:          "v1.22",
		Lang:             "go2",
		TracerVersion:    "v442",
		RuntimeID:        "123jkl2",
		Sequence:         22,
		AgentAggregation: "blah2",
		Service:          "mysql2",
		ContainerID:      "abcdef1234562",
		Tags:             []string{"a:b2", "c:d2"},
		Stats: []*pb.ClientStatsBucket{
			{
				Start:    102,
				Duration: 12,
				Stats: []*pb.ClientGroupedStats{
					{
						Service:        "kafka2",
						Name:           "queue.add2",
						Resource:       "append2",
						HTTPStatusCode: 2202,
						Type:           "queue2",
						Hits:           152,
						Errors:         32,
						Duration:       1432,
						OkSummary:      testSketchBytes(7, 8),
						ErrorSummary:   testSketchBytes(9, 10, 11),
						TopLevelHits:   52,
					},
				},
			},
		},
	},
}

StatsPayloads contains a couple of *pb.ClientStatsPayloads used for testing.

Functions

func GenerateLogsNoLogRecords

func GenerateLogsNoLogRecords() plog.Logs

GenerateLogsNoLogRecords generates a logs structure with one entry.

func GenerateLogsOneEmptyLogRecord

func GenerateLogsOneEmptyLogRecord() plog.Logs

GenerateLogsOneEmptyLogRecord generates a log structure with one empty record.

func GenerateLogsOneEmptyResourceLogs

func GenerateLogsOneEmptyResourceLogs() plog.Logs

GenerateLogsOneEmptyResourceLogs generates one empty logs structure.

func GenerateLogsOneLogRecord

func GenerateLogsOneLogRecord() plog.Logs

GenerateLogsOneLogRecord generates a logs structure with one record.

func GenerateLogsOneLogRecordNoResource

func GenerateLogsOneLogRecordNoResource() plog.Logs

GenerateLogsOneLogRecordNoResource generates a logs structure with one record and no resource.

func GenerateLogsTwoLogRecordsSameResource

func GenerateLogsTwoLogRecordsSameResource() plog.Logs

GenerateLogsTwoLogRecordsSameResource generates a logs structure with two log records sharding the same resource.

func LoadConfig

func LoadConfig(t *testing.T, path string) (pkgConfigModel.Reader, error)

LoadConfig from a given path.

func NewAttributeMap

func NewAttributeMap(mp map[string]string) pcommon.Map

NewAttributeMap creates a new attribute map (string only) from a Go map

func NewGaugeMetrics

func NewGaugeMetrics(tgs []TestGauge) pmetric.Metrics

NewGaugeMetrics creates a set of pmetric.Metrics containing all the specified test gauges.

func OTLPConfigFromPorts

func OTLPConfigFromPorts(bindHost string, gRPCPort uint, httpPort uint) map[string]interface{}

OTLPConfigFromPorts creates a test OTLP config map.

func ValidateAPIKeyEndpointInvalid

func ValidateAPIKeyEndpointInvalid() (string, http.HandlerFunc)

ValidateAPIKeyEndpointInvalid returns a handler function that returns an invalid API key response

Types

type DataPoint

type DataPoint struct {
	Value      float64
	Attributes map[string]string
}

DataPoint specifies a DoubleVal data point and its attributes.

type DatadogLogsServer

type DatadogLogsServer struct {
	*httptest.Server
	// LogsData is the array of json requests sent to datadog backend
	LogsData JSONLogs
}

DatadogLogsServer implements a HTTP server that accepts Datadog logs

func DatadogLogServerMock

func DatadogLogServerMock(overwriteHandlerFuncs ...OverwriteHandleFunc) *DatadogLogsServer

DatadogLogServerMock mocks a Datadog Logs Intake backend server

type DatadogServer

type DatadogServer struct {
	*httptest.Server
	MetadataChan chan payload.HostMetadata
}

DatadogServer is the mock Datadog backend server

func DatadogServerMock

func DatadogServerMock(overwriteHandlerFuncs ...OverwriteHandleFunc) *DatadogServer

DatadogServerMock mocks a Datadog backend server

type HTTPRequestRecorder

type HTTPRequestRecorder struct {
	Pattern  string
	Header   http.Header
	ByteBody []byte
}

HTTPRequestRecorder records a HTTP request.

func (*HTTPRequestRecorder) HandlerFunc

func (rec *HTTPRequestRecorder) HandlerFunc() (string, http.HandlerFunc)

HandlerFunc implements an HTTP handler

type HTTPRequestRecorderWithChan

type HTTPRequestRecorderWithChan struct {
	Pattern string
	ReqChan chan []byte
}

HTTPRequestRecorderWithChan puts all incoming HTTP request bytes to the given channel.

func (*HTTPRequestRecorderWithChan) HandlerFunc

func (rec *HTTPRequestRecorderWithChan) HandlerFunc() (string, http.HandlerFunc)

HandlerFunc implements an HTTP handler

type JSONLog

type JSONLog map[string]any

JSONLog is the type for the processed JSON log data from a single log

type JSONLogs

type JSONLogs []map[string]any

JSONLogs is the type for the array of processed JSON log data from each request

func MockLogsEndpoint

func MockLogsEndpoint(w http.ResponseWriter, r *http.Request) JSONLogs

MockLogsEndpoint returns the processed JSON log data for each endpoint call

func ProcessLogsAgentRequest

func ProcessLogsAgentRequest(w http.ResponseWriter, r *http.Request) JSONLogs

ProcessLogsAgentRequest handles HTTP requests from logs agent

func (*JSONLogs) HasDDTag

func (jsonLogs *JSONLogs) HasDDTag(ddtags string) bool

HasDDTag returns true if every log has the given ddtags

type MockSourceProvider

type MockSourceProvider struct {
	Src source.Source
}

MockSourceProvider mocks a source provider

func (*MockSourceProvider) Source

Source implements the source provider interface

type MockStatsProcessor

type MockStatsProcessor struct {
	In []*pb.ClientStatsPayload
}

MockStatsProcessor mocks a stats processor

func (*MockStatsProcessor) ProcessStats

func (s *MockStatsProcessor) ProcessStats(in *pb.ClientStatsPayload, _, _ string)

ProcessStats implements the stats processor interface

type OverwriteHandleFunc

type OverwriteHandleFunc func() (string, http.HandlerFunc)

OverwriteHandleFunc allows to overwrite the default handler functions

type TestGauge

type TestGauge struct {
	Name       string
	DataPoints []DataPoint
}

TestGauge holds the definition of a basic gauge.

Jump to

Keyboard shortcuts

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