Skip to content

Commit be2b8b1

Browse files
authored
Merge pull request #39 from pohly/contextual-logging
fix and test for contextual logging
2 parents efc9428 + dbe91b9 commit be2b8b1

File tree

7 files changed

+118
-13
lines changed

7 files changed

+118
-13
lines changed

Makefile

+7
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ misspell:
8787
vet:
8888
go vet $(MODULE)/...
8989

90+
# Ensure that all log calls support contextual logging.
91+
test: logcheck
92+
.PHONY: logcheck
93+
logcheck:
94+
(cd hack/tools && GOBIN=$(PWD) go install sigs.k8s.io/logtools/logcheck)
95+
./logcheck -check-contextual -check-deprecations ./...
96+
9097
COVERAGE_FILE := coverage.out
9198
test: build cmds
9299
go test -v -coverprofile=$(COVERAGE_FILE) $(MODULE)/...

cmd/dra-example-controller/main.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func newApp() *cli.App {
138138
}
139139

140140
if flags.httpEndpoint != "" {
141-
err = SetupHTTPEndpoint(config)
141+
err = SetupHTTPEndpoint(ctx, config)
142142
if err != nil {
143143
return fmt.Errorf("create http endpoint: %v", err)
144144
}
@@ -156,7 +156,9 @@ func newApp() *cli.App {
156156
return app
157157
}
158158

159-
func SetupHTTPEndpoint(config *Config) error {
159+
func SetupHTTPEndpoint(ctx context.Context, config *Config) error {
160+
logger := klog.FromContext(ctx)
161+
logger = klog.LoggerWithName(logger, "http-server")
160162
if config.flags.metricsPath != "" {
161163
// To collect metrics data from the metric handler itself, we
162164
// let it register itself and then collect from that registry.
@@ -169,7 +171,7 @@ func SetupHTTPEndpoint(config *Config) error {
169171
gatherers = append(gatherers, reg)
170172

171173
actualPath := path.Join("/", config.flags.metricsPath)
172-
klog.InfoS("Starting metrics", "path", actualPath)
174+
logger.Info("Starting metrics", "path", actualPath)
173175
// This is similar to k8s.io/component-base/metrics HandlerWithReset
174176
// except that we gather from multiple sources.
175177
config.mux.Handle(actualPath,
@@ -180,7 +182,7 @@ func SetupHTTPEndpoint(config *Config) error {
180182

181183
if config.flags.profilePath != "" {
182184
actualPath := path.Join("/", config.flags.profilePath)
183-
klog.InfoS("Starting profiling", "path", actualPath)
185+
logger.Info("Starting profiling", "path", actualPath)
184186
config.mux.HandleFunc(actualPath, pprof.Index)
185187
config.mux.HandleFunc(path.Join(actualPath, "cmdline"), pprof.Cmdline)
186188
config.mux.HandleFunc(path.Join(actualPath, "profile"), pprof.Profile)
@@ -194,10 +196,10 @@ func SetupHTTPEndpoint(config *Config) error {
194196
}
195197

196198
go func() {
197-
klog.InfoS("Starting HTTP server", "endpoint", config.flags.httpEndpoint)
199+
logger.Info("Starting HTTP server", "endpoint", config.flags.httpEndpoint)
198200
err := http.Serve(listener, config.mux)
199201
if err != nil {
200-
klog.ErrorS(err, "HTTP server failed")
202+
logger.Error(err, "HTTP server failed")
201203
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
202204
}
203205
}()

cmd/dra-example-kubeletplugin/driver.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ func (d *driver) Shutdown(ctx context.Context) error {
9696
}
9797

9898
func (d *driver) NodePrepareResources(ctx context.Context, req *drapbv1.NodePrepareResourcesRequest) (*drapbv1.NodePrepareResourcesResponse, error) {
99-
100-
klog.Infof("NodePrepareResource is called: number of claims: %d", len(req.Claims))
99+
logger := klog.FromContext(ctx)
100+
logger.Info("NodePrepareResource", "numClaims", len(req.Claims))
101101
preparedResources := &drapbv1.NodePrepareResourcesResponse{Claims: map[string]*drapbv1.NodePrepareResourceResponse{}}
102102

103103
// In production version some common operations of d.nodeUnprepareResources
@@ -111,6 +111,7 @@ func (d *driver) NodePrepareResources(ctx context.Context, req *drapbv1.NodePrep
111111
}
112112

113113
func (d *driver) nodePrepareResource(ctx context.Context, claim *drapbv1.Claim) *drapbv1.NodePrepareResourceResponse {
114+
logger := klog.FromContext(ctx)
114115
var err error
115116
var prepared []string
116117
err = retry.RetryOnConflict(retry.DefaultRetry, func() error {
@@ -127,7 +128,7 @@ func (d *driver) nodePrepareResource(ctx context.Context, claim *drapbv1.Claim)
127128
err = d.nasclient.Update(ctx, updatedSpec)
128129
if err != nil {
129130
if err := d.state.Unprepare(claim.Uid); err != nil {
130-
klog.Errorf("Failed to unprepare after claim '%v' Update() error: %v", claim.Uid, err)
131+
logger.Error(err, "Failed to unprepare after Update", "claim", claim.Uid)
131132
}
132133
return err
133134
}
@@ -141,12 +142,13 @@ func (d *driver) nodePrepareResource(ctx context.Context, claim *drapbv1.Claim)
141142
}
142143
}
143144

144-
klog.Infof("Prepared devices for claim '%v': %s", claim.Uid, prepared)
145+
klog.FromContext(ctx).Info("Prepared devices", "claim", claim.Uid)
145146
return &drapbv1.NodePrepareResourceResponse{CDIDevices: prepared}
146147
}
147148

148149
func (d *driver) NodeUnprepareResources(ctx context.Context, req *drapbv1.NodeUnprepareResourcesRequest) (*drapbv1.NodeUnprepareResourcesResponse, error) {
149-
klog.Infof("NodeUnprepareResource is called: number of claims: %d", len(req.Claims))
150+
logger := klog.FromContext(ctx)
151+
logger.Info("NodeUnprepareResource", "numClaims", len(req.Claims))
150152
unpreparedResources := &drapbv1.NodeUnprepareResourcesResponse{
151153
Claims: map[string]*drapbv1.NodeUnprepareResourceResponse{},
152154
}
@@ -186,7 +188,7 @@ func (d *driver) nodeUnprepareResource(ctx context.Context, claim *drapbv1.Claim
186188
}
187189
}
188190

189-
klog.Infof("Unprepared devices for claim '%v'", claim.Uid)
191+
klog.FromContext(ctx).Info("Unprepared devices", "claim", claim.Uid)
190192
return &drapbv1.NodeUnprepareResourceResponse{}
191193
}
192194

cmd/dra-example-kubeletplugin/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ func StartPlugin(ctx context.Context, config *Config) error {
159159

160160
err = driver.Shutdown(ctx)
161161
if err != nil {
162-
klog.Errorf("Unable to cleanly shutdown driver: %v", err)
162+
klog.FromContext(ctx).Error(err, "Unable to cleanly shutdown driver")
163163
}
164164

165165
return nil

hack/tools/go.mod

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module sigs.k8s.io/dra-example-driver/hack/tools
2+
3+
go 1.21.0
4+
5+
require sigs.k8s.io/logtools v0.7.0
6+
7+
require (
8+
golang.org/x/exp v0.0.0-20230807204917-050eac23e9de // indirect
9+
golang.org/x/mod v0.12.0 // indirect
10+
golang.org/x/sys v0.11.0 // indirect
11+
golang.org/x/tools v0.12.0 // indirect
12+
)

hack/tools/go.sum

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
2+
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
3+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
4+
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
5+
golang.org/x/crypto v0.1.0/go.mod h1:RecgLatLF4+eUMCP1PoPZQb+cVrJcOPbHkTkbkB9sbw=
6+
golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw=
7+
golang.org/x/exp v0.0.0-20230807204917-050eac23e9de h1:l5Za6utMv/HsBWWqzt4S8X17j+kt1uVETUX5UFhn2rE=
8+
golang.org/x/exp v0.0.0-20230807204917-050eac23e9de/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
9+
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
10+
golang.org/x/mod v0.6.0/go.mod h1:4mET923SAdbXp2ki8ey+zGs1SLqsuM2Y0uvdZR/fUNI=
11+
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
12+
golang.org/x/mod v0.11.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
13+
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
14+
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
15+
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
16+
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
17+
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
18+
golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco=
19+
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
20+
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
21+
golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI=
22+
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
23+
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
24+
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
25+
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
26+
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
27+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
28+
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
29+
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
30+
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
31+
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
32+
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
33+
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
34+
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
35+
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
36+
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
37+
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
38+
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
39+
golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
40+
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
41+
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
42+
golang.org/x/term v0.11.0/go.mod h1:zC9APTIj3jG3FdV/Ons+XE1riIZXG4aZ4GTHiPZJPIU=
43+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
44+
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
45+
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
46+
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
47+
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
48+
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
49+
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
50+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
51+
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
52+
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
53+
golang.org/x/tools v0.2.0/go.mod h1:y4OqIKeOV/fWJetJ8bXPU1sEVniLMIyDAZWeHdV+NTA=
54+
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
55+
golang.org/x/tools v0.12.0 h1:YW6HUoUmYBpwSgyaGaZq1fHjrBjX1rlpZ54T6mu2kss=
56+
golang.org/x/tools v0.12.0/go.mod h1:Sc0INKfu04TlqNoRA1hgpFZbhYXHPr4V5DzpSBTPqQM=
57+
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
58+
sigs.k8s.io/logtools v0.7.0 h1:T1MyHujJGubwMDr2Xd9pc0Pwtcg9x6w04zkUWh9c5Do=
59+
sigs.k8s.io/logtools v0.7.0/go.mod h1:WPITRuV0T26MH6PCQrGKZ6hHoRCOEmsdA1+raufT3E8=

hack/tools/tools.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Package tools is used to track binary dependencies with go modules
18+
// https://github.com/golang/go/wiki/Modules#how-can-i-track-tool-dependencies-for-a-module
19+
package tools
20+
21+
import (
22+
_ "sigs.k8s.io/logtools/logcheck"
23+
)

0 commit comments

Comments
 (0)