Help us shape the future of Go! Take the 2026 Go Developer Survey

telemetry

package
v1.9.3 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package telemetry holds the OpenTelemetry spans and metric instruments Dispatch records for the work it does itself: the job pool and runner, cron, checkpoint workflows, the durable runtime, the outbox publisher and the artifact sweeper.

It is a leaf package. It imports the standard library, OpenTelemetry and go-utils metrics, and nothing else from Dispatch, so every subsystem can import it without a cycle. exec deliberately does not import it.

You rarely build a Telemetry yourself. engine.Build builds one from engine.WithMetricFactory and engine.WithTracerProvider and hands it to every subsystem it constructs, and the Forge extension passes the app's metrics. Build one with New only when you assemble a subsystem (a worker.Pool, a durable runtime Worker, a delivery Publisher) outside the engine.

A nil *Telemetry is valid everywhere: spans go to the global tracer provider and no metrics are recorded.

Index

Constants

View Source
const (
	MetricJobsEnqueued       = "dispatch_jobs_enqueued_total"
	MetricJobAttemptDuration = "dispatch_job_attempt_duration_seconds"
	MetricJobsDeadLettered   = "dispatch_jobs_dead_lettered_total"
	MetricJobQueueWait       = "dispatch_job_queue_wait_seconds"
	MetricJobPolls           = "dispatch_job_polls_total"
	MetricJobsRequeued       = "dispatch_jobs_requeued_total"
	MetricJobLeaseRenewals   = "dispatch_job_lease_renewals_total"
	MetricJobsReclaimed      = "dispatch_jobs_reclaimed_total"
	MetricJobsActive         = "dispatch_jobs_active"

	MetricCronFires    = "dispatch_cron_fires_total"
	MetricCronFireLag  = "dispatch_cron_fire_lag_seconds"
	MetricCronLeader   = "dispatch_cron_leader"
	MetricWorkflowRuns = "dispatch_workflow_run_duration_seconds"
	MetricWorkflowStep = "dispatch_workflow_step_duration_seconds"

	MetricDurableTaskDuration   = "dispatch_durable_task_duration_seconds"
	MetricDurablePolls          = "dispatch_durable_polls_total"
	MetricDurableReplayDuration = "dispatch_durable_replay_duration_seconds"

	MetricOutboxDeliveryDuration = "dispatch_outbox_delivery_duration_seconds"
	MetricOutboxDeliveryLag      = "dispatch_outbox_delivery_lag_seconds"

	MetricArtifactsSwept         = "dispatch_artifacts_swept_total"
	MetricArtifactsPurged        = "dispatch_artifacts_purged_total"
	MetricArtifactBytesReclaimed = "dispatch_artifact_bytes_reclaimed_total"
)

Metric names, as Dispatch registers them. A Forge app scrapes each one with its namespace in front: dispatch_jobs_enqueued_total is forge_dispatch_jobs_enqueued_total on /_/metrics.

View Source
const (
	OutcomeCompleted    = "completed"
	OutcomeRetry        = "retry"
	OutcomeDeadLettered = "dead_lettered"
	OutcomeRequeued     = "requeued"
	OutcomeLeaseLost    = "lease_lost"
	OutcomeCancelled    = "cancelled"
	OutcomeStoreError   = "store_error"
)

Job attempt outcomes, the outcome label on dispatch_job_attempt_duration_seconds and the dispatch.job.outcome span attribute. Cancelled covers an operator's cancel and a worker shutting down mid-attempt; store_error means the store refused the terminal write.

View Source
const (
	DeadLetterRetriesExhausted = "retries_exhausted"
	DeadLetterPermanent        = "permanent"
	DeadLetterLaunchFailures   = "launch_failures"
)

Dead letter reasons, the reason label on dispatch_jobs_dead_lettered_total.

View Source
const (
	RequeueRateLimited = "rate_limited"
	RequeueNoCapacity  = "no_capacity"
	RequeueShutdown    = "shutdown"
)

Requeue reasons, the reason label on dispatch_jobs_requeued_total: a job this worker claimed and handed back without running it.

View Source
const (
	ResultOK    = "ok"
	ResultError = "error"
)

Shared result values.

View Source
const (
	PollWork  = "work"
	PollEmpty = "empty"
	PollError = "error"
)

Poll results, the result label on dispatch_job_polls_total and dispatch_durable_polls_total.

View Source
const (
	RenewalOK    = "ok"
	RenewalLost  = "lost"
	RenewalError = "error"
)

Lease renewal results, the result label on dispatch_job_lease_renewals_total.

View Source
const (
	CronFired   = "fired"
	CronLocked  = "locked"
	CronSkipped = "skipped"
	CronError   = "error"
)

Cron fire results, the result label on dispatch_cron_fires_total.

View Source
const (
	DurableOK         = "ok"
	DurableContention = "contention"
	DurableLeaseLost  = "lease_lost"
	DurableNoHandler  = "no_handler"
	DurableCancelled  = "cancelled"
	DurableError      = "error"
)

Durable task results, the result label on dispatch_durable_task_duration_seconds.

View Source
const (
	OutboxAcknowledged   = "acknowledged"
	OutboxCancelled      = "cancelled"
	OutboxRejected       = "rejected"
	OutboxTimeout        = "timeout"
	OutboxConflict       = "conflict"
	OutboxUnavailable    = "unavailable"
	OutboxInvalidReceipt = "invalid_receipt"
)

Outbox delivery results, the result label on dispatch_outbox_delivery_duration_seconds. Rejected through invalid_receipt are the failure categories the publisher already persists on a retry; cancelled means Stop interrupted the attempt, which leaves the delivery to be claimed again.

View Source
const (
	PurgeOK      = "ok"
	PurgeSkipped = "skipped"
)

Artifact purge results, the result label on dispatch_artifacts_purged_total.

View Source
const OtherQueue = "other"

OtherQueue is the queue label for any queue outside the configured set. Queue names are chosen by whoever enqueues, so only the names you configured (WithQueues) become label values. Everything else collapses into this one.

View Source
const ScopeName = "github.com/xraph/dispatch"

ScopeName is the instrumentation scope on every span Dispatch starts.

Variables

This section is empty.

Functions

func Detach

func Detach(ctx context.Context) context.Context

Detach returns ctx without its span or baggage, keeping its values, deadline and cancellation. A long-lived loop calls it once on the context it was started with. forge v1.12.9 hands an extension's Start a context that carries the "start <ext>" boot span, and a request context carries the request's server span; without this, every query an idle poll makes would land in that long-finished trace.

func ErrorType

func ErrorType(err error) string

ErrorType names err for an exception event: "context.Canceled" or "context.DeadlineExceeded" when the chain holds one, otherwise the Go type of the innermost error, such as "*net.OpError". It returns "" for nil.

func Fail

func Fail(span trace.Span, class, errType string)

Fail marks span as failed. class is a short, bounded description (a failure class such as "handler error", never err.Error()), and the exception event carries only exception.type: the error's Go type or a sentinel's name. Driver and handler messages quote values (payload fragments, keys, addresses), and none of that belongs on a span.

func StartChild

func StartChild(ctx context.Context, name string, opts ...trace.SpanStartOption) (context.Context, trace.Span)

StartChild starts a span only when ctx already carries a local Dispatch span, using that span's own tracer provider. Code that has no Telemetry of its own (the staging middleware) uses it to nest under the job attempt it runs in without a span of its own outside one. The caller ends it.

Types

type ArtifactMetrics

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

ArtifactMetrics are the artifact sweeper instruments.

func (*ArtifactMetrics) BytesReclaimed

func (m *ArtifactMetrics) BytesReclaimed(n int64)

BytesReclaimed counts bytes the purge pass deleted.

func (*ArtifactMetrics) Purged

func (m *ArtifactMetrics) Purged(result string, n int)

Purged counts artifacts the purge pass handled.

func (*ArtifactMetrics) Swept

func (m *ArtifactMetrics) Swept(reason string, n int)

Swept counts artifacts marked for purge.

type CronMetrics

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

CronMetrics are the cron scheduler instruments.

func (*CronMetrics) Fire

func (m *CronMetrics) Fire(result string)

Fire counts one due cron entry handled.

func (*CronMetrics) Lag

func (m *CronMetrics) Lag(d time.Duration)

Lag records how late a cron entry fired.

func (*CronMetrics) Leader

func (m *CronMetrics) Leader(leading bool)

Leader records whether this scheduler leads.

type DurableMetrics

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

DurableMetrics are the durable runtime instruments.

func (*DurableMetrics) Poll

func (m *DurableMetrics) Poll(kind, result string)

Poll counts one durable claim poll.

func (*DurableMetrics) Replay

func (m *DurableMetrics) Replay(d time.Duration)

Replay records one history replay.

func (*DurableMetrics) Task

func (m *DurableMetrics) Task(kind, result string, d time.Duration)

Task records one claimed durable task.

type JobMetrics

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

JobMetrics are the job pool, runner and enqueue instruments.

func (*JobMetrics) Active

func (m *JobMetrics) Active(n int)

Active sets the number of attempts running on this worker.

func (*JobMetrics) Attempt

func (m *JobMetrics) Attempt(queue, outcome string, d time.Duration)

Attempt records one finished job attempt.

func (*JobMetrics) DeadLettered

func (m *JobMetrics) DeadLettered(queue, reason string)

DeadLettered counts one job moved to the dead letter queue.

func (*JobMetrics) Enqueued

func (m *JobMetrics) Enqueued(queue string)

Enqueued counts one job written to the store.

func (*JobMetrics) Poll

func (m *JobMetrics) Poll(result string)

Poll counts one dequeue poll.

func (*JobMetrics) QueueWait

func (m *JobMetrics) QueueWait(queue string, d time.Duration)

QueueWait records how long a claimed job waited past its due time.

func (*JobMetrics) Reclaimed

func (m *JobMetrics) Reclaimed(n int)

Reclaimed counts jobs the reaper returned to pending.

func (*JobMetrics) Renewal

func (m *JobMetrics) Renewal(result string)

Renewal counts one lease renewal or heartbeat.

func (*JobMetrics) Requeued

func (m *JobMetrics) Requeued(queue, reason string)

Requeued counts one claimed job handed back without running.

type Option

type Option func(*Telemetry)

Option configures a Telemetry.

func WithMetrics

func WithMetrics(f gu.MetricFactory) Option

WithMetrics records Dispatch's metrics through f. In a Forge app that is app.Metrics(). The factory has to key series by name and labels, as Forge's does. A nil factory leaves metrics off.

func WithQueues

func WithQueues(names ...string) Option

WithQueues names the queues that may appear as a metric label. Pass the queues your workers poll and the ones you configured limits for. Any other queue name is recorded as OtherQueue.

func WithTracerProvider

func WithTracerProvider(tp trace.TracerProvider) Option

WithTracerProvider starts Dispatch's spans on tp. A nil tp, the default, means the global provider, looked up on every span so a provider installed later (forge.New, a test recorder) still receives them.

type OutboxMetrics

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

OutboxMetrics are the outbox publisher instruments.

func (*OutboxMetrics) Delivery

func (m *OutboxMetrics) Delivery(destination, result string, d time.Duration)

Delivery records one delivery attempt.

func (*OutboxMetrics) Lag

func (m *OutboxMetrics) Lag(destination string, d time.Duration)

Lag records how long an acknowledged delivery waited since acceptance.

type Telemetry

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

Telemetry carries the tracer provider and metric factory Dispatch records through. Build it once with New and share it; it is safe for concurrent use. A nil *Telemetry records no metrics and starts spans on the global tracer provider.

func New

func New(opts ...Option) *Telemetry

New builds a Telemetry.

func (*Telemetry) Artifacts

func (t *Telemetry) Artifacts() *ArtifactMetrics

Artifacts returns the artifact sweeper instruments, or nil when metrics are off.

func (*Telemetry) Cron

func (t *Telemetry) Cron() *CronMetrics

Cron returns the cron instruments, or nil when metrics are off.

func (*Telemetry) Durable

func (t *Telemetry) Durable() *DurableMetrics

Durable returns the durable runtime instruments, or nil when metrics are off.

func (t *Telemetry) EnqueueLinks(jobID string) []trace.Link

EnqueueLinks returns a link to the span that enqueued jobID, when this process enqueued it recently enough to remember.

func (*Telemetry) Jobs

func (t *Telemetry) Jobs() *JobMetrics

Jobs returns the job instruments, building them on the first call. It returns nil, which records nothing, when metrics are off.

func (*Telemetry) Outbox

func (t *Telemetry) Outbox() *OutboxMetrics

Outbox returns the outbox publisher instruments, or nil when metrics are off.

func (*Telemetry) QueueLabel

func (t *Telemetry) QueueLabel(queue string) string

QueueLabel maps a queue name to its metric label: the name itself when it is one of the configured queues, OtherQueue otherwise.

func (*Telemetry) RememberEnqueue

func (t *Telemetry) RememberEnqueue(jobID string, sc trace.SpanContext)

RememberEnqueue records the span context that enqueued a job, so an attempt this process runs can link back to it. Nothing about it is stored with the job: the cache is in memory, bounded, and an attempt on another worker simply starts without a link.

func (*Telemetry) Start

func (t *Telemetry) Start(ctx context.Context, name string, opts ...trace.SpanStartOption) (context.Context, trace.Span)

Start starts a span under Dispatch's instrumentation scope. The caller ends it.

func (*Telemetry) Tracer

func (t *Telemetry) Tracer() trace.Tracer

Tracer returns the tracer Dispatch's spans start on.

func (*Telemetry) Workflows

func (t *Telemetry) Workflows() *WorkflowMetrics

Workflows returns the checkpoint workflow instruments, or nil when metrics are off.

type WorkflowMetrics

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

WorkflowMetrics are the checkpoint workflow instruments.

func (*WorkflowMetrics) Run

func (m *WorkflowMetrics) Run(result string, d time.Duration)

Run records one finished workflow run. result is completed or failed.

func (*WorkflowMetrics) Step

func (m *WorkflowMetrics) Step(kind, result string, d time.Duration)

Step records one workflow step that ran.

Jump to

Keyboard shortcuts

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