Skip to content

Commit

Permalink
feat: sync general monitor config
Browse files Browse the repository at this point in the history
  • Loading branch information
CeerDecy committed Jan 15, 2025
1 parent f7b32d6 commit c683dd0
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
INSERT INTO `sp_monitor_config_register` (`scope`, `scope_id`, `namespace`, `type`, `names`, `filters`, `enable`, `update_time`, `desc`, `hash`) VALUES ('org','','','log','*','[]',1,'2025-01-15 00:00:00','','log_common')
18 changes: 18 additions & 0 deletions internal/tools/monitor/common/db/monitor_config_register.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package db

import (
"errors"
"fmt"

"github.com/jinzhu/gorm"
)
Expand Down Expand Up @@ -49,3 +50,20 @@ func (m *MonitorConfigRegisterDB) ListRegisterByType(tpy string) ([]SpMonitorCon
}
return res, nil

Check warning on line 51 in internal/tools/monitor/common/db/monitor_config_register.go

View check run for this annotation

Codecov / codecov/patch

internal/tools/monitor/common/db/monitor_config_register.go#L51

Added line #L51 was not covered by tests
}

func (m *MonitorConfigRegisterDB) ListRegisterWithFilter(filters map[string]any) ([]SpMonitorConfigRegister, error) {
var res = make([]SpMonitorConfigRegister, 0)
tx := m.Model(&SpMonitorConfigRegister{})
for k, v := range filters {
key := k
value := v
tx = tx.Where(fmt.Sprintf("`%s` = ?", key), value)
}
if err := tx.Find(&res).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return make([]SpMonitorConfigRegister, 0), nil
}
return nil, err

Check warning on line 66 in internal/tools/monitor/common/db/monitor_config_register.go

View check run for this annotation

Codecov / codecov/patch

internal/tools/monitor/common/db/monitor_config_register.go#L54-L66

Added lines #L54 - L66 were not covered by tests
}
return res, nil

Check warning on line 68 in internal/tools/monitor/common/db/monitor_config_register.go

View check run for this annotation

Codecov / codecov/patch

internal/tools/monitor/common/db/monitor_config_register.go#L68

Added line #L68 was not covered by tests
}
25 changes: 23 additions & 2 deletions internal/tools/monitor/core/settings/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,24 @@ func (s *settingsService) monitorConfigMap(ns string) *configDefine {

if ns == "general" {
cd.defaults = map[string]func(langs i18n.LanguageCodes) *pb.ConfigItem{
"logs_ttl": func(langs i18n.LanguageCodes) *pb.ConfigItem {
return &pb.ConfigItem{
Key: "logs_ttl",
Name: s.t.Text(langs, "base") + " " + s.t.Text(langs, "logs_ttl"),
Type: "number",
Value: structpb.NewNumberValue(float64(log.TTL)),
Unit: s.t.Text(langs, "days"),
}
},
"logs_hot_ttl": func(langs i18n.LanguageCodes) *pb.ConfigItem {
return &pb.ConfigItem{
Key: "logs_hot_ttl",
Name: s.t.Text(langs, "base") + " " + s.t.Text(langs, "logs_hot_ttl"),
Type: "number",
Value: structpb.NewNumberValue(float64(log.HotTTL)),
Unit: s.t.Text(langs, "days"),
}
},

Check warning on line 96 in internal/tools/monitor/core/settings/monitor.go

View check run for this annotation

Codecov / codecov/patch

internal/tools/monitor/core/settings/monitor.go#L80-L96

Added lines #L80 - L96 were not covered by tests
"metrics_ttl": func(langs i18n.LanguageCodes) *pb.ConfigItem {
return &pb.ConfigItem{
Key: "metrics_ttl",
Expand Down Expand Up @@ -186,15 +204,18 @@ type monitorConfigRegister struct {
}

func (s *settingsService) generateKey(orgID, ns string) string {
return md5x.SumString(orgID + "/" + ns).String16()
if ns != "" {
return md5x.SumString(orgID + "/" + ns).String16()
}
return md5x.SumString(orgID).String16()

Check warning on line 210 in internal/tools/monitor/core/settings/monitor.go

View check run for this annotation

Codecov / codecov/patch

internal/tools/monitor/core/settings/monitor.go#L206-L210

Added lines #L206 - L210 were not covered by tests
}

func (s *settingsService) updateMonitorConfig(tx *gorm.DB, orgid int64, orgName, ns, group string, keys map[string]interface{}) error {
if ns == "general" {
ns = ""
}
orgID := strconv.FormatInt(orgid, 10)
key := md5x.SumString(orgID + "/" + ns).String16()
key := s.generateKey(orgID, ns)

Check warning on line 218 in internal/tools/monitor/core/settings/monitor.go

View check run for this annotation

Codecov / codecov/patch

internal/tools/monitor/core/settings/monitor.go#L218

Added line #L218 was not covered by tests

ttl := ttl{}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package retention

import (
"fmt"
"github.com/erda-project/erda/pkg/router"
"testing"
)

func TestRouter(t *testing.T) {
matcher := router.New()
matcher.Add("application_*", []*router.KeyValue{
{
Key: "application_key",
Value: "application",
},
}, "application")
matcher.Add("*", []*router.KeyValue{
{
Key: "terminus_key",
Value: "terminus",
},
}, "")

find := matcher.Find("applicatdfsdfffion_sss*", map[string]string{
"terminus_key": "terminus",
//"terminus_value": "123",
})

fmt.Println(find)
}
3 changes: 3 additions & 0 deletions internal/tools/monitor/core/settings/settings.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ type settingsService struct {
}

func (s *settingsService) PutSettingsWithType(ctx context.Context, req *pb.PutSettingsWithTypeRequest) (*pb.PutSettingsWithTypeResponse, error) {
if req.Namespace == "general" {
req.Namespace = ""
}
orgName, err := s.getOrgName(req.OrgID)
if err != nil {
return nil, err
Expand Down
30 changes: 21 additions & 9 deletions internal/tools/monitor/core/settings/sync_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,29 @@ func (p *provider) syncCreateOrgMonitorConfig() error {
p.Log.Errorf("failed to get monitor config register by type: log, err: %v", err)
return err

Check warning on line 52 in internal/tools/monitor/core/settings/sync_config.go

View check run for this annotation

Codecov / codecov/patch

internal/tools/monitor/core/settings/sync_config.go#L49-L52

Added lines #L49 - L52 were not covered by tests
}
registers = make([]db.SpMonitorConfigRegister, 0, len(logRegisters)+len(monitorRegisters))

generalRegisters, err := client.MonitorConfigRegister.ListRegisterWithFilter(map[string]any{
"scope": "org",
"scope_id": "",
"namespace": "",
})
registers = make([]db.SpMonitorConfigRegister, 0, len(logRegisters)+len(monitorRegisters)+len(generalRegisters))
registers = append(registers, monitorRegisters...)
registers = append(registers, logRegisters...)
registers = append(registers, generalRegisters...)

for _, register := range registers {
if !p.isEmptyConfig(&register, org) {

Check warning on line 66 in internal/tools/monitor/core/settings/sync_config.go

View check run for this annotation

Codecov / codecov/patch

internal/tools/monitor/core/settings/sync_config.go#L55-L66

Added lines #L55 - L66 were not covered by tests
continue
}

nsConfig := defaultConfig[register.Namespace]
defConfig := nsConfig["monitor"]
var nsConfig map[string]map[string]*pb.ConfigItem
if register.Namespace == "" {
nsConfig = defaultConfig["general"]
} else {
nsConfig = defaultConfig[register.Namespace]

Check warning on line 74 in internal/tools/monitor/core/settings/sync_config.go

View check run for this annotation

Codecov / codecov/patch

internal/tools/monitor/core/settings/sync_config.go#L70-L74

Added lines #L70 - L74 were not covered by tests
}
monitorConfig := nsConfig["monitor"]

req := &pb.PutSettingsWithTypeRequest{
OrgID: int64(org.ID),
Expand All @@ -78,20 +90,20 @@ func (p *provider) syncCreateOrgMonitorConfig() error {

switch register.Type {
case "log":
ttlItem = defConfig[LogsTTLKey]
hotTTLItem = defConfig[LogsHotTTLKey]
ttlItem = monitorConfig[LogsTTLKey]
hotTTLItem = monitorConfig[LogsHotTTLKey]
case "metric":
ttlItem = defConfig[MetricsTTLKey]
hotTTLItem = defConfig[MetricsHotTTLKey]
ttlItem = monitorConfig[MetricsTTLKey]
hotTTLItem = monitorConfig[MetricsHotTTLKey]

Check warning on line 97 in internal/tools/monitor/core/settings/sync_config.go

View check run for this annotation

Codecov / codecov/patch

internal/tools/monitor/core/settings/sync_config.go#L76-L97

Added lines #L76 - L97 were not covered by tests
}

if ttlItem == nil {
err = fmt.Errorf("ttl item is nil, monitor type: %s", register.Type)
err = fmt.Errorf("ttl item is nil, monitor type: %v", register)
p.Log.Error(err)
return err
}
if hotTTLItem == nil {
err = fmt.Errorf("hot_ttl item is nil, monitor type: %s", register.Type)
err = fmt.Errorf("hot_ttl item is nil, monitor type: %v", register)
p.Log.Error(err)
return err
}

Check warning on line 109 in internal/tools/monitor/core/settings/sync_config.go

View check run for this annotation

Codecov / codecov/patch

internal/tools/monitor/core/settings/sync_config.go#L100-L109

Added lines #L100 - L109 were not covered by tests
Expand Down

0 comments on commit c683dd0

Please sign in to comment.