Skip to content

Commit

Permalink
[7.x] [Logs UI] Add context fields to /log_entries/entries… (#61784)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alejandro Fernández authored Mar 30, 2020
1 parent e2e3a2a commit e3f16a9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
5 changes: 5 additions & 0 deletions x-pack/plugins/infra/common/http_api/log_entries/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ export const logEntryRT = rt.type({
id: rt.string,
cursor: logEntriesCursorRT,
columns: rt.array(logColumnRT),
context: rt.partial({
'log.file.path': rt.string,
'host.name': rt.string,
'container.id': rt.string,
}),
});

export type LogMessageConstantPart = rt.TypeOf<typeof logMessageConstantPartRT>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ export interface LogEntriesAroundParams {

export const LOG_ENTRIES_PAGE_SIZE = 200;

const FIELDS_FROM_CONTEXT = ['log.file.path', 'host.name', 'container.id'] as const;

export class InfraLogEntriesDomain {
constructor(
private readonly adapter: LogEntriesAdapter,
Expand Down Expand Up @@ -154,6 +156,14 @@ export class InfraLogEntriesDomain {
}
}
),
context: FIELDS_FROM_CONTEXT.reduce<LogEntry['context']>((ctx, field) => {
// Users might have different types here in their mappings.
const value = doc.fields[field];
if (typeof value === 'string') {
ctx[field] = value;
}
return ctx;
}, {}),
};
});

Expand Down Expand Up @@ -329,7 +339,9 @@ const getRequiredFields = (
);
const fieldsFromFormattingRules = messageFormattingRules.requiredFields;

return Array.from(new Set([...fieldsFromCustomColumns, ...fieldsFromFormattingRules]));
return Array.from(
new Set([...fieldsFromCustomColumns, ...fieldsFromFormattingRules, ...FIELDS_FROM_CONTEXT])
);
};

const createHighlightQueryDsl = (phrase: string, fields: string[]) => ({
Expand Down
26 changes: 26 additions & 0 deletions x-pack/test/api_integration/apis/infra/log_entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,32 @@ export default function({ getService }: FtrProviderContext) {
expect(messageColumn.message.length).to.be.greaterThan(0);
});

it('Returns the context fields', async () => {
const { body } = await supertest
.post(LOG_ENTRIES_PATH)
.set(COMMON_HEADERS)
.send(
logEntriesRequestRT.encode({
sourceId: 'default',
startTimestamp: EARLIEST_KEY_WITH_DATA.time,
endTimestamp: LATEST_KEY_WITH_DATA.time,
center: KEY_WITHIN_DATA_RANGE,
})
)
.expect(200);

const logEntriesResponse = pipe(
logEntriesResponseRT.decode(body),
fold(throwErrors(createPlainError), identity)
);

const entries = logEntriesResponse.data.entries;
const entry = entries[0];

expect(entry.context).to.have.property('host.name');
expect(entry.context['host.name']).to.be('demo-stack-nginx-01');
});

it('Paginates correctly with `after`', async () => {
const { body: firstPageBody } = await supertest
.post(LOG_ENTRIES_PATH)
Expand Down

0 comments on commit e3f16a9

Please sign in to comment.