Skip to content

Commit 16b4f1a

Browse files
[Fleet] Vastly improve performance of Fleet final pipeline's date formatting logic for event.ingested (#167318)
## Summary Vastly improve performance of the Fleet final pipeline's date processing for the `event.ingested` field by using a `script` processor and `DateTimeFormatter`. All credit to @joegallo for the performance improvements. Closes #157430 Our test coverage for the `event.ingested` field [here](https://github.com/elastic/kibana/blob/090569bb45573cc5df92add8c1a114134e8079e7/x-pack/test/fleet_api_integration/apis/epm/final_pipeline.ts#L109-L127) should suffice for ensuring we're not breaking anything here. ## Screenshots I fired up a local cluster and ran Fleet Server + a single Agent to verify that `event.ingested` is set correctly, and that other pipeline fields like `agent.id` are working as expected. ![image](https://github.com/elastic/kibana/assets/6766512/684f6b5d-c053-4adb-93fa-96b85efaffd1) ![image](https://github.com/elastic/kibana/assets/6766512/2c948e42-1a45-43b4-8927-807fd155e077) ![image](https://github.com/elastic/kibana/assets/6766512/c802090a-b6aa-41fc-ac7b-d5efcf6e20cf) --------- Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
1 parent 274139e commit 16b4f1a

2 files changed

Lines changed: 71 additions & 8 deletions

File tree

x-pack/plugins/fleet/server/constants/fleet_es_assets.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export const FLEET_COMPONENT_TEMPLATES = [
8181
},
8282
];
8383

84-
export const FLEET_FINAL_PIPELINE_VERSION = 3;
84+
export const FLEET_FINAL_PIPELINE_VERSION = 4;
8585

8686
// If the content is updated you probably need to update the FLEET_FINAL_PIPELINE_VERSION too to allow upgrade of the pipeline
8787
export const FLEET_FINAL_PIPELINE_CONTENT = `---
@@ -92,21 +92,28 @@ _meta:
9292
description: >
9393
Final pipeline for processing all incoming Fleet Agent documents.
9494
processors:
95-
- date:
95+
- script:
9696
description: Add time when event was ingested (and remove sub-seconds to improve storage efficiency)
9797
tag: truncate-subseconds-event-ingested
98-
field: _ingest.timestamp
99-
target_field: event.ingested
100-
formats:
101-
- ISO8601
102-
output_format: date_time_no_millis
10398
ignore_failure: true
99+
source: |-
100+
if (ctx?.event == null) {
101+
ctx.event = [:];
102+
}
103+
104+
ctx.event.ingested = metadata().now.withNano(0).format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
104105
- remove:
105106
description: Remove any pre-existing untrusted values.
106107
field:
107108
- event.agent_id_status
108109
- _security
109110
ignore_missing: true
111+
- remove:
112+
description: Remove event.original unless the preserve_original_event tag is set
113+
field: event.original
114+
if: "ctx?.tags == null || !(ctx.tags.contains('preserve_original_event'))"
115+
ignore_failure: true
116+
ignore_missing: true
110117
- set_security_user:
111118
field: _security
112119
properties:

x-pack/test/fleet_api_integration/apis/epm/final_pipeline.ts

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export default function (providerContext: FtrProviderContext) {
9292
await supertest.post(`/api/fleet/setup`).set('kbn-xsrf', 'xxxx');
9393
const pipelineRes = await es.ingest.getPipeline({ id: FINAL_PIPELINE_ID });
9494
expect(pipelineRes).to.have.property(FINAL_PIPELINE_ID);
95-
expect(pipelineRes[FINAL_PIPELINE_ID].version).to.be(3);
95+
expect(pipelineRes[FINAL_PIPELINE_ID].version).to.be(4);
9696
});
9797

9898
it('should correctly setup the final pipeline and apply to fleet managed index template', async () => {
@@ -149,6 +149,62 @@ export default function (providerContext: FtrProviderContext) {
149149
expect(event).to.have.property('ingested');
150150
});
151151

152+
it('removes event.original if preserve_original_event is not set', async () => {
153+
const res = await es.index({
154+
index: 'logs-log.log-test',
155+
body: {
156+
message: 'message-test-1',
157+
event: {
158+
original: {
159+
foo: 'bar',
160+
},
161+
},
162+
'@timestamp': '2023-01-01T09:00:00',
163+
tags: [],
164+
agent: {
165+
id: 'agent1',
166+
},
167+
},
168+
});
169+
170+
const doc: any = await es.get({
171+
id: res._id,
172+
index: res._index,
173+
});
174+
175+
const event = doc._source.event;
176+
177+
expect(event.original).to.be(undefined);
178+
});
179+
180+
it('preserves event.original if preserve_original_event is set', async () => {
181+
const res = await es.index({
182+
index: 'logs-log.log-test',
183+
body: {
184+
message: 'message-test-1',
185+
event: {
186+
original: {
187+
foo: 'bar',
188+
},
189+
},
190+
'@timestamp': '2023-01-01T09:00:00',
191+
tags: ['preserve_original_event'],
192+
agent: {
193+
id: 'agent1',
194+
},
195+
},
196+
});
197+
198+
const doc: any = await es.get({
199+
id: res._id,
200+
index: res._index,
201+
});
202+
203+
const event = doc._source.event;
204+
205+
expect(event.original).to.eql({ foo: 'bar' });
206+
});
207+
152208
const scenarios = [
153209
{
154210
name: 'API key without metadata',

0 commit comments

Comments
 (0)