Skip to content

Commit

Permalink
fix: agent group api responses invalid int value
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhengYa-0110 authored and SongZhen0704 committed Dec 12, 2024
1 parent 3e91c8f commit 3c264b0
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 54 deletions.
11 changes: 9 additions & 2 deletions server/agent_config/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (m *Upgrader) lowerToHigher(lowerVerData interface{}, ancestor string, high
lowers := m.higherVerToLowerVerKeys[higher]
if len(lowers) > 1 {
for _, lower := range lowers {
log.Warnf("%s has been upgraded to %s", lower, higher) // TODO return?
log.Warnf("%s has been upgraded to %s, please configure it manually", lower, higher) // TODO return?
}
} else {
m.setNestedValue(higherVerData, higher, m.fmtLowerVersionValue(newAncestor, value))
Expand Down Expand Up @@ -199,6 +199,13 @@ func (m *Upgrader) fmtLowerVersionValue(longKey string, value interface{}) inter
return value
}
}
// TODO
// 升级 static_config.ebpf.uprobe-process-name-regexs.golang-symbol 时,需要:
// 1. 将 inputs.proc.symbol_table.golang_specific.enabled 设置为 true
// 2. 新增一条 inputs.proc.process_matcher
// 升级 static_config.os-proc-sync-tagged-only 时,需要:
// 1. 将 inputs.proc.process_matcher 里所有的 only_with_tag 设置为 static_config.os-proc-sync-tagged-only

return m.convDictData(longKey, value)
}

Expand Down Expand Up @@ -552,7 +559,7 @@ func (p *MigrationToolData) fmtDictValKeyMap() {
// "only_with_tag": "static_config.os-proc-sync-tagged-only", // TODO
"ignore": "action",
"rewrite_name": "rewrite-name",
// "enabled_features": []string{"static_config.ebpf.on-cpu-profile.regex", "static_config.ebpf.off-cpu-profile.regex"}, // 无法降级
// "enabled_features": []string{"static_config.ebpf.on-cpu-profile.regex", "static_config.ebpf.off-cpu-profile.regex"}, // 无法升降级
},

"inputs.cbpf.af_packet.bond_interfaces": {
Expand Down
83 changes: 39 additions & 44 deletions server/agent_config/template_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ func (f *DataFomatter) LoadMapData(data map[string]interface{}) error {
return fmt.Errorf("unmarshal yaml to node error: %v", err)
}

err = f.stringToDictValue(f.mapData, "", f.keyToComment)
err = f.fmtVal("", f.mapData, f.keyToComment, false)
if err != nil {
return fmt.Errorf("convert dict value to string error: %v", err)
}
Expand Down Expand Up @@ -603,17 +603,12 @@ func (f *DataFomatter) mapToJSON(keyToComment KeyToComment) ([]byte, error) {
}

func (f *DataFomatter) fmtMapValAndRefresh(keyToComment KeyToComment, dictValToStr bool) error {
var err error
if dictValToStr {
err = f.dictValueToString(f.mapData, "", keyToComment)
} else {
err = f.stringToDictValue(f.mapData, "", keyToComment)
}
err := f.fmtVal("", f.mapData, keyToComment, dictValToStr)
if err != nil {
return fmt.Errorf("convert dict value to string error: %v", err)
}
// refresh yamlNode if changes dict value
if _, ok := keyToComment["changesDictValue"]; ok {
if _, ok := keyToComment["valueChanged"]; ok {
yamlBytes, err := yaml.Marshal(f.mapData)
if err != nil {
return fmt.Errorf("marshal map to yaml error: %v", err)
Expand All @@ -628,54 +623,47 @@ func (f *DataFomatter) fmtMapValAndRefresh(keyToComment KeyToComment, dictValToS
return nil
}

func (f *DataFomatter) dictValueToString(data interface{}, ancestors string, keyToComment KeyToComment) error {
switch data := data.(type) {
case map[string]interface{}:
for key, value := range data {
newAncestors := f.appendAncestor(ancestors, key)
if !f.isKeyComment(key) {
if f.isDictValue(keyToComment[newAncestors]) {
valueStr, err := f.dictToString(value)
if err != nil {
return fmt.Errorf("convert dict value to string error: %v, key: %s", err, newAncestors)
}
data[key] = valueStr
keyToComment["changesDictValue"] = make(map[string]interface{})
}
f.dictValueToString(value, newAncestors, keyToComment)
}
}
default:
return nil
}
return nil
}

func (f *DataFomatter) stringToDictValue(data interface{}, ancestors string, keyToComment KeyToComment) error {
func (f *DataFomatter) fmtVal(ancestors string, data interface{}, keyToComment KeyToComment, dictValToStr bool) error {
switch data := data.(type) {
case map[string]interface{}:
for key, value := range data {
newAncestors := f.appendAncestor(ancestors, key)
if !f.isKeyComment(key) {
if f.isDictValue(keyToComment[newAncestors]) {
if strings.HasPrefix(strings.TrimSpace(value.(string)), "-") {
var valueMap map[string]interface{}
err := yaml.Unmarshal([]byte(key+":\n"+value.(string)), &valueMap)
if dictValToStr {
valueStr, err := f.dictToString(value)
if err != nil {
return fmt.Errorf("unmarshal string to map error: %v, key: %s", err, newAncestors)
return fmt.Errorf("convert dict value to string error: %v, key: %s", err, newAncestors)
}
data[key] = valueMap[key]
data[key] = valueStr
} else {
var valueMap map[string]interface{}
err := yaml.Unmarshal([]byte(value.(string)), &valueMap)
if err != nil {
return fmt.Errorf("unmarshal string to map error: %v, key: %s", err, newAncestors)
if strings.HasPrefix(strings.TrimSpace(value.(string)), "-") {
var valueMap map[string]interface{}
err := yaml.Unmarshal([]byte(key+":\n"+value.(string)), &valueMap)
if err != nil {
return fmt.Errorf("unmarshal string to map error: %v, key: %s", err, newAncestors)
}
data[key] = valueMap[key]
} else {
var valueMap map[string]interface{}
err := yaml.Unmarshal([]byte(value.(string)), &valueMap)
if err != nil {
return fmt.Errorf("unmarshal string to map error: %v, key: %s", err, newAncestors)
}
data[key] = valueMap
}
data[key] = valueMap
}
keyToComment["changesDictValue"] = make(map[string]interface{})
keyToComment["valueChanged"] = make(map[string]interface{})
} else if f.isIntValue(keyToComment[newAncestors]) {
switch value := value.(type) {
case int:
data[key] = value
case float64:
data[key] = int(value)
keyToComment["valueChanged"] = make(map[string]interface{})
}
}
f.stringToDictValue(value, newAncestors, keyToComment)
f.fmtVal(newAncestors, value, keyToComment, dictValToStr)
}
}
default:
Expand Down Expand Up @@ -722,3 +710,10 @@ func (f *DataFomatter) isDictValue(comment map[string]interface{}) bool {
}
return false
}

func (f *DataFomatter) isIntValue(comment map[string]interface{}) bool {
if _, ok := comment["type"]; ok {
return comment["type"] == "int"
}
return false
}
10 changes: 5 additions & 5 deletions server/agent_config/template_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ outputs:
if err := dataFmt.LoadYAMLData(tt.args.yamlData); err != nil {
t.Fatalf("Failed to init yaml data: %v", err)
}
err := dataFmt.dictValueToString(dataFmt.mapData, "", keyToComment)
err := dataFmt.fmtVal("", dataFmt.mapData, keyToComment, true)
if (err != nil) != tt.wantErr {
t.Errorf("DictValueToString() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down Expand Up @@ -796,7 +796,7 @@ outputs:
} else {
dataFmt.mapData = tt.args.mapData
}
err := dataFmt.stringToDictValue(dataFmt.mapData, "", keyToComment)
err := dataFmt.fmtVal("", dataFmt.mapData, keyToComment, false)
if (err != nil) != tt.wantErr {
t.Errorf("stringToDictValue() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down Expand Up @@ -1238,9 +1238,9 @@ func TestConvertJSONToYAMLAndValidate(t *testing.T) {
},
}
for _, tt := range tests {
// if tt.name != "case02" {
// continue
// }
if tt.name != "case06" {
continue
}
t.Run(tt.name, func(t *testing.T) {
got, err := ConvertJSONToYAMLAndValidate(tt.args.jsonData)
if (err != nil) != tt.wantErr {
Expand Down
2 changes: 1 addition & 1 deletion server/controller/http/service/agent_group_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (a *AgentGroupConfig) GetAgentGroupConfigTemplateJson() ([]byte, error) {
// TODO get from ck
l7Protocols := []string{
"HTTP", "HTTP2", "Dubbo", "gRPC", "SOFARPC", "FastCGI", "bRPC", "Tars", "Some/IP", "MySQL", "PostgreSQL",
"Oracle", "Redis", "MongoDB", "Kafka", "MQTT", "AMQP", "OpenWire", "NATS", "Pulsar", "ZMTP", "DNS", "TLS", "Custom"}
"Oracle", "Redis", "MongoDB", "Memcached", "Kafka", "MQTT", "AMQP", "OpenWire", "NATS", "Pulsar", "ZMTP", "DNS", "TLS", "Custom"}
l7ProtocolsYamlBytes, err := yaml.Marshal(l7Protocols)
if err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions server/controller/recorder/statsd/statsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (r *DomainStatsd) GetMetadata() *common.Metadata {
}

func (r *DomainStatsd) Start() {
log.Infof("start statsd", r.md.Domain.Lcuuid, r.md.LogPrefixes)
log.Info("start statsd", r.md.LogPrefixes)
err := stats.RegisterCountableWithModulePrefix(
counterModulePrefix,
counterModuleTypeCloudTaskCost,
Expand Down Expand Up @@ -159,7 +159,7 @@ func (r *SubDomainStatsd) GetMetadata() *common.Metadata {
}

func (r *SubDomainStatsd) Start() {
log.Infof("start statsd", r.md.LogPrefixes)
log.Info("start statsd", r.md.LogPrefixes)
err := stats.RegisterCountableWithModulePrefix(
counterModulePrefix,
counterModuleTypeResourceSyncDelay,
Expand Down

0 comments on commit 3c264b0

Please sign in to comment.