diff --git a/x-pack/plugins/infra/common/http_api/log_sources/log_source_configuration.ts b/x-pack/plugins/infra/common/http_api/log_sources/log_source_configuration.ts index ca640fb8f6cd0..ea723e12256eb 100644 --- a/x-pack/plugins/infra/common/http_api/log_sources/log_source_configuration.ts +++ b/x-pack/plugins/infra/common/http_api/log_sources/log_source_configuration.ts @@ -40,7 +40,7 @@ const logSourceMessageColumnConfigurationRT = rt.strict({ messageColumn: logSourceCommonColumnConfigurationRT, }); -const logSourceFieldColumnConfigurationRT = rt.strict({ +export const logSourceFieldColumnConfigurationRT = rt.strict({ fieldColumn: rt.intersection([ logSourceCommonColumnConfigurationRT, rt.strict({ diff --git a/x-pack/plugins/infra/server/services/log_entries/log_entries_search_strategy.ts b/x-pack/plugins/infra/server/services/log_entries/log_entries_search_strategy.ts index d8fb409f4eef3..bf7e497385f9a 100644 --- a/x-pack/plugins/infra/server/services/log_entries/log_entries_search_strategy.ts +++ b/x-pack/plugins/infra/server/services/log_entries/log_entries_search_strategy.ts @@ -18,7 +18,11 @@ import type { ISearchStrategy, PluginStart as DataPluginStart, } from '../../../../../../src/plugins/data/server'; -import { LogSourceColumnConfiguration } from '../../../common/http_api/log_sources'; +import { + LogSourceColumnConfiguration, + LogSourceConfigurationProperties, + logSourceFieldColumnConfigurationRT, +} from '../../../common/http_api/log_sources'; import { getLogEntryCursorFromHit, LogColumn, @@ -103,7 +107,7 @@ export const logEntriesSearchStrategyProvider = ({ params.size + 1, configuration.fields.timestamp, configuration.fields.tiebreaker, - messageFormattingRules.requiredFields, + getRequiredFields(configuration, messageFormattingRules, params.columns), params.query, params.highlightPhrase ), @@ -125,7 +129,12 @@ export const logEntriesSearchStrategyProvider = ({ const entries = rawResponse.hits.hits .slice(0, request.params.size) - .map(getLogEntryFromHit(configuration.logColumns, messageFormattingRules)); + .map( + getLogEntryFromHit( + request.params.columns ? request.params.columns : configuration.logColumns, + messageFormattingRules + ) + ); const sortDirection = getSortDirection(pickRequestCursor(request.params)); @@ -244,3 +253,26 @@ function getResponseCursors(entries: LogEntry[]) { return { topCursor, bottomCursor }; } + +const VIEW_IN_CONTEXT_FIELDS = ['log.file.path', 'host.name', 'container.id']; + +const getRequiredFields = ( + configuration: LogSourceConfigurationProperties, + messageFormattingRules: CompiledLogMessageFormattingRule, + columnOverrides?: LogSourceColumnConfiguration[] +): string[] => { + const columns = columnOverrides ? columnOverrides : configuration.logColumns; + + const fieldsFromColumns = columns.reduce((accumulatedFields, logColumn) => { + if (logSourceFieldColumnConfigurationRT.is(logColumn)) { + return [...accumulatedFields, logColumn.fieldColumn.field]; + } + return accumulatedFields; + }, []); + + const fieldsFromFormattingRules = messageFormattingRules.requiredFields; + + return Array.from( + new Set([...fieldsFromColumns, ...fieldsFromFormattingRules, ...VIEW_IN_CONTEXT_FIELDS]) + ); +}; diff --git a/x-pack/plugins/infra/server/services/log_entries/queries/log_entries.ts b/x-pack/plugins/infra/server/services/log_entries/queries/log_entries.ts index 613469fe75816..460703b22766f 100644 --- a/x-pack/plugins/infra/server/services/log_entries/queries/log_entries.ts +++ b/x-pack/plugins/infra/server/services/log_entries/queries/log_entries.ts @@ -20,8 +20,6 @@ import { } from '../../../utils/elasticsearch_runtime_types'; import { createSortClause, createTimeRangeFilterClauses } from './common'; -const CONTEXT_FIELDS = ['log.file.path', 'host.name', 'container.id']; - export const createGetLogEntriesQuery = ( logEntriesIndex: string, startTimestamp: number, @@ -36,7 +34,6 @@ export const createGetLogEntriesQuery = ( ): RequestParams.AsyncSearchSubmit> => { const sortDirection = getSortDirection(cursor); const highlightQuery = createHighlightQuery(highlightTerm, fields); - const fieldsWithContext = createFieldsWithContext(fields); return { index: logEntriesIndex, @@ -54,7 +51,7 @@ export const createGetLogEntriesQuery = ( ], }, }, - fields: fieldsWithContext, + fields, _source: false, ...createSortClause(sortDirection, timestampField, tiebreakerField), ...createSearchAfterClause(cursor), @@ -120,9 +117,6 @@ const createHighlightQuery = ( } }; -const createFieldsWithContext = (fields: string[]): string[] => - Array.from(new Set([...fields, ...CONTEXT_FIELDS])); - export const logEntryHitRT = rt.intersection([ commonHitFieldsRT, rt.type({