Merge pull request 'fix' (#24) from master into dev

Reviewed-on: https://gitea.chocomarsh.com/self/monitoring/pulls/24
This commit is contained in:
emmatveev 2025-06-18 15:24:15 +03:00
commit 5838806424
3 changed files with 21 additions and 0 deletions

View File

@ -8,6 +8,7 @@ services:
- clickhouse-development
environment:
CLICKHOUSE_PASSWORD: $CLICKHOUSE_PASSWORD_DEV
STAGE: "development"
deploy:
mode: replicated
restart_policy:

View File

@ -8,6 +8,7 @@ services:
- monitoring
environment:
CLICKHOUSE_PASSWORD: $CLICKHOUSE_PASSWORD_PROD
STAGE: "production"
deploy:
mode: replicated
restart_policy:

19
main.go
View File

@ -7,10 +7,28 @@ import (
increment "monitoring/app/routers/metrics"
task "monitoring/app/routers/metrics"
client "monitoring/app/storage/clickhouse"
endpoints "monitoring/app/storage/clickhouse/tables"
"net/http"
"os"
"time"
)
func writeMetric(timestamp time.Time, endpoint string, statusCode int, responseTime int, method string) {
loc, _ := time.LoadLocation("Europe/Moscow")
err := endpoints.AddEndpointMetric(endpoints.EndpointMetric{
Timestamp: timestamp.In(loc),
Service: "monitoring",
Environment: os.Getenv("STAGE"),
Endpoint: endpoint,
StatusCode: statusCode,
ResponseTime: responseTime,
Method: method,
})
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()
@ -26,6 +44,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))
}
}