Skip to content

Commit

Permalink
fix: Log output fails when JSON logging is enabled (argoproj#4911) (a…
Browse files Browse the repository at this point in the history
…rgoproj#5446)

Signed-off-by: Sho Okada <shokada3@gmail.com>
  • Loading branch information
shokada authored and Shubhama19 committed Apr 15, 2021
1 parent c2d085c commit ffaa956
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
8 changes: 6 additions & 2 deletions util/grpc/logging.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package grpc

import (
"bytes"
"encoding/json"
"fmt"

"github.com/gogo/protobuf/jsonpb"
"github.com/gogo/protobuf/proto"
grpc_logging "github.com/grpc-ecosystem/go-grpc-middleware/logging"
ctx_logrus "github.com/grpc-ecosystem/go-grpc-middleware/tags/logrus"
Expand All @@ -29,11 +31,13 @@ type jsonpbMarshalleble struct {
}

func (j *jsonpbMarshalleble) MarshalJSON() ([]byte, error) {
b, err := proto.Marshal(j.Message)
var b bytes.Buffer
m := &jsonpb.Marshaler{}
err := m.Marshal(&b, j.Message)
if err != nil {
return nil, fmt.Errorf("jsonpb serializer failed: %v", err)
}
return b, nil
return b.Bytes(), nil
}

type loggingServerStream struct {
Expand Down
39 changes: 39 additions & 0 deletions util/grpc/logging_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package grpc

import (
"bytes"
"context"
"fmt"
"testing"

"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc"

"github.com/argoproj/argo-cd/pkg/apiclient/account"
)

func Test_JSONLogging(t *testing.T) {
l := logrus.New()
l.SetFormatter(&logrus.JSONFormatter{})
var buf bytes.Buffer
l.SetOutput(&buf)
entry := logrus.NewEntry(l)

c := context.Background()
req := new(account.CreateTokenRequest)
req.Name = "create-token-name"
info := &grpc.UnaryServerInfo{}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return nil, nil
}
decider := func(ctx context.Context, fullMethodName string, servingObject interface{}) bool {
return true
}
interceptor := PayloadUnaryServerInterceptor(entry, false, decider)
_, err := interceptor(c, req, info, handler)
assert.NoError(t, err)

out := buf.String()
assert.Contains(t, out, fmt.Sprintf(`"grpc.request.content":{"name":"%s"`, req.Name))
}

0 comments on commit ffaa956

Please sign in to comment.