Skip to content

Commit

Permalink
convert httpgrpc to google protobuf
Browse files Browse the repository at this point in the history
  • Loading branch information
francoposa committed Dec 17, 2024
1 parent 0450f2b commit 4c7e0f0
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 1,262 deletions.
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ require (
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 +51,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 +89,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 +100,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
11 changes: 4 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -318,18 +318,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
15 changes: 8 additions & 7 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 @@ -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

0 comments on commit 4c7e0f0

Please sign in to comment.