Merge pull request 'master' (#23) from master into prod
Reviewed-on: https://gitea.chocomarsh.com/self/monitoring/pulls/23
This commit is contained in:
commit
fbeb173df0
25
app/routers/metrics/increment.go
Normal file
25
app/routers/metrics/increment.go
Normal file
@ -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
|
||||
}
|
@ -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
|
||||
}
|
||||
|
34
app/storage/clickhouse/tables/increments.go
Normal file
34
app/storage/clickhouse/tables/increments.go
Normal file
@ -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()
|
||||
}
|
2
main.go
2
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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user