feat(otel): lab6 export worker slots and process CPU and RSS

This commit is contained in:
Голиков Maximillian Антон
2026-11-22 07:08:47 +03:00
parent f5cc00cd98
commit 7c57c46961
4 changed files with 77 additions and 3 deletions
+22 -3
View File
@@ -13,6 +13,7 @@ import (
amqp "github.com/rabbitmq/amqp091-go"
"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/config"
@@ -80,11 +81,26 @@ func main() {
os.Exit(1)
}
meter := telemetry.Meter("worker.consumer")
busy, err := meter.Int64UpDownCounter("worker_busy_slots", metric.WithDescription("Занятые слоты worker"))
if err != nil {
slog.Error("Ошибка регистрации метрики занятости worker", "error", err)
os.Exit(1)
}
capacity, err := meter.Int64Gauge("worker_capacity_slots", metric.WithDescription("Число слотов worker"))
if err != nil {
slog.Error("Ошибка регистрации метрики ёмкости worker", "error", err)
os.Exit(1)
}
// Цикл обрабатывает одну доставку за раз. Prefetch не задаёт параллельность.
capacity.Record(ctx, 1)
busy.Add(ctx, 0)
pipeline := worker.NewPipeline(store.Attachments(), objectStorage, store.Variants(), store.FailedJobs(), store.Outbox())
// Цикл переподключения к RabbitMQ
for {
err := runWorker(ctx, cfg, pipeline)
err := runWorker(ctx, cfg, pipeline, busy)
if err == nil {
break // Нормальное завершение по сигналу
}
@@ -93,7 +109,7 @@ func main() {
}
}
func runWorker(ctx context.Context, cfg *config.Config, pipeline *worker.Pipeline) error {
func runWorker(ctx context.Context, cfg *config.Config, pipeline *worker.Pipeline, busy metric.Int64UpDownCounter) error {
conn, err := amqp.Dial(cfg.RabbitMQURL)
if err != nil {
return err
@@ -133,7 +149,10 @@ func runWorker(ctx context.Context, cfg *config.Config, pipeline *worker.Pipelin
if !ok {
return context.Canceled // Канал закрыт (соединение разорвано)
}
if err := consume(ctx, d, pipeline); err != nil {
busy.Add(ctx, 1)
err := consume(ctx, d, pipeline)
busy.Add(ctx, -1)
if err != nil {
return err
}
}