Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 失败详情过长时截取并拼接... #101

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions client/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
24 changes: 24 additions & 0 deletions internal/util/convert.go
Original file line number Diff line number Diff line change
@@ -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
}
47 changes: 47 additions & 0 deletions internal/util/convert_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
4 changes: 2 additions & 2 deletions internal/util/process_collect/process_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Loading