From aade320400dd83d47cd86066319ce7e4c43634c7 Mon Sep 17 00:00:00 2001 From: Naeel Date: Sun, 12 Apr 2026 11:56:53 +0300 Subject: [PATCH] feat(billing): add queue_name column to usage records --- app/billing/billing.go | 12 +++++++++--- app/router/router.go | 7 ++++++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/app/billing/billing.go b/app/billing/billing.go index 5b5046c..2510a8e 100644 --- a/app/billing/billing.go +++ b/app/billing/billing.go @@ -82,12 +82,18 @@ func autoMigrate() error { id BIGSERIAL PRIMARY KEY, tenant_id TEXT NOT NULL, operation TEXT NOT NULL, + queue_name TEXT NOT NULL DEFAULT '', msg_count INTEGER DEFAULT 1, msg_bytes BIGINT DEFAULT 0, recorded_at TIMESTAMPTZ DEFAULT NOW() ); CREATE INDEX IF NOT EXISTS idx_sqs_usage_tenant_time ON sqs_usage_records(tenant_id, recorded_at); + -- Миграция: добавляем queue_name если таблица уже существует без него + DO $$ BEGIN + ALTER TABLE sqs_usage_records ADD COLUMN queue_name TEXT NOT NULL DEFAULT ''; + EXCEPTION WHEN duplicate_column THEN NULL; + END $$; `) return err } @@ -95,15 +101,15 @@ func autoMigrate() error { // RecordUsage — записывает одну SQS-операцию в таблицу биллинга. // Вызывается асинхронно (горутина) чтобы не добавлять latency к SQS-ответу. // Если billing отключён — no-op. Ошибки логируются, SQS-операция не ломается. -func RecordUsage(tenantID, operation string, msgCount int, msgBytes int64) { +func RecordUsage(tenantID, operation, queueName string, msgCount int, msgBytes int64) { if db == nil { return } go func() { _, err := db.Exec( - `INSERT INTO sqs_usage_records (tenant_id, operation, msg_count, msg_bytes) VALUES ($1, $2, $3, $4)`, - tenantID, operation, msgCount, msgBytes, + `INSERT INTO sqs_usage_records (tenant_id, operation, queue_name, msg_count, msg_bytes) VALUES ($1, $2, $3, $4, $5)`, + tenantID, operation, queueName, msgCount, msgBytes, ) if err != nil { log.Errorf("billing: failed to record usage [%s/%s]: %v", tenantID, operation, err) diff --git a/app/router/router.go b/app/router/router.go index 8f8d71e..d8ef3a8 100644 --- a/app/router/router.go +++ b/app/router/router.go @@ -139,7 +139,12 @@ func actionHandler(w http.ResponseWriter, req *http.Request) { if msgBytes < 0 { msgBytes = 0 } - billing.RecordUsage(t.ID, action, 1, msgBytes) + // Извлекаем имя очереди: из URL path vars или из form-параметра QueueName (CreateQueue) + queueName := mux.Vars(req)["queueName"] + if queueName == "" { + queueName = req.FormValue("QueueName") + } + billing.RecordUsage(t.ID, action, queueName, 1, msgBytes) } } return