From d11ae23369c07c27366d28828bfadcc98ca34d17 Mon Sep 17 00:00:00 2001 From: Egor Matveev Date: Fri, 13 Jun 2025 23:25:34 +0300 Subject: [PATCH] fix --- app/routers/metrics/task.go | 25 ++++++++++++++++ app/storage/clickhouse/tables/endpoints.go | 12 ++++---- app/storage/clickhouse/tables/tasks.go | 34 ++++++++++++++++++++++ main.go | 6 ++-- 4 files changed, 69 insertions(+), 8 deletions(-) create mode 100644 app/routers/metrics/task.go create mode 100644 app/storage/clickhouse/tables/tasks.go diff --git a/app/routers/metrics/task.go b/app/routers/metrics/task.go new file mode 100644 index 0000000..409bfcb --- /dev/null +++ b/app/routers/metrics/task.go @@ -0,0 +1,25 @@ +package routers + +import ( + "encoding/json" + "log" + tasks "monitoring/app/storage/clickhouse/tables" + "net/http" +) + +func AddTaskMetric(r *http.Request) (interface{}, int) { + d := json.NewDecoder(r.Body) + body := tasks.TaskMetric{} + err := d.Decode(&body) + if err != nil { + return nil, http.StatusBadRequest + } + + err = tasks.AddTaskMetric(body) + if err != nil { + log.Print(err.Error()) + return nil, http.StatusInternalServerError + } + + return nil, http.StatusAccepted +} diff --git a/app/storage/clickhouse/tables/endpoints.go b/app/storage/clickhouse/tables/endpoints.go index 01b800e..13636e1 100644 --- a/app/storage/clickhouse/tables/endpoints.go +++ b/app/storage/clickhouse/tables/endpoints.go @@ -9,12 +9,12 @@ import ( ) type EndpointMetric struct { - Timestamp time.Time `json:"timestamp"` - Service string `json:"service"` - Endpoint string `json:"endpoint"` - StatusCode int `json:"status_code"` - ResponseTime int `json:"response_time"` - Method string `json:"method"` + Timestamp time.Time `json:"timestamp"` + Service string `json:"service"` + Endpoint string `json:"endpoint"` + StatusCode int `json:"status_code"` + ResponseTime int `json:"response_time"` + Method string `json:"method"` } func AddEndpointMetric(metric EndpointMetric) error { diff --git a/app/storage/clickhouse/tables/tasks.go b/app/storage/clickhouse/tables/tasks.go new file mode 100644 index 0000000..3b03485 --- /dev/null +++ b/app/storage/clickhouse/tables/tasks.go @@ -0,0 +1,34 @@ +package storage + +import ( + "context" + "time" +) + +type TaskMetric struct { + Timestamp time.Time `json:"timestamp"` + Service string `json:"service"` + Queue string `json:"queue"` + Success bool `json:"success"` + ExecutionTimeMs int `json:"execution_time_ms"` +} + +func AddTaskMetric(metric TaskMetric) error { + batch, err := connection().PrepareBatch(context.Background(), "INSERT INTO tasks") + if err != nil { + return err + } + + err = batch.Append( + metric.Timestamp, + metric.Service, + metric.Queue, + metric.Success, + metric.ExecutionTimeMs, + ) + if err != nil { + return err + } + + return batch.Send() +} diff --git a/main.go b/main.go index 3c886d1..20a8ed4 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "encoding/json" "log" endpoint "monitoring/app/routers/metrics" + task "monitoring/app/routers/metrics" client "monitoring/app/storage/clickhouse" "net/http" "time" @@ -39,8 +40,9 @@ func main() { if err != nil { panic(err) } - + http.HandleFunc("/api/v1/metrics/endpoint", handlerWrapper(endpoint.AddEndpointMetric)) + http.HandleFunc("/api/v1/metrics/task", handlerWrapper(task.AddTaskMetric)) log.Printf("Server started") http.ListenAndServe(":1237", nil) -} \ No newline at end of file +}