Compare commits
No commits in common. "master" and "v1.0.4" have entirely different histories.
50
server.go
50
server.go
|
@ -19,8 +19,6 @@ import (
|
|||
"github.com/google/uuid"
|
||||
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/codes"
|
||||
)
|
||||
|
||||
const tracerName = "git.bit5.ru/backend/rpc"
|
||||
|
@ -102,38 +100,11 @@ func (server *RPCServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
versionHeader := r.Header.Get(server.versionHeader)
|
||||
cltVersion, cltPlatform := parseVersionHeader(versionHeader)
|
||||
cltIP := GetIP(r)
|
||||
|
||||
span.SetAttributes(
|
||||
attribute.String("request.method", r.Method),
|
||||
attribute.String("request.proto", r.Proto),
|
||||
attribute.Int("request.proto_major", r.ProtoMajor),
|
||||
attribute.Int("request.proto_minor", r.ProtoMinor),
|
||||
attribute.Int64("request.content_length", r.ContentLength),
|
||||
attribute.String("request.host", r.Host),
|
||||
attribute.String("request.remote_addr", r.RemoteAddr),
|
||||
attribute.String("request.request_uri", r.RequestURI),
|
||||
attribute.String("client_ver", cltVersion),
|
||||
attribute.Int("client_platform", cltPlatform),
|
||||
attribute.String("client_ip", cltIP.String()),
|
||||
attribute.Bool("can_gzip", canGzip),
|
||||
)
|
||||
|
||||
if url := r.URL; url != nil {
|
||||
span.SetAttributes(
|
||||
attribute.String("request.url.scheme", url.Scheme),
|
||||
attribute.String("request.url.host", url.Host),
|
||||
attribute.String("request.url.path", url.Path),
|
||||
attribute.String("request.url.raw_query", url.RawQuery),
|
||||
)
|
||||
}
|
||||
|
||||
server.logger.V(1).Info("New request", "client_ver", cltVersion)
|
||||
|
||||
payload, err := readRequestBody(ctx, r.Body)
|
||||
payload, err := ioutil.ReadAll(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
|
||||
}
|
||||
|
@ -149,25 +120,6 @@ 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)
|
||||
|
||||
span.SetAttributes(
|
||||
attribute.Int("body_len", len(payload)),
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
span.SetStatus(codes.Error, "Error reading body. 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)
|
||||
|
|
Loading…
Reference in New Issue