Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convert httpgrpc to google protobuf #627

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,24 @@ clean-protos: ## Removes the proto files
.PHONY: protos
protos: .tools/bin/protoc .tools/bin/protoc-gen-gogoslick .tools/bin/protoc-gen-go $(PROTO_GOS) ## Creates proto files

PROTO_DEFS_VITESS := ./httpgrpc/httpgrpc.proto
.PHONY: protos-vitess
protos-vitess:
@for name in $(PROTO_DEFS_VITESS); do \
.tools/protoc/bin/protoc \
--go_out=. --plugin protoc-gen-go=".tools/bin/protoc-gen-go" \
--go-grpc_out=. --plugin protoc-gen-go-grpc=".tools/bin/protoc-gen-go-grpc" \
--go-vtproto_out=. --plugin protoc-gen-go-vtproto=".tools/bin/protoc-gen-go-vtproto" \
--go-vtproto_opt=features=marshal+unmarshal+size+pool \
--go-vtproto_opt=pool=github.com/grafana/dskit/httpgrpc.HTTPRequest \
--go-vtproto_opt=pool=github.com/grafana/dskit/httpgrpc.HTTPResponse \
--go-vtproto_opt=pool=github.com/grafana/dskit/httpgrpc.Header \
--go-grpc_opt=require_unimplemented_servers=false \
$${name}; \
done

%.pb.go:
.tools/protoc/bin/protoc -I $(GOPATH):./vendor/github.com/gogo/protobuf:./vendor:./$(@D) --gogoslick_out=plugins=grpc,Mgoogle/protobuf/any.proto=github.com/gogo/protobuf/types,:./$(@D) ./$(patsubst %.pb.go,%.proto,$@)
.tools/protoc/bin/protoc protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative ./$(patsubst %.pb.go,%.proto,$@)

.PHONY: check-protos
check-protos: clean-protos protos ## Re-generates protos and git diffs them
Expand Down Expand Up @@ -95,4 +111,7 @@ endif
GOPATH=$(CURDIR)/.tools go install github.com/gogo/protobuf/protoc-gen-gogoslick@v1.3.0

.tools/bin/protoc-gen-go: .tools
GOPATH=$(CURDIR)/.tools go install github.com/golang/protobuf/protoc-gen-go@v1.3.1
GOPATH=$(CURDIR)/.tools go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.35.2

.tools/bin/protoc-gen-go-vtproto: .tools
GOPATH=$(CURDIR)/.tools go install github.com/planetscale/vtprotobuf/cmd/protoc-gen-go-vtproto@latest
9 changes: 5 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@ require (
github.com/opentracing/opentracing-go v1.2.0
github.com/pires/go-proxyproto v0.7.0
github.com/pkg/errors v0.9.1
github.com/planetscale/vtprotobuf v0.6.0
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2
github.com/prometheus/client_golang v1.15.1
github.com/prometheus/client_model v0.4.0
github.com/prometheus/common v0.44.0
github.com/prometheus/exporter-toolkit v0.10.1-0.20230714054209-2f4150c63f97
github.com/sercand/kuberesolver/v5 v5.1.1
github.com/stretchr/testify v1.8.1
github.com/stretchr/testify v1.10.0
github.com/uber/jaeger-client-go v2.28.0+incompatible
github.com/uber/jaeger-lib v2.2.0+incompatible
go.etcd.io/etcd/api/v3 v3.5.0
Expand All @@ -51,7 +52,9 @@ require (
golang.org/x/net v0.26.0
golang.org/x/sync v0.7.0
golang.org/x/time v0.1.0
google.golang.org/genproto/googleapis/rpc v0.0.0-20240604185151-ef581f913117
google.golang.org/grpc v1.65.0
google.golang.org/protobuf v1.34.1
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1
)
Expand Down Expand Up @@ -87,7 +90,7 @@ require (
github.com/onsi/gomega v1.24.0 // indirect
github.com/prometheus/procfs v0.9.0 // indirect
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/yuin/gopher-lua v0.0.0-20210529063254-f4c35e4016d9 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.17.0 // indirect
Expand All @@ -98,8 +101,6 @@ require (
golang.org/x/text v0.16.0 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240604185151-ef581f913117 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240604185151-ef581f913117 // indirect
google.golang.org/protobuf v1.34.1 // indirect
)

replace k8s.io/client-go v12.0.0+incompatible => k8s.io/client-go v0.21.4
Expand Down
13 changes: 6 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,8 @@ github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/planetscale/vtprotobuf v0.6.0 h1:nBeETjudeJ5ZgBHUz1fVHvbqUKnYOXNhsIEabROxmNA=
github.com/planetscale/vtprotobuf v0.6.0/go.mod h1:t/avpk3KcrXxUnYOhZhMXJlSEyie6gQbtLq5NM3loB8=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down Expand Up @@ -318,18 +320,15 @@ github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6Mwd
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM=
github.com/uber/jaeger-client-go v2.28.0+incompatible h1:G4QSBfvPKvg5ZM2j9MrJFdfI5iSljY/WnJqOGFao6HI=
github.com/uber/jaeger-client-go v2.28.0+incompatible/go.mod h1:WVhlPFC8FDjOFMMWRy2pZqQJSXxYSwNYOkTr/Z6d3Kk=
Expand Down
16 changes: 16 additions & 0 deletions grpcutil/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@ func ErrorToStatus(err error) (*status.Status, bool) {
return nil, false
}

func ErrorToGRPCStatus(err error) (*grpcstatus.Status, bool) {
if err == nil {
return nil, false
}
type grpcStatus interface{ GRPCStatus() *grpcstatus.Status }
var gs grpcStatus
if errors.As(err, &gs) {
st := gs.GRPCStatus()
if st == nil {
return nil, false
}
return st, true
}
return nil, false
}

// ErrorToStatusCode extracts gRPC status code from error and returns it.
//
// - If err is nil, codes.OK is returned.
Expand Down
27 changes: 14 additions & 13 deletions httpgrpc/httpgrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import (
"bytes"
"context"
"fmt"

"io"
"net/http"

"github.com/go-kit/log/level"
spb "github.com/gogo/googleapis/google/rpc"
"github.com/gogo/protobuf/types"
"github.com/gogo/status"
spb "google.golang.org/genproto/googleapis/rpc/status"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/anypb"

"github.com/grafana/dskit/grpcutil"
"github.com/grafana/dskit/log"
Expand Down Expand Up @@ -48,12 +49,12 @@ func FromHTTPRequest(r *http.Request) (*HTTPRequest, error) {
if err != nil {
return nil, err
}
return &HTTPRequest{
Method: r.Method,
Url: r.RequestURI,
Body: body,
Headers: FromHeader(r.Header),
}, nil
req := HTTPRequestFromVTPool()
req.Method = r.Method
req.Url = r.RequestURI
req.Body = body
req.Headers = FromHeader(r.Header)
return req, nil
}

// ToHTTPRequest converts httpgrpc.HTTPRequest to http.Request.
Expand Down Expand Up @@ -131,21 +132,21 @@ func ErrorFromHTTPResponse(resp *HTTPResponse) error {

// ErrorFromHTTPResponseWithMessage converts an HTTP response into a grpc error, and uses supplied message for Error message.
func ErrorFromHTTPResponseWithMessage(resp *HTTPResponse, msg string) error {
a, err := types.MarshalAny(resp)
a, err := anypb.New(resp)
if err != nil {
return err
}

return status.ErrorProto(&spb.Status{
Code: resp.Code,
Message: msg,
Details: []*types.Any{a},
Details: []*anypb.Any{a},
})
}

// HTTPResponseFromError converts a grpc error into an HTTP response
func HTTPResponseFromError(err error) (*HTTPResponse, bool) {
s, ok := grpcutil.ErrorToStatus(err)
s, ok := grpcutil.ErrorToGRPCStatus(err)
if !ok {
return nil, false
}
Expand All @@ -156,7 +157,7 @@ func HTTPResponseFromError(err error) (*HTTPResponse, bool) {
}

var resp HTTPResponse
if err := types.UnmarshalAny(status.Details[0], &resp); err != nil {
if err := status.Details[0].UnmarshalTo(&resp); err != nil {
level.Error(log.Global()).Log("msg", "got error containing non-response", "err", err)
return nil, false
}
Expand Down
Loading
Loading