Add func readRequestBody.

This commit is contained in:
Владислав Весельский 2024-04-15 13:33:41 +03:00
parent 42a883777c
commit fc1292f24c
1 changed files with 16 additions and 1 deletions

View File

@ -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)