Skip to content

Commit d721bd4

Browse files
strawgateclaude
andcommitted
perf: remove unnecessary allocations in hot-path processors
Remove wasteful Clone() calls and defer allocations in docker metadata, kubernetes metadata, timestamp, and publisher/processing processors. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 2d10f57 commit d721bd4

File tree

10 files changed

+638
-28
lines changed

10 files changed

+638
-28
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
kind: enhancement
2+
summary: Remove unnecessary allocations in hot-path processors (docker metadata, kubernetes metadata, dissect, timestamp)
3+
component: libbeat
4+
pr:
5+
- https://github.com/elastic/beats/pull/49761

libbeat/beat/event_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func TestEvent(t *testing.T) {
129129
event := &Event{}
130130
require.NotPanics(t, func() {
131131
s := event.String()
132-
require.JSONEq(t, `{"@metadata":{},"@timestamp":"0001-01-01T00:00:00Z"}`, s)
132+
require.Equal(t, `{"@metadata":{},"@timestamp":"0001-01-01T00:00:00Z"}`, s)
133133
})
134134
})
135135
})

libbeat/processors/add_docker_metadata/add_docker_metadata.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ func (d *addDockerMetadata) Run(event *beat.Event) (*beat.Event, error) {
218218
_, _ = meta.Put("container.id", container.ID)
219219
_, _ = meta.Put("container.image.name", container.Image)
220220
_, _ = meta.Put("container.name", container.Name)
221-
event.Fields.DeepUpdate(meta.Clone())
221+
event.Fields.DeepUpdate(meta)
222222
} else {
223223
d.log.Debugf("Container not found: cid=%s", cid)
224224
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
//go:build (linux || darwin || windows) && !integration
19+
20+
package add_docker_metadata
21+
22+
import (
23+
"testing"
24+
25+
"github.com/elastic/beats/v7/libbeat/beat"
26+
"github.com/elastic/elastic-agent-autodiscover/docker"
27+
conf "github.com/elastic/elastic-agent-libs/config"
28+
"github.com/elastic/elastic-agent-libs/logp/logptest"
29+
"github.com/elastic/elastic-agent-libs/mapstr"
30+
)
31+
32+
func BenchmarkAddDockerMetadata(b *testing.B) {
33+
cfg, err := conf.NewConfigFrom(map[string]interface{}{
34+
"match_fields": []string{"container.id"},
35+
})
36+
if err != nil {
37+
b.Fatal(err)
38+
}
39+
40+
p, err := buildDockerMetadataProcessor(logptest.NewTestingLogger(b, ""), cfg, MockWatcherFactory(
41+
map[string]*docker.Container{
42+
"abc123": {
43+
ID: "abc123def456",
44+
Image: "myrepo/myimage:latest",
45+
Name: "my-container",
46+
Labels: map[string]string{
47+
"app": "myapp",
48+
"version": "v1.2.3",
49+
"env": "production",
50+
},
51+
},
52+
}, nil))
53+
if err != nil {
54+
b.Fatal(err)
55+
}
56+
57+
b.ReportAllocs()
58+
b.ResetTimer()
59+
for i := 0; i < b.N; i++ {
60+
event := &beat.Event{
61+
Fields: mapstr.M{
62+
"container": mapstr.M{"id": "abc123"},
63+
"message": "test log line",
64+
},
65+
}
66+
_, err := p.Run(event)
67+
if err != nil {
68+
b.Fatal(err)
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)