From f2ab91cbced9a069759844c701c422253b226e73 Mon Sep 17 00:00:00 2001 From: Egor Matveev Date: Sun, 15 Jun 2025 22:24:06 +0300 Subject: [PATCH 1/2] fix --- app/routers/metrics/increment.go | 25 +++++++++++++++ app/storage/clickhouse/tables/increments.go | 34 +++++++++++++++++++++ main.go | 2 ++ 3 files changed, 61 insertions(+) create mode 100644 app/routers/metrics/increment.go create mode 100644 app/storage/clickhouse/tables/increments.go diff --git a/app/routers/metrics/increment.go b/app/routers/metrics/increment.go new file mode 100644 index 0000000..5e3502b --- /dev/null +++ b/app/routers/metrics/increment.go @@ -0,0 +1,25 @@ +package routers + +import ( + "encoding/json" + "log" + increments "monitoring/app/storage/clickhouse/tables" + "net/http" +) + +func AddIncrementMetric(r *http.Request) (interface{}, int) { + d := json.NewDecoder(r.Body) + body := increments.IncrementMetric{} + err := d.Decode(&body) + if err != nil { + return nil, http.StatusBadRequest + } + + err = increments.AddIncrementMetric(body) + if err != nil { + log.Print(err.Error()) + return nil, http.StatusInternalServerError + } + + return nil, http.StatusAccepted +} diff --git a/app/storage/clickhouse/tables/increments.go b/app/storage/clickhouse/tables/increments.go new file mode 100644 index 0000000..4f0123b --- /dev/null +++ b/app/storage/clickhouse/tables/increments.go @@ -0,0 +1,34 @@ +package storage + +import ( + "context" + "time" +) + +type IncrementMetric struct { + Timestamp time.Time `json:"timestamp"` + Service string `json:"service"` + Environment string `json:"environment"` + Name string `json:"name"` + Count int `json:"count"` +} + +func AddIncrementMetric(metric IncrementMetric) error { + batch, err := connection().PrepareBatch(context.Background(), "INSERT INTO increments") + if err != nil { + return err + } + + err = batch.Append( + metric.Timestamp, + metric.Service, + metric.Environment, + metric.Name, + metric.Count, + ) + if err != nil { + return err + } + + return batch.Send() +} diff --git a/main.go b/main.go index 20a8ed4..92c1fa9 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "encoding/json" "log" endpoint "monitoring/app/routers/metrics" + increment "monitoring/app/routers/metrics" task "monitoring/app/routers/metrics" client "monitoring/app/storage/clickhouse" "net/http" @@ -43,6 +44,7 @@ func main() { http.HandleFunc("/api/v1/metrics/endpoint", handlerWrapper(endpoint.AddEndpointMetric)) http.HandleFunc("/api/v1/metrics/task", handlerWrapper(task.AddTaskMetric)) + http.HandleFunc("/api/v1/metrics/increment", handlerWrapper(increment.AddIncrementMetric)) log.Printf("Server started") http.ListenAndServe(":1237", nil) } -- 2.45.2 From 502c767a2ae58950e485c2368000dd1f6e468a4a Mon Sep 17 00:00:00 2001 From: Egor Matveev Date: Sun, 15 Jun 2025 22:35:04 +0300 Subject: [PATCH 2/2] fix --- app/storage/clickhouse/client.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/app/storage/clickhouse/client.go b/app/storage/clickhouse/client.go index 5c1e37a..f2f067e 100644 --- a/app/storage/clickhouse/client.go +++ b/app/storage/clickhouse/client.go @@ -91,5 +91,24 @@ func Migrate() error { log.Fatal(err) return err } + + err = Connection.Exec( + context.TODO(), + `CREATE TABLE IF NOT EXISTS increments ( + timestamp DateTime, + service LowCardinality(String), + environment LowCardinality(String), + name LowCardinality(String), + count UInt16 + ) + ENGINE = MergeTree + PARTITION BY toYYYYMM(timestamp) + ORDER BY (service, environment, name, timestamp);`, + ) + if err != nil { + log.Fatal(err) + return err + } + return nil } -- 2.45.2