diff --git a/client/client.go b/client/client.go index f75081faa..ec3426530 100644 --- a/client/client.go +++ b/client/client.go @@ -387,6 +387,7 @@ func (c *client) buildVas() (*kit.Vas, context.CancelFunc) { // nolint // sendClientMessaging 发送客户端连接信息 func (c *client) sendClientMessaging(vas *kit.Vas, meta *sfs.SideAppMeta, annotations map[string]interface{}) error { + meta.FailedDetailReason = util.TruncateString(meta.FailedDetailReason, 1024) clientInfoPayload := sfs.VersionChangePayload{ BasicData: &sfs.BasicData{ BizID: c.opts.bizID, diff --git a/client/types.go b/client/types.go index 0876eba7f..d905e1d42 100644 --- a/client/types.go +++ b/client/types.go @@ -385,6 +385,7 @@ func (r *Release) Execute(steps ...Function) error { // sendVersionChangeMessaging 发送客户端版本变更信息 func (r *Release) sendVersionChangeMessaging(bd *sfs.BasicData) error { + r.AppMate.FailedDetailReason = util.TruncateString(r.AppMate.FailedDetailReason, 1024) pullPayload := sfs.VersionChangePayload{ BasicData: bd, Application: r.AppMate, diff --git a/internal/util/convert.go b/internal/util/convert.go new file mode 100644 index 000000000..4f2701b00 --- /dev/null +++ b/internal/util/convert.go @@ -0,0 +1,24 @@ +/* + * Tencent is pleased to support the open source community by making Blueking Container Service available. + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * Licensed under the MIT License (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * http://opensource.org/licenses/MIT + * Unless required by applicable law or agreed to in writing, software distributed under + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific language governing permissions and + * limitations under the License. + */ + +package util + +// TruncateString It accepts a string s and an integer maxLength as parameters, +// If the length of string s is greater than maxLength, the first maxLength characters are +// truncated and '...' is appended to the end. +// If the length of string s does not exceed maxLength, the string s is returned directly. +func TruncateString(s string, maxLength int) string { + if len(s) > maxLength { + return s[:maxLength] + "..." + } + return s +} diff --git a/internal/util/convert_test.go b/internal/util/convert_test.go new file mode 100644 index 000000000..8b5009d64 --- /dev/null +++ b/internal/util/convert_test.go @@ -0,0 +1,47 @@ +/* + * Tencent is pleased to support the open source community by making Blueking Container Service available. + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * Licensed under the MIT License (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * http://opensource.org/licenses/MIT + * Unless required by applicable law or agreed to in writing, software distributed under + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific language governing permissions and + * limitations under the License. + */ + +package util + +import ( + "fmt" + "strings" + "testing" +) + +func TestTruncateString(t *testing.T) { + // 使用 strings.Builder 高效地构建长字符串 + var builder strings.Builder + + // 设置生成字符串的长度 + length := 1024 + + // 使用循环生成一个包含 1500 个字符的字符串 + for i := 0; i < length; i++ { + builder.WriteString("a") // 每次写入一个字符 'a' + } + + // 获取生成的字符串 + longString := builder.String() + + // 打印字符串的长度 + fmt.Println("字符串长度:", longString) + fmt.Println("字符串长度:", len(longString)) + + // 设置最大长度 + maxLength := 1024 + + // 调用截断函数 + result := TruncateString(longString, maxLength) + + fmt.Println(result) +} diff --git a/internal/util/process_collect/process_collector.go b/internal/util/process_collect/process_collector.go index 6bb870191..9be994e6d 100644 --- a/internal/util/process_collect/process_collector.go +++ b/internal/util/process_collect/process_collector.go @@ -74,7 +74,7 @@ func setCpuUsage(usage float64) { if cpuUsage > cpuMaxUsage { cpuMaxUsage = cpuUsage } - if cpuUsage > 0 && cpuUsage < cpuMinUsage { + if cpuUsage < cpuMinUsage || cpuMinUsage == 0 { cpuMinUsage = cpuUsage } cpuTotalUsage += cpuUsage @@ -87,7 +87,7 @@ func setMemUsage(usage uint64) { if memoryUsage > memoryMaxUsage { memoryMaxUsage = memoryUsage } - if memoryUsage > 0 && memoryUsage < memoryMinUsage { + if memoryUsage < memoryMinUsage || memoryMinUsage == 0 { memoryMinUsage = memoryUsage } memoryTotalUsage += memoryUsage