forked from mtusi2026/mtusi2026lab7
на оценку 4
This commit is contained in:
@@ -0,0 +1,173 @@
|
||||
package telemetry
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/codes"
|
||||
"go.opentelemetry.io/otel/metric"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
type JobRecorder struct {
|
||||
runtime *Runtime
|
||||
processed metric.Int64Counter
|
||||
duration metric.Float64Histogram
|
||||
readyDuration metric.Float64Histogram
|
||||
busy atomic.Int64
|
||||
capacity int64
|
||||
}
|
||||
|
||||
type JobAttempt struct {
|
||||
recorder *JobRecorder
|
||||
ctx context.Context
|
||||
span trace.Span
|
||||
started time.Time
|
||||
createdAt time.Time
|
||||
jobType string
|
||||
attempt int
|
||||
ended atomic.Bool
|
||||
}
|
||||
|
||||
func NewJobRecorder(runtime *Runtime, capacity int) (*JobRecorder, error) {
|
||||
if capacity < 1 {
|
||||
return nil, fmt.Errorf("worker capacity must be positive")
|
||||
}
|
||||
processed, err := runtime.Meter.Int64Counter(
|
||||
"messenger.worker.jobs",
|
||||
metric.WithDescription("Number of completed worker processing attempts"),
|
||||
metric.WithUnit("{job}"),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create worker jobs counter: %w", err)
|
||||
}
|
||||
duration, err := runtime.Meter.Float64Histogram(
|
||||
"messenger.worker.job.duration",
|
||||
metric.WithDescription("Worker processing attempt duration"),
|
||||
metric.WithUnit("s"),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create worker duration histogram: %w", err)
|
||||
}
|
||||
readyDuration, err := runtime.Meter.Float64Histogram(
|
||||
"messenger.attachment.ready.duration",
|
||||
metric.WithDescription("Time from durable acceptance until attachment ready"),
|
||||
metric.WithUnit("s"),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create attachment ready duration histogram: %w", err)
|
||||
}
|
||||
busy, err := runtime.Meter.Int64ObservableGauge(
|
||||
"messenger.worker.busy",
|
||||
metric.WithDescription("Number of workers processing jobs"),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create worker busy metric: %w", err)
|
||||
}
|
||||
workerCapacity, err := runtime.Meter.Int64ObservableGauge(
|
||||
"messenger.worker.capacity",
|
||||
metric.WithDescription("Configured worker processing capacity"),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create worker capacity metric: %w", err)
|
||||
}
|
||||
recorder := &JobRecorder{
|
||||
runtime: runtime,
|
||||
processed: processed,
|
||||
duration: duration,
|
||||
readyDuration: readyDuration,
|
||||
capacity: int64(capacity),
|
||||
}
|
||||
_, err = runtime.Meter.RegisterCallback(
|
||||
func(ctx context.Context, observer metric.Observer) error {
|
||||
observer.ObserveInt64(busy, recorder.busy.Load())
|
||||
observer.ObserveInt64(workerCapacity, recorder.capacity)
|
||||
return nil
|
||||
},
|
||||
busy,
|
||||
workerCapacity,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("register worker saturation metrics: %w", err)
|
||||
}
|
||||
return recorder, nil
|
||||
}
|
||||
|
||||
func (recorder *JobRecorder) Start(
|
||||
ctx context.Context,
|
||||
jobType string,
|
||||
destination string,
|
||||
attempt int,
|
||||
createdAt time.Time,
|
||||
) (context.Context, *JobAttempt) {
|
||||
ctx, span := recorder.runtime.Tracer.Start(
|
||||
ctx,
|
||||
"attachment.process",
|
||||
trace.WithSpanKind(trace.SpanKindConsumer),
|
||||
trace.WithAttributes(
|
||||
attribute.String("messaging.system", "rabbitmq"),
|
||||
attribute.String("messaging.destination.name", destination),
|
||||
attribute.String("messaging.operation.name", "process"),
|
||||
attribute.String("job.type", jobType),
|
||||
attribute.Int("job.attempt", attempt),
|
||||
),
|
||||
)
|
||||
recorder.busy.Add(1)
|
||||
return ctx, &JobAttempt{
|
||||
recorder: recorder,
|
||||
ctx: ctx,
|
||||
span: span,
|
||||
started: time.Now(),
|
||||
createdAt: createdAt,
|
||||
jobType: jobType,
|
||||
attempt: attempt,
|
||||
}
|
||||
}
|
||||
|
||||
func (attempt *JobAttempt) End(processErr error) {
|
||||
if !attempt.ended.CompareAndSwap(false, true) {
|
||||
return
|
||||
}
|
||||
defer attempt.recorder.busy.Add(-1)
|
||||
elapsed := time.Since(attempt.started)
|
||||
outcome := "succeeded"
|
||||
event := "attachment_job_completed"
|
||||
if processErr != nil {
|
||||
outcome = "failed"
|
||||
event = "attachment_job_failed"
|
||||
attempt.span.SetStatus(codes.Error, "job processing failed")
|
||||
} else {
|
||||
attempt.span.SetStatus(codes.Ok, "")
|
||||
}
|
||||
labels := metric.WithAttributes(
|
||||
attribute.String("job_type", attempt.jobType),
|
||||
attribute.String("outcome", outcome),
|
||||
)
|
||||
attempt.recorder.processed.Add(attempt.ctx, 1, labels)
|
||||
attempt.recorder.duration.Record(attempt.ctx, elapsed.Seconds(), labels)
|
||||
if processErr == nil && !attempt.createdAt.IsZero() {
|
||||
readySeconds := time.Since(attempt.createdAt).Seconds()
|
||||
if readySeconds >= 0 {
|
||||
attempt.recorder.readyDuration.Record(
|
||||
attempt.ctx,
|
||||
readySeconds,
|
||||
metric.WithAttributes(attribute.String("job_type", attempt.jobType)),
|
||||
)
|
||||
}
|
||||
}
|
||||
fields := Fields{
|
||||
"job_type": attempt.jobType,
|
||||
"outcome": outcome,
|
||||
"attempt": attempt.attempt,
|
||||
"duration_ms": float64(elapsed.Microseconds()) / 1000,
|
||||
}
|
||||
if processErr != nil {
|
||||
attempt.recorder.runtime.Logger.Warn(attempt.ctx, event, fields)
|
||||
} else {
|
||||
attempt.recorder.runtime.Logger.Info(attempt.ctx, event, fields)
|
||||
}
|
||||
attempt.span.End()
|
||||
}
|
||||
Reference in New Issue
Block a user