Skip to content

Commit

Permalink
解决trace报错的问题 (#2709)
Browse files Browse the repository at this point in the history
  • Loading branch information
LidolLxf authored Nov 3, 2023
1 parent 6d503cf commit 8ec56ea
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
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

0 comments on commit 8ec56ea

Please sign in to comment.