otel

package module
v0.4.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: 18 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package otel instruments taskmill with OpenTelemetry traces and metrics, following OpenTelemetry's semantic conventions for messaging.

Create an Instrumentation with New, and add its middleware to the client and to the workers:

  • Instrumentation.EnqueueMiddleware traces every enqueue with a producer span, "send {queue}", and stores that span's context in the metadata of the tasks.
  • Instrumentation.Middleware traces every attempt with a consumer span, "process {queue}", a child of the producer span: a task's work shows in the trace of the request that enqueued it.

Both record metrics: messaging.client.sent.messages and messaging.client.operation.duration for enqueues, and messaging.process.duration for attempts. Instrumentation.ObserveQueues adds gauges of the installation's queues, and Instrumentation.Publisher traces what the outbox publishes to a broker.

Attributes are those of the conventions, with messaging.system "taskmill" and the queue as messaging.destination.name, plus taskmill.task.kind and, on spans, taskmill.task.attempt. Metrics carry no task IDs, so their cardinality is bounded by the queues, kinds, states, outbox topics, and error types. error.type is the error's type as semconv.ErrorType reports it: an error with an ErrorType() string method names its own. taskmill's errors name theirs, such as "invalid_argument", "panic", or "timeout" for an attempt that ran out of time.

Example

This example instruments a client, its worker, and an outbox relay, with OpenTelemetry's global providers.

package main

import (
	"context"
	"log"

	"github.com/jackc/pgx/v5/pgxpool"

	"github.com/code19m/taskmill"
	tmotel "github.com/code19m/taskmill/otel"
	"github.com/code19m/taskmill/outbox"
)

func main() {
	ctx := context.Background()
	var pool *pgxpool.Pool         // from pgxpool.New
	var handlers taskmill.Handlers // with your handlers
	var kafka outbox.PublisherFunc // publishes to your broker

	tel := tmotel.New(nil)
	client, err := taskmill.NewClient(ctx, pool, &taskmill.ClientConfig{
		EnqueueMiddleware: []taskmill.EnqueueMiddleware{tel.EnqueueMiddleware},
	})
	if err != nil {
		log.Fatal(err)
	}
	outbox.New(client).AddRelay(&handlers, tel.Publisher("kafka", kafka), nil)
	worker, err := taskmill.NewWorker(client, &taskmill.WorkerConfig{
		Handlers:   &handlers,
		Middleware: []taskmill.Middleware{tel.Middleware},
	})
	if err != nil {
		log.Fatal(err)
	}
	if _, err := tel.ObserveQueues(client); err != nil {
		log.Fatal(err)
	}
	_ = worker.Run(ctx)
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// TracerProvider creates the tracer. Default: otel.GetTracerProvider().
	TracerProvider trace.TracerProvider

	// MeterProvider creates the meter. Default: otel.GetMeterProvider().
	MeterProvider metric.MeterProvider

	// Propagator carries trace context in task metadata and message headers.
	// Default: otel.GetTextMapPropagator(), which carries nothing until the
	// application sets one, such as propagation.TraceContext{}.
	Propagator propagation.TextMapPropagator
}

Config configures New. The zero value uses OpenTelemetry's global providers and propagator.

type Instrumentation

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

Instrumentation traces and measures taskmill. Create one with New and share it: it is safe for concurrent use.

func New

func New(cfg *Config) *Instrumentation

New returns an Instrumentation. cfg may be nil. An instrument that cannot be created is reported to otel.Handle and records nothing.

func (*Instrumentation) EnqueueMiddleware

func (in *Instrumentation) EnqueueMiddleware(ctx context.Context, tasks []*taskmill.EnqueueTask, next func(context.Context) error) error

EnqueueMiddleware is a taskmill.EnqueueMiddleware that traces each enqueue with a producer span, "send {queue}", and stores the span's context in the metadata of every task it enqueues, replacing any already there. A batch has one span: its messaging.batch.message_count says how many tasks it held, and its name and attributes leave out the queue or kind when its tasks differ in them. Inside the caller's transaction, the span ends before the transaction commits.

func (*Instrumentation) Middleware

func (in *Instrumentation) Middleware(ctx context.Context, task *taskmill.TaskInfo, next func(context.Context) error) error

Middleware is a taskmill.Middleware that traces each attempt with a consumer span, "process {queue}", and measures it. The span's parent is the producer span whose context the task's metadata holds, so the attempt shows in the trace that enqueued the task, and the span links to it too, as the conventions ask; a span already in ctx becomes another link. The handler's context carries the span. A handler error sets the span's status and error.type, which names why the attempt's context ended if it did, such as "timeout"; a snooze is not an error. Add it first, so that it measures the other middleware too.

func (*Instrumentation) ObserveQueues

func (in *Instrumentation) ObserveQueues(client *taskmill.Client) (metric.Registration, error)

ObserveQueues reports gauges of client's queues, read with taskmill.Client.Queues at each collection, from the statistics that the maintainer refreshes every 10 seconds:

  • taskmill.queue.tasks: the tasks of each queue per state (taskmill.task.state), except completed ones. Counting stops at 10,000 tasks per queue and state, and at 100,000 scheduled and retryable tasks across all queues, so on a large backlog a count is a lower bound.
  • taskmill.queue.capped: per state, as taskmill.queue.tasks, 1 when that count stopped at its cap with more tasks left, else 0.
  • taskmill.queue.lag: in seconds, how long the queue's oldest due task has waited, as taskmill.QueueInfo.Lag says: it may read up to one refresh high. It stays right on a large backlog, and keeps growing when no maintainer runs: alert on it, not on the counts.
  • taskmill.queue.halted_groups: the queue's FIFO groups that a dead task halts, as taskmill.QueueInfo.HaltedGroups says, counted up to 10,000. Their tasks wait for an operator, and do not count in the lag: alert when it is above 0.
  • taskmill.queue.most_failures: the most times in a row that one task of the queue, waiting to retry, has failed, as taskmill.QueueInfo.MostFailures says. It grows while a task keeps failing, and the lag stays near zero. A task drops out of it while it runs its next attempt, so alert on its maximum over some minutes.
  • taskmill.stats.age: in seconds, by this process's clock, how long ago the maintainer last counted the queues. Alert when it passes a minute: no maintainer runs, or it cannot count, and a queue that was empty at its last count cannot show lag. It is absent until the maintainer first counts.

The gauges describe the whole installation, read from one small table, so every process that observes it reports the same values: aggregate them with max, not sum. A read takes at most 5 seconds; one that fails is reported to otel.Handle. Unregister the returned registration to stop observing. It returns an error that wraps taskmill.ErrInvalidArgument if client is nil.

func (*Instrumentation) Publisher

func (in *Instrumentation) Publisher(system string, pub outbox.Publisher) outbox.Publisher

Publisher wraps pub, the outbox's publisher to a broker, so that each delivery is traced with a producer span, "send {topic}", and counted in messaging.client.sent.messages. The span's parent is the span that stored the message, from outbox.Delivery.Metadata, and its context is added to the message's headers, replacing any already there, so that consumers continue the trace. system names the broker as the conventions do for messaging.system, such as "kafka" or "rabbitmq". A nil pub returns nil, which outbox.Outbox.AddRelay reports.

Jump to

Keyboard shortcuts

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