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 and kinds. 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))
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)
}
Output:
Index ¶
- type Config
- type Instrumentation
- func (in *Instrumentation) EnqueueMiddleware(ctx context.Context, tasks []*taskmill.EnqueueTask, ...) error
- func (in *Instrumentation) Middleware(ctx context.Context, task *taskmill.TaskInfo, next func(context.Context) error) error
- func (in *Instrumentation) ObserveQueues(client *taskmill.Client) (metric.Registration, error)
- func (in *Instrumentation) Publisher(system string, pub outbox.Publisher) outbox.Publisher
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: 1 when a count of the queue stopped at its cap, 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.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.
func (*Instrumentation) 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.