From fc1292f24c811164ee86eb0c9e36b90c9eb32745 Mon Sep 17 00:00:00 2001 From: Vladislav Veselskiy Date: Mon, 15 Apr 2024 13:33:41 +0300 Subject: [PATCH] Add func readRequestBody. --- server.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/server.go b/server.go index 63e8784..952420a 100644 --- a/server.go +++ b/server.go @@ -19,6 +19,7 @@ import ( "github.com/google/uuid" "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/codes" ) const tracerName = "git.bit5.ru/backend/rpc" @@ -102,7 +103,7 @@ func (server *RPCServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { cltIP := GetIP(r) server.logger.V(1).Info("New request", "client_ver", cltVersion) - payload, err := ioutil.ReadAll(r.Body) + payload, err := readRequestBody(ctx, r.Body) if err != nil { payloadLen := len(payload) server.logger.Error(err, "Error reading body", "client_ver", cltVersion, "client_platform", cltPlatform, "client_ip", cltIP, "body_len", payloadLen) @@ -120,6 +121,20 @@ func (server *RPCServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { server.writeResponse(canGzip, w, result.Payload) } +func readRequestBody(ctx context.Context, body io.Reader) ([]byte, error) { + _, span := tracer.Start(ctx, "readRequestBody") + defer span.End() + + payload, err := ioutil.ReadAll(body) + if err != nil { + span.SetStatus(codes.Error, "Got error from ioutil.ReadAll.") + span.RecordError(err) + return payload, err + } + + return payload, nil +} + func GetIP(r *http.Request) net.IP { ipStr := GetIPStr(r) return net.ParseIP(ipStr)