[EEM] Avoid extra initialization steps in ingest pipeline scripts (#188301)

Avoid extra initialization steps in ingest pipeline scripts.

---------

Co-authored-by: Kevin Lacabane <kevin.lacabane@elastic.co>
This commit is contained in:
Milton Hultgren 2024-07-16 14:44:56 +02:00 committed by GitHub
parent 0fcfa2321f
commit 85e6193887
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 15 additions and 52 deletions

View file

@ -9,18 +9,11 @@ import { initializePathScript, cleanScript } from './ingest_pipeline_script_proc
describe('Ingest Pipeline script processor helpers', () => {
describe('initializePathScript', () => {
it('initializes a single depth field', () => {
expect(initializePathScript('someField')).toMatchInlineSnapshot(`
"
if (ctx.someField == null) {
ctx.someField = new HashMap();
}
"
`);
it('skips initializing a single depth field', () => {
expect(initializePathScript('someField')).toMatchInlineSnapshot(`""`);
});
it('initializes a multi depth field', () => {
it('initializes a multi depth field, skipping the last segment', () => {
expect(initializePathScript('some.nested.field')).toMatchInlineSnapshot(`
"
@ -32,11 +25,6 @@ describe('Ingest Pipeline script processor helpers', () => {
if (ctx.some.nested == null) {
ctx.some.nested = new HashMap();
}
if (ctx.some.nested.field == null) {
ctx.some.nested.field = new HashMap();
}
"
`);
});
@ -140,18 +128,12 @@ describe('Ingest Pipeline script processor helpers', () => {
if (ctx.some == null) {
ctx.some = new HashMap();
}
if (ctx.some.whatever == null) {
ctx.some.whatever = new HashMap();
}
if (ctx.some == null) {
ctx.some = new HashMap();
}
if (ctx.some.else == null) {
ctx.some.else = new HashMap();
}
if (ctx.some.else.whatever == null) {
ctx.some.else.whatever = new HashMap();
}
ctx.some.thing.else = whatever;
if (nothing) {
more.stuff = otherStuff;

View file

@ -8,13 +8,24 @@
import { first, last } from 'lodash';
export function initializePathScript(field: string) {
return field.split('.').reduce((acc, _part, currentIndex, parts) => {
const fieldParts = field.split('.');
if (fieldParts.length === 1) {
return '';
}
return fieldParts.reduce((acc, _part, currentIndex, parts) => {
if (currentIndex + 1 === parts.length) {
return acc;
}
const currentSegment = parts.slice(0, currentIndex + 1).join('.');
const next = `
if (ctx.${currentSegment} == null) {
ctx.${currentSegment} = new HashMap();
}
`;
return `${acc}\n${next}`;
}, '');
}

View file

@ -88,18 +88,12 @@ if (ctx[\\"entity\\"][\\"identity\\"] != null) {
Object {
"script": Object {
"source": "if (ctx.entity?.metadata?.tags != null) {
if (ctx.tags == null) {
ctx.tags = new HashMap();
}
ctx.tags = ctx.entity.metadata.tags.keySet();
}
if (ctx.entity?.metadata?.host?.name != null) {
if (ctx.host == null) {
ctx.host = new HashMap();
}
if (ctx.host.name == null) {
ctx.host.name = new HashMap();
}
ctx.host.name = ctx.entity.metadata.host.name.keySet();
}
if (ctx.entity?.metadata?.host?.os?.name != null) {
@ -109,15 +103,9 @@ if (ctx.entity?.metadata?.host?.os?.name != null) {
if (ctx.host.os == null) {
ctx.host.os = new HashMap();
}
if (ctx.host.os.name == null) {
ctx.host.os.name = new HashMap();
}
ctx.host.os.name = ctx.entity.metadata.host.os.name.keySet();
}
if (ctx.entity?.metadata?.sourceIndex != null) {
if (ctx.sourceIndex == null) {
ctx.sourceIndex = new HashMap();
}
ctx.sourceIndex = ctx.entity.metadata.sourceIndex.keySet();
}",
},

View file

@ -44,18 +44,12 @@ Array [
Object {
"script": Object {
"source": "if (ctx.entity?.metadata?.tags.data != null) {
if (ctx.tags == null) {
ctx.tags = new HashMap();
}
ctx.tags = ctx.entity.metadata.tags.data.keySet();
}
if (ctx.entity?.metadata?.host?.name.data != null) {
if (ctx.host == null) {
ctx.host = new HashMap();
}
if (ctx.host.name == null) {
ctx.host.name = new HashMap();
}
ctx.host.name = ctx.entity.metadata.host.name.data.keySet();
}
if (ctx.entity?.metadata?.host?.os?.name.data != null) {
@ -65,15 +59,9 @@ if (ctx.entity?.metadata?.host?.os?.name.data != null) {
if (ctx.host.os == null) {
ctx.host.os = new HashMap();
}
if (ctx.host.os.name == null) {
ctx.host.os.name = new HashMap();
}
ctx.host.os.name = ctx.entity.metadata.host.os.name.data.keySet();
}
if (ctx.entity?.metadata?.sourceIndex.data != null) {
if (ctx.sourceIndex == null) {
ctx.sourceIndex = new HashMap();
}
ctx.sourceIndex = ctx.entity.metadata.sourceIndex.data.keySet();
}",
},
@ -90,9 +78,6 @@ if (ctx.entity?.metadata?.sourceIndex.data != null) {
"source": "if (ctx.log == null) {
ctx.log = new HashMap();
}
if (ctx.log.logger == null) {
ctx.log.logger = new HashMap();
}
ctx.log.logger = ctx.entity.identity.log.logger.keySet().toArray()[0];",
},
},
@ -102,9 +87,6 @@ ctx.log.logger = ctx.entity.identity.log.logger.keySet().toArray()[0];",
"source": "if (ctx.event == null) {
ctx.event = new HashMap();
}
if (ctx.event.category == null) {
ctx.event.category = new HashMap();
}
ctx.event.category = ctx.entity.identity.event.category.keySet().toArray()[0];",
},
},