Skip to content

Commit e8b965f

Browse files
authored
refactor: replace stringset with stdlib funcs (#46752)
* refactor: replace stringset with stdlib funcs stringset is only used as a temporary data structure to remove duplicates. This can be handled by the stdlib new slices package. Replace stringset+toslice with slices.sort+slices.compact * refactor: replace more stringset usage with stdlib * Update fingerprint.go
1 parent 4c8b7a9 commit e8b965f

File tree

9 files changed

+32
-185
lines changed

9 files changed

+32
-185
lines changed

auditbeat/module/auditd/audit_linux.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,17 @@ package auditd
2020
import (
2121
"errors"
2222
"fmt"
23+
"maps"
2324
"os"
2425
"runtime"
26+
"slices"
2527
"strconv"
2628
"strings"
2729
"sync"
2830
"syscall"
2931
"time"
3032

3133
"github.com/elastic/beats/v7/auditbeat/ab"
32-
"github.com/elastic/beats/v7/libbeat/common"
3334
"github.com/elastic/beats/v7/metricbeat/mb"
3435
"github.com/elastic/beats/v7/metricbeat/mb/parse"
3536
"github.com/elastic/elastic-agent-libs/logp"
@@ -629,13 +630,13 @@ func buildMetricbeatEvent(msgs []*auparse.AuditMessage, config Config) mb.Event
629630
normalizeEventFields(auditEvent, out.RootFields)
630631

631632
// User set for related.user
632-
var userSet common.StringSet
633+
var userSet map[string]struct{}
633634
if config.ResolveIDs {
634-
userSet = make(common.StringSet)
635+
userSet = make(map[string]struct{})
635636
}
636637

637638
// Copy user.*/group.* fields from event
638-
setECSEntity := func(key string, ent aucoalesce.ECSEntityData, root mapstr.M, set common.StringSet) {
639+
setECSEntity := func(key string, ent aucoalesce.ECSEntityData, root mapstr.M, set map[string]struct{}) {
639640
if ent.ID == "" && ent.Name == "" {
640641
return
641642
}
@@ -652,7 +653,7 @@ func buildMetricbeatEvent(msgs []*auparse.AuditMessage, config Config) mb.Event
652653
if ent.Name != "" {
653654
_, _ = root.Put(nameField, ent.Name)
654655
if set != nil {
655-
set.Add(ent.Name)
656+
set[ent.Name] = struct{}{}
656657
}
657658
} else {
658659
_ = root.Delete(nameField)
@@ -665,10 +666,9 @@ func buildMetricbeatEvent(msgs []*auparse.AuditMessage, config Config) mb.Event
665666
setECSEntity("user.changes", auditEvent.ECS.User.Changes, out.RootFields, userSet)
666667
setECSEntity("group", auditEvent.ECS.Group, out.RootFields, nil)
667668

668-
if userSet != nil {
669-
if userSet.Count() != 0 {
670-
_, _ = out.RootFields.Put("related.user", userSet.ToSlice())
671-
}
669+
if len(userSet) != 0 {
670+
relatedUser := slices.Compact(slices.Sorted(maps.Keys(userSet)))
671+
_, _ = out.RootFields.Put("related.user", relatedUser)
672672
}
673673
getStringField := func(key string, m mapstr.M) (str string) {
674674
if asIf, _ := m.GetValue(key); asIf != nil {

auditbeat/module/file_integrity/action.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ package file_integrity
1919

2020
import (
2121
"math/bits"
22+
"slices"
2223
"strings"
23-
24-
"github.com/elastic/beats/v7/libbeat/common"
2524
)
2625

2726
// Action is a description of the changes described by an event.
@@ -128,7 +127,9 @@ func (action Action) ECSTypes() []string {
128127
list = append(list, name)
129128
}
130129
}
131-
return common.MakeStringSet(list...).ToSlice()
130+
slices.Sort(list)
131+
list = slices.Compact(list)
132+
return list
132133
}
133134

134135
// MarshalText marshals the Action to a textual representation of itself.
@@ -211,5 +212,7 @@ func (actions ActionArray) ECSTypes() []string {
211212
for _, action := range actions {
212213
list = append(list, action.ECSTypes()...)
213214
}
214-
return common.MakeStringSet(list...).ToSlice()
215+
slices.Sort(list)
216+
list = slices.Compact(list)
217+
return list
215218
}

libbeat/common/stringset.go

Lines changed: 0 additions & 78 deletions
This file was deleted.

libbeat/common/stringset_test.go

Lines changed: 0 additions & 74 deletions
This file was deleted.

libbeat/processors/fingerprint/fingerprint.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ import (
2121
"encoding/json"
2222
"fmt"
2323
"io"
24+
"slices"
2425
"time"
2526

2627
"github.com/elastic/beats/v7/libbeat/beat"
27-
"github.com/elastic/beats/v7/libbeat/common"
2828
"github.com/elastic/beats/v7/libbeat/processors"
2929
jsprocessor "github.com/elastic/beats/v7/libbeat/processors/script/javascript/module/processor/registry"
3030
"github.com/elastic/elastic-agent-libs/config"
@@ -56,8 +56,8 @@ func New(cfg *config.C, log *logp.Logger) (beat.Processor, error) {
5656

5757
// The fields array must be sorted, to guarantee that we always
5858
// get the same hash for a similar set of configured keys.
59-
// The call `ToSlice` always returns a sorted slice.
60-
fields := common.MakeStringSet(config.Fields...).ToSlice()
59+
slices.Sort(config.Fields)
60+
fields := slices.Compact(config.Fields)
6161

6262
p := &fingerprint{
6363
config: config,

metricbeat/module/logstash/node/data.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"encoding/json"
2222
"fmt"
2323

24-
"github.com/elastic/beats/v7/libbeat/common"
2524
s "github.com/elastic/beats/v7/libbeat/common/schema"
2625
c "github.com/elastic/beats/v7/libbeat/common/schema/mapstriface"
2726
"github.com/elastic/beats/v7/metricbeat/helper/elastic"
@@ -151,17 +150,17 @@ func makeClusterToPipelinesMap(pipelines []logstash.PipelineState, overrideClust
151150
}
152151

153152
for _, pipeline := range pipelines {
154-
clusterUUIDs := common.StringSet{}
153+
clusterUUIDs := make(map[string]struct{})
155154
for _, vertex := range pipeline.Graph.Graph.Vertices {
156155
clusterUUID := logstash.GetVertexClusterUUID(vertex, overrideClusterUUID)
157156
if clusterUUID != "" {
158-
clusterUUIDs.Add(clusterUUID)
157+
clusterUUIDs[clusterUUID] = struct{}{}
159158
}
160159
}
161160

162161
// If no cluster UUID was found in this pipeline, assign it a blank one
163162
if len(clusterUUIDs) == 0 {
164-
clusterUUIDs.Add("")
163+
clusterUUIDs[""] = struct{}{}
165164
}
166165

167166
for clusterUUID := range clusterUUIDs {

metricbeat/module/logstash/node_stats/data.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,17 +244,17 @@ func makeClusterToPipelinesMap(pipelines []PipelineStats, overrideClusterUUID st
244244
}
245245

246246
for _, pipeline := range pipelines {
247-
clusterUUIDs := common.StringSet{}
247+
clusterUUIDs := make(map[string]struct{})
248248
for _, vertex := range pipeline.Vertices {
249249
clusterUUID := logstash.GetVertexClusterUUID(vertex, overrideClusterUUID)
250250
if clusterUUID != "" {
251-
clusterUUIDs.Add(clusterUUID)
251+
clusterUUIDs[clusterUUID] = struct{}{}
252252
}
253253
}
254254

255255
// If no cluster UUID was found in this pipeline, assign it a blank one
256256
if len(clusterUUIDs) == 0 {
257-
clusterUUIDs.Add("")
257+
clusterUUIDs[""] = struct{}{}
258258
}
259259

260260
for clusterUUID := range clusterUUIDs {

x-pack/auditbeat/module/system/socket/socket_linux.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525

2626
"github.com/elastic/beats/v7/auditbeat/ab"
2727
"github.com/elastic/beats/v7/auditbeat/tracing"
28-
"github.com/elastic/beats/v7/libbeat/common"
2928
"github.com/elastic/beats/v7/libbeat/common/cfgwarn"
3029
"github.com/elastic/beats/v7/metricbeat/mb"
3130
"github.com/elastic/beats/v7/x-pack/auditbeat/module/system"
@@ -499,9 +498,10 @@ func (m *MetricSet) clockSyncLoop(interval time.Duration, done <-chan struct{})
499498
}
500499
}
501500

502-
func (m *MetricSet) isKernelFunctionAvailable(name string, tracingFns common.StringSet) bool {
503-
if tracingFns.Count() != 0 {
504-
return tracingFns.Has(name)
501+
func (m *MetricSet) isKernelFunctionAvailable(name string, tracingFns map[string]struct{}) bool {
502+
if len(tracingFns) != 0 {
503+
_, ok := tracingFns[name]
504+
return ok
505505
}
506506
defer m.installer.UninstallInstalled()
507507
checkProbe := helper.ProbeDef{

x-pack/auditbeat/module/system/socket/template.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"unsafe"
1212

1313
"github.com/elastic/beats/v7/auditbeat/tracing"
14-
"github.com/elastic/beats/v7/libbeat/common"
1514
"github.com/elastic/elastic-agent-libs/mapstr"
1615
)
1716

@@ -52,21 +51,19 @@ func syscallAlternatives(syscall string) []string {
5251
}
5352
}
5453

55-
func LoadTracingFunctions(tfs *tracing.TraceFS) (common.StringSet, error) {
54+
func LoadTracingFunctions(tfs *tracing.TraceFS) (map[string]struct{}, error) {
5655
fnList, err := tfs.AvailableFilterFunctions()
5756
if err != nil {
5857
return nil, err
5958
}
60-
// This uses make() instead of common.MakeStringSet() because the later
61-
// doesn't allow to create empty sets.
62-
functions := common.StringSet(make(map[string]struct{}, len(fnList)))
59+
functions := make(map[string]struct{}, len(fnList))
6360
for _, fn := range fnList {
6461
// Strip the module name (if any)
6562
end := strings.IndexByte(fn, ' ')
6663
if end == -1 {
6764
end = len(fn)
6865
}
69-
functions.Add(fn[:end])
66+
functions[fn[:end]] = struct{}{}
7067
}
7168
return functions, nil
7269
}

0 commit comments

Comments
 (0)