From 3cdc41f8645258bcc70fa1cece048dc57d923b08 Mon Sep 17 00:00:00 2001 From: Egor Matveev Date: Sun, 15 Jun 2025 22:51:06 +0300 Subject: [PATCH] fix --- app/routers/take.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/app/routers/take.go b/app/routers/take.go index 1e5bd02..21fd5dd 100644 --- a/app/routers/take.go +++ b/app/routers/take.go @@ -1,9 +1,13 @@ package routers import ( + "bytes" + "fmt" "net/http" + "os" tasks "queues-go/app/storage/mongo/collections" "sync" + "time" ) type TaskResponse struct { @@ -18,6 +22,20 @@ type TakeResponse struct { var MutexMap map[string]*sync.Mutex +func sendLatency(timestamp time.Time, latency int) error { + loc, _ := time.LoadLocation("Europe/Moscow") + s := fmt.Sprintf( + `{"timestamp":"%s","service":"queues","environment":"%s","name":"latency","count":%s}`, + timestamp.In(loc).Format("2006-01-02T15:04:05Z"), + os.Getenv("STAGE"), + latency, + ) + data := []byte(s) + r := bytes.NewReader(data) + _, err := http.Post("http://monitoring:1237/api/v1/metrics/increment", "application/json", r) + return err +} + func Take(r *http.Request) (interface{}, int) { queue := r.Header.Get("queue") mutex, ok := MutexMap[queue] @@ -41,6 +59,8 @@ func Take(r *http.Request) (interface{}, int) { Attempt: task.Attempts, Payload: task.Payload, } + now := time.Now() + go sendLatency(now, int(now.Sub(task.PutAt).Milliseconds())) } return response, http.StatusOK