update loki publish

This commit is contained in:
Liujian
2024-12-09 00:17:50 +08:00
parent d80bfe61df
commit bc00f2d577
6 changed files with 121 additions and 64 deletions
+104 -5
View File
@@ -4,6 +4,13 @@ import (
"context"
"encoding/json"
"errors"
"time"
log_driver "github.com/APIParkLab/APIPark/log-driver"
"github.com/APIParkLab/APIPark/gateway"
"github.com/eolinker/go-common/store"
"gorm.io/gorm"
@@ -18,10 +25,58 @@ import (
var _ ILogModule = (*imlLogModule)(nil)
type imlLogModule struct {
service log.ILogService `autowired:""`
service log.ILogService `autowired:""`
clusterService cluster.IClusterService `autowired:""`
transaction store.ITransaction `autowired:""`
}
var labels = map[string]string{
"cluster": "$cluster",
"node": "$node",
}
var logFormatter = map[string]interface{}{
"fields": []string{
"$msec",
"$service",
"$provider",
"$scheme as request_scheme",
"$url as request_uri",
"$host as request_host",
"$header as request_header",
"$remote_addr",
"$request_body",
"$proxy_body",
"$proxy_method",
"$proxy_scheme",
"$proxy_uri",
"$api",
"$proxy_host",
"$proxy_header",
"$proxy_addr",
"$response_headers",
"$status",
"$content_type",
"$proxy_status",
"$request_time",
"$response_time",
"$node",
"$cluster",
"$application",
"$src_ip",
"$block_name as strategy",
"$request_id",
"$request_method",
"$authorization",
"$response_body",
"$proxy_response_body",
},
}
func (i *imlLogModule) Save(ctx context.Context, driver string, input *log_dto.Save) error {
factory, has := log_driver.GetFactory(driver)
if !has {
return errors.New("driver not found")
}
input.Cluster = cluster.DefaultClusterID
var cfg *string
if input.Config != nil {
@@ -29,10 +84,54 @@ func (i *imlLogModule) Save(ctx context.Context, driver string, input *log_dto.S
tmp := string(data)
cfg = &tmp
}
return i.service.UpdateLogSource(ctx, driver, &log.Save{
ID: input.ID,
Cluster: &input.Cluster,
Config: cfg,
return i.transaction.Transaction(ctx, func(txCtx context.Context) error {
err := i.service.UpdateLogSource(ctx, driver, &log.Save{
ID: input.ID,
Cluster: &input.Cluster,
Config: cfg,
})
if err != nil {
return err
}
info, err := i.service.GetLogSource(ctx, driver)
if err != nil {
return err
}
d, c, err := factory.Create(info.Config)
if err != nil {
return err
}
client, err := i.clusterService.GatewayClient(ctx, input.Cluster)
if err != nil {
return err
}
dynamicClient, err := client.Dynamic(driver)
if err != nil {
return err
}
attr := make(map[string]interface{})
attr["driver"] = driver
attr["formatter"] = logFormatter
attr["labels"] = labels
attr["method"] = "POST"
for k, v := range c {
attr[k] = v
}
err = dynamicClient.Online(ctx, &gateway.DynamicRelease{
BasicItem: &gateway.BasicItem{
ID: driver,
Description: "collect access log",
Version: time.Now().Format("20060102150405"),
Resource: gateway.ProfessionOutput,
},
Attr: attr,
})
if err != nil {
return err
}
log_driver.SetDriver(driver, d)
return nil
})
}