Skip to content

Commit a5ff8e9

Browse files
committed
[FAB-12363] grpc server logging interceptors
Change-Id: I868f1e13189168b7c91ff96238a9bdb1ffe7b6ba Signed-off-by: Matthew Sykes <sykesmat@us.ibm.com>
1 parent 179eb83 commit a5ff8e9

File tree

23 files changed

+1666
-15
lines changed

23 files changed

+1666
-15
lines changed

Gopkg.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
# SPDX-License-Identifier: Apache-2.0
44

55
required = [
6+
"golang.org/x/lint/golint",
7+
"golang.org/x/tools/cmd/goimports",
68
"github.com/golang/protobuf/protoc-gen-go",
7-
"golang.org/x/lint/golint",
8-
"golang.org/x/tools/cmd/goimports"
99
]
1010

1111
ignored = [
@@ -101,6 +101,10 @@ noverify = [
101101
branch = "master"
102102
name = "golang.org/x/crypto"
103103

104+
[[constraint]]
105+
name = "golang.org/x/lint"
106+
revision = "c67002cb31c3a748b7688c27f20d8358b4193582"
107+
104108
[[constraint]]
105109
branch = "master"
106110
name = "golang.org/x/net"
@@ -109,6 +113,10 @@ noverify = [
109113
branch = "master"
110114
name = "golang.org/x/sync"
111115

116+
[[constraint]]
117+
name = "golang.org/x/tools"
118+
revision = "f60e5f99f0816fc2d9ecb338008ea420248d2943"
119+
112120
[[constraint]]
113121
name = "google.golang.org/grpc"
114122
version = "=1.15.0"
@@ -154,12 +162,8 @@ noverify = [
154162
version = "1.1.8"
155163

156164
[[constraint]]
157-
name = "golang.org/x/lint"
158-
revision = "c67002cb31c3a748b7688c27f20d8358b4193582"
159-
160-
[[constraint]]
161-
name = "golang.org/x/tools"
162-
revision = "f60e5f99f0816fc2d9ecb338008ea420248d2943"
165+
name = "github.com/go-kit/kit"
166+
version = "0.7.0"
163167

164168
[prune]
165169
go-tests = true
@@ -177,7 +181,3 @@ noverify = [
177181
[[prune.project]]
178182
name = "github.com/coreos/etcd"
179183
non-go = false
180-
181-
[[constraint]]
182-
name = "github.com/go-kit/kit"
183-
version = "0.7.0"

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ K := $(foreach exec,$(EXECUTABLES),\
9090
$(if $(shell which $(exec)),some string,$(error "No $(exec) in PATH: Check dependencies")))
9191

9292
GOSHIM_DEPS = $(shell ./scripts/goListFiles.sh $(PKGNAME)/core/chaincode/shim)
93-
PROTOS = $(shell git ls-files *.proto | grep -v vendor)
93+
PROTOS = $(shell git ls-files *.proto | grep -Ev 'vendor/|testdata/')
9494
# No sense rebuilding when non production code is changed
9595
PROJECT_FILES = $(shell git ls-files | grep -v ^test | grep -v ^unit-test | \
9696
grep -v ^.git | grep -v ^examples | grep -v ^devenv | grep -v .png$ | \

common/grpclogging/context.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package grpclogging
8+
9+
import (
10+
"context"
11+
12+
"go.uber.org/zap/zapcore"
13+
)
14+
15+
type fieldKeyType struct{}
16+
17+
var fieldKey = &fieldKeyType{}
18+
19+
func ZapFields(ctx context.Context) []zapcore.Field {
20+
fields, ok := ctx.Value(fieldKey).([]zapcore.Field)
21+
if ok {
22+
return fields
23+
}
24+
return nil
25+
}
26+
27+
func Fields(ctx context.Context) []interface{} {
28+
fields, ok := ctx.Value(fieldKey).([]zapcore.Field)
29+
if !ok {
30+
return nil
31+
}
32+
genericFields := make([]interface{}, len(fields))
33+
for i := range fields {
34+
genericFields[i] = fields[i]
35+
}
36+
return genericFields
37+
}
38+
39+
func WithFields(ctx context.Context, fields []zapcore.Field) context.Context {
40+
return context.WithValue(ctx, fieldKey, fields)
41+
}

common/grpclogging/context_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package grpclogging_test
8+
9+
import (
10+
"context"
11+
"time"
12+
13+
"github.com/hyperledger/fabric/common/grpclogging"
14+
. "github.com/onsi/ginkgo"
15+
. "github.com/onsi/gomega"
16+
"go.uber.org/zap"
17+
"go.uber.org/zap/zapcore"
18+
)
19+
20+
var _ = Describe("Context", func() {
21+
var inputFields []zapcore.Field
22+
23+
BeforeEach(func() {
24+
inputFields = []zapcore.Field{
25+
zap.String("string-key", "string-value"),
26+
zap.Duration("duration-key", time.Second),
27+
zap.Int("int-key", 42),
28+
}
29+
})
30+
31+
It("decorates a context with fields", func() {
32+
ctx := grpclogging.WithFields(context.Background(), inputFields)
33+
Expect(ctx).NotTo(Equal(context.Background()))
34+
35+
fields := grpclogging.Fields(ctx)
36+
Expect(fields).NotTo(BeEmpty())
37+
})
38+
39+
It("extracts fields from a decorated context as a slice of zapcore.Field", func() {
40+
ctx := grpclogging.WithFields(context.Background(), inputFields)
41+
42+
fields := grpclogging.Fields(ctx)
43+
Expect(fields).To(ConsistOf(inputFields))
44+
})
45+
46+
It("extracts fields from a decorated context as a slice of interface{}", func() {
47+
ctx := grpclogging.WithFields(context.Background(), inputFields)
48+
49+
zapFields := grpclogging.ZapFields(ctx)
50+
Expect(zapFields).To(Equal(inputFields))
51+
})
52+
53+
It("returns the same fields regardless of type", func() {
54+
ctx := grpclogging.WithFields(context.Background(), inputFields)
55+
56+
fields := grpclogging.Fields(ctx)
57+
zapFields := grpclogging.ZapFields(ctx)
58+
Expect(zapFields).To(ConsistOf(fields))
59+
})
60+
61+
Context("when the context isn't decorated", func() {
62+
It("returns no fields", func() {
63+
fields := grpclogging.Fields(context.Background())
64+
Expect(fields).To(BeNil())
65+
66+
zapFields := grpclogging.ZapFields(context.Background())
67+
Expect(zapFields).To(BeNil())
68+
})
69+
})
70+
})

common/grpclogging/fakes/echo_service.go

Lines changed: 165 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

common/grpclogging/fields.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package grpclogging
8+
9+
import (
10+
"github.com/gogo/protobuf/proto"
11+
"github.com/golang/protobuf/jsonpb"
12+
"go.uber.org/zap"
13+
"go.uber.org/zap/zapcore"
14+
)
15+
16+
type protoMarshaler struct {
17+
jsonpb.Marshaler
18+
message proto.Message
19+
}
20+
21+
func (m *protoMarshaler) MarshalJSON() ([]byte, error) {
22+
out, err := m.Marshaler.MarshalToString(m.message)
23+
if err != nil {
24+
return nil, err
25+
}
26+
return []byte(out), nil
27+
}
28+
29+
func ProtoMessage(key string, val interface{}) zapcore.Field {
30+
if pm, ok := val.(proto.Message); ok {
31+
return zap.Reflect(key, &protoMarshaler{message: pm})
32+
}
33+
return zap.Any(key, val)
34+
}

0 commit comments

Comments
 (0)