Skip to content

Commit

Permalink
Utilise index in log item flyout to prevent scanning of all available…
Browse files Browse the repository at this point in the history
… indices on getLogItem which can bring down small clusters
  • Loading branch information
driskell committed Apr 6, 2020
1 parent c19e0da commit 284037b
Show file tree
Hide file tree
Showing 15 changed files with 81 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const sharedFragments = {
InfraLogEntryFields: gql`
fragment InfraLogEntryFields on InfraLogEntry {
gid
index
key {
time
tiebreaker
Expand Down
6 changes: 6 additions & 0 deletions x-pack/plugins/infra/common/graphql/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ export interface InfraLogEntry {
key: InfraTimeKey;
/** The log entry's id */
gid: string;
/** The log entry's index */
index: string;
/** The source id */
source: string;
/** The columns used for rendering the log entry */
Expand Down Expand Up @@ -993,6 +995,8 @@ export namespace InfraLogEntryFields {

gid: string;

index: string;

key: Key;

columns: Columns[];
Expand Down Expand Up @@ -1062,6 +1066,8 @@ export namespace InfraLogEntryHighlightFields {

gid: string;

index: string;

key: Key;

columns: Columns[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export const logColumnRT = rt.union([logTimestampColumnRT, logFieldColumnRT, log

export const logEntryRT = rt.type({
id: rt.string,
index: rt.string,
cursor: logEntriesCursorRT,
columns: rt.array(logColumnRT),
context: rt.partial({
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/infra/common/http_api/log_entries/item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const LOG_ENTRIES_ITEM_PATH = '/api/log_entries/item';
export const logEntriesItemRequestRT = rt.type({
sourceId: rt.string,
id: rt.string,
index: rt.string,
});

export type LogEntriesItemRequest = rt.TypeOf<typeof logEntriesItemRequestRT>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ interface LogEntryRowProps {
isActiveHighlight: boolean;
isHighlighted: boolean;
logEntry: LogEntry;
openFlyoutWithItem?: (id: string) => void;
openFlyoutWithItem?: (id: string, index: string) => void;
scale: TextScale;
wrap: boolean;
}
Expand All @@ -58,9 +58,10 @@ export const LogEntryRow = memo(
const setItemIsHovered = useCallback(() => setIsHovered(true), []);
const setItemIsNotHovered = useCallback(() => setIsHovered(false), []);

const openFlyout = useCallback(() => openFlyoutWithItem?.(logEntry.id), [
const openFlyout = useCallback(() => openFlyoutWithItem?.(logEntry.id, logEntry.index), [
openFlyoutWithItem,
logEntry.id,
logEntry.index,
]);

const logEntryColumnsById = useMemo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ interface ScrollableLogTextStreamViewProps {
}) => any;
loadNewerItems: () => void;
reloadItems: () => void;
setFlyoutItem: (id: string) => void;
setFlyoutRef: (ref: { id: string; index: string }) => void;
setFlyoutVisibility: (visible: boolean) => void;
highlightedItem: string | null;
currentHighlightKey: UniqueTimeKey | null;
Expand Down Expand Up @@ -286,8 +286,8 @@ export class ScrollableLogTextStreamView extends React.PureComponent<
);
}

private handleOpenFlyout = (id: string) => {
this.props.setFlyoutItem(id);
private handleOpenFlyout = (id: string, index: string) => {
this.props.setFlyoutRef({ id, index });
this.props.setFlyoutVisibility(true);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ export interface InfraLogEntry {
key: InfraTimeKey;
/** The log entry's id */
gid: string;
/** The log entry's index */
index: string;
/** The source id */
source: string;
/** The columns used for rendering the log entry */
Expand Down
36 changes: 21 additions & 15 deletions x-pack/plugins/infra/public/containers/logs/log_flyout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,26 @@ export enum FlyoutVisibility {

export interface FlyoutOptionsUrlState {
flyoutId?: string | null;
flyoutIndex?: string | null;
flyoutVisibility?: string | null;
surroundingLogsId?: string | null;
}

export const useLogFlyout = () => {
const { sourceId } = useContext(Source.Context);
const [flyoutVisible, setFlyoutVisibility] = useState<boolean>(false);
const [flyoutId, setFlyoutId] = useState<string | null>(null);
const [flyoutRef, setFlyoutRef] = useState<{ id: string; index: string } | null>(null);
const [flyoutItem, setFlyoutItem] = useState<LogEntriesItem | null>(null);
const [surroundingLogsId, setSurroundingLogsId] = useState<string | null>(null);

const [loadFlyoutItemRequest, loadFlyoutItem] = useTrackedPromise(
{
cancelPreviousOn: 'creation',
createPromise: async () => {
if (!flyoutId) {
if (!flyoutRef) {
return;
}
return await fetchLogEntriesItem({ sourceId, id: flyoutId });
return await fetchLogEntriesItem({ sourceId, id: flyoutRef.id, index: flyoutRef.index });
},
onResolve: response => {
if (response) {
Expand All @@ -48,24 +49,24 @@ export const useLogFlyout = () => {
}
},
},
[sourceId, flyoutId]
[sourceId, flyoutRef]
);

const isLoading = useMemo(() => {
return loadFlyoutItemRequest.state === 'pending';
}, [loadFlyoutItemRequest.state]);

useEffect(() => {
if (flyoutId) {
if (flyoutRef) {
loadFlyoutItem();
}
}, [loadFlyoutItem, flyoutId]);
}, [loadFlyoutItem, flyoutRef]);

return {
flyoutVisible,
setFlyoutVisibility,
flyoutId,
setFlyoutId,
flyoutRef,
setFlyoutRef,
surroundingLogsId,
setSurroundingLogsId,
isLoading,
Expand All @@ -79,8 +80,8 @@ export const WithFlyoutOptionsUrlState = () => {
const {
flyoutVisible,
setFlyoutVisibility,
flyoutId,
setFlyoutId,
flyoutRef,
setFlyoutRef,
surroundingLogsId,
setSurroundingLogsId,
} = useContext(LogFlyout.Context);
Expand All @@ -89,14 +90,15 @@ export const WithFlyoutOptionsUrlState = () => {
<UrlStateContainer
urlState={{
flyoutVisibility: flyoutVisible ? FlyoutVisibility.visible : FlyoutVisibility.hidden,
flyoutId,
flyoutId: flyoutRef ? flyoutRef.id : null,
flyoutIndex: flyoutRef ? flyoutRef.index : null,
surroundingLogsId,
}}
urlStateKey="flyoutOptions"
mapToUrlState={mapToUrlState}
onChange={newUrlState => {
if (newUrlState && newUrlState.flyoutId) {
setFlyoutId(newUrlState.flyoutId);
if (newUrlState && newUrlState.flyoutId && newUrlState.flyoutIndex) {
setFlyoutRef({ id: newUrlState.flyoutId, index: newUrlState.flyoutIndex });
}
if (newUrlState && newUrlState.surroundingLogsId) {
setSurroundingLogsId(newUrlState.surroundingLogsId);
Expand All @@ -109,8 +111,8 @@ export const WithFlyoutOptionsUrlState = () => {
}
}}
onInitialize={initialUrlState => {
if (initialUrlState && initialUrlState.flyoutId) {
setFlyoutId(initialUrlState.flyoutId);
if (initialUrlState && initialUrlState.flyoutId && initialUrlState.flyoutIndex) {
setFlyoutRef({ id: initialUrlState.flyoutId, index: initialUrlState.flyoutIndex });
}
if (initialUrlState && initialUrlState.surroundingLogsId) {
setSurroundingLogsId(initialUrlState.surroundingLogsId);
Expand All @@ -130,6 +132,7 @@ const mapToUrlState = (value: any): FlyoutOptionsUrlState | undefined =>
value
? {
flyoutId: mapToFlyoutIdState(value.flyoutId),
flyoutIndex: mapToFlyoutIndexState(value.flyoutIndex),
flyoutVisibility: mapToFlyoutVisibilityState(value.flyoutVisibility),
surroundingLogsId: mapToSurroundingLogsIdState(value.surroundingLogsId),
}
Expand All @@ -138,6 +141,9 @@ const mapToUrlState = (value: any): FlyoutOptionsUrlState | undefined =>
const mapToFlyoutIdState = (subject: any) => {
return subject && isString(subject) ? subject : undefined;
};
const mapToFlyoutIndexState = (subject: any) => {
return subject && isString(subject) ? subject : undefined;
};
const mapToSurroundingLogsIdState = (subject: any) => {
return subject && isString(subject) ? subject : undefined;
};
Expand Down
12 changes: 12 additions & 0 deletions x-pack/plugins/infra/public/graphql/introspection.json
Original file line number Diff line number Diff line change
Expand Up @@ -1185,6 +1185,18 @@
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "index",
"description": "The log entry's index",
"args": [],
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": { "kind": "SCALAR", "name": "String", "ofType": null }
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "source",
"description": "The source id",
Expand Down
6 changes: 6 additions & 0 deletions x-pack/plugins/infra/public/graphql/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ export interface InfraLogEntry {
key: InfraTimeKey;
/** The log entry's id */
gid: string;
/** The log entry's index */
index: string;
/** The source id */
source: string;
/** The columns used for rendering the log entry */
Expand Down Expand Up @@ -995,6 +997,8 @@ export namespace InfraLogEntryFields {

gid: string;

index: string;

key: Key;

columns: Columns[];
Expand Down Expand Up @@ -1064,6 +1068,8 @@ export namespace InfraLogEntryHighlightFields {

gid: string;

index: string;

key: Key;

columns: Columns[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const LogsPageLogsContent: React.FunctionComponent = () => {
const {
setFlyoutVisibility,
flyoutVisible,
setFlyoutId,
setFlyoutRef,
surroundingLogsId,
setSurroundingLogsId,
flyoutItem,
Expand Down Expand Up @@ -102,7 +102,7 @@ export const LogsPageLogsContent: React.FunctionComponent = () => {
scale={textScale}
target={targetPosition}
wrap={textWrap}
setFlyoutItem={setFlyoutId}
setFlyoutRef={setFlyoutRef}
setFlyoutVisibility={setFlyoutVisibility}
highlightedItem={surroundingLogsId ? surroundingLogsId : null}
currentHighlightKey={currentHighlightKey}
Expand Down
9 changes: 9 additions & 0 deletions x-pack/plugins/infra/server/graphql/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ export interface InfraLogEntry {
key: InfraTimeKey;
/** The log entry's id */
gid: string;
/** The log entry's index */
index: string;
/** The source id */
source: string;
/** The columns used for rendering the log entry */
Expand Down Expand Up @@ -1137,6 +1139,8 @@ export namespace InfraLogEntryResolvers {
key?: KeyResolver<InfraTimeKey, TypeParent, Context>;
/** The log entry's id */
gid?: GidResolver<string, TypeParent, Context>;
/** The log entry's index */
index?: IndexResolver<string, TypeParent, Context>;
/** The source id */
source?: SourceResolver<string, TypeParent, Context>;
/** The columns used for rendering the log entry */
Expand All @@ -1153,6 +1157,11 @@ export namespace InfraLogEntryResolvers {
Parent,
Context
>;
export type IndexResolver<R = string, Parent = InfraLogEntry, Context = InfraContext> = Resolver<
R,
Parent,
Context
>;
export type SourceResolver<R = string, Parent = InfraLogEntry, Context = InfraContext> = Resolver<
R,
Parent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,14 @@ export class InfraKibanaLogEntriesAdapter implements LogEntriesAdapter {
public async getLogItem(
requestContext: RequestHandlerContext,
id: string,
index: string,
sourceConfiguration: InfraSourceConfiguration
) {
const search = (searchOptions: object) =>
this.framework.callWithRequest<LogItemHit, {}>(requestContext, 'search', searchOptions);

const params = {
index: sourceConfiguration.logAlias,
index,
terminate_after: 1,
body: {
size: 1,
Expand Down Expand Up @@ -240,6 +241,7 @@ function mapHitsToLogEntryDocuments(hits: SortedSearchHit[], fields: string[]):

return {
id: hit._id,
index: hit._index,
cursor: { time: hit.sort[0], tiebreaker: hit.sort[1] },
fields: logFields,
highlights: hit.highlight || {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,10 @@ export class InfraLogEntriesDomain {
public async getLogItem(
requestContext: RequestHandlerContext,
id: string,
index: string,
sourceConfiguration: InfraSourceConfiguration
): Promise<LogEntriesItem> {
const document = await this.adapter.getLogItem(requestContext, id, sourceConfiguration);
const document = await this.adapter.getLogItem(requestContext, id, index, sourceConfiguration);
const defaultFields = [
{ field: '_index', value: document._index },
{ field: '_id', value: document._id },
Expand Down Expand Up @@ -292,6 +293,7 @@ export interface LogEntriesAdapter {
getLogItem(
requestContext: RequestHandlerContext,
id: string,
index: string,
source: InfraSourceConfiguration
): Promise<LogItemHit>;
}
Expand All @@ -300,6 +302,7 @@ export type LogEntryQuery = JsonObject;

export interface LogEntryDocument {
id: string;
index: string;
fields: Fields;
highlights: Highlights;
cursor: LogEntriesCursor;
Expand Down
9 changes: 7 additions & 2 deletions x-pack/plugins/infra/server/routes/log_entries/item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,16 @@ export const initLogEntriesItemRoute = ({ framework, sources, logEntries }: Infr
fold(throwErrors(Boom.badRequest), identity)
);

const { id, sourceId } = payload;
const { id, index, sourceId } = payload;
const sourceConfiguration = (await sources.getSourceConfiguration(requestContext, sourceId))
.configuration;

const logEntry = await logEntries.getLogItem(requestContext, id, sourceConfiguration);
const logEntry = await logEntries.getLogItem(
requestContext,
id,
index,
sourceConfiguration
);

return response.ok({
body: logEntriesItemResponseRT.encode({
Expand Down

0 comments on commit 284037b

Please sign in to comment.