Skip to content

Commit c0709b2

Browse files
authored
pkg/acquisition: method docs, deduplicate module names (#4192)
Also prevent mistyping and make it clear when a string refers to a module name vs type label.
1 parent 160a42a commit c0709b2

Some content is hidden

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

54 files changed

+160
-112
lines changed

pkg/acquisition/modules/appsec/init.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ var (
1414
_ types.LAPIClientAware = (*Source)(nil)
1515
)
1616

17+
const ModuleName = "appsec"
18+
1719
//nolint:gochecknoinits
1820
func init() {
19-
registry.RegisterFactory("appsec", func() types.DataSource { return &Source{} })
21+
registry.RegisterFactory(ModuleName, func() types.DataSource { return &Source{} })
2022
}

pkg/acquisition/modules/appsec/source.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func (w *Source) GetMode() string {
7878
}
7979

8080
func (*Source) GetName() string {
81-
return "appsec"
81+
return ModuleName
8282
}
8383

8484
func (*Source) CanRun() error {

pkg/acquisition/modules/appsec/utils.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,8 @@ func EventFromRequest(r *appsec.ParsedRequest, labels map[string]string, txUuid
305305
// should we add some info like listen addr/port/path ?
306306
Labels: labels,
307307
Process: true,
308-
Module: "appsec",
309-
Src: "appsec",
308+
Module: ModuleName,
309+
Src: ModuleName,
310310
Raw: "dummy-appsec-data", // we discard empty Line.Raw items :)
311311
}
312312
evt.Appsec = pipeline.AppsecEvent{}
@@ -322,19 +322,19 @@ func LogAppsecEvent(evt *pipeline.Event, logger *log.Entry) {
322322

323323
if evt.Meta["appsec_interrupted"] == "true" {
324324
logger.WithFields(log.Fields{
325-
"module": "appsec",
325+
"module": ModuleName,
326326
"source": evt.Parsed["source_ip"],
327327
"target_uri": req,
328328
}).Infof("%s blocked on %s (%d rules) [%v]", evt.Parsed["source_ip"], req, len(evt.Appsec.MatchedRules), evt.Appsec.GetRuleIDs())
329329
} else if evt.Parsed["outofband_interrupted"] == "true" {
330330
logger.WithFields(log.Fields{
331-
"module": "appsec",
331+
"module": ModuleName,
332332
"source": evt.Parsed["source_ip"],
333333
"target_uri": req,
334334
}).Infof("%s out-of-band blocking rules on %s (%d rules) [%v]", evt.Parsed["source_ip"], req, len(evt.Appsec.MatchedRules), evt.Appsec.GetRuleIDs())
335335
} else {
336336
logger.WithFields(log.Fields{
337-
"module": "appsec",
337+
"module": ModuleName,
338338
"source": evt.Parsed["source_ip"],
339339
"target_uri": req,
340340
}).Debugf("%s triggered non-blocking rules on %s (%d rules) [%v]", evt.Parsed["source_ip"], req, len(evt.Appsec.MatchedRules), evt.Appsec.GetRuleIDs())

pkg/acquisition/modules/cloudwatch/init.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ var (
1414
_ types.MetricsProvider = (*Source)(nil)
1515
)
1616

17+
const ModuleName = "cloudwatch"
18+
1719
//nolint:gochecknoinits
1820
func init() {
19-
registry.RegisterFactory("cloudwatch", func() types.DataSource { return &Source{} })
21+
registry.RegisterFactory(ModuleName, func() types.DataSource { return &Source{} })
2022
}

pkg/acquisition/modules/cloudwatch/run.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ func (s *Source) TailLogStream(ctx context.Context, cfg *LogStreamTailConfig, ou
334334
if s.metricsLevel != metrics.AcquisitionMetricsLevelNone {
335335
metrics.CloudWatchDatasourceLinesRead.With(prometheus.Labels{
336336
"group": cfg.GroupName, "stream": cfg.StreamName,
337-
"datasource_type": "cloudwatch", "acquis_type": evt.Line.Labels["type"],
337+
"datasource_type": ModuleName, "acquis_type": evt.Line.Labels["type"],
338338
}).Inc()
339339
}
340340

@@ -446,7 +446,6 @@ func (s *Source) CatLogStream(ctx context.Context, cfg *LogStreamTailConfig, out
446446
}
447447

448448
func cwLogToEvent(log cwTypes.OutputLogEvent, cfg *LogStreamTailConfig) (pipeline.Event, error) {
449-
l := pipeline.Line{}
450449
evt := pipeline.MakeEvent(cfg.ExpectMode == pipeline.TIMEMACHINE, pipeline.LOG, true)
451450

452451
if log.Message == nil {
@@ -459,12 +458,15 @@ func cwLogToEvent(log cwTypes.OutputLogEvent, cfg *LogStreamTailConfig) (pipelin
459458
msg = eventTimestamp.String() + " " + msg
460459
}
461460

462-
l.Raw = msg
463-
l.Labels = cfg.Labels
464-
l.Time = time.Now().UTC()
465-
l.Src = fmt.Sprintf("%s/%s", cfg.GroupName, cfg.StreamName)
466-
l.Process = true
467-
l.Module = "cloudwatch"
461+
l := pipeline.Line{
462+
Raw: msg,
463+
Labels: cfg.Labels,
464+
Time: time.Now().UTC(),
465+
Src: cfg.GroupName + "/" + cfg.StreamName,
466+
Process: true,
467+
Module: ModuleName,
468+
}
469+
468470
evt.Line = l
469471
cfg.logger.Debugf("returned event labels : %+v", evt.Line.Labels)
470472

pkg/acquisition/modules/cloudwatch/source.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func (s *Source) GetMode() string {
2929
}
3030

3131
func (*Source) GetName() string {
32-
return "cloudwatch"
32+
return ModuleName
3333
}
3434

3535
func (*Source) CanRun() error {

pkg/acquisition/modules/docker/docker_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ service_id_regexp:
126126
},
127127
}
128128

129-
subLogger := log.WithField("type", "docker")
129+
subLogger := log.WithField("type", ModuleName)
130130

131131
for _, tc := range tests {
132132
t.Run(tc.config, func(t *testing.T) {
@@ -186,7 +186,7 @@ func TestConfigureDSN(t *testing.T) {
186186
expectedErr: "",
187187
},
188188
}
189-
subLogger := log.WithField("type", "docker")
189+
subLogger := log.WithField("type", ModuleName)
190190

191191
for _, test := range tests {
192192
t.Run(test.name, func(t *testing.T) {
@@ -335,7 +335,7 @@ service_name_regexp:
335335

336336
for _, ts := range tests {
337337
t.Run(ts.name, func(t *testing.T) {
338-
subLogger := log.WithField("type", "docker")
338+
subLogger := log.WithField("type", ModuleName)
339339

340340
dockerTomb := tomb.Tomb{}
341341
out := make(chan pipeline.Event)
@@ -486,7 +486,7 @@ use_service_labels: true`,
486486
},
487487
}
488488

489-
subLogger := log.WithField("type", "docker")
489+
subLogger := log.WithField("type", ModuleName)
490490

491491
for _, test := range tests {
492492
t.Run(test.name, func(t *testing.T) {
@@ -563,7 +563,7 @@ service_name:
563563

564564
for _, test := range tests {
565565
t.Run(test.name, func(t *testing.T) {
566-
subLogger := log.WithField("type", "docker")
566+
subLogger := log.WithField("type", ModuleName)
567567
f := Source{
568568
Client: &mockDockerCli{},
569569
}
@@ -690,7 +690,7 @@ func TestOneShot(t *testing.T) {
690690

691691
for _, ts := range tests {
692692
t.Run(ts.dsn, func(t *testing.T) {
693-
subLogger := log.WithField("type", "docker")
693+
subLogger := log.WithField("type", ModuleName)
694694

695695
dockerClient := &Source{}
696696
labels := make(map[string]string)

pkg/acquisition/modules/docker/init.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ var (
1414
_ types.MetricsProvider = (*Source)(nil)
1515
)
1616

17+
const ModuleName = "docker"
18+
1719
//nolint:gochecknoinits
1820
func init() {
19-
registry.RegisterFactory("docker", func() types.DataSource { return &Source{} })
21+
registry.RegisterFactory(ModuleName, func() types.DataSource { return &Source{} })
2022
}

pkg/acquisition/modules/docker/run.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func (d *Source) OneShotAcquisition(ctx context.Context, out chan pipeline.Event
9797
l.Module = d.GetName()
9898

9999
if d.metricsLevel != metrics.AcquisitionMetricsLevelNone {
100-
metrics.DockerDatasourceLinesRead.With(prometheus.Labels{"source": containerConfig.Name, "acquis_type": l.Labels["type"], "datasource_type": "docker"}).Inc()
100+
metrics.DockerDatasourceLinesRead.With(prometheus.Labels{"source": containerConfig.Name, "acquis_type": l.Labels["type"], "datasource_type": ModuleName}).Inc()
101101
}
102102

103103
evt := pipeline.MakeEvent(true, pipeline.LOG, true)
@@ -668,7 +668,7 @@ func (d *Source) tailContainerAttempt(ctx context.Context, container *ContainerC
668668
evt.Line = l
669669

670670
if d.metricsLevel != metrics.AcquisitionMetricsLevelNone {
671-
metrics.DockerDatasourceLinesRead.With(prometheus.Labels{"source": container.Name, "datasource_type": "docker", "acquis_type": evt.Line.Labels["type"]}).Inc()
671+
metrics.DockerDatasourceLinesRead.With(prometheus.Labels{"source": container.Name, "datasource_type": ModuleName, "acquis_type": evt.Line.Labels["type"]}).Inc()
672672
}
673673

674674
outChan <- evt
@@ -787,7 +787,7 @@ func (d *Source) tailServiceAttempt(ctx context.Context, service *ContainerConfi
787787
evt.Line = l
788788

789789
if d.metricsLevel != metrics.AcquisitionMetricsLevelNone {
790-
metrics.DockerDatasourceLinesRead.With(prometheus.Labels{"source": service.Name, "acquis_type": l.Labels["type"], "datasource_type": "docker"}).Inc()
790+
metrics.DockerDatasourceLinesRead.With(prometheus.Labels{"source": service.Name, "acquis_type": l.Labels["type"], "datasource_type": ModuleName}).Inc()
791791
}
792792

793793
outChan <- evt

pkg/acquisition/modules/docker/source.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (d *Source) GetMode() string {
5050
}
5151

5252
func (*Source) GetName() string {
53-
return "docker"
53+
return ModuleName
5454
}
5555

5656
func (*Source) CanRun() error {

0 commit comments

Comments
 (0)