Documentation ¶
Overview ¶
Package task provides some useful helpers to express common tasks like retries and adding contextual information to a context-scoped logger.
Index ¶
- func ExtractTracer(ctx context.Context) (oteltrace.Tracer, bool)
- func ExtractTracerProvider(ctx context.Context) (oteltrace.TracerProvider, bool)
- func Float64() float64
- func InjectTracer(ctx context.Context, t oteltrace.Tracer) context.Context
- func InjectTracerProvider(ctx context.Context, tp oteltrace.TracerProvider) context.Context
- type RetryConfig
- type Task
- func (t Task) AsThunk(ctx context.Context, err *error) func()
- func (t Task) LoopUntilError() Task
- func (t Task) WithCtx(ctx context.Context) func() error
- func (t Task) WithLogContext(fn func(zerolog.Context) zerolog.Context) Task
- func (t Task) WithLogField(k, v string) Task
- func (t Task) WithNewSpan(name string, opts ...oteltrace.SpanStartOption) Task
- func (t Task) WithOtelTracer(pkg string) Task
- func (t Task) WithOtelTracerProvider(tp oteltrace.TracerProvider) Task
- func (t Task) WithPanicCatcher() Task
- func (t Task) WithRetries(rc RetryConfig) Task
- func (t Task) WithSpanAttributes(attrs ...attribute.KeyValue) Task
- func (t Task) WithStartingStoppingLogs(name string, lvl zerolog.Level) Task
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ExtractTracer ¶
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 ¶
InjectTracer returns a new context with the provided Tracer injected. Use `ExtractTracer` to retrieve it.
func InjectTracerProvider ¶
InjectTracerProvider returns a new context with the provided TracerProvider injected. Use `ExtractTracerProvider` to retrieve it.
Types ¶
type RetryConfig ¶
type Task ¶
Task is an abstraction over any context-bound, fallible activity.
func Group ¶
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 (Task) AsThunk ¶
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 ¶
LoopUntilError returns a Task that runs the inner task in a loop until it returns a non-nil error.
func (Task) WithCtx ¶
WithCtx converts the inner task into a `func() error` that uses the provided context.
func (Task) WithLogContext ¶
WithLogContext returns a new task that will apply the provided function to the context-scoped logger before invoking the inner task.
func (Task) WithLogField ¶
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 ¶
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 (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 ¶
WithSpanAttributes returns a task that sets the trace span attributes to `attrs` before invoking the inner task.