Merge pull request 'fix' (#16) from master into dev
Reviewed-on: https://gitea.chocomarsh.com/self/monitoring/pulls/16
This commit is contained in:
commit
828eca445a
25
app/routers/metrics/task.go
Normal file
25
app/routers/metrics/task.go
Normal file
@ -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
|
||||||
|
}
|
@ -9,12 +9,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type EndpointMetric struct {
|
type EndpointMetric struct {
|
||||||
Timestamp time.Time `json:"timestamp"`
|
Timestamp time.Time `json:"timestamp"`
|
||||||
Service string `json:"service"`
|
Service string `json:"service"`
|
||||||
Endpoint string `json:"endpoint"`
|
Endpoint string `json:"endpoint"`
|
||||||
StatusCode int `json:"status_code"`
|
StatusCode int `json:"status_code"`
|
||||||
ResponseTime int `json:"response_time"`
|
ResponseTime int `json:"response_time"`
|
||||||
Method string `json:"method"`
|
Method string `json:"method"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func AddEndpointMetric(metric EndpointMetric) error {
|
func AddEndpointMetric(metric EndpointMetric) error {
|
||||||
|
34
app/storage/clickhouse/tables/tasks.go
Normal file
34
app/storage/clickhouse/tables/tasks.go
Normal file
@ -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()
|
||||||
|
}
|
2
main.go
2
main.go
@ -4,6 +4,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"log"
|
"log"
|
||||||
endpoint "monitoring/app/routers/metrics"
|
endpoint "monitoring/app/routers/metrics"
|
||||||
|
task "monitoring/app/routers/metrics"
|
||||||
client "monitoring/app/storage/clickhouse"
|
client "monitoring/app/storage/clickhouse"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
@ -41,6 +42,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
http.HandleFunc("/api/v1/metrics/endpoint", handlerWrapper(endpoint.AddEndpointMetric))
|
http.HandleFunc("/api/v1/metrics/endpoint", handlerWrapper(endpoint.AddEndpointMetric))
|
||||||
|
http.HandleFunc("/api/v1/metrics/task", handlerWrapper(task.AddTaskMetric))
|
||||||
log.Printf("Server started")
|
log.Printf("Server started")
|
||||||
http.ListenAndServe(":1237", nil)
|
http.ListenAndServe(":1237", nil)
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user