
All checks were successful
Deploy Dev / Build (pull_request) Successful in 53s
Deploy Dev / Push (pull_request) Successful in 40s
Deploy Dev / Deploy dev (pull_request) Successful in 13s
Deploy Prod / Build (pull_request) Successful in 58s
Deploy Prod / Push (pull_request) Successful in 34s
Deploy Prod / Deploy prod (pull_request) Successful in 12s
115 lines
2.4 KiB
Go
115 lines
2.4 KiB
Go
package storage
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
|
|
"github.com/ClickHouse/clickhouse-go/v2"
|
|
"github.com/ClickHouse/clickhouse-go/v2/lib/driver"
|
|
)
|
|
|
|
var Connection driver.Conn
|
|
|
|
func Connect() error {
|
|
conn, err := connect()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
Connection = *conn
|
|
return nil
|
|
}
|
|
|
|
func Close() {
|
|
Connection.Close()
|
|
}
|
|
|
|
func connect() (*driver.Conn, error) {
|
|
var (
|
|
ctx = context.Background()
|
|
conn, err = clickhouse.Open(&clickhouse.Options{
|
|
Addr: []string{"clickhouse:9000"},
|
|
Auth: clickhouse.Auth{
|
|
Database: "monitoring",
|
|
Username: "default",
|
|
Password: os.Getenv("CLICKHOUSE_PASSWORD"),
|
|
},
|
|
TLS: nil,
|
|
})
|
|
)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err := conn.Ping(ctx); err != nil {
|
|
if exception, ok := err.(*clickhouse.Exception); ok {
|
|
fmt.Printf("Exception [%d] %s \n%s\n", exception.Code, exception.Message, exception.StackTrace)
|
|
}
|
|
return nil, err
|
|
}
|
|
return &conn, nil
|
|
}
|
|
|
|
func Migrate() error {
|
|
err := Connection.Exec(
|
|
context.TODO(),
|
|
`CREATE TABLE IF NOT EXISTS endpoints (
|
|
timestamp DateTime,
|
|
service LowCardinality(String),
|
|
environment LowCardinality(String),
|
|
endpoint LowCardinality(String),
|
|
status_code UInt16,
|
|
response_time_ms UInt32,
|
|
method LowCardinality(String)
|
|
)
|
|
ENGINE = MergeTree
|
|
PARTITION BY toYYYYMM(timestamp)
|
|
ORDER BY (service, environment, endpoint, method, timestamp);`,
|
|
)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
return err
|
|
}
|
|
|
|
err = Connection.Exec(
|
|
context.TODO(),
|
|
`CREATE TABLE IF NOT EXISTS tasks (
|
|
timestamp DateTime,
|
|
service LowCardinality(String),
|
|
environment LowCardinality(String),
|
|
queue LowCardinality(String),
|
|
success Bool,
|
|
execution_time_ms UInt32
|
|
)
|
|
ENGINE = MergeTree
|
|
PARTITION BY toYYYYMM(timestamp)
|
|
ORDER BY (service, environment, queue, timestamp);`,
|
|
)
|
|
if err != nil {
|
|
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
|
|
}
|