go-clickhouse-metrics
More documentation can be found here.
Install
go get git.debtfair.ru/external/go-clickhouse-metrics
Mandatory |
Name |
Type |
Description |
Yes |
Exchange |
string |
Name of ClickHouse exchange in RabbitMQ. |
Yes |
RoutingKey |
string |
Routing key between exchange and queue in RabbitMQ. |
Yes |
BatchSize |
int |
Maximum number of metrics in one RabbitMQ message. |
Yes |
MaxSenders |
int |
Maximum number of parallel senders. |
Yes |
ExportPeriod |
time.Duration |
Period between metrics exporting. |
Yes |
StopTimeout |
time.Duration |
Timeout for last metrics export after graceful stop. |
Yes |
IdleTimeout |
time.Duration |
IDLE timeout for RabbitMQ channel. |
No |
ExportEnabled |
bool |
This setting allows you to disable export. May be helpful in development environment. |
A basic example
package main
import (
"context"
"errors"
"log"
"sync"
"time"
rabbit "git.debtfair.ru/external/amqp-reconnect"
clickhousemetrics "git.debtfair.ru/external/go-clickhouse-metrics"
"github.com/google/uuid"
"github.com/rabbitmq/amqp091-go"
)
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
conn, err := rabbit.NewConnection(ctx, &rabbit.Config{
Host: "localhost",
Port: 5672,
User: "guest",
Password: "guest",
MaxRetries: 1,
Heartbeat: 1 * time.Second,
AmqpCfg: amqp091.Config{
ChannelMax: 0,
FrameSize: 0,
},
})
if err != nil {
log.Default().Fatal(err)
}
collector := clickhousemetrics.New(
clickhousemetrics.Deps{
Rabbit: conn,
},
clickhousemetrics.Config{
BatchSize: 25,
MaxSenders: 72,
Exchange: "clickhouse",
RoutingKey: "metrics",
IdleTimeout: 30 * time.Second,
ExportPeriod: 15 * time.Second,
StopTimeout: 10 * time.Second,
ExportEnabled: true,
},
)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
if err := collector.Collect(ctx); err != nil && !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) {
log.Default().Fatal(err)
}
}()
t := time.Now()
modelId := uuid.New().String()
for i := 0; i < 72; i++ {
wg.Add(1)
go func(index int) {
defer wg.Done()
for j := 0; j < 1000000; j++ {
if err := collector.Add("metric_name", "value", "model", modelId, 1, 0, 1, 1, 14.3, 10.1, false, &t, &t, []string{}); err != nil {
break
}
}
}(i)
}
<-ctx.Done()
collector.Stop()
wg.Wait()
}
Output RabbitMQ message:
{"uuid":"ae8e645a-1115-459a-97b9-3517b1139b32","name":"metric_name","value_string":"value","model_type":"model","model_id":"2db7f373-fdb2-49dc-885f-8602788a3aca","created_at":"2023-07-06 13:29:41","value_datetime":"2023-07-06 13:29:41","created_date":"2023-07-06","value":1,"user_id":0,"company_id":1,"domain_id":1,"value_float":14.3,"duration":10.1,"expirable":false,"labels":[]}
{"uuid":"d44768f9-bf7c-4922-85f8-92244b998706","name":"metric_name","value_string":"value","model_type":"model","model_id":"2db7f373-fdb2-49dc-885f-8602788a3aca","created_at":"2023-07-06 13:29:41","value_datetime":"2023-07-06 13:29:41","created_date":"2023-07-06","value":1,"user_id":0,"company_id":1,"domain_id":1,"value_float":14.3,"duration":10.1,"expirable":false,"labels":[]}
{"uuid":"d9784a89-af12-48ee-a865-99c6b8d03c14","name":"metric_name","value_string":"value","model_type":"model","model_id":"2db7f373-fdb2-49dc-885f-8602788a3aca","created_at":"2023-07-06 13:29:41","value_datetime":"2023-07-06 13:29:41","created_date":"2023-07-06","value":1,"user_id":0,"company_id":1,"domain_id":1,"value_float":14.3,"duration":10.1,"expirable":false,"labels":[]}
{"uuid":"f7e143d1-19a7-4382-87b8-63cb4f341e8d","name":"metric_name","value_string":"value","model_type":"model","model_id":"2db7f373-fdb2-49dc-885f-8602788a3aca","created_at":"2023-07-06 13:29:41","value_datetime":"2023-07-06 13:29:41","created_date":"2023-07-06","value":1,"user_id":0,"company_id":1,"domain_id":1,"value_float":14.3,"duration":10.1,"expirable":false,"labels":[]}
{"uuid":"bc9f7404-5ede-4a3e-a60c-7010dc5ee12f","name":"metric_name","value_string":"value","model_type":"model","model_id":"2db7f373-fdb2-49dc-885f-8602788a3aca","created_at":"2023-07-06 13:29:41","value_datetime":"2023-07-06 13:29:41","created_date":"2023-07-06","value":1,"user_id":0,"company_id":1,"domain_id":1,"value_float":14.3,"duration":10.1,"expirable":false,"labels":[]}
{"uuid":"b06f054b-b72a-4c29-9f90-104d3d34d95e","name":"metric_name","value_string":"value","model_type":"model","model_id":"2db7f373-fdb2-49dc-885f-8602788a3aca","created_at":"2023-07-06 13:29:41","value_datetime":"2023-07-06 13:29:41","created_date":"2023-07-06","value":1,"user_id":0,"company_id":1,"domain_id":1,"value_float":14.3,"duration":10.1,"expirable":false,"labels":[]}
{"uuid":"21376579-ed73-477b-8675-b028aae7500b","name":"metric_name","value_string":"value","model_type":"model","model_id":"2db7f373-fdb2-49dc-885f-8602788a3aca","created_at":"2023-07-06 13:29:41","value_datetime":"2023-07-06 13:29:41","created_date":"2023-07-06","value":1,"user_id":0,"company_id":1,"domain_id":1,"value_float":14.3,"duration":10.1,"expirable":false,"labels":[]}
{"uuid":"99fe6eea-7171-4c08-9088-e65be5fae01b","name":"metric_name","value_string":"value","model_type":"model","model_id":"2db7f373-fdb2-49dc-885f-8602788a3aca","created_at":"2023-07-06 13:29:41","value_datetime":"2023-07-06 13:29:41","created_date":"2023-07-06","value":1,"user_id":0,"company_id":1,"domain_id":1,"value_float":14.3,"duration":10.1,"expirable":false,"labels":[]}
{"uuid":"51394e63-2b64-42c3-b4d5-296b35047cc9","name":"metric_name","value_string":"value","model_type":"model","model_id":"2db7f373-fdb2-49dc-885f-8602788a3aca","created_at":"2023-07-06 13:29:41","value_datetime":"2023-07-06 13:29:41","created_date":"2023-07-06","value":1,"user_id":0,"company_id":1,"domain_id":1,"value_float":14.3,"duration":10.1,"expirable":false,"labels":[]}
{"uuid":"604f9ad4-1372-4f3f-b55c-64024567fe0c","name":"metric_name","value_string":"value","model_type":"model","model_id":"2db7f373-fdb2-49dc-885f-8602788a3aca","created_at":"2023-07-06 13:29:41","value_datetime":"2023-07-06 13:29:41","created_date":"2023-07-06","value":1,"user_id":0,"company_id":1,"domain_id":1,"value_float":14.3,"duration":10.1,"expirable":false,"labels":[]}
{"uuid":"96a3002f-7e5d-479f-8052-cf9ee7dcbfd1","name":"metric_name","value_string":"value","model_type":"model","model_id":"2db7f373-fdb2-49dc-885f-8602788a3aca","created_at":"2023-07-06 13:29:41","value_datetime":"2023-07-06 13:29:41","created_date":"2023-07-06","value":1,"user_id":0,"company_id":1,"domain_id":1,"value_float":14.3,"duration":10.1,"expirable":false,"labels":[]}
{"uuid":"b46a5763-1846-4c18-8d6e-109cc1ae4e02","name":"metric_name","value_string":"value","model_type":"model","model_id":"2db7f373-fdb2-49dc-885f-8602788a3aca","created_at":"2023-07-06 13:29:41","value_datetime":"2023-07-06 13:29:41","created_date":"2023-07-06","value":1,"user_id":0,"company_id":1,"domain_id":1,"value_float":14.3,"duration":10.1,"expirable":false,"labels":[]}
{"uuid":"a0b10be0-1c96-4b55-9d46-02c02b66341c","name":"metric_name","value_string":"value","model_type":"model","model_id":"2db7f373-fdb2-49dc-885f-8602788a3aca","created_at":"2023-07-06 13:29:41","value_datetime":"2023-07-06 13:29:41","created_date":"2023-07-06","value":1,"user_id":0,"company_id":1,"domain_id":1,"value_float":14.3,"duration":10.1,"expirable":false,"labels":[]}
{"uuid":"5ea8c1f4-bc4f-499d-af49-07ebe6d39913","name":"metric_name","value_string":"value","model_type":"model","model_id":"2db7f373-fdb2-49dc-885f-8602788a3aca","created_at":"2023-07-06 13:29:41","value_datetime":"2023-07-06 13:29:41","created_date":"2023-07-06","value":1,"user_id":0,"company_id":1,"domain_id":1,"value_float":14.3,"duration":10.1,"expirable":false,"labels":[]}
{"uuid":"2ec57f7c-eed4-4116-90a7-db1bb3a1148f","name":"metric_name","value_string":"value","model_type":"model","model_id":"2db7f373-fdb2-49dc-885f-8602788a3aca","created_at":"2023-07-06 13:29:41","value_datetime":"2023-07-06 13:29:41","created_date":"2023-07-06","value":1,"user_id":0,"company_id":1,"domain_id":1,"value_float":14.3,"duration":10.1,"expirable":false,"labels":[]}
{"uuid":"5cc95df2-7628-4cde-b2cd-624f7a541c12","name":"metric_name","value_string":"value","model_type":"model","model_id":"2db7f373-fdb2-49dc-885f-8602788a3aca","created_at":"2023-07-06 13:29:41","value_datetime":"2023-07-06 13:29:41","created_date":"2023-07-06","value":1,"user_id":0,"company_id":1,"domain_id":1,"value_float":14.3,"duration":10.1,"expirable":false,"labels":[]}
{"uuid":"22f7f1b7-306c-479f-8988-62804531ca2d","name":"metric_name","value_string":"value","model_type":"model","model_id":"2db7f373-fdb2-49dc-885f-8602788a3aca","created_at":"2023-07-06 13:29:41","value_datetime":"2023-07-06 13:29:41","created_date":"2023-07-06","value":1,"user_id":0,"company_id":1,"domain_id":1,"value_float":14.3,"duration":10.1,"expirable":false,"labels":[]}
{"uuid":"be381fc9-3108-40f3-be85-a20d58d88b41","name":"metric_name","value_string":"value","model_type":"model","model_id":"2db7f373-fdb2-49dc-885f-8602788a3aca","created_at":"2023-07-06 13:29:41","value_datetime":"2023-07-06 13:29:41","created_date":"2023-07-06","value":1,"user_id":0,"company_id":1,"domain_id":1,"value_float":14.3,"duration":10.1,"expirable":false,"labels":[]}
{"uuid":"d50c650b-f351-45eb-9b59-ab77f13d436a","name":"metric_name","value_string":"value","model_type":"model","model_id":"2db7f373-fdb2-49dc-885f-8602788a3aca","created_at":"2023-07-06 13:29:41","value_datetime":"2023-07-06 13:29:41","created_date":"2023-07-06","value":1,"user_id":0,"company_id":1,"domain_id":1,"value_float":14.3,"duration":10.1,"expirable":false,"labels":[]}
{"uuid":"3ff05054-7b02-4d92-9ef9-397c4d7e052e","name":"metric_name","value_string":"value","model_type":"model","model_id":"2db7f373-fdb2-49dc-885f-8602788a3aca","created_at":"2023-07-06 13:29:41","value_datetime":"2023-07-06 13:29:41","created_date":"2023-07-06","value":1,"user_id":0,"company_id":1,"domain_id":1,"value_float":14.3,"duration":10.1,"expirable":false,"labels":[]}
{"uuid":"6eeb87ce-57ac-499b-ad77-4c3ba45d28f2","name":"metric_name","value_string":"value","model_type":"model","model_id":"2db7f373-fdb2-49dc-885f-8602788a3aca","created_at":"2023-07-06 13:29:41","value_datetime":"2023-07-06 13:29:41","created_date":"2023-07-06","value":1,"user_id":0,"company_id":1,"domain_id":1,"value_float":14.3,"duration":10.1,"expirable":false,"labels":[]}
{"uuid":"f0126bb7-7db3-40d8-949e-aa2329523eb4","name":"metric_name","value_string":"value","model_type":"model","model_id":"2db7f373-fdb2-49dc-885f-8602788a3aca","created_at":"2023-07-06 13:29:41","value_datetime":"2023-07-06 13:29:41","created_date":"2023-07-06","value":1,"user_id":0,"company_id":1,"domain_id":1,"value_float":14.3,"duration":10.1,"expirable":false,"labels":[]}
{"uuid":"62f53f5b-0ddc-4148-85bd-a6c07757f92e","name":"metric_name","value_string":"value","model_type":"model","model_id":"2db7f373-fdb2-49dc-885f-8602788a3aca","created_at":"2023-07-06 13:29:41","value_datetime":"2023-07-06 13:29:41","created_date":"2023-07-06","value":1,"user_id":0,"company_id":1,"domain_id":1,"value_float":14.3,"duration":10.1,"expirable":false,"labels":[]}
{"uuid":"596f9988-9973-4626-bdb8-bb7bc47f9216","name":"metric_name","value_string":"value","model_type":"model","model_id":"2db7f373-fdb2-49dc-885f-8602788a3aca","created_at":"2023-07-06 13:29:41","value_datetime":"2023-07-06 13:29:41","created_date":"2023-07-06","value":1,"user_id":0,"company_id":1,"domain_id":1,"value_float":14.3,"duration":10.1,"expirable":false,"labels":[]}
{"uuid":"3798b114-b1a5-4485-a7d1-3c7951901d64","name":"metric_name","value_string":"value","model_type":"model","model_id":"2db7f373-fdb2-49dc-885f-8602788a3aca","created_at":"2023-07-06 13:29:41","value_datetime":"2023-07-06 13:29:41","created_date":"2023-07-06","value":1,"user_id":0,"company_id":1,"domain_id":1,"value_float":14.3,"duration":10.1,"expirable":false,"labels":[]}