logstream

package
v1.22.7 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2024 License: MIT Imports: 22 Imported by: 1

Documentation

Index

Constants

View Source
const NO_TEST = "no_test"

Variables

This section is empty.

Functions

func NoOpConsumerFn

func NoOpConsumerFn(consumer *ContainerLogConsumer) error

NoOpConsumerFn is a no-op consumer function

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 testcontainers.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) GetRunId

func (h *FileLogHandler) GetRunId() string

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) SetRunId

func (h *FileLogHandler) SetRunId(runId string)

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
	SetRunId(string)
	GetRunId() string
	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) GetRunId

func (h *InMemoryLogHandler) GetRunId() string

func (InMemoryLogHandler) GetTarget

func (h InMemoryLogHandler) GetTarget() LogTarget

func (*InMemoryLogHandler) Handle

func (*InMemoryLogHandler) Init

func (*InMemoryLogHandler) SetRunId

func (h *InMemoryLogHandler) SetRunId(runId string)

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 LogProducingContainer

type LogProducingContainer interface {
	Name(ctx context.Context) (string, error)
	FollowOutput(consumer testcontainers.LogConsumer)
	StartLogProducer(ctx context.Context, timeout time.Duration) error
	StopLogProducer() error
	GetLogProducerErrorChannel() <-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, patterns map[string][]*regexp.Regexp, 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) 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) GetRunId

func (h *LokiLogHandler) GetRunId() string

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) SetRunId

func (h *LokiLogHandler) SetRunId(runId string)

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

func WithLogProducerRetryLimit

func WithLogProducerRetryLimit(retryLimit int) Option

WithLogProducerRetryLimit allows to override default log producer retry limit of 10

func WithLogProducerTimeout

func WithLogProducerTimeout(timeout time.Duration) Option

WithLogProducerTimeout allows to override default log producer timeout of 5 seconds

func WithLogTarget

func WithLogTarget(logTarget LogTarget) Option

WithLogTarget allows setting log targets programmatically (also overrides LOGSTREAM_LOG_TARGETS env var)

Jump to

Keyboard shortcuts

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