package worker import ( "context" "log/slog" "time" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/codes" "go.opentelemetry.io/otel/metric" "go.opentelemetry.io/otel/trace" "high-loud-apps-labs/internal/domain" "high-loud-apps-labs/internal/telemetry" ) type jobMetrics struct { processed metric.Int64Counter duration metric.Float64Histogram ready metric.Float64Histogram } func newJobMetrics() jobMetrics { meter := telemetry.Meter("worker.pipeline") processed, err := meter.Int64Counter("jobs_processed_total") if err != nil { panic(err) } unit := metric.WithUnit("s") duration, err := meter.Float64Histogram("job_processing_duration_seconds", unit, metric.WithExplicitBucketBoundaries(.01, .05, .1, .25, .5, 1, 2, 5, 10, 30, 60)) if err != nil { panic(err) } ready, err := meter.Float64Histogram("end_to_end_ready_seconds", unit, metric.WithExplicitBucketBoundaries(.1, .5, 1, 2, 5, 10, 30, 60, 120, 300, 900, 3600)) if err != nil { panic(err) } return jobMetrics{processed: processed, duration: duration, ready: ready} } func (m jobMetrics) recordAttempt(ctx context.Context, start time.Time, outcome string) { attrs := metric.WithAttributes(attribute.String("job.type", domain.OutboxEventProcess), attribute.String("outcome", outcome)) m.duration.Record(ctx, time.Since(start).Seconds(), attrs) m.processed.Add(ctx, 1, attrs) span := trace.SpanFromContext(ctx) span.SetAttributes(attribute.String("job.outcome", outcome)) level := slog.LevelInfo switch outcome { case "failed", "error", "retry": level = slog.LevelWarn span.SetStatus(codes.Error, outcome) } slog.Log(ctx, level, "Попытка обработки завершена", "event", "job.processed", "outcome", outcome) } func (m jobMetrics) recordReady(ctx context.Context, createdAt time.Time) { if createdAt.IsZero() { return } m.ready.Record(ctx, max(0, time.Since(createdAt).Seconds()), metric.WithAttributes(attribute.String("job.type", domain.OutboxEventProcess))) }