Compare commits
13 Commits
Author | SHA1 | Date |
---|---|---|
|
2b8897470c | |
|
0af53f260d | |
|
b72ff3ffe8 | |
|
941b60ed00 | |
|
aaee4c64d1 | |
|
54357f64f2 | |
|
f4b03d7e40 | |
|
ad59658962 | |
|
fc1292f24c | |
|
42a883777c | |
|
d51fbcc9c5 | |
|
a9def2a926 | |
|
ac3bd6c2bf |
5
go.mod
5
go.mod
|
@ -5,6 +5,7 @@ go 1.13
|
||||||
require (
|
require (
|
||||||
git.bit5.ru/backend/errors v1.0.0
|
git.bit5.ru/backend/errors v1.0.0
|
||||||
git.bit5.ru/backend/meta v1.0.0
|
git.bit5.ru/backend/meta v1.0.0
|
||||||
github.com/go-logr/logr v1.2.3
|
github.com/go-logr/logr v1.4.1
|
||||||
github.com/google/uuid v1.3.0
|
github.com/google/uuid v1.6.0
|
||||||
|
go.opentelemetry.io/otel v1.11.1
|
||||||
)
|
)
|
||||||
|
|
19
go.sum
19
go.sum
|
@ -7,10 +7,16 @@ git.bit5.ru/backend/msgpack v1.0.0/go.mod h1:Syf8E+3pr9z3TropB/eN4PJUekRg5ZD/0sH
|
||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0=
|
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
|
||||||
github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
|
github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
|
||||||
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
|
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
|
||||||
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
|
||||||
|
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
|
||||||
|
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
|
||||||
|
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
|
||||||
|
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||||
|
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||||
|
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
@ -18,14 +24,19 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
github.com/stretchr/testify v1.7.3 h1:dAm0YRdRQlWojc3CrCRgPBzG5f941d0zvAKu7qY4e+I=
|
|
||||||
github.com/stretchr/testify v1.7.3/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
github.com/stretchr/testify v1.7.3/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||||
|
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
|
||||||
|
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||||
github.com/ugorji/go v1.2.7 h1:qYhyWUUd6WbiM+C6JZAUkIJt/1WrjzNHY9+KCIjVqTo=
|
github.com/ugorji/go v1.2.7 h1:qYhyWUUd6WbiM+C6JZAUkIJt/1WrjzNHY9+KCIjVqTo=
|
||||||
github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6M=
|
github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6M=
|
||||||
github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0=
|
github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0=
|
||||||
github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY=
|
github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY=
|
||||||
github.com/vmihailenco/bufio v0.0.0-20140618134113-fe7b595919de h1:U+I4zEVstMdfNES/2UO8iqkIf214SDMRhdaFTE3A5rA=
|
github.com/vmihailenco/bufio v0.0.0-20140618134113-fe7b595919de h1:U+I4zEVstMdfNES/2UO8iqkIf214SDMRhdaFTE3A5rA=
|
||||||
github.com/vmihailenco/bufio v0.0.0-20140618134113-fe7b595919de/go.mod h1:ghSGoeEoFFkXNguSget72dMA0+OLq3AGZiqRohVojxI=
|
github.com/vmihailenco/bufio v0.0.0-20140618134113-fe7b595919de/go.mod h1:ghSGoeEoFFkXNguSget72dMA0+OLq3AGZiqRohVojxI=
|
||||||
|
go.opentelemetry.io/otel v1.11.1 h1:4WLLAmcfkmDk2ukNXJyq3/kiz/3UzCaYq6PskJsaou4=
|
||||||
|
go.opentelemetry.io/otel v1.11.1/go.mod h1:1nNhXBbWSD0nsL38H6btgnFN2k4i0sNLHNNMZMSbUGE=
|
||||||
|
go.opentelemetry.io/otel/trace v1.11.1 h1:ofxdnzsNrGBYXbP7t7zpUK281+go5rF7dvdIZXF8gdQ=
|
||||||
|
go.opentelemetry.io/otel/trace v1.11.1/go.mod h1:f/Q9G7vzk5u91PhbmKbg1Qn0rzH1LJ4vbPHFGkTPtOk=
|
||||||
gopkg.in/bufio.v1 v1.0.0-20140618132640-567b2bfa514e h1:wGA78yza6bu/mWcc4QfBuIEHEtc06xdiU0X8sY36yUU=
|
gopkg.in/bufio.v1 v1.0.0-20140618132640-567b2bfa514e h1:wGA78yza6bu/mWcc4QfBuIEHEtc06xdiU0X8sY36yUU=
|
||||||
gopkg.in/bufio.v1 v1.0.0-20140618132640-567b2bfa514e/go.mod h1:xsQCaysVCudhrYTfzYWe577fCe7Ceci+6qjO2Rdc0Z4=
|
gopkg.in/bufio.v1 v1.0.0-20140618132640-567b2bfa514e/go.mod h1:xsQCaysVCudhrYTfzYWe577fCe7Ceci+6qjO2Rdc0Z4=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
|
|
65
server.go
65
server.go
|
@ -17,8 +17,16 @@ import (
|
||||||
|
|
||||||
"github.com/go-logr/logr"
|
"github.com/go-logr/logr"
|
||||||
"github.com/google/uuid"
|
"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"
|
||||||
|
|
||||||
|
var tracer = otel.Tracer(tracerName)
|
||||||
|
|
||||||
var DevOnlyErr = errors.New("Allowed in dev mode only")
|
var DevOnlyErr = errors.New("Allowed in dev mode only")
|
||||||
|
|
||||||
const GenericErrorCode = -1
|
const GenericErrorCode = -1
|
||||||
|
@ -83,6 +91,10 @@ func NewRPCServer(logger logr.Logger,
|
||||||
}
|
}
|
||||||
|
|
||||||
func (server *RPCServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (server *RPCServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
|
ctx, span := tracer.Start(r.Context(), "RPCServer.ServeHTTP")
|
||||||
|
defer span.End()
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/text; charset=utf-8")
|
w.Header().Set("Content-Type", "application/text; charset=utf-8")
|
||||||
|
|
||||||
canGzip := strings.Contains(r.Header.Get("Accept-Encoding"), "gzip")
|
canGzip := strings.Contains(r.Header.Get("Accept-Encoding"), "gzip")
|
||||||
|
@ -90,11 +102,39 @@ 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 {
|
||||||
server.logger.Error(err, "Error reading body", "client_ver", cltVersion, "client_ip", cltIP)
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -104,11 +144,30 @@ func (server *RPCServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
logger = logger.WithValues("uid", reqId.String())
|
logger = logger.WithValues("uid", reqId.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
result := server.serveRequest(r.Context(), logger, payload, cltVersion, cltPlatform, cltIP)
|
result := server.serveRequest(ctx, logger, payload, cltVersion, cltPlatform, cltIP)
|
||||||
|
|
||||||
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