batch

package
v1.15.0 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package batch sends items to an exporter in batches, from a goroutine, with a bounded queue and retries: what logging's sinks and tracing's exporters share.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsPermanent

func IsPermanent(err error) bool

IsPermanent reports whether err was marked with Permanent.

func Permanent

func Permanent(err error) error

Permanent marks an export error as one retrying won't fix.

func RetryAfter

func RetryAfter(err error, d time.Duration) error

RetryAfter marks an export error as one to retry after d.

Types

type Config

type Config struct {
	// Name is the exporter's, for error lines: "otlp".
	Name string
	// Prefix starts each error line: "logging", "tracing".
	Prefix string
	// What the items are, plural: "entries", "spans".
	What string
	// ErrOut gets a line when exporting fails, at most once a minute;
	// nil for none.
	ErrOut io.Writer
	// Close is called by Close after the last batch, if set.
	Close func() error
}

Config names a shipper and says where its errors go.

type Options

type Options struct {
	Size       int           // default 512
	Interval   time.Duration // default 1s
	Queue      int           // default 16384
	MaxRetries int           // default 5; -1 for none
	// Timeout bounds each export. Default 15s.
	Timeout time.Duration
}

Options says how items are sent. Items wait in a queue; a batch goes out when Size have gathered or Interval has passed. A batch that fails is retried MaxRetries times, pausing 1s, 2s, 4s… up to 30s between tries; meanwhile new items queue. When the queue holds Queue items, new ones are dropped and counted.

func (Options) WithDefaults

func (b Options) WithDefaults() Options

WithDefaults fills in the zero fields.

type Shipper

type Shipper[T any] struct {

	// Sleep pauses between tries; tests replace it.
	Sleep func(ctx context.Context, d time.Duration) bool
	// contains filtered or unexported fields
}

Shipper queues items and exports them from a goroutine. Make one with New.

func New

func New[T any](c Config, export func(context.Context, []T) error, b Options) *Shipper[T]

New starts a shipper that exports with export.

func (*Shipper[T]) Add

func (sh *Shipper[T]) Add(item T)

Add queues an item.

func (*Shipper[T]) Close

func (sh *Shipper[T]) Close(ctx context.Context) error

Close sends what is queued, stops the goroutine and calls Config.Close.

func (*Shipper[T]) Dropped

func (sh *Shipper[T]) Dropped() uint64

Dropped counts items left out because the queue was full, or added after Close.

func (*Shipper[T]) Failed

func (sh *Shipper[T]) Failed() uint64

Failed counts items the exporter didn't take after retrying.

func (*Shipper[T]) Flush

func (sh *Shipper[T]) Flush(ctx context.Context) error

Flush waits until what was queued before it has been sent, or ctx ends.

Jump to

Keyboard shortcuts

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