forked from mtusi2026/mtusi2026lab6
43 lines
1.3 KiB
Go
43 lines
1.3 KiB
Go
package pipeline
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/observation"
|
|
)
|
|
|
|
// Observer preserves job causality and records bounded processing measurements.
|
|
type Observer interface {
|
|
observation.Tracer
|
|
// Extract restores a durable parent context.
|
|
Extract(context.Context, string, string) context.Context
|
|
// Inject serializes the producer's W3C carrier.
|
|
Inject(context.Context) (string, string)
|
|
// JobStarted increments current worker occupancy.
|
|
JobStarted()
|
|
// JobFinished records a real attempt's outcome and duration.
|
|
JobFinished(context.Context, time.Duration, error)
|
|
// Ready records latency from durable acceptance to confirmed publication.
|
|
Ready(context.Context, time.Time)
|
|
}
|
|
|
|
// WithObserver binds optional telemetry at the composition root.
|
|
func (w *Worker) WithObserver(observer Observer) *Worker {
|
|
w.observer = observer
|
|
return w
|
|
}
|
|
|
|
// publish traces one durable handoff and propagates the producer context to its consumer.
|
|
func (w *Worker) publish(ctx context.Context, job Job) error {
|
|
if w.observer == nil {
|
|
return w.queue.Publish(ctx, job)
|
|
}
|
|
ctx = w.observer.Extract(ctx, job.Traceparent, job.Tracestate)
|
|
ctx, end := w.observer.Start(ctx, "attachment.publish", "producer")
|
|
job.Traceparent, job.Tracestate = w.observer.Inject(ctx)
|
|
err := w.queue.Publish(ctx, job)
|
|
end(err)
|
|
return err
|
|
}
|