intiail
This commit is contained in:
49
app/storage/clickhouse/client.go
Normal file
49
app/storage/clickhouse/client.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
|
||||
"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 connect() (*driver.Conn, error) {
|
||||
var (
|
||||
ctx = context.Background()
|
||||
conn, err = clickhouse.Open(&clickhouse.Options{
|
||||
Addr: []string{"clickhouse:9440"},
|
||||
Auth: clickhouse.Auth{
|
||||
Database: "monitoring",
|
||||
Username: "default",
|
||||
},
|
||||
TLS: &tls.Config{
|
||||
InsecureSkipVerify: true,
|
||||
},
|
||||
})
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
42
app/storage/clickhouse/tables/endpoints.go
Normal file
42
app/storage/clickhouse/tables/endpoints.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"context"
|
||||
client "monitoring/app/storage/clickhouse"
|
||||
|
||||
"github.com/ClickHouse/clickhouse-go/v2/lib/driver"
|
||||
)
|
||||
|
||||
type EndpointMetric struct {
|
||||
Timestamp int `json:"timestamp"`
|
||||
Service string `json:"service"`
|
||||
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.Endpoint,
|
||||
metric.StatusCode,
|
||||
metric.ResponseTime,
|
||||
metric.Method,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return batch.Send()
|
||||
}
|
||||
|
||||
func connection() driver.Conn {
|
||||
return client.Connection
|
||||
}
|
||||
Reference in New Issue
Block a user