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(net/ghttp&gclient,contrib/rpc/grpcx): remove request and response contents in opentelemetry tracing attributes #3810

Merged
merged 1 commit into from
Sep 26, 2024
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
2 changes: 0 additions & 2 deletions contrib/rpc/grpcx/internal/tracing/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@ const (
tracingInstrumentGrpcClient = "github.com/gogf/gf/contrib/rpc/grpcx/v2/krpc.GrpcClient"
tracingInstrumentGrpcServer = "github.com/gogf/gf/contrib/rpc/grpcx/v2/krpc.GrpcServer"
tracingEventGrpcRequest = "grpc.request"
tracingEventGrpcRequestMessage = "grpc.request.message"
tracingEventGrpcRequestBaggage = "grpc.request.baggage"
tracingEventGrpcMetadataOutgoing = "grpc.metadata.outgoing"
tracingEventGrpcMetadataIncoming = "grpc.metadata.incoming"
tracingEventGrpcResponse = "grpc.response"
tracingEventGrpcResponseMessage = "grpc.response.message"
)

type metadataSupplier struct {
Expand Down
16 changes: 0 additions & 16 deletions contrib/rpc/grpcx/internal/tracing/tracing_interceptor_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"google.golang.org/grpc/status"

"github.com/gogf/gf/contrib/rpc/grpcx/v2/internal/grpcctx"
"github.com/gogf/gf/contrib/rpc/grpcx/v2/internal/utils"
"github.com/gogf/gf/v2"
"github.com/gogf/gf/v2/net/gtrace"
"github.com/gogf/gf/v2/util/gconv"
Expand Down Expand Up @@ -59,25 +58,10 @@ func UnaryClientInterceptor(ctx context.Context, method string, req, reply inter
span.AddEvent(tracingEventGrpcRequest, trace.WithAttributes(
attribute.String(tracingEventGrpcRequestBaggage, gconv.String(gtrace.GetBaggageMap(ctx))),
attribute.String(tracingEventGrpcMetadataOutgoing, gconv.String(grpcctx.Ctx{}.OutgoingMap(ctx))),
attribute.String(
tracingEventGrpcRequestMessage,
utils.MarshalMessageToJsonStringForTracing(
req, "Request", tracingMaxContentLogSize,
),
),
))

err := invoker(ctx, method, req, reply, cc, callOpts...)

span.AddEvent(tracingEventGrpcResponse, trace.WithAttributes(
attribute.String(
tracingEventGrpcResponseMessage,
utils.MarshalMessageToJsonStringForTracing(
reply, "Response", tracingMaxContentLogSize,
),
),
))

if err != nil {
s, _ := status.FromError(err)
span.SetStatus(codes.Error, s.Message())
Expand Down
16 changes: 0 additions & 16 deletions contrib/rpc/grpcx/internal/tracing/tracing_interceptor_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"google.golang.org/grpc/status"

"github.com/gogf/gf/contrib/rpc/grpcx/v2/internal/grpcctx"
"github.com/gogf/gf/contrib/rpc/grpcx/v2/internal/utils"
"github.com/gogf/gf/v2"
"github.com/gogf/gf/v2/net/gtrace"
"github.com/gogf/gf/v2/util/gconv"
Expand Down Expand Up @@ -58,25 +57,10 @@ func UnaryServerInterceptor(ctx context.Context, req interface{}, info *grpc.Una
span.AddEvent(tracingEventGrpcRequest, trace.WithAttributes(
attribute.String(tracingEventGrpcRequestBaggage, gconv.String(gtrace.GetBaggageMap(ctx))),
attribute.String(tracingEventGrpcMetadataIncoming, gconv.String(grpcctx.Ctx{}.IncomingMap(ctx))),
attribute.String(
tracingEventGrpcRequestMessage,
utils.MarshalMessageToJsonStringForTracing(
req, "Request", tracingMaxContentLogSize,
),
),
))

res, err := handler(ctx, req)

span.AddEvent(tracingEventGrpcResponse, trace.WithAttributes(
attribute.String(
tracingEventGrpcResponseMessage,
utils.MarshalMessageToJsonStringForTracing(
res, "Response", tracingMaxContentLogSize,
),
),
))

if err != nil {
s, _ := status.FromError(err)
span.SetStatus(codes.Error, s.Message())
Expand Down
18 changes: 4 additions & 14 deletions net/gclient/gclient_observability.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package gclient
import (
"context"
"fmt"
"io"
"net/http"
"net/http/httptrace"

Expand All @@ -21,7 +20,6 @@ import (

"github.com/gogf/gf/v2"
"github.com/gogf/gf/v2/internal/httputil"
"github.com/gogf/gf/v2/internal/utils"
"github.com/gogf/gf/v2/net/gtrace"
"github.com/gogf/gf/v2/os/gctx"
"github.com/gogf/gf/v2/os/gmetric"
Expand All @@ -39,10 +37,8 @@ const (
tracingEventHttpRequest = "http.request"
tracingEventHttpRequestHeaders = "http.request.headers"
tracingEventHttpRequestBaggage = "http.request.baggage"
tracingEventHttpRequestBody = "http.request.body"
tracingEventHttpResponse = "http.response"
tracingEventHttpResponseHeaders = "http.response.headers"
tracingEventHttpResponseBody = "http.response.body"
tracingMiddlewareHandled gctx.StrKey = `MiddlewareClientTracingHandled`
)

Expand Down Expand Up @@ -101,18 +97,12 @@ func internalMiddlewareObservability(c *Client, r *http.Request) (response *Resp
if response == nil || response.Response == nil {
return
}
// TODO ignore binary content ReadAll, for example downloading request.
reqBodyContentBytes, _ := io.ReadAll(response.Body)
response.Body = utils.NewReadCloser(reqBodyContentBytes, false)

resBodyContent, err := gtrace.SafeContentForHttp(reqBodyContentBytes, response.Header)
if err != nil {
span.SetStatus(codes.Error, fmt.Sprintf(`converting safe content failed: %s`, err.Error()))
}

span.AddEvent(tracingEventHttpResponse, trace.WithAttributes(
attribute.String(tracingEventHttpResponseHeaders, gconv.String(httputil.HeaderToMap(response.Header))),
attribute.String(tracingEventHttpResponseBody, resBodyContent),
attribute.String(
tracingEventHttpResponseHeaders,
gconv.String(httputil.HeaderToMap(response.Header)),
),
))
return
}
6 changes: 0 additions & 6 deletions net/gclient/gclient_tracer_tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,14 +218,8 @@ func (ct *clientTracerTracing) WroteRequest(info httptrace.WroteRequestInfo) {
ct.span.SetStatus(codes.Error, fmt.Sprintf(`%+v`, info.Err))
}

reqBodyContent, err := gtrace.SafeContentForHttp(ct.requestBody, ct.request.Header)
if err != nil {
ct.span.SetStatus(codes.Error, fmt.Sprintf(`converting safe content failed: %s`, err.Error()))
}

ct.span.AddEvent(tracingEventHttpRequest, trace.WithAttributes(
attribute.String(tracingEventHttpRequestHeaders, gconv.String(ct.headers)),
attribute.String(tracingEventHttpRequestBaggage, gtrace.GetBaggageMap(ct.Context).String()),
attribute.String(tracingEventHttpRequestBody, reqBodyContent),
))
}
33 changes: 5 additions & 28 deletions net/ghttp/ghttp_middleware_tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package ghttp
import (
"context"
"fmt"
"io"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
Expand All @@ -18,9 +17,7 @@ import (
"go.opentelemetry.io/otel/trace"

"github.com/gogf/gf/v2"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/internal/httputil"
"github.com/gogf/gf/v2/internal/utils"
"github.com/gogf/gf/v2/net/gtrace"
"github.com/gogf/gf/v2/os/gctx"
"github.com/gogf/gf/v2/util/gconv"
Expand All @@ -31,10 +28,8 @@ const (
tracingEventHttpRequest = "http.request"
tracingEventHttpRequestHeaders = "http.request.headers"
tracingEventHttpRequestBaggage = "http.request.baggage"
tracingEventHttpRequestBody = "http.request.body"
tracingEventHttpResponse = "http.response"
tracingEventHttpResponseHeaders = "http.response.headers"
tracingEventHttpResponseBody = "http.response.body"
tracingEventHttpRequestUrl = "http.request.url"
tracingMiddlewareHandled gctx.StrKey = `MiddlewareServerTracingHandled`
)
Expand Down Expand Up @@ -80,24 +75,10 @@ func internalMiddlewareServerTracing(r *Request) {
return
}

// Request content logging.
reqBodyContentBytes, err := io.ReadAll(r.Body)
if err != nil {
r.SetError(gerror.Wrap(err, `read request body failed`))
span.SetStatus(codes.Error, fmt.Sprintf(`%+v`, err))
return
}
r.Body = utils.NewReadCloser(reqBodyContentBytes, false)
reqBodyContent, err := gtrace.SafeContentForHttp(reqBodyContentBytes, r.Header)
if err != nil {
span.SetStatus(codes.Error, fmt.Sprintf(`converting safe content failed: %s`, err.Error()))
}

span.AddEvent(tracingEventHttpRequest, trace.WithAttributes(
attribute.String(tracingEventHttpRequestUrl, r.URL.String()),
attribute.String(tracingEventHttpRequestHeaders, gconv.String(httputil.HeaderToMap(r.Header))),
attribute.String(tracingEventHttpRequestBaggage, gtrace.GetBaggageMap(ctx).String()),
attribute.String(tracingEventHttpRequestBody, reqBodyContent),
))

// Continue executing.
Expand All @@ -109,18 +90,14 @@ func internalMiddlewareServerTracing(r *Request) {
}

// Error logging.
if err = r.GetError(); err != nil {
if err := r.GetError(); err != nil {
span.SetStatus(codes.Error, fmt.Sprintf(`%+v`, err))
}

// Response content logging.
resBodyContent, err := gtrace.SafeContentForHttp(r.Response.Buffer(), r.Response.Header())
if err != nil {
span.SetStatus(codes.Error, fmt.Sprintf(`converting safe content failed: %s`, err.Error()))
}

span.AddEvent(tracingEventHttpResponse, trace.WithAttributes(
attribute.String(tracingEventHttpResponseHeaders, gconv.String(httputil.HeaderToMap(r.Response.Header()))),
attribute.String(tracingEventHttpResponseBody, resBodyContent),
attribute.String(
tracingEventHttpResponseHeaders,
gconv.String(httputil.HeaderToMap(r.Response.Header())),
),
))
}
Loading