fix #25
@ -8,6 +8,7 @@ services:
|
||||
- clickhouse-development
|
||||
environment:
|
||||
CLICKHOUSE_PASSWORD: $CLICKHOUSE_PASSWORD_DEV
|
||||
STAGE: "development"
|
||||
deploy:
|
||||
mode: replicated
|
||||
restart_policy:
|
||||
|
@ -8,6 +8,7 @@ services:
|
||||
- monitoring
|
||||
environment:
|
||||
CLICKHOUSE_PASSWORD: $CLICKHOUSE_PASSWORD_PROD
|
||||
STAGE: "production"
|
||||
deploy:
|
||||
mode: replicated
|
||||
restart_policy:
|
||||
|
19
main.go
19
main.go
@ -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))
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user