Compare commits
No commits in common. "ff4df20d2ae0d0a1dea74b2edb118466560b5281" and "58388064248ae81e73e2ddc9447bc44d11a826c2" have entirely different histories.
ff4df20d2a
...
5838806424
@ -3,24 +3,23 @@ package routers
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"log"
|
"log"
|
||||||
client "monitoring/app/storage/clickhouse"
|
|
||||||
endpoints "monitoring/app/storage/clickhouse/tables"
|
endpoints "monitoring/app/storage/clickhouse/tables"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
func AddEndpointMetric(r *http.Request) (interface{}, int) {
|
func AddEndpointMetric (r *http.Request) (interface{}, int) {
|
||||||
d := json.NewDecoder(r.Body)
|
d := json.NewDecoder(r.Body)
|
||||||
body := client.EndpointMetric{}
|
body := endpoints.EndpointMetric{}
|
||||||
err := d.Decode(&body)
|
err := d.Decode(&body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, http.StatusBadRequest
|
return nil, http.StatusBadRequest
|
||||||
}
|
}
|
||||||
|
|
||||||
endpoints.AddEndpointMetric(body)
|
err = endpoints.AddEndpointMetric(body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print(err.Error())
|
log.Print(err.Error())
|
||||||
return nil, http.StatusInternalServerError
|
return nil, http.StatusInternalServerError
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, http.StatusAccepted
|
return nil, http.StatusAccepted
|
||||||
}
|
}
|
@ -3,20 +3,19 @@ package routers
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"log"
|
"log"
|
||||||
client "monitoring/app/storage/clickhouse"
|
|
||||||
increments "monitoring/app/storage/clickhouse/tables"
|
increments "monitoring/app/storage/clickhouse/tables"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
func AddIncrementMetric(r *http.Request) (interface{}, int) {
|
func AddIncrementMetric(r *http.Request) (interface{}, int) {
|
||||||
d := json.NewDecoder(r.Body)
|
d := json.NewDecoder(r.Body)
|
||||||
body := client.IncrementMetric{}
|
body := increments.IncrementMetric{}
|
||||||
err := d.Decode(&body)
|
err := d.Decode(&body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, http.StatusBadRequest
|
return nil, http.StatusBadRequest
|
||||||
}
|
}
|
||||||
|
|
||||||
increments.AddIncrementMetric(body)
|
err = increments.AddIncrementMetric(body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print(err.Error())
|
log.Print(err.Error())
|
||||||
return nil, http.StatusInternalServerError
|
return nil, http.StatusInternalServerError
|
||||||
|
@ -3,20 +3,19 @@ package routers
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"log"
|
"log"
|
||||||
client "monitoring/app/storage/clickhouse"
|
|
||||||
tasks "monitoring/app/storage/clickhouse/tables"
|
tasks "monitoring/app/storage/clickhouse/tables"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
func AddTaskMetric(r *http.Request) (interface{}, int) {
|
func AddTaskMetric(r *http.Request) (interface{}, int) {
|
||||||
d := json.NewDecoder(r.Body)
|
d := json.NewDecoder(r.Body)
|
||||||
body := client.TaskMetric{}
|
body := tasks.TaskMetric{}
|
||||||
err := d.Decode(&body)
|
err := d.Decode(&body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, http.StatusBadRequest
|
return nil, http.StatusBadRequest
|
||||||
}
|
}
|
||||||
|
|
||||||
tasks.AddTaskMetric(body)
|
err = tasks.AddTaskMetric(body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print(err.Error())
|
log.Print(err.Error())
|
||||||
return nil, http.StatusInternalServerError
|
return nil, http.StatusInternalServerError
|
||||||
|
@ -5,47 +5,12 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/ClickHouse/clickhouse-go/v2"
|
"github.com/ClickHouse/clickhouse-go/v2"
|
||||||
"github.com/ClickHouse/clickhouse-go/v2/lib/driver"
|
"github.com/ClickHouse/clickhouse-go/v2/lib/driver"
|
||||||
)
|
)
|
||||||
|
|
||||||
type EndpointMetric struct {
|
|
||||||
Timestamp time.Time `json:"timestamp"`
|
|
||||||
Service string `json:"service"`
|
|
||||||
Environment string `json:"environment"`
|
|
||||||
Endpoint string `json:"endpoint"`
|
|
||||||
StatusCode int `json:"status_code"`
|
|
||||||
ResponseTime int `json:"response_time"`
|
|
||||||
Method string `json:"method"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type IncrementMetric struct {
|
|
||||||
Timestamp time.Time `json:"timestamp"`
|
|
||||||
Service string `json:"service"`
|
|
||||||
Environment string `json:"environment"`
|
|
||||||
Name string `json:"name"`
|
|
||||||
Count int `json:"count"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type TaskMetric struct {
|
|
||||||
Timestamp time.Time `json:"timestamp"`
|
|
||||||
Service string `json:"service"`
|
|
||||||
Environment string `json:"environment"`
|
|
||||||
Queue string `json:"queue"`
|
|
||||||
Success bool `json:"success"`
|
|
||||||
ExecutionTimeMs int `json:"execution_time_ms"`
|
|
||||||
}
|
|
||||||
|
|
||||||
var Connection driver.Conn
|
var Connection driver.Conn
|
||||||
var EndpointsCol []EndpointMetric
|
|
||||||
var EndpointsMutex sync.Mutex
|
|
||||||
var IncrementsCol []IncrementMetric
|
|
||||||
var IncrementsMutex sync.Mutex
|
|
||||||
var TasksCol []TaskMetric
|
|
||||||
var TasksMutex sync.Mutex
|
|
||||||
|
|
||||||
func Connect() error {
|
func Connect() error {
|
||||||
conn, err := connect()
|
conn, err := connect()
|
||||||
@ -53,13 +18,6 @@ func Connect() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
Connection = *conn
|
Connection = *conn
|
||||||
EndpointsCol = make([]EndpointMetric, 0)
|
|
||||||
IncrementsCol = make([]IncrementMetric, 0)
|
|
||||||
TasksCol = make([]TaskMetric, 0)
|
|
||||||
EndpointsMutex = sync.Mutex{}
|
|
||||||
IncrementsMutex = sync.Mutex{}
|
|
||||||
TasksMutex = sync.Mutex{}
|
|
||||||
go pushMetrics()
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -94,111 +52,6 @@ func connect() (*driver.Conn, error) {
|
|||||||
return &conn, nil
|
return &conn, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func pushEndpoints() error {
|
|
||||||
if len(EndpointsCol) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
EndpointsMutex.Lock()
|
|
||||||
newCollection := EndpointsCol
|
|
||||||
EndpointsCol = make([]EndpointMetric, 0)
|
|
||||||
EndpointsMutex.Unlock()
|
|
||||||
|
|
||||||
batch, err := Connection.PrepareBatch(context.Background(), "INSERT INTO endpoints")
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, metric := range newCollection {
|
|
||||||
err := batch.Append(
|
|
||||||
metric.Timestamp,
|
|
||||||
metric.Service,
|
|
||||||
metric.Environment,
|
|
||||||
metric.Endpoint,
|
|
||||||
metric.StatusCode,
|
|
||||||
metric.ResponseTime,
|
|
||||||
metric.Method,
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return batch.Send()
|
|
||||||
}
|
|
||||||
|
|
||||||
func pushIncrements() error {
|
|
||||||
if len(IncrementsCol) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
IncrementsMutex.Lock()
|
|
||||||
newCollection := IncrementsCol
|
|
||||||
IncrementsCol = make([]IncrementMetric, 0)
|
|
||||||
IncrementsMutex.Unlock()
|
|
||||||
|
|
||||||
batch, err := Connection.PrepareBatch(context.Background(), "INSERT INTO increments")
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, metric := range newCollection {
|
|
||||||
err := batch.Append(
|
|
||||||
metric.Timestamp,
|
|
||||||
metric.Service,
|
|
||||||
metric.Environment,
|
|
||||||
metric.Name,
|
|
||||||
metric.Count,
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return batch.Send()
|
|
||||||
}
|
|
||||||
|
|
||||||
func pushTasks() error {
|
|
||||||
if len(IncrementsCol) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
TasksMutex.Lock()
|
|
||||||
newCollection := TasksCol
|
|
||||||
TasksCol = make([]TaskMetric, 0)
|
|
||||||
TasksMutex.Unlock()
|
|
||||||
|
|
||||||
batch, err := Connection.PrepareBatch(context.Background(), "INSERT INTO tasks")
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, metric := range newCollection {
|
|
||||||
err := batch.Append(
|
|
||||||
metric.Timestamp,
|
|
||||||
metric.Service,
|
|
||||||
metric.Environment,
|
|
||||||
metric.Queue,
|
|
||||||
metric.Success,
|
|
||||||
metric.ExecutionTimeMs,
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return batch.Send()
|
|
||||||
}
|
|
||||||
|
|
||||||
func pushMetrics() {
|
|
||||||
for {
|
|
||||||
pushEndpoints()
|
|
||||||
pushIncrements()
|
|
||||||
pushTasks()
|
|
||||||
time.Sleep(time.Second)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func Migrate() error {
|
func Migrate() error {
|
||||||
err := Connection.Exec(
|
err := Connection.Exec(
|
||||||
context.TODO(),
|
context.TODO(),
|
||||||
|
@ -1,11 +1,45 @@
|
|||||||
package storage
|
package storage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
client "monitoring/app/storage/clickhouse"
|
client "monitoring/app/storage/clickhouse"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/ClickHouse/clickhouse-go/v2/lib/driver"
|
||||||
)
|
)
|
||||||
|
|
||||||
func AddEndpointMetric(metric client.EndpointMetric) {
|
type EndpointMetric struct {
|
||||||
client.EndpointsMutex.Lock()
|
Timestamp time.Time `json:"timestamp"`
|
||||||
defer client.EndpointsMutex.Unlock()
|
Service string `json:"service"`
|
||||||
client.EndpointsCol = append(client.EndpointsCol, metric)
|
Environment string `json:"environment"`
|
||||||
|
Endpoint string `json:"endpoint"`
|
||||||
|
StatusCode int `json:"status_code"`
|
||||||
|
ResponseTime int `json:"response_time"`
|
||||||
|
Method string `json:"method"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func AddEndpointMetric(metric EndpointMetric) error {
|
||||||
|
batch, err := connection().PrepareBatch(context.Background(), "INSERT INTO endpoints")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = batch.Append(
|
||||||
|
metric.Timestamp,
|
||||||
|
metric.Service,
|
||||||
|
metric.Environment,
|
||||||
|
metric.Endpoint,
|
||||||
|
metric.StatusCode,
|
||||||
|
metric.ResponseTime,
|
||||||
|
metric.Method,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return batch.Send()
|
||||||
|
}
|
||||||
|
|
||||||
|
func connection() driver.Conn {
|
||||||
|
return client.Connection
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,34 @@
|
|||||||
package storage
|
package storage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
client "monitoring/app/storage/clickhouse"
|
"context"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func AddIncrementMetric(metric client.IncrementMetric) {
|
type IncrementMetric struct {
|
||||||
client.IncrementsMutex.Lock()
|
Timestamp time.Time `json:"timestamp"`
|
||||||
defer client.IncrementsMutex.Unlock()
|
Service string `json:"service"`
|
||||||
client.IncrementsCol = append(client.IncrementsCol, metric)
|
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()
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,36 @@
|
|||||||
package storage
|
package storage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
client "monitoring/app/storage/clickhouse"
|
"context"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func AddTaskMetric(metric client.TaskMetric) {
|
type TaskMetric struct {
|
||||||
client.TasksMutex.Lock()
|
Timestamp time.Time `json:"timestamp"`
|
||||||
defer client.TasksMutex.Unlock()
|
Service string `json:"service"`
|
||||||
client.TasksCol = append(client.TasksCol, metric)
|
Environment string `json:"environment"`
|
||||||
|
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.Environment,
|
||||||
|
metric.Queue,
|
||||||
|
metric.Success,
|
||||||
|
metric.ExecutionTimeMs,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return batch.Send()
|
||||||
}
|
}
|
||||||
|
8
main.go
8
main.go
@ -14,8 +14,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func writeMetric(timestamp time.Time, endpoint string, statusCode int, responseTime int, method string) {
|
func writeMetric(timestamp time.Time, endpoint string, statusCode int, responseTime int, method string) {
|
||||||
endpoints.AddEndpointMetric(client.EndpointMetric{
|
loc, _ := time.LoadLocation("Europe/Moscow")
|
||||||
Timestamp: timestamp.Add(time.Hour * 3),
|
err := endpoints.AddEndpointMetric(endpoints.EndpointMetric{
|
||||||
|
Timestamp: timestamp.In(loc),
|
||||||
Service: "monitoring",
|
Service: "monitoring",
|
||||||
Environment: os.Getenv("STAGE"),
|
Environment: os.Getenv("STAGE"),
|
||||||
Endpoint: endpoint,
|
Endpoint: endpoint,
|
||||||
@ -23,6 +24,9 @@ func writeMetric(timestamp time.Time, endpoint string, statusCode int, responseT
|
|||||||
ResponseTime: responseTime,
|
ResponseTime: responseTime,
|
||||||
Method: method,
|
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) {
|
func handlerWrapper(f func(*http.Request) (interface{}, int)) func(http.ResponseWriter, *http.Request) {
|
||||||
|
Loading…
Reference in New Issue
Block a user