mirror of
https://github.com/elastic/kibana.git
synced 2025-04-23 17:28:26 -04:00
* use LogRecord.meta: LogMeta * refactor other types * fix meta overrides tracing props test Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
parent
ade7b5f468
commit
56fa1f1e22
4 changed files with 43 additions and 37 deletions
|
@ -6,7 +6,8 @@
|
|||
* Side Public License, v 1.
|
||||
*/
|
||||
|
||||
import { LogLevel } from './log_level';
|
||||
import type { LogLevel } from './log_level';
|
||||
import type { LogMeta } from './log_meta';
|
||||
|
||||
/**
|
||||
* Essential parts of every log message.
|
||||
|
@ -18,7 +19,7 @@ export interface LogRecord {
|
|||
context: string;
|
||||
message: string;
|
||||
error?: Error;
|
||||
meta?: { [name: string]: any };
|
||||
meta?: LogMeta;
|
||||
pid: number;
|
||||
spanId?: string;
|
||||
traceId?: string;
|
||||
|
|
|
@ -29,26 +29,27 @@ describe('MetaRewritePolicy', () => {
|
|||
// @ts-expect-error ECS custom meta
|
||||
const log = createLogRecord({ a: 'before' });
|
||||
const policy = createPolicy('update', [{ path: 'a', value: 'after' }]);
|
||||
// @ts-expect-error ECS custom meta
|
||||
expect(policy.rewrite(log).meta!.a).toBe('after');
|
||||
});
|
||||
|
||||
it('updates nested properties in LogMeta', () => {
|
||||
// @ts-expect-error ECS custom meta
|
||||
const log = createLogRecord({ a: 'before a', b: { c: 'before b.c' }, d: [0, 1] });
|
||||
const log = createLogRecord({
|
||||
error: { message: 'before b.c' },
|
||||
tags: ['0', '1'],
|
||||
});
|
||||
const policy = createPolicy('update', [
|
||||
{ path: 'a', value: 'after a' },
|
||||
{ path: 'b.c', value: 'after b.c' },
|
||||
{ path: 'd[1]', value: 2 },
|
||||
{ path: 'error.message', value: 'after b.c' },
|
||||
{ path: 'tags[1]', value: '2' },
|
||||
]);
|
||||
expect(policy.rewrite(log).meta).toMatchInlineSnapshot(`
|
||||
Object {
|
||||
"a": "after a",
|
||||
"b": Object {
|
||||
"c": "after b.c",
|
||||
"error": Object {
|
||||
"message": "after b.c",
|
||||
},
|
||||
"d": Array [
|
||||
0,
|
||||
2,
|
||||
"tags": Array [
|
||||
"0",
|
||||
"2",
|
||||
],
|
||||
}
|
||||
`);
|
||||
|
@ -80,14 +81,13 @@ describe('MetaRewritePolicy', () => {
|
|||
|
||||
it(`does not add properties which don't exist yet`, () => {
|
||||
const policy = createPolicy('update', [
|
||||
{ path: 'a.b', value: 'foo' },
|
||||
{ path: 'a.c', value: 'bar' },
|
||||
{ path: 'error.message', value: 'foo' },
|
||||
{ path: 'error.id', value: 'bar' },
|
||||
]);
|
||||
// @ts-expect-error ECS custom meta
|
||||
const log = createLogRecord({ a: { b: 'existing meta' } });
|
||||
const log = createLogRecord({ error: { message: 'existing meta' } });
|
||||
const { meta } = policy.rewrite(log);
|
||||
expect(meta!.a.b).toBe('foo');
|
||||
expect(meta!.a.c).toBeUndefined();
|
||||
expect(meta?.error?.message).toBe('foo');
|
||||
expect(meta?.error?.id).toBeUndefined();
|
||||
});
|
||||
|
||||
it('does not touch anything outside of LogMeta', () => {
|
||||
|
@ -110,22 +110,19 @@ describe('MetaRewritePolicy', () => {
|
|||
|
||||
describe('mode: remove', () => {
|
||||
it('removes existing properties in LogMeta', () => {
|
||||
// @ts-expect-error ECS custom meta
|
||||
const log = createLogRecord({ a: 'goodbye' });
|
||||
const policy = createPolicy('remove', [{ path: 'a' }]);
|
||||
expect(policy.rewrite(log).meta!.a).toBeUndefined();
|
||||
const log = createLogRecord({ error: { message: 'before' } });
|
||||
const policy = createPolicy('remove', [{ path: 'error' }]);
|
||||
expect(policy.rewrite(log).meta?.error).toBeUndefined();
|
||||
});
|
||||
|
||||
it('removes nested properties in LogMeta', () => {
|
||||
// @ts-expect-error ECS custom meta
|
||||
const log = createLogRecord({ a: 'a', b: { c: 'b.c' }, d: [0, 1] });
|
||||
const policy = createPolicy('remove', [{ path: 'b.c' }, { path: 'd[1]' }]);
|
||||
const log = createLogRecord({ error: { message: 'reason' }, tags: ['0', '1'] });
|
||||
const policy = createPolicy('remove', [{ path: 'error.message' }, { path: 'tags[1]' }]);
|
||||
expect(policy.rewrite(log).meta).toMatchInlineSnapshot(`
|
||||
Object {
|
||||
"a": "a",
|
||||
"b": Object {},
|
||||
"d": Array [
|
||||
0,
|
||||
"error": Object {},
|
||||
"tags": Array [
|
||||
"0",
|
||||
undefined,
|
||||
],
|
||||
}
|
||||
|
@ -133,12 +130,11 @@ describe('MetaRewritePolicy', () => {
|
|||
});
|
||||
|
||||
it('has no effect if property does not exist', () => {
|
||||
// @ts-expect-error ECS custom meta
|
||||
const log = createLogRecord({ a: 'a' });
|
||||
const log = createLogRecord({ error: {} });
|
||||
const policy = createPolicy('remove', [{ path: 'b' }]);
|
||||
expect(policy.rewrite(log).meta).toMatchInlineSnapshot(`
|
||||
Object {
|
||||
"a": "a",
|
||||
"error": Object {},
|
||||
}
|
||||
`);
|
||||
});
|
||||
|
|
|
@ -98,6 +98,7 @@ test('`format()` correctly formats record with meta-data', () => {
|
|||
timestamp,
|
||||
pid: 5355,
|
||||
meta: {
|
||||
// @ts-expect-error ECS custom meta
|
||||
version: {
|
||||
from: 'v7',
|
||||
to: 'v8',
|
||||
|
@ -140,6 +141,7 @@ test('`format()` correctly formats error record with meta-data', () => {
|
|||
timestamp,
|
||||
pid: 5355,
|
||||
meta: {
|
||||
// @ts-expect-error ECS custom meta
|
||||
version: {
|
||||
from: 'v7',
|
||||
to: 'v8',
|
||||
|
@ -182,6 +184,7 @@ test('format() meta can merge override logs', () => {
|
|||
pid: 3,
|
||||
meta: {
|
||||
log: {
|
||||
// @ts-expect-error ECS custom meta
|
||||
kbn_custom_field: 'hello',
|
||||
},
|
||||
},
|
||||
|
@ -213,6 +216,7 @@ test('format() meta can not override message', () => {
|
|||
context: 'bar',
|
||||
pid: 3,
|
||||
meta: {
|
||||
// @ts-expect-error cannot override message
|
||||
message: 'baz',
|
||||
},
|
||||
})
|
||||
|
@ -242,7 +246,8 @@ test('format() meta can not override ecs version', () => {
|
|||
context: 'bar',
|
||||
pid: 3,
|
||||
meta: {
|
||||
message: 'baz',
|
||||
// @ts-expect-error cannot override ecs version
|
||||
ecs: 1,
|
||||
},
|
||||
})
|
||||
)
|
||||
|
@ -272,6 +277,7 @@ test('format() meta can not override logger or level', () => {
|
|||
pid: 3,
|
||||
meta: {
|
||||
log: {
|
||||
// @ts-expect-error cannot override log.level
|
||||
level: 'IGNORE',
|
||||
logger: 'me',
|
||||
},
|
||||
|
@ -303,6 +309,7 @@ test('format() meta can not override timestamp', () => {
|
|||
context: 'bar',
|
||||
pid: 3,
|
||||
meta: {
|
||||
// @ts-expect-error cannot override @timestamp
|
||||
'@timestamp': '2099-02-01T09:30:22.011-05:00',
|
||||
},
|
||||
})
|
||||
|
@ -332,9 +339,9 @@ test('format() meta can not override tracing properties', () => {
|
|||
context: 'bar',
|
||||
pid: 3,
|
||||
meta: {
|
||||
span: 'span_override',
|
||||
trace: 'trace_override',
|
||||
transaction: 'transaction_override',
|
||||
span: { id: 'span_override' },
|
||||
trace: { id: 'trace_override' },
|
||||
transaction: { id: 'transaction_override' },
|
||||
},
|
||||
spanId: 'spanId-1',
|
||||
traceId: 'traceId-1',
|
||||
|
|
|
@ -118,6 +118,7 @@ test('`format()` correctly formats record with meta data.', () => {
|
|||
timestamp,
|
||||
pid: 5355,
|
||||
meta: {
|
||||
// @ts-expect-error not valid ECS field
|
||||
from: 'v7',
|
||||
to: 'v8',
|
||||
},
|
||||
|
@ -177,6 +178,7 @@ test('`format()` allows specifying pattern with meta.', () => {
|
|||
to: 'v8',
|
||||
},
|
||||
};
|
||||
// @ts-expect-error not valid ECS field
|
||||
expect(layout.format(record)).toBe('context-{"from":"v7","to":"v8"}-message');
|
||||
});
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue