From 6f8f64e2f591366143e60d5f0cfacda6659de621 Mon Sep 17 00:00:00 2001 From: LidolLxf <13660354959@163.com> Date: Thu, 26 Oct 2023 15:48:48 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3trace=E6=8A=A5=E9=94=99?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bcs-common/pkg/otel/trace/gin/middleware.go | 7 +++++++ bcs-common/pkg/otel/trace/micro/v2/wrapper.go | 4 ++++ bcs-common/pkg/otel/trace/micro/v4/wrapper.go | 4 ++++ 3 files changed, 15 insertions(+) diff --git a/bcs-common/pkg/otel/trace/gin/middleware.go b/bcs-common/pkg/otel/trace/gin/middleware.go index 01082fe73f..90ded0407b 100644 --- a/bcs-common/pkg/otel/trace/gin/middleware.go +++ b/bcs-common/pkg/otel/trace/gin/middleware.go @@ -19,6 +19,7 @@ import ( "fmt" "io" "net/http" + "strings" "time" "github.com/dustin/go-humanize" @@ -116,6 +117,8 @@ func Middleware(server string, opts ...Option) gin.HandlerFunc { // nolint if len(query) > 1024 { query = fmt.Sprintf("%s...(Total %s)", query[:1024], humanize.Bytes(uint64(len(query)))) } + // 以utf-8方式合法截取字符串 + query = strings.ToValidUTF8(query, "") span.SetAttributes(attribute.Key("query").String(query)) // 记录body @@ -123,6 +126,8 @@ func Middleware(server string, opts ...Option) gin.HandlerFunc { // nolint if len(body) > 1024 { body = fmt.Sprintf("%s...(Total %s)", body[:1024], humanize.Bytes(uint64(len(body)))) } + // 以utf-8方式合法截取字符串 + body = strings.ToValidUTF8(body, "") span.SetAttributes(attribute.Key("body").String(body)) // pass the span through the request context @@ -139,6 +144,8 @@ func Middleware(server string, opts ...Option) gin.HandlerFunc { // nolint if len(respBody) > 1024 { respBody = fmt.Sprintf("%s...(Total %s)", writer.b.String()[:1024], humanize.Bytes(uint64(len(writer.b.String())))) } + // 以utf-8方式合法截取字符串 + respBody = strings.ToValidUTF8(respBody, "") span.SetAttributes(attribute.Key("rsp").String(respBody)) elapsedTime := time.Since(startTime) span.SetAttributes(attribute.Key("elapsed_ime").String(elapsedTime.String())) diff --git a/bcs-common/pkg/otel/trace/micro/v2/wrapper.go b/bcs-common/pkg/otel/trace/micro/v2/wrapper.go index 48bb4a46ce..e8e28f8117 100644 --- a/bcs-common/pkg/otel/trace/micro/v2/wrapper.go +++ b/bcs-common/pkg/otel/trace/micro/v2/wrapper.go @@ -17,6 +17,7 @@ import ( "context" "encoding/json" "fmt" + "strings" "time" "github.com/dustin/go-humanize" @@ -71,6 +72,9 @@ func NewTracingWrapper(fn server.HandlerFunc) server.HandlerFunc { respBody = fmt.Sprintf("%s...(Total %s)", respBody[:1024], humanize.Bytes(uint64(len(respBody)))) } + // 以utf-8方式合法截取字符串 + reqBody = strings.ToValidUTF8(reqBody, "") + respBody = strings.ToValidUTF8(respBody, "") // 设置额外标签 span.SetAttributes(attribute.Key("req").String(reqBody)) span.SetAttributes(attribute.Key("elapsed_ime").String(elapsedTime.String())) diff --git a/bcs-common/pkg/otel/trace/micro/v4/wrapper.go b/bcs-common/pkg/otel/trace/micro/v4/wrapper.go index d600d9020b..0f13ad85dd 100644 --- a/bcs-common/pkg/otel/trace/micro/v4/wrapper.go +++ b/bcs-common/pkg/otel/trace/micro/v4/wrapper.go @@ -18,6 +18,7 @@ import ( "encoding/json" "fmt" "net/http" + "strings" "time" "github.com/dustin/go-humanize" @@ -87,6 +88,9 @@ func NewTracingWrapper() server.HandlerWrapper { respBody = fmt.Sprintf("%s...(Total %s)", respBody[:1024], humanize.Bytes(uint64(len(respBody)))) } + // 以utf-8方式合法截取字符串 + reqBody = strings.ToValidUTF8(reqBody, "") + respBody = strings.ToValidUTF8(respBody, "") // 设置额外标签 span.SetAttributes(attribute.Key("req").String(reqBody)) span.SetAttributes(attribute.Key("elapsed_ime").String(elapsedTime.String()))