diff --git a/.deploy/deploy-dev.yaml b/.deploy/deploy-dev.yaml index 3d1a93a..8a7cf66 100644 --- a/.deploy/deploy-dev.yaml +++ b/.deploy/deploy-dev.yaml @@ -8,6 +8,7 @@ services: - clickhouse-development environment: CLICKHOUSE_PASSWORD: $CLICKHOUSE_PASSWORD_DEV + STAGE: "development" deploy: mode: replicated restart_policy: diff --git a/.deploy/deploy-prod.yaml b/.deploy/deploy-prod.yaml index 51a8e20..41a7755 100644 --- a/.deploy/deploy-prod.yaml +++ b/.deploy/deploy-prod.yaml @@ -8,6 +8,7 @@ services: - monitoring environment: CLICKHOUSE_PASSWORD: $CLICKHOUSE_PASSWORD_PROD + STAGE: "production" deploy: mode: replicated restart_policy: diff --git a/main.go b/main.go index 92c1fa9..f2e2d64 100644 --- a/main.go +++ b/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)) } }