Compare commits
9 Commits
Author | SHA1 | Date |
---|---|---|
|
2b8897470c | |
|
0af53f260d | |
|
b72ff3ffe8 | |
|
941b60ed00 | |
|
aaee4c64d1 | |
|
54357f64f2 | |
|
f4b03d7e40 | |
|
ad59658962 | |
|
fc1292f24c |
50
server.go
50
server.go
|
@ -19,6 +19,8 @@ import (
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
|
||||||
"go.opentelemetry.io/otel"
|
"go.opentelemetry.io/otel"
|
||||||
|
"go.opentelemetry.io/otel/attribute"
|
||||||
|
"go.opentelemetry.io/otel/codes"
|
||||||
)
|
)
|
||||||
|
|
||||||
const tracerName = "git.bit5.ru/backend/rpc"
|
const tracerName = "git.bit5.ru/backend/rpc"
|
||||||
|
@ -100,11 +102,38 @@ func (server *RPCServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
versionHeader := r.Header.Get(server.versionHeader)
|
versionHeader := r.Header.Get(server.versionHeader)
|
||||||
cltVersion, cltPlatform := parseVersionHeader(versionHeader)
|
cltVersion, cltPlatform := parseVersionHeader(versionHeader)
|
||||||
cltIP := GetIP(r)
|
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)
|
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)
|
||||||
|
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)
|
server.logger.Error(err, "Error reading body", "client_ver", cltVersion, "client_platform", cltPlatform, "client_ip", cltIP, "body_len", payloadLen)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -120,6 +149,25 @@ 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)
|
||||||
|
|
||||||
|
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 {
|
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