-
Notifications
You must be signed in to change notification settings - Fork 8.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FAB-12363] grpc server logging interceptors
Change-Id: I868f1e13189168b7c91ff96238a9bdb1ffe7b6ba Signed-off-by: Matthew Sykes <sykesmat@us.ibm.com>
- Loading branch information
Showing
23 changed files
with
1,666 additions
and
15 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
Copyright IBM Corp. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package grpclogging | ||
|
||
import ( | ||
"context" | ||
|
||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
type fieldKeyType struct{} | ||
|
||
var fieldKey = &fieldKeyType{} | ||
|
||
func ZapFields(ctx context.Context) []zapcore.Field { | ||
fields, ok := ctx.Value(fieldKey).([]zapcore.Field) | ||
if ok { | ||
return fields | ||
} | ||
return nil | ||
} | ||
|
||
func Fields(ctx context.Context) []interface{} { | ||
fields, ok := ctx.Value(fieldKey).([]zapcore.Field) | ||
if !ok { | ||
return nil | ||
} | ||
genericFields := make([]interface{}, len(fields)) | ||
for i := range fields { | ||
genericFields[i] = fields[i] | ||
} | ||
return genericFields | ||
} | ||
|
||
func WithFields(ctx context.Context, fields []zapcore.Field) context.Context { | ||
return context.WithValue(ctx, fieldKey, fields) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
Copyright IBM Corp. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package grpclogging_test | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/hyperledger/fabric/common/grpclogging" | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
var _ = Describe("Context", func() { | ||
var inputFields []zapcore.Field | ||
|
||
BeforeEach(func() { | ||
inputFields = []zapcore.Field{ | ||
zap.String("string-key", "string-value"), | ||
zap.Duration("duration-key", time.Second), | ||
zap.Int("int-key", 42), | ||
} | ||
}) | ||
|
||
It("decorates a context with fields", func() { | ||
ctx := grpclogging.WithFields(context.Background(), inputFields) | ||
Expect(ctx).NotTo(Equal(context.Background())) | ||
|
||
fields := grpclogging.Fields(ctx) | ||
Expect(fields).NotTo(BeEmpty()) | ||
}) | ||
|
||
It("extracts fields from a decorated context as a slice of zapcore.Field", func() { | ||
ctx := grpclogging.WithFields(context.Background(), inputFields) | ||
|
||
fields := grpclogging.Fields(ctx) | ||
Expect(fields).To(ConsistOf(inputFields)) | ||
}) | ||
|
||
It("extracts fields from a decorated context as a slice of interface{}", func() { | ||
ctx := grpclogging.WithFields(context.Background(), inputFields) | ||
|
||
zapFields := grpclogging.ZapFields(ctx) | ||
Expect(zapFields).To(Equal(inputFields)) | ||
}) | ||
|
||
It("returns the same fields regardless of type", func() { | ||
ctx := grpclogging.WithFields(context.Background(), inputFields) | ||
|
||
fields := grpclogging.Fields(ctx) | ||
zapFields := grpclogging.ZapFields(ctx) | ||
Expect(zapFields).To(ConsistOf(fields)) | ||
}) | ||
|
||
Context("when the context isn't decorated", func() { | ||
It("returns no fields", func() { | ||
fields := grpclogging.Fields(context.Background()) | ||
Expect(fields).To(BeNil()) | ||
|
||
zapFields := grpclogging.ZapFields(context.Background()) | ||
Expect(zapFields).To(BeNil()) | ||
}) | ||
}) | ||
}) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
Copyright IBM Corp. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package grpclogging | ||
|
||
import ( | ||
"github.com/gogo/protobuf/proto" | ||
"github.com/golang/protobuf/jsonpb" | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
type protoMarshaler struct { | ||
jsonpb.Marshaler | ||
message proto.Message | ||
} | ||
|
||
func (m *protoMarshaler) MarshalJSON() ([]byte, error) { | ||
out, err := m.Marshaler.MarshalToString(m.message) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return []byte(out), nil | ||
} | ||
|
||
func ProtoMessage(key string, val interface{}) zapcore.Field { | ||
if pm, ok := val.(proto.Message); ok { | ||
return zap.Reflect(key, &protoMarshaler{message: pm}) | ||
} | ||
return zap.Any(key, val) | ||
} |
Oops, something went wrong.