From ad596589622fc6a463cef6879640224982916ff7 Mon Sep 17 00:00:00 2001 From: Vladislav Veselskiy Date: Mon, 15 Apr 2024 13:42:49 +0300 Subject: [PATCH] Add span attr "body_len" --- server.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/server.go b/server.go index 952420a..99f06c5 100644 --- a/server.go +++ b/server.go @@ -19,6 +19,7 @@ import ( "github.com/google/uuid" "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/codes" ) @@ -106,6 +107,8 @@ func (server *RPCServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { payload, err := readRequestBody(ctx, r.Body) if err != nil { payloadLen := len(payload) + span.SetStatus(codes.Error, "Got error from readRequestBody.") + span.RecordError(err) server.logger.Error(err, "Error reading body", "client_ver", cltVersion, "client_platform", cltPlatform, "client_ip", cltIP, "body_len", payloadLen) return } @@ -126,8 +129,13 @@ func readRequestBody(ctx context.Context, body io.Reader) ([]byte, error) { defer span.End() payload, err := ioutil.ReadAll(body) + + span.SetAttributes( + attribute.Int("body_len", len(payload)), + ) + if err != nil { - span.SetStatus(codes.Error, "Got error from ioutil.ReadAll.") + span.SetStatus(codes.Error, "Error reading body. Got error from ioutil.ReadAll.") span.RecordError(err) return payload, err }