-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Logs UI] HTTP API for log entries #53798
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
c807c4e
Scaffold `log_entries/entries` route
1fc7663
Scaffold a log entry response
3699bcf
Add `after` pagination
30a0a8d
Add `before` pagination
38435ab
Process `query` parameter
c57e50d
Use pre-existing structure for the columns
57ad6d5
Change type of date ranges
54dfd0f
Add `center` parameter
97f87ec
Change default page size
6b6b15b
Test the defaults of the API
c52b511
Add optional `size` parameter
54efba4
Test the pagination
6e17c53
Test centering around a point
d04f15e
Handle `0` sizes
afgomez 5829456
Add highlights endpoint
c454479
Refactor `processCursor`
c5e231a
Tweak cursor handling in the routes
1971e5c
Refine `LogEntry` type
1836ba3
Add tests for highlights endpoint
f860c5e
Merge branch 'master' into 51047-log-entries-http-api
451be62
Tweak the types for the LogEntry
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
x-pack/legacy/plugins/infra/common/http_api/log_entries/entries.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import * as rt from 'io-ts'; | ||
import { logEntriesCursorRT } from './common'; | ||
|
||
export const LOG_ENTRIES_PATH = '/api/log_entries/entries'; | ||
|
||
export const logEntriesBaseRequestRT = rt.intersection([ | ||
rt.type({ | ||
sourceId: rt.string, | ||
startDate: rt.number, | ||
endDate: rt.number, | ||
}), | ||
rt.partial({ | ||
query: rt.string, | ||
size: rt.number, | ||
}), | ||
]); | ||
|
||
export const logEntriesBeforeRequestRT = rt.intersection([ | ||
logEntriesBaseRequestRT, | ||
rt.type({ before: rt.union([logEntriesCursorRT, rt.literal('last')]) }), | ||
]); | ||
|
||
export const logEntriesAfterRequestRT = rt.intersection([ | ||
logEntriesBaseRequestRT, | ||
rt.type({ after: rt.union([logEntriesCursorRT, rt.literal('first')]) }), | ||
]); | ||
|
||
export const logEntriesCenteredRT = rt.intersection([ | ||
logEntriesBaseRequestRT, | ||
rt.type({ center: logEntriesCursorRT }), | ||
]); | ||
|
||
export const logEntriesRequestRT = rt.union([ | ||
logEntriesBaseRequestRT, | ||
logEntriesBeforeRequestRT, | ||
logEntriesAfterRequestRT, | ||
logEntriesCenteredRT, | ||
]); | ||
|
||
export type LogEntriesRequest = rt.TypeOf<typeof logEntriesRequestRT>; | ||
|
||
// JSON value | ||
const valueRT = rt.union([rt.string, rt.number, rt.boolean, rt.object, rt.null, rt.undefined]); | ||
|
||
export const logMessagePartRT = rt.union([ | ||
rt.type({ | ||
constant: rt.string, | ||
}), | ||
rt.type({ | ||
field: rt.string, | ||
value: valueRT, | ||
highlights: rt.array(rt.string), | ||
}), | ||
]); | ||
|
||
export const logColumnRT = rt.union([ | ||
rt.type({ columnId: rt.string, timestamp: rt.number }), | ||
rt.type({ | ||
columnId: rt.string, | ||
field: rt.string, | ||
value: rt.union([rt.string, rt.undefined]), | ||
highlights: rt.array(rt.string), | ||
}), | ||
rt.type({ | ||
columnId: rt.string, | ||
message: rt.array(logMessagePartRT), | ||
}), | ||
]); | ||
|
||
export const logEntryRT = rt.type({ | ||
id: rt.string, | ||
cursor: logEntriesCursorRT, | ||
columns: rt.array(logColumnRT), | ||
}); | ||
|
||
export type LogMessagepart = rt.TypeOf<typeof logMessagePartRT>; | ||
export type LogColumn = rt.TypeOf<typeof logColumnRT>; | ||
export type LogEntry = rt.TypeOf<typeof logEntryRT>; | ||
|
||
export const logEntriesResponseRT = rt.type({ | ||
data: rt.type({ | ||
entries: rt.array(logEntryRT), | ||
topCursor: logEntriesCursorRT, | ||
bottomCursor: logEntriesCursorRT, | ||
}), | ||
}); | ||
|
||
export type LogEntriesResponse = rt.TypeOf<typeof logEntriesResponseRT>; |
62 changes: 62 additions & 0 deletions
62
x-pack/legacy/plugins/infra/common/http_api/log_entries/highlights.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import * as rt from 'io-ts'; | ||
import { | ||
logEntriesBaseRequestRT, | ||
logEntriesBeforeRequestRT, | ||
logEntriesAfterRequestRT, | ||
logEntriesCenteredRT, | ||
logEntryRT, | ||
} from './entries'; | ||
import { logEntriesCursorRT } from './common'; | ||
|
||
export const LOG_ENTRIES_HIGHLIGHTS_PATH = '/api/log_entries/highlights'; | ||
|
||
const highlightsRT = rt.type({ | ||
highlightTerms: rt.array(rt.string), | ||
}); | ||
|
||
export const logEntriesHighlightsBaseRequestRT = rt.intersection([ | ||
logEntriesBaseRequestRT, | ||
highlightsRT, | ||
]); | ||
|
||
export const logEntriesHighlightsBeforeRequestRT = rt.intersection([ | ||
logEntriesBeforeRequestRT, | ||
highlightsRT, | ||
]); | ||
|
||
export const logEntriesHighlightsAfterRequestRT = rt.intersection([ | ||
logEntriesAfterRequestRT, | ||
highlightsRT, | ||
]); | ||
|
||
export const logEntriesHighlightsCenteredRequestRT = rt.intersection([ | ||
logEntriesCenteredRT, | ||
highlightsRT, | ||
]); | ||
|
||
export const logEntriesHighlightsRequestRT = rt.union([ | ||
logEntriesHighlightsBaseRequestRT, | ||
logEntriesHighlightsBeforeRequestRT, | ||
logEntriesHighlightsAfterRequestRT, | ||
logEntriesHighlightsCenteredRequestRT, | ||
]); | ||
|
||
export type LogEntriesHighlightsRequest = rt.TypeOf<typeof logEntriesHighlightsRequestRT>; | ||
|
||
export const logEntriesHighlightsResponseRT = rt.type({ | ||
data: rt.array( | ||
rt.type({ | ||
topCursor: logEntriesCursorRT, | ||
bottomCursor: logEntriesCursorRT, | ||
entries: rt.array(logEntryRT), | ||
}) | ||
), | ||
}); | ||
|
||
export type LogEntriesHighlightsResponse = rt.TypeOf<typeof logEntriesHighlightsResponseRT>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
|
||
import { timeMilliseconds } from 'd3-time'; | ||
import * as runtimeTypes from 'io-ts'; | ||
import { compact } from 'lodash'; | ||
import first from 'lodash/fp/first'; | ||
import get from 'lodash/fp/get'; | ||
import has from 'lodash/fp/has'; | ||
|
@@ -17,12 +18,14 @@ import { map, fold } from 'fp-ts/lib/Either'; | |
import { identity, constant } from 'fp-ts/lib/function'; | ||
import { RequestHandlerContext } from 'src/core/server'; | ||
import { compareTimeKeys, isTimeKey, TimeKey } from '../../../../common/time'; | ||
import { JsonObject } from '../../../../common/typed_json'; | ||
import { JsonObject, JsonValue } from '../../../../common/typed_json'; | ||
import { | ||
LogEntriesAdapter, | ||
LogEntriesParams, | ||
LogEntryDocument, | ||
LogEntryQuery, | ||
LogSummaryBucket, | ||
LOG_ENTRIES_PAGE_SIZE, | ||
} from '../../domains/log_entries_domain'; | ||
import { InfraSourceConfiguration } from '../../sources'; | ||
import { SortedSearchHit } from '../framework'; | ||
|
@@ -82,6 +85,84 @@ export class InfraKibanaLogEntriesAdapter implements LogEntriesAdapter { | |
return direction === 'asc' ? documents : documents.reverse(); | ||
} | ||
|
||
public async getLogEntries( | ||
requestContext: RequestHandlerContext, | ||
sourceConfiguration: InfraSourceConfiguration, | ||
fields: string[], | ||
params: LogEntriesParams | ||
): Promise<LogEntryDocument[]> { | ||
const { startDate, endDate, query, cursor, size, highlightTerm } = params; | ||
|
||
const { sortDirection, searchAfterClause } = processCursor(cursor); | ||
|
||
const highlightQuery = createHighlightQuery(highlightTerm, fields); | ||
|
||
const highlightClause = highlightQuery | ||
? { | ||
highlight: { | ||
boundary_scanner: 'word', | ||
fields: fields.reduce( | ||
(highlightFieldConfigs, fieldName) => ({ | ||
...highlightFieldConfigs, | ||
[fieldName]: {}, | ||
}), | ||
{} | ||
), | ||
fragment_size: 1, | ||
number_of_fragments: 100, | ||
post_tags: [''], | ||
pre_tags: [''], | ||
highlight_query: highlightQuery, | ||
}, | ||
} | ||
: {}; | ||
|
||
const sort = { | ||
[sourceConfiguration.fields.timestamp]: sortDirection, | ||
[sourceConfiguration.fields.tiebreaker]: sortDirection, | ||
}; | ||
|
||
const esQuery = { | ||
allowNoIndices: true, | ||
index: sourceConfiguration.logAlias, | ||
ignoreUnavailable: true, | ||
body: { | ||
size: typeof size !== 'undefined' ? size : LOG_ENTRIES_PAGE_SIZE, | ||
track_total_hits: false, | ||
_source: fields, | ||
query: { | ||
bool: { | ||
filter: [ | ||
...createFilterClauses(query, highlightQuery), | ||
{ | ||
range: { | ||
[sourceConfiguration.fields.timestamp]: { | ||
gte: startDate, | ||
lte: endDate, | ||
format: TIMESTAMP_FORMAT, | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
sort, | ||
...highlightClause, | ||
...searchAfterClause, | ||
}, | ||
}; | ||
|
||
const esResult = await this.framework.callWithRequest<SortedSearchHit>( | ||
requestContext, | ||
'search', | ||
esQuery | ||
); | ||
|
||
const hits = sortDirection === 'asc' ? esResult.hits.hits : esResult.hits.hits.reverse(); | ||
return mapHitsToLogEntryDocuments(hits, sourceConfiguration.fields.timestamp, fields); | ||
} | ||
|
||
/** @deprecated */ | ||
public async getContainedLogEntryDocuments( | ||
requestContext: RequestHandlerContext, | ||
sourceConfiguration: InfraSourceConfiguration, | ||
|
@@ -319,6 +400,34 @@ function getLookupIntervals(start: number, direction: 'asc' | 'desc'): Array<[nu | |
return intervals; | ||
} | ||
|
||
function mapHitsToLogEntryDocuments( | ||
hits: SortedSearchHit[], | ||
timestampField: string, | ||
fields: string[] | ||
): LogEntryDocument[] { | ||
return hits.map(hit => { | ||
const logFields = fields.reduce<{ [fieldName: string]: JsonValue }>( | ||
(flattenedFields, field) => { | ||
if (has(field, hit._source)) { | ||
flattenedFields[field] = get(field, hit._source); | ||
} | ||
return flattenedFields; | ||
}, | ||
{} | ||
); | ||
|
||
return { | ||
gid: hit._id, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With the above comment in mind, this whole function might be easier to follow if you destructure the const { _id: gid, _source: fieldName, sort: [time, tiebreaker] } There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See previous comment :) |
||
// timestamp: hit._source[timestampField], | ||
// FIXME s/key/cursor/g | ||
key: { time: hit.sort[0], tiebreaker: hit.sort[1] }, | ||
fields: logFields, | ||
highlights: hit.highlight || {}, | ||
}; | ||
}); | ||
} | ||
|
||
/** @deprecated */ | ||
const convertHitToLogEntryDocument = (fields: string[]) => ( | ||
hit: SortedSearchHit | ||
): LogEntryDocument => ({ | ||
|
@@ -352,9 +461,62 @@ const convertDateRangeBucketToSummaryBucket = ( | |
})), | ||
}); | ||
|
||
const createHighlightQuery = ( | ||
highlightTerm: string | undefined, | ||
fields: string[] | ||
): LogEntryQuery | undefined => { | ||
if (highlightTerm) { | ||
return { | ||
multi_match: { | ||
fields, | ||
lenient: true, | ||
query: highlightTerm, | ||
type: 'phrase', | ||
}, | ||
}; | ||
} | ||
}; | ||
|
||
const createFilterClauses = ( | ||
filterQuery?: LogEntryQuery, | ||
highlightQuery?: LogEntryQuery | ||
): LogEntryQuery[] => { | ||
if (filterQuery && highlightQuery) { | ||
return [{ bool: { filter: [filterQuery, highlightQuery] } }]; | ||
} | ||
|
||
return compact([filterQuery, highlightQuery]) as LogEntryQuery[]; | ||
}; | ||
|
||
const createQueryFilterClauses = (filterQuery: LogEntryQuery | undefined) => | ||
filterQuery ? [filterQuery] : []; | ||
|
||
function processCursor( | ||
cursor: LogEntriesParams['cursor'] | ||
): { | ||
sortDirection: 'asc' | 'desc'; | ||
searchAfterClause: { search_after?: readonly [number, number] }; | ||
} { | ||
if (cursor) { | ||
if ('before' in cursor) { | ||
return { | ||
sortDirection: 'desc', | ||
searchAfterClause: | ||
cursor.before !== 'last' | ||
? { search_after: [cursor.before.time, cursor.before.tiebreaker] as const } | ||
: {}, | ||
}; | ||
} else if (cursor.after !== 'first') { | ||
return { | ||
sortDirection: 'asc', | ||
searchAfterClause: { search_after: [cursor.after.time, cursor.after.tiebreaker] as const }, | ||
}; | ||
} | ||
} | ||
|
||
return { sortDirection: 'asc', searchAfterClause: {} }; | ||
} | ||
|
||
const LogSummaryDateRangeBucketRuntimeType = runtimeTypes.intersection([ | ||
runtimeTypes.type({ | ||
doc_count: runtimeTypes.number, | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hard to understand at a glance that
hit._source
is anobject.path.like.this
. Anything you can do with type definitions to make this more obvious? Or maybe just add a commentThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I hear you, but I don't want to dedicate much time to this function. It will go away in a separate PR.
Right now the API is handled in three files
lib/adapters/kibana_log_entries_adapter
, which connects with Elasticsearch.lib/domains/log_entries_domain
, which connects the adapter with the route filesThis function in the adapter takes the ES response and transforms it onto a
LogEntryDocument
, an intermediate format for thedomain
that then gets transformed again in the route.I had a chat with @weltenwort and @Kerry350 a couple of weeks ago about how this code was organised, and the conclusion was to merge the
domain
and theadapter
files into one. Once we do that we don't need an intermediate format anymore and this function will go away.I will take your comment into account when I join the two files in one. I agree with you that it's not clear straight away what is in
_source
. I guess there's some documentation somewhere of how filebeat stores the log metadata in ES. We could just add a comment with a link to it.