Files
mtusi2026lab7-fork/src/backend/telemetry/worker.go
T

38 lines
1.3 KiB
Go

package telemetry
import (
"context"
"time"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
otellog "go.opentelemetry.io/otel/log"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/trace"
)
func (s *Signals) Job(ctx context.Context, jobType, attachmentID string, attempt int, accepted time.Time) (context.Context, func(string)) {
if s == nil {
return ctx, func(string) {}
}
start := time.Now()
s.busy.Add(ctx, 1)
ctx, span := s.tracer.Start(ctx, jobType+" process", trace.WithSpanKind(trace.SpanKindConsumer), trace.WithAttributes(attribute.Int("job.attempt", attempt)))
return ctx, func(outcome string) {
defer span.End()
s.busy.Add(ctx, -1)
if outcome == "success" {
s.ready.Record(ctx, time.Since(accepted).Seconds())
}
attrs := []attribute.KeyValue{attribute.String("job.type", jobType), attribute.String("job.outcome", outcome)}
span.SetAttributes(attrs...)
failed := outcome == "error"
if failed {
span.SetStatus(codes.Error, "")
}
s.jobs.Add(ctx, 1, metric.WithAttributes(attrs...))
s.jobDuration.Record(ctx, time.Since(start).Seconds(), metric.WithAttributes(attrs...))
s.event(ctx, "job.processing.completed", failed, otellog.String("job.type", jobType), otellog.String("job.outcome", outcome), otellog.String("attachment_id", attachmentID))
}
}