Перенеси реализацию мессенджера из ЛР 6
Источник — проверенное рабочее дерево ЛР 6 поверх 18878d6, включая изменения после повторного ревью. SHA-256 списка исходных файлов: e45a3d85b41feff1da78dce3f20fd1f4af1c5ee629e6ed703218bc8e21ad58fe.
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
package pipeline
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/metric"
|
||||
)
|
||||
|
||||
var pipelineMeter = otel.Meter("messenger/pipeline")
|
||||
var readyDuration, _ = pipelineMeter.Float64Histogram("messenger.ready.duration", metric.WithUnit("s"),
|
||||
metric.WithExplicitBucketBoundaries(.1, .25, .5, 1, 2, 3, 5, 10, 30, 60, 120))
|
||||
|
||||
type snapshot struct {
|
||||
shortTotal, shortBad, longTotal, longBad, depth int64
|
||||
oldest, timestamp float64
|
||||
}
|
||||
|
||||
// Both cohorts use the same database snapshot. The window is (now-window, now]
|
||||
// in deadline time. Rows that never completed become visible as soon as due.
|
||||
func (s *Store) snapshot(ctx context.Context, asOf time.Time, deadline time.Duration) (snapshot, error) {
|
||||
var sample snapshot
|
||||
var override *time.Time
|
||||
if !asOf.IsZero() {
|
||||
override = &asOf
|
||||
}
|
||||
err := s.pool.QueryRow(ctx, `
|
||||
WITH bounds AS (
|
||||
SELECT coalesce($1::timestamptz,statement_timestamp()) AS t,
|
||||
$2::bigint * interval '1 microsecond' AS deadline
|
||||
), cohort AS (
|
||||
SELECT accepted_at > t-deadline-interval '30 seconds' AS short,
|
||||
first_ready_at IS NULL OR first_ready_at > accepted_at+deadline AS bad
|
||||
FROM attachments,bounds
|
||||
WHERE accepted_at > t-deadline-interval '2 minutes' AND accepted_at <= t-deadline
|
||||
), queue AS (
|
||||
SELECT count(*) AS depth,min(available_at) FILTER (WHERE available_at <= t) AS oldest
|
||||
FROM attachment_jobs,bounds WHERE state='queued'
|
||||
)
|
||||
SELECT count(*) FILTER (WHERE short), count(*) FILTER (WHERE short AND bad),
|
||||
count(*), count(*) FILTER (WHERE bad), (SELECT depth FROM queue),
|
||||
(SELECT greatest(0,extract(epoch FROM t-oldest))::float8 FROM bounds,queue),
|
||||
(SELECT extract(epoch FROM t)::float8 FROM bounds)
|
||||
FROM cohort`, override, deadline.Microseconds()).Scan(&sample.shortTotal, &sample.shortBad, &sample.longTotal, &sample.longBad, &sample.depth, &sample.oldest, &sample.timestamp)
|
||||
return sample, err
|
||||
}
|
||||
|
||||
func (s *Store) RegisterMetrics(deadline time.Duration) error {
|
||||
if deadline < time.Microsecond {
|
||||
return errors.New("ready deadline must be at least one microsecond")
|
||||
}
|
||||
total, err := pipelineMeter.Int64ObservableGauge("messenger.ready.cohort.total.count")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
bad, err := pipelineMeter.Int64ObservableGauge("messenger.ready.cohort.bad.count")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
depth, err := pipelineMeter.Int64ObservableGauge("messenger.queue.depth")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
oldest, err := pipelineMeter.Float64ObservableGauge("messenger.queue.oldest", metric.WithUnit("s"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
timestamp, err := pipelineMeter.Float64ObservableGauge("messenger.queue.snapshot.timestamp", metric.WithUnit("s"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = pipelineMeter.RegisterCallback(func(ctx context.Context, obs metric.Observer) error {
|
||||
ctx, cancel := context.WithTimeout(ctx, 500*time.Millisecond)
|
||||
defer cancel()
|
||||
v, err := s.snapshot(ctx, time.Time{}, deadline)
|
||||
if err != nil {
|
||||
// Keep exporting HTTP and process measurements throughout a database outage.
|
||||
// Omitting this observation represents missing data.
|
||||
return nil
|
||||
}
|
||||
short := metric.WithAttributes(attribute.String("window", "30s"))
|
||||
long := metric.WithAttributes(attribute.String("window", "2m"))
|
||||
obs.ObserveInt64(total, v.shortTotal, short)
|
||||
obs.ObserveInt64(total, v.longTotal, long)
|
||||
obs.ObserveInt64(bad, v.shortBad, short)
|
||||
obs.ObserveInt64(bad, v.longBad, long)
|
||||
queue := metric.WithAttributes(attribute.String("queue", "attachment_jobs"))
|
||||
obs.ObserveInt64(depth, v.depth, queue)
|
||||
obs.ObserveFloat64(oldest, v.oldest, queue)
|
||||
obs.ObserveFloat64(timestamp, v.timestamp)
|
||||
return nil
|
||||
}, total, bad, depth, oldest, timestamp)
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user