Add func readRequestBody.
This commit is contained in:
parent
42a883777c
commit
fc1292f24c
17
server.go
17
server.go
|
@ -19,6 +19,7 @@ import (
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
|
||||||
"go.opentelemetry.io/otel"
|
"go.opentelemetry.io/otel"
|
||||||
|
"go.opentelemetry.io/otel/codes"
|
||||||
)
|
)
|
||||||
|
|
||||||
const tracerName = "git.bit5.ru/backend/rpc"
|
const tracerName = "git.bit5.ru/backend/rpc"
|
||||||
|
@ -102,7 +103,7 @@ func (server *RPCServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
cltIP := GetIP(r)
|
cltIP := GetIP(r)
|
||||||
server.logger.V(1).Info("New request", "client_ver", cltVersion)
|
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 {
|
if err != nil {
|
||||||
payloadLen := len(payload)
|
payloadLen := len(payload)
|
||||||
server.logger.Error(err, "Error reading body", "client_ver", cltVersion, "client_platform", cltPlatform, "client_ip", cltIP, "body_len", payloadLen)
|
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)
|
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 {
|
func GetIP(r *http.Request) net.IP {
|
||||||
ipStr := GetIPStr(r)
|
ipStr := GetIPStr(r)
|
||||||
return net.ParseIP(ipStr)
|
return net.ParseIP(ipStr)
|
||||||
|
|
Loading…
Reference in New Issue