Skip to content

Commit 6cb17bd

Browse files
authored
Clean up and align logs. (#171)
1 parent e0a49c6 commit 6cb17bd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+492
-224
lines changed

beater/bundle/bundle.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,11 @@ func StartServer() (*http.Server, error) {
6666
Handler: h,
6767
}
6868

69+
log := logp.NewLogger("cloudbeat_bundle_server")
70+
6971
go func() {
7072
if err := srv.ListenAndServe(); err != nil {
71-
logp.L().Errorf("bundle server closed: %v", err)
73+
log.Errorf("Bundle server closed: %v", err)
7274
}
7375
}()
7476

beater/cloudbeat.go

+17-16
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,19 @@ func New(b *beat.Beat, cfg *common.Config) (beat.Beater, error) {
6565

6666
log.Info("Config initiated.")
6767

68-
fetchersRegistry, err := InitRegistry(c)
68+
fetchersRegistry, err := InitRegistry(log, c)
6969
if err != nil {
7070
cancel()
7171
return nil, err
7272
}
7373

74-
data, err := manager.NewData(c.Period, time.Minute, fetchersRegistry)
74+
data, err := manager.NewData(log, c.Period, time.Minute, fetchersRegistry)
7575
if err != nil {
7676
cancel()
7777
return nil, err
7878
}
7979

80-
eval, err := evaluator.NewOpaEvaluator(ctx)
80+
eval, err := evaluator.NewOpaEvaluator(ctx, log)
8181
if err != nil {
8282
cancel()
8383
return nil, err
@@ -90,7 +90,7 @@ func New(b *beat.Beat, cfg *common.Config) (beat.Beater, error) {
9090
return nil, err
9191
}
9292

93-
cdp, err := transformer.NewCommonDataProvider(c)
93+
cdp, err := transformer.NewCommonDataProvider(log, c)
9494
if err != nil {
9595
cancel()
9696
return nil, err
@@ -102,13 +102,13 @@ func New(b *beat.Beat, cfg *common.Config) (beat.Beater, error) {
102102
return nil, err
103103
}
104104

105-
t := transformer.NewTransformer(ctx, eval, commonData, resultsIndex)
105+
t := transformer.NewTransformer(ctx, log, eval, commonData, resultsIndex)
106106

107107
bt := &cloudbeat{
108108
ctx: ctx,
109109
cancel: cancel,
110110
config: c,
111-
configUpdates: config.Updates(ctx),
111+
configUpdates: config.Updates(ctx, log),
112112
evaluator: eval,
113113
data: data,
114114
transformer: t,
@@ -155,33 +155,33 @@ func (bt *cloudbeat) Run(b *beat.Beat) error {
155155

156156
case update := <-bt.configUpdates:
157157
if err := bt.config.Update(update); err != nil {
158-
logp.L().Errorf("Could not update cloudbeat config: %v", err)
158+
bt.log.Errorf("Could not update cloudbeat config: %v", err)
159159
break
160160
}
161161

162162
policies, err := csppolicies.CISKubernetes()
163163
if err != nil {
164-
logp.L().Errorf("Could not load CIS Kubernetes policies: %v", err)
164+
bt.log.Errorf("Could not load CIS Kubernetes policies: %v", err)
165165
break
166166
}
167167

168168
if len(bt.config.Streams) == 0 {
169-
logp.L().Infof("Did not receive any input stream, skipping.")
169+
bt.log.Infof("Did not receive any input stream, skipping.")
170170
break
171171
}
172172

173173
y, err := bt.config.DataYaml()
174174
if err != nil {
175-
logp.L().Errorf("Could not marshal to YAML: %v", err)
175+
bt.log.Errorf("Could not marshal to YAML: %v", err)
176176
break
177177
}
178178

179179
if err := csppolicies.HostBundleWithDataYaml("bundle.tar.gz", policies, y); err != nil {
180-
logp.L().Errorf("Could not update bundle with dataYaml: %v", err)
180+
bt.log.Errorf("Could not update bundle with dataYaml: %v", err)
181181
break
182182
}
183183

184-
logp.L().Infof("Bundle updated with dataYaml: %s", y)
184+
bt.log.Infof("Bundle updated with dataYaml: %s", y)
185185

186186
case fetchedResources := <-output:
187187
cycleId, _ := uuid.NewV4()
@@ -195,12 +195,13 @@ func (bt *cloudbeat) Run(b *beat.Beat) error {
195195
}
196196
}
197197

198-
func InitRegistry(c config.Config) (manager.FetchersRegistry, error) {
199-
registry := manager.NewFetcherRegistry()
200-
err := manager.Factories.RegisterFetchers(registry, c)
201-
if err != nil {
198+
func InitRegistry(log *logp.Logger, c config.Config) (manager.FetchersRegistry, error) {
199+
registry := manager.NewFetcherRegistry(log)
200+
201+
if err := manager.Factories.RegisterFetchers(log, registry, c); err != nil {
202202
return nil, err
203203
}
204+
204205
return registry, nil
205206
}
206207

config/config_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"time"
2727

2828
"github.com/elastic/beats/v7/libbeat/common"
29+
"github.com/elastic/beats/v7/libbeat/logp"
2930
"github.com/stretchr/testify/suite"
3031
)
3132

@@ -34,6 +35,10 @@ type ConfigTestSuite struct {
3435
}
3536

3637
func TestConfigTestSuite(t *testing.T) {
38+
if err := logp.TestingSetup(); err != nil {
39+
t.Error(err)
40+
}
41+
3742
suite.Run(t, new(ConfigTestSuite))
3843
}
3944

config/updates.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030

3131
type reloader struct {
3232
ctx context.Context
33+
log *logp.Logger
3334
ch chan<- *common.Config
3435
}
3536

@@ -38,7 +39,7 @@ func (r *reloader) Reload(configs []*reload.ConfigWithMeta) error {
3839
return nil
3940
}
4041

41-
logp.L().Infof("Received %v new configs for reload.", len(configs))
42+
r.log.Infof("Received %v new configs for reload.", len(configs))
4243

4344
select {
4445
case <-r.ctx.Done():
@@ -51,10 +52,11 @@ func (r *reloader) Reload(configs []*reload.ConfigWithMeta) error {
5152
return nil
5253
}
5354

54-
func Updates(ctx context.Context) <-chan *common.Config {
55+
func Updates(ctx context.Context, log *logp.Logger) <-chan *common.Config {
5556
ch := make(chan *common.Config)
5657
r := &reloader{
5758
ctx: ctx,
59+
log: log,
5860
ch: ch,
5961
}
6062

evaluator/opa.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ import (
2121
"bytes"
2222
"context"
2323
"fmt"
24-
"github.com/elastic/beats/v7/libbeat/logp"
2524
"net/http"
2625

26+
"github.com/elastic/beats/v7/libbeat/logp"
27+
2728
"github.com/mitchellh/mapstructure"
2829
"github.com/open-policy-agent/opa/logging"
2930
"github.com/open-policy-agent/opa/sdk"
@@ -33,11 +34,12 @@ import (
3334
)
3435

3536
type OpaEvaluator struct {
37+
log *logp.Logger
3638
opa *sdk.OPA
3739
bundleServer *http.Server
3840
}
3941

40-
func NewOpaEvaluator(ctx context.Context) (Evaluator, error) {
42+
func NewOpaEvaluator(ctx context.Context, log *logp.Logger) (Evaluator, error) {
4143
server, err := bundle.StartServer()
4244
if err != nil {
4345
return nil, err
@@ -59,6 +61,7 @@ func NewOpaEvaluator(ctx context.Context) (Evaluator, error) {
5961
}
6062

6163
return &OpaEvaluator{
64+
log: log,
6265
opa: opa,
6366
bundleServer: server,
6467
}, nil
@@ -81,7 +84,7 @@ func (o *OpaEvaluator) Stop(ctx context.Context) {
8184
o.opa.Stop(ctx)
8285
err := o.bundleServer.Shutdown(ctx)
8386
if err != nil {
84-
logp.L().Errorf("Could not stop OPA evaluator: %v", err)
87+
o.log.Errorf("Could not stop OPA evaluator: %v", err)
8588
}
8689
}
8790

resources/conditions/lease_fetcher_condition.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,21 @@ type LeaderLeaseProvider interface {
2727
}
2828

2929
type LeaseFetcherCondition struct {
30+
log *logp.Logger
3031
provider LeaderLeaseProvider
3132
}
3233

33-
func NewLeaseFetcherCondition(provider LeaderLeaseProvider) fetching.Condition {
34+
func NewLeaseFetcherCondition(log *logp.Logger, provider LeaderLeaseProvider) fetching.Condition {
3435
return &LeaseFetcherCondition{
36+
log: log,
3537
provider: provider,
3638
}
3739
}
3840

3941
func (c *LeaseFetcherCondition) Condition() bool {
4042
l, err := c.provider.IsLeader()
4143
if err != nil {
42-
logp.L().Errorf("could not read leader value, using default value %v: %v", l, err)
44+
c.log.Errorf("Could not read leader value, using default value %v: %v", l, err)
4345
}
4446
return l
4547
}

resources/fetchers/ecr_factory.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package fetchers
1919

2020
import (
2121
"fmt"
22+
"regexp"
23+
2224
"github.com/docker/distribution/context"
2325
"github.com/elastic/beats/v7/libbeat/common"
2426
"github.com/elastic/beats/v7/libbeat/common/kubernetes"
@@ -27,7 +29,6 @@ import (
2729
"github.com/elastic/cloudbeat/resources/manager"
2830
"github.com/elastic/cloudbeat/resources/providers"
2931
"github.com/elastic/cloudbeat/resources/providers/awslib"
30-
"regexp"
3132
)
3233

3334
const (
@@ -49,8 +50,9 @@ type ecrExtraElements struct {
4950
ecrRepoDescriber awslib.EcrRepositoryDescriber
5051
}
5152

52-
func (f *ECRFactory) Create(c *common.Config) (fetching.Fetcher, error) {
53-
logp.L().Info("ECR factory has started")
53+
func (f *ECRFactory) Create(log *logp.Logger, c *common.Config) (fetching.Fetcher, error) {
54+
log.Debug("Starting ECRFactory.Create")
55+
5456
cfg := ECRFetcherConfig{}
5557
err := c.Unpack(&cfg)
5658
if err != nil {
@@ -61,7 +63,7 @@ func (f *ECRFactory) Create(c *common.Config) (fetching.Fetcher, error) {
6163
return nil, err
6264
}
6365

64-
return f.CreateFrom(cfg, elements)
66+
return f.CreateFrom(log, cfg, elements)
6567
}
6668

6769
func getEcrExtraElements() (ecrExtraElements, error) {
@@ -84,7 +86,7 @@ func getEcrExtraElements() (ecrExtraElements, error) {
8486
return extraElements, nil
8587
}
8688

87-
func (f *ECRFactory) CreateFrom(cfg ECRFetcherConfig, elements ecrExtraElements) (fetching.Fetcher, error) {
89+
func (f *ECRFactory) CreateFrom(log *logp.Logger, cfg ECRFetcherConfig, elements ecrExtraElements) (fetching.Fetcher, error) {
8890
ctx := context.Background()
8991
identity, err := elements.identityProviderGetter.GetIdentity(ctx)
9092
if err != nil {
@@ -98,6 +100,7 @@ func (f *ECRFactory) CreateFrom(cfg ECRFetcherConfig, elements ecrExtraElements)
98100
}
99101

100102
fe := &ECRFetcher{
103+
log: log,
101104
cfg: cfg,
102105
ecrProvider: elements.ecrRepoDescriber,
103106
kubeClient: kubeClient,

resources/fetchers/ecr_factory_test.go

+14-3
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,33 @@
1818
package fetchers
1919

2020
import (
21+
"testing"
22+
2123
"github.com/aws/aws-sdk-go-v2/aws"
2224
"github.com/elastic/beats/v7/libbeat/common"
25+
"github.com/elastic/beats/v7/libbeat/logp"
2326
"github.com/elastic/cloudbeat/resources/providers"
2427
"github.com/elastic/cloudbeat/resources/providers/awslib"
2528
"github.com/stretchr/testify/mock"
2629
"github.com/stretchr/testify/suite"
2730
k8sfake "k8s.io/client-go/kubernetes/fake"
28-
"testing"
2931
)
3032

3133
type EcrFactoryTestSuite struct {
3234
suite.Suite
35+
36+
log *logp.Logger
3337
}
3438

3539
func TestEcrFactoryTestSuite(t *testing.T) {
36-
suite.Run(t, new(EcrFactoryTestSuite))
40+
s := new(EcrFactoryTestSuite)
41+
s.log = logp.NewLogger("cloudbeat_ecr_factory_test_suite")
42+
43+
if err := logp.TestingSetup(); err != nil {
44+
t.Error(err)
45+
}
46+
47+
suite.Run(t, s)
3748
}
3849

3950
func (s *EcrFactoryTestSuite) TestCreateFetcher() {
@@ -89,7 +100,7 @@ name: aws-ecr
89100
cfg, err := common.NewConfigFrom(test.config)
90101
s.NoError(err)
91102

92-
fetcher, err := factory.Create(cfg)
103+
fetcher, err := factory.Create(s.log, cfg)
93104
s.NoError(err)
94105
s.NotNil(fetcher)
95106

resources/fetchers/ecr_fetcher.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ package fetchers
2020
import (
2121
"context"
2222
"fmt"
23-
"github.com/elastic/cloudbeat/resources/providers/awslib"
2423
"regexp"
2524

25+
"github.com/elastic/cloudbeat/resources/providers/awslib"
26+
2627
"github.com/aws/aws-sdk-go-v2/service/ecr"
2728
"github.com/elastic/beats/v7/libbeat/logp"
2829
"github.com/elastic/cloudbeat/resources/fetching"
@@ -34,6 +35,7 @@ const PrivateRepoRegexTemplate = "^%s\\.dkr\\.ecr\\.%s\\.amazonaws\\.com\\/([-\\
3435
const PublicRepoRegex = "public\\.ecr\\.aws\\/\\w+\\/([\\w-]+)\\:?"
3536

3637
type ECRFetcher struct {
38+
log *logp.Logger
3739
cfg ECRFetcherConfig
3840
ecrProvider awslib.EcrRepositoryDescriber
3941
kubeClient k8s.Interface
@@ -55,7 +57,8 @@ func (f *ECRFetcher) Stop() {
5557
}
5658

5759
func (f *ECRFetcher) Fetch(ctx context.Context) ([]fetching.Resource, error) {
58-
logp.L().Debug("ecr fetcher starts to fetch data")
60+
f.log.Debug("Starting ECRFetcher.Fetch")
61+
5962
results := make([]fetching.Resource, 0)
6063
podsAwsRepositories, err := f.getAwsPodRepositories(ctx)
6164
if err != nil {

0 commit comments

Comments
 (0)