logstream

package
v0.0.0-...-171e6b0 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2024 License: MIT Imports: 27 Imported by: 1

Documentation

Index

Constants

View Source
const NO_TEST = "no_test"
View Source
const (
	ShorteningFailedErr = "failed to shorten Grafana URL"
)

Variables

This section is empty.

Functions

func GetRegexMatchingProcessor

func GetRegexMatchingProcessor(logStream *LogStream, pattern string) (*LogProcessor[int], LogProcessorFn[int], error)

GetRegexMatchingProcessor creates a LogProcessor that counts the number of logs matching a regex pattern. Function returns the LogProcessor, the processing function, and an error if the regex pattern is invalid.

func NoOpConsumerFn

func NoOpConsumerFn(consumer *ContainerLogConsumer) error

NoOpConsumerFn is a no-op consumer function

func ShortenUrl

func ShortenUrl(grafanaUrl, urlToShorten, bearerToken string) (string, error)

Types

type ConsumerConsumingFn

type ConsumerConsumingFn = func(consumer *ContainerLogConsumer) error

type ConsumerLogConsumingFn

type ConsumerLogConsumingFn = func(consumer *ContainerLogConsumer, log LogContent) error

type ContainerLogConsumer

type ContainerLogConsumer struct {
	// contains filtered or unexported fields
}

ContainerLogConsumer is a container log lines consumer

func (*ContainerLogConsumer) Accept

func (g *ContainerLogConsumer) Accept(l tc.Log)

Accept accepts the log message from particular container and saves it to the temp gob file

func (*ContainerLogConsumer) GetContainer

func (g *ContainerLogConsumer) GetContainer() LogProducingContainer

GetContainer returns the container that this consumer is connected to

func (*ContainerLogConsumer) GetStartTime

func (g *ContainerLogConsumer) GetStartTime() time.Time

GetStartTime returns the time of the first log line

func (*ContainerLogConsumer) MarkAsErrored

func (g *ContainerLogConsumer) MarkAsErrored()

MarkAsErrored marks the consumer as errored (which makes it stop accepting logs)

func (*ContainerLogConsumer) ResetTempFile

func (g *ContainerLogConsumer) ResetTempFile() error

ResetTempFile resets the temp file and gob encoder

type FileLogHandler

type FileLogHandler struct {
	// contains filtered or unexported fields
}

FileLogHandler saves logs to local files

func (FileLogHandler) GetLogLocation

func (h FileLogHandler) GetLogLocation(_ map[string]*ContainerLogConsumer) (string, error)

func (FileLogHandler) GetTarget

func (h FileLogHandler) GetTarget() LogTarget

func (*FileLogHandler) Handle

func (h *FileLogHandler) Handle(c *ContainerLogConsumer, content LogContent) error

func (*FileLogHandler) Init

func (*FileLogHandler) Teardown

func (h *FileLogHandler) Teardown() error

type HandleLogTarget

type HandleLogTarget interface {
	Handle(*ContainerLogConsumer, LogContent) error
	GetLogLocation(map[string]*ContainerLogConsumer) (string, error)
	GetTarget() LogTarget
	Init(*ContainerLogConsumer) error
	Teardown() error
}

type InMemoryLogHandler

type InMemoryLogHandler struct {
	// contains filtered or unexported fields
}

InMemoryLogHandler stores logs in memory

func (InMemoryLogHandler) GetLogLocation

func (h InMemoryLogHandler) GetLogLocation(_ map[string]*ContainerLogConsumer) (string, error)

func (InMemoryLogHandler) GetTarget

func (h InMemoryLogHandler) GetTarget() LogTarget

func (*InMemoryLogHandler) Handle

func (*InMemoryLogHandler) Init

func (*InMemoryLogHandler) Teardown

func (h *InMemoryLogHandler) Teardown() error

type LogContent

type LogContent struct {
	TestName      string
	ContainerName string
	Content       []byte
	Time          time.Time
}

LogContent is a representation of log that will be send to Loki

type LogNotification

type LogNotification struct {
	Container string
	Prefix    string
	Log       string
}

LogNotification notification about log line match for some container

type LogProcessor

type LogProcessor[ReturnType any] struct {
	// contains filtered or unexported fields
}

func NewLogProcessor

func NewLogProcessor[ReturnType any](logStream *LogStream) *LogProcessor[ReturnType]

NewLogProcessor creates a new LogProcessor instance

func (*LogProcessor[ReturnType]) ProcessContainerLogs

func (l *LogProcessor[ReturnType]) ProcessContainerLogs(containerName string, processFn func(content LogContent, returnValue *ReturnType) error) (*ReturnType, error)

ProcessContainerLogs reads the logs of a container and processes them with the provided function

type LogProcessorFn

type LogProcessorFn[ReturnType any] func(content LogContent, returnValue *ReturnType) error

type LogProducingContainer

type LogProducingContainer interface {
	Name(ctx context.Context) (string, error)
	FollowOutput(consumer tc.LogConsumer)
	StartLogProducer(ctx context.Context, opts ...tc.LogProductionOption) error
	StopLogProducer() error
	GetLogProductionErrorChannel() <-chan error
	IsRunning() bool
	GetContainerID() string
	Terminate(context.Context) error
}

LogProducingContainer is a facade that needs to be implemented by any container that wants to be connected to LogStream

type LogStream

type LogStream struct {
	// contains filtered or unexported fields
}

LogStream is a test helper struct to monitor docker container logs for some patterns and push their logs into Loki for further analysis

func NewLogStream

func NewLogStream(t *testing.T, loggingConfig *config.LoggingConfig, options ...Option) (*LogStream, error)

NewLogStream creates a new LogStream instance, with Loki client only if Loki log target is enabled (lazy init)

func (*LogStream) ConnectContainer

func (m *LogStream) ConnectContainer(ctx context.Context, container LogProducingContainer, prefix string) error

ConnectContainer connects consumer to selected container, starts testcontainers.LogProducer and listens to it's failures in a detached goroutine

func (*LogStream) ContainerLogs

func (m *LogStream) ContainerLogs(name string) ([]string, error)

ContainerLogs return all logs for particular container

func (*LogStream) DisconnectContainer

func (m *LogStream) DisconnectContainer(container LogProducingContainer) error

DisconnectContainer disconnects particular container

func (*LogStream) FlushAndShutdown

func (m *LogStream) FlushAndShutdown() error

FlushAndShutdown flushes all logs to their targets and shuts down the log stream in a default sequence

func (*LogStream) FlushLogsToTargets

func (m *LogStream) FlushLogsToTargets() error

FlushLogsToTargets flushes all logs for all consumers (containers) to their targets

func (*LogStream) GetAllLogsAndConsume

func (m *LogStream) GetAllLogsAndConsume(preExecuteFn ConsumerConsumingFn, consumeLogFn ConsumerLogConsumingFn, postExecuteFn ConsumerConsumingFn) (loopErr error)

GetAllLogsAndConsume gets all logs for all consumers (containers) and consumes them using consumeLogFn

func (*LogStream) GetConsumers

func (m *LogStream) GetConsumers() map[string]*ContainerLogConsumer

GetConsumers returns all consumers

func (*LogStream) GetLogLocation

func (m *LogStream) GetLogLocation() string

SaveLogTargetsLocations saves all log targets locations to test summary

func (*LogStream) PrintLogTargetsLocations

func (m *LogStream) PrintLogTargetsLocations()

PrintLogTargetsLocations prints all log targets locations to stdout

func (*LogStream) SaveLogLocationInTestSummary

func (m *LogStream) SaveLogLocationInTestSummary()

SaveLogTargetsLocations saves all log targets locations to test summary

func (*LogStream) SaveLogTargetsLocations

func (m *LogStream) SaveLogTargetsLocations(writer LogWriter)

SaveLogTargetsLocations saves all log targets given writer

func (*LogStream) Shutdown

func (m *LogStream) Shutdown(context context.Context) error

Shutdown disconnects all containers and stops all consumers

type LogTarget

type LogTarget string
const (
	Loki     LogTarget = "loki"
	File     LogTarget = "file"
	InMemory LogTarget = "in-memory"
)

type LogWriter

type LogWriter = func(testName string, name string, location interface{}) error

type LokiLogHandler

type LokiLogHandler struct {
	// contains filtered or unexported fields
}

LokiLogHandler sends logs to Loki

func (*LokiLogHandler) GetLogLocation

func (h *LokiLogHandler) GetLogLocation(consumers map[string]*ContainerLogConsumer) (string, error)

func (LokiLogHandler) GetTarget

func (h LokiLogHandler) GetTarget() LogTarget

func (*LokiLogHandler) Handle

func (h *LokiLogHandler) Handle(c *ContainerLogConsumer, content LogContent) error

func (*LokiLogHandler) Init

func (*LokiLogHandler) Teardown

func (h *LokiLogHandler) Teardown() error

type Option

type Option func(*LogStream)

func WithCustomLogHandler

func WithCustomLogHandler(logTarget LogTarget, handler HandleLogTarget) Option

WithCustomLogHandler allows to override default log handler for particular log target

Jump to

Keyboard shortcuts

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