fix
This commit is contained in:
parent
4d2e0198f3
commit
ff040e1a6d
@ -6,9 +6,11 @@ services:
|
||||
image: mathwave/sprint-repo:queues
|
||||
networks:
|
||||
- queues-development
|
||||
- monitoring
|
||||
environment:
|
||||
MONGO_HOST: "mongo.develop.sprinthub.ru"
|
||||
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
|
||||
STAGE: "development"
|
||||
deploy:
|
||||
mode: replicated
|
||||
restart_policy:
|
||||
@ -22,3 +24,5 @@ services:
|
||||
networks:
|
||||
queues-development:
|
||||
external: true
|
||||
monitoring:
|
||||
external: true
|
||||
|
@ -6,9 +6,11 @@ services:
|
||||
image: mathwave/sprint-repo:queues
|
||||
networks:
|
||||
- queues
|
||||
- monitoring
|
||||
environment:
|
||||
MONGO_HOST: "mongo.sprinthub.ru"
|
||||
MONGO_PASSWORD: $MONGO_PASSWORD_PROD
|
||||
STAGE: "production"
|
||||
deploy:
|
||||
mode: replicated
|
||||
restart_policy:
|
||||
@ -22,3 +24,5 @@ services:
|
||||
networks:
|
||||
queues:
|
||||
external: true
|
||||
monitoring:
|
||||
external: true
|
||||
|
23
main.go
23
main.go
@ -1,15 +1,37 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"queues-go/app/routers"
|
||||
client "queues-go/app/storage/mongo"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
func writeMetric(timestamp time.Time, endpoint string, statusCode int, responseTime int, method string) {
|
||||
s := fmt.Sprintf(
|
||||
`{"timestamp":"%s","service":"queues","environment":"%s","endpoint":"%s","status_code":%s,"response_time":%s,"method":"%s"}`,
|
||||
timestamp.Format("2006-01-02T15:04:05Z"),
|
||||
os.Getenv("STAGE"),
|
||||
endpoint,
|
||||
strconv.Itoa(statusCode),
|
||||
strconv.Itoa(responseTime),
|
||||
method,
|
||||
)
|
||||
data := []byte(s)
|
||||
r := bytes.NewReader(data)
|
||||
_, err := http.Post("http://monitoring:1237/api/v1/metrics/endpoint", "application/json", r)
|
||||
if err != nil {
|
||||
log.Printf("Error sending metrics %s", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func handlerWrapper(f func(*http.Request) (interface{}, int)) func(http.ResponseWriter, *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
start := time.Now()
|
||||
@ -25,6 +47,7 @@ func handlerWrapper(f func(*http.Request) (interface{}, int)) func(http.Response
|
||||
} else {
|
||||
w.WriteHeader(status)
|
||||
}
|
||||
go writeMetric(start, r.URL.Path, status, int(time.Since(start).Milliseconds()), r.Method)
|
||||
log.Printf("%s %d %s", r.URL, status, time.Since(start))
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user