task

package
v9.75.1718400648-SHA-c... Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2024 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package task provides some useful helpers to express common tasks like retries and adding contextual information to a context-scoped logger.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractTracer

func ExtractTracer(ctx context.Context) (oteltrace.Tracer, bool)

ExtractTracer extracts the otel tracer from the provided context. Use `InjectTracer` to prepare a context for use with this function.

func ExtractTracerProvider

func ExtractTracerProvider(ctx context.Context) (oteltrace.TracerProvider, bool)

ExtractTracerProvider extracts the otel tracer provider from the provided context. Use `InjectTracerProvider` to prepare a context for use with this function.

func Float64

func Float64() float64

Float64 generates a float64 between [0, 1). It uses the technique described in the comments on math/rand's Float64 implementation: https://cs.opensource.google/go/go/+/refs/tags/go1.22.2:src/math/rand/rand.go;l=190;drc=c29444ef39a44ad56ddf7b3d2aa8a51df1163e04

func InjectTracer

func InjectTracer(ctx context.Context, t oteltrace.Tracer) context.Context

InjectTracer returns a new context with the provided Tracer injected. Use `ExtractTracer` to retrieve it.

func InjectTracerProvider

func InjectTracerProvider(ctx context.Context, tp oteltrace.TracerProvider) context.Context

InjectTracerProvider returns a new context with the provided TracerProvider injected. Use `ExtractTracerProvider` to retrieve it.

Types

type RetryConfig

type RetryConfig struct {
	Clock           clockwork.Clock
	MaxRetries      int
	BackoffDuration time.Duration
	ErrIsFatal      func(error) bool
}

type Task

type Task func(context.Context) error

Task is an abstraction over any context-bound, fallible activity.

func Group

func Group(fns ...Task) Task

Group takes a sequence of Tasks and returns a task that starts them all in separate goroutines and waits for them all to finish. The semantics mirror those of the errgroup package, so only the first non-nil error will be returned.

func Noop

func Noop() Task

func (Task) AsThunk

func (t Task) AsThunk(ctx context.Context, err *error) func()

AsThunk returns a no-argument, no-result function that calls the inner task and sets the provided `err` pointer to the returned error. Note that `err` will be overwritten regardless of the inner function result.

func (Task) LoopUntilError

func (t Task) LoopUntilError() Task

LoopUntilError returns a Task that runs the inner task in a loop until it returns a non-nil error.

func (Task) WithCtx

func (t Task) WithCtx(ctx context.Context) func() error

WithCtx converts the inner task into a `func() error` that uses the provided context.

func (Task) WithLogContext

func (t Task) WithLogContext(fn func(zerolog.Context) zerolog.Context) Task

WithLogContext returns a new task that will apply the provided function to the context-scoped logger before invoking the inner task.

func (Task) WithLogField

func (t Task) WithLogField(k, v string) Task

WithLogField returns a new task that will add the provided key/value pair to the context-scoped logger before invoking the inner task.

func (Task) WithNewSpan

func (t Task) WithNewSpan(name string, opts ...oteltrace.SpanStartOption) Task

WithNewSpan starts a new trace span named `name`.

func (Task) WithOtelTracer

func (t Task) WithOtelTracer(pkg string) Task

WithOtelTracer returns a task that injects a tracer, created using the `pkg` argument, into the context before invoking the inner task.

func (Task) WithOtelTracerProvider

func (t Task) WithOtelTracerProvider(tp oteltrace.TracerProvider) Task

WithOtelTracerProvider returns a task that injects the provided TracerProvider into the context before invoking the inner task.

func (Task) WithPanicCatcher

func (t Task) WithPanicCatcher() Task

func (Task) WithRetries

func (t Task) WithRetries(rc RetryConfig) Task

WithRetries returns a new task that will retry the inner task according to the provided retryConfig.

func (Task) WithSpanAttributes

func (t Task) WithSpanAttributes(attrs ...attribute.KeyValue) Task

WithSpanAttributes returns a task that sets the trace span attributes to `attrs` before invoking the inner task.

func (Task) WithStartingStoppingLogs

func (t Task) WithStartingStoppingLogs(name string, lvl zerolog.Level) Task

WithStartingStoppingLogs returns a new task that will log a "starting" message before and a "stopping" message after invoking the inner task.

Jump to

Keyboard shortcuts

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