Merge pull request 'fix' (#10) from master into dev

Reviewed-on: https://gitea.chocomarsh.com/self/monitoring/pulls/10
This commit is contained in:
emmatveev 2025-06-13 02:24:12 +03:00
commit 22e5c275a9
4 changed files with 33 additions and 3 deletions

View File

@ -17,7 +17,7 @@ func AddEndpointMetric (r *http.Request) (interface{}, int) {
err = endpoints.AddEndpointMetric(body)
if err != nil {
log.Printf(err.Error())
log.Print(err.Error())
return nil, http.StatusInternalServerError
}

View File

@ -3,6 +3,7 @@ package storage
import (
"context"
"fmt"
"log"
"os"
"github.com/ClickHouse/clickhouse-go/v2"
@ -20,6 +21,10 @@ func Connect() error {
return nil
}
func Close() {
Connection.Close()
}
func connect() (*driver.Conn, error) {
var (
ctx = context.Background()
@ -45,4 +50,27 @@ func connect() (*driver.Conn, error) {
return nil, err
}
return &conn, nil
}
func Migrate() error {
rows, err := Connection.Query(
context.TODO(),
`CREATE TABLE IF NOT EXISTS endpoints (
timestamp DateTime,
service_name LowCardinality(String),
endpoint LowCardinality(String),
status_code UInt16,
response_time_ms UInt32,
request_method LowCardinality(String),
)
ENGINE = MergeTree
PARTITION BY toYYYYMM(timestamp)
ORDER BY (service_name, endpoint, request_method, timestamp)`,
)
if err != nil {
log.Fatal(err)
return err
}
defer rows.Close()
return nil
}

View File

@ -3,12 +3,13 @@ package storage
import (
"context"
client "monitoring/app/storage/clickhouse"
"time"
"github.com/ClickHouse/clickhouse-go/v2/lib/driver"
)
type EndpointMetric struct {
Timestamp int `json:"timestamp"`
Timestamp time.Time `json:"timestamp"`
Service string `json:"service"`
Endpoint string `json:"endpoint"`
StatusCode int `json:"status_code"`

View File

@ -33,7 +33,8 @@ func main() {
if err != nil {
panic(err)
}
defer client.Close()
http.HandleFunc("/api/v1/metrics/endpoint", handlerWrapper(endpoint.AddEndpointMetric))
log.Printf("Server started")
http.ListenAndServe(":1237", nil)