Skip to content

Commit 814ef67

Browse files
authored
Merge pull request #22 from jaypipes/moar-logging
additional logging in core reconciler
2 parents 974d6fb + b40efb3 commit 814ef67

File tree

9 files changed

+431
-36
lines changed

9 files changed

+431
-36
lines changed

mocks/pkg/types/logger.go

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

mocks/pkg/types/trace_exiter.go

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

mocks/pkg/types/tracer.go

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

pkg/runtime/log/context.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"). You may
4+
// not use this file except in compliance with the License. A copy of the
5+
// License is located at
6+
//
7+
// http://aws.amazon.com/apache2.0/
8+
//
9+
// or in the "license" file accompanying this file. This file is distributed
10+
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
// express or implied. See the License for the specific language governing
12+
// permissions and limitations under the License.
13+
14+
package log
15+
16+
import (
17+
"context"
18+
19+
acktypes "github.com/aws-controllers-k8s/runtime/pkg/types"
20+
)
21+
22+
const (
23+
// ContextKey is the string key used to store a logger in a Context
24+
ContextKey = "ack.logger"
25+
)
26+
27+
// FromContext returns a `pkg/types.Logger` from a saved key in the request
28+
// context
29+
func FromContext(ctx context.Context) acktypes.Logger {
30+
if v := ctx.Value(ContextKey); v != nil {
31+
return v.(*ResourceLogger)
32+
}
33+
return NoopLogger
34+
}

pkg/runtime/log/noop.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"). You may
4+
// not use this file except in compliance with the License. A copy of the
5+
// License is located at
6+
//
7+
// http://aws.amazon.com/apache2.0/
8+
//
9+
// or in the "license" file accompanying this file. This file is distributed
10+
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
// express or implied. See the License for the specific language governing
12+
// permissions and limitations under the License.
13+
14+
package log
15+
16+
import (
17+
acktypes "github.com/aws-controllers-k8s/runtime/pkg/types"
18+
)
19+
20+
var (
21+
// NoopLogger is useful for testing/mocking
22+
NoopLogger acktypes.Logger = &voidLogger{}
23+
)
24+
25+
// voidLogger implements Logger but does nothing. Useful for
26+
// testing and mocking...
27+
type voidLogger struct{}
28+
29+
func (l *voidLogger) WithValues(...interface{}) {}
30+
func (l *voidLogger) Info(string, ...interface{}) {}
31+
func (l *voidLogger) Debug(string, ...interface{}) {}
32+
func (l *voidLogger) Trace(name string, additionalValues ...interface{}) acktypes.TraceExiter {
33+
f := func(err error, args ...interface{}) {
34+
l.Exit(name, err, args...)
35+
}
36+
return f
37+
}
38+
func (l *voidLogger) Enter(string, ...interface{}) {}
39+
func (l *voidLogger) Exit(string, error, ...interface{}) {}

pkg/runtime/log/resource.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,106 @@
1414
package log
1515

1616
import (
17+
"strings"
18+
1719
"github.com/go-logr/logr"
1820

1921
"github.com/aws-controllers-k8s/runtime/apis/core/v1alpha1"
2022
acktypes "github.com/aws-controllers-k8s/runtime/pkg/types"
2123
)
2224

25+
// ResourceLogger is a wrapper around a logr.Logger that writes log messages
26+
// about resources involved in a controller loop. It implements
27+
// `pkg/types.Logger`
28+
type ResourceLogger struct {
29+
log logr.Logger
30+
res acktypes.AWSResource
31+
blockDepth int
32+
}
33+
34+
// WithValues adapts the internal logger with a set of additional values
35+
func (rl *ResourceLogger) WithValues(
36+
values ...interface{},
37+
) {
38+
rl.log = rl.log.WithValues(values...)
39+
}
40+
41+
// Debug writes a supplied log message about a resource that includes a set of
42+
// standard log values for the resource's kind, namespace, name, etc
43+
func (rl *ResourceLogger) Debug(
44+
msg string,
45+
additionalValues ...interface{},
46+
) {
47+
AdaptResource(rl.log, rl.res, additionalValues...).V(1).Info(msg)
48+
}
49+
50+
// Info writes a supplied log message about a resource that includes a
51+
// set of standard log values for the resource's kind, namespace, name, etc
52+
func (rl *ResourceLogger) Info(
53+
msg string,
54+
additionalValues ...interface{},
55+
) {
56+
AdaptResource(rl.log, rl.res, additionalValues...).V(0).Info(msg)
57+
}
58+
59+
// Enter logs an entry to a function or code block
60+
func (rl *ResourceLogger) Enter(
61+
name string, // name of the function or code block we're entering
62+
additionalValues ...interface{},
63+
) {
64+
if rl.log.V(1).Enabled() {
65+
rl.blockDepth++
66+
depth := strings.Repeat(">", rl.blockDepth)
67+
msg := depth + " " + name
68+
AdaptResource(rl.log, rl.res, additionalValues...).V(1).Info(msg)
69+
}
70+
}
71+
72+
// Exit logs an exit from a function or code block
73+
func (rl *ResourceLogger) Exit(
74+
name string, // name of the function or code block we're exiting
75+
err error,
76+
additionalValues ...interface{},
77+
) {
78+
if rl.log.V(1).Enabled() {
79+
depth := strings.Repeat("<", rl.blockDepth)
80+
msg := depth + " " + name
81+
if err != nil {
82+
additionalValues = append(additionalValues, "error")
83+
additionalValues = append(additionalValues, err)
84+
}
85+
AdaptResource(rl.log, rl.res, additionalValues...).V(1).Info(msg)
86+
rl.blockDepth--
87+
}
88+
}
89+
90+
// Trace logs an entry to a function or code block and returns a functor
91+
// that can be called to log the exit of the function or code block
92+
func (rl *ResourceLogger) Trace(
93+
name string,
94+
additionalValues ...interface{},
95+
) acktypes.TraceExiter {
96+
rl.Enter(name, additionalValues...)
97+
f := func(err error, args ...interface{}) {
98+
rl.Exit(name, err, args...)
99+
}
100+
return f
101+
}
102+
103+
// NewResourceLogger returns a resourceLogger that can write log messages about
104+
// a resource.
105+
func NewResourceLogger(
106+
log logr.Logger,
107+
res acktypes.AWSResource,
108+
additionalValues ...interface{},
109+
) *ResourceLogger {
110+
return &ResourceLogger{
111+
log: AdaptResource(log, res, additionalValues...),
112+
res: res,
113+
blockDepth: 0,
114+
}
115+
}
116+
23117
// AdaptResource returns a logger with log values set for the resource's kind,
24118
// namespace, name, etc
25119
func AdaptResource(

0 commit comments

Comments
 (0)