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

解决trace报错的问题 #2709

Merged
merged 1 commit into from
Nov 3, 2023
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
7 changes: 7 additions & 0 deletions bcs-common/pkg/otel/trace/gin/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"io"
"net/http"
"strings"
"time"

"github.com/dustin/go-humanize"
Expand Down Expand Up @@ -116,13 +117,17 @@ 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
body := string(getRequestBody(c.Request))
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
Expand All @@ -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()))
Expand Down
4 changes: 4 additions & 0 deletions bcs-common/pkg/otel/trace/micro/v2/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"context"
"encoding/json"
"fmt"
"strings"
"time"

"github.com/dustin/go-humanize"
Expand Down Expand Up @@ -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()))
Expand Down
4 changes: 4 additions & 0 deletions bcs-common/pkg/otel/trace/micro/v4/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"strings"
"time"

"github.com/dustin/go-humanize"
Expand Down Expand Up @@ -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()))
Expand Down