-
Notifications
You must be signed in to change notification settings - Fork 8.1k
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
[Fleet] Support URL query state in agent logs UI #84298
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
257 changes: 257 additions & 0 deletions
257
...pplications/fleet/sections/agents/agent_details_page/components/agent_logs/agent_logs.tsx
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,257 @@ | ||
/* | ||
* 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 React, { memo, useMemo, useState, useCallback } from 'react'; | ||
import styled from 'styled-components'; | ||
import url from 'url'; | ||
import { encode } from 'rison-node'; | ||
import { stringify } from 'query-string'; | ||
import { | ||
EuiFlexGroup, | ||
EuiFlexItem, | ||
EuiSuperDatePicker, | ||
EuiFilterGroup, | ||
EuiPanel, | ||
EuiButtonEmpty, | ||
} from '@elastic/eui'; | ||
import { FormattedMessage } from '@kbn/i18n/react'; | ||
import semverGte from 'semver/functions/gte'; | ||
import semverCoerce from 'semver/functions/coerce'; | ||
import { createStateContainerReactHelpers } from '../../../../../../../../../../../src/plugins/kibana_utils/public'; | ||
import { RedirectAppLinks } from '../../../../../../../../../../../src/plugins/kibana_react/public'; | ||
import { TimeRange, esKuery } from '../../../../../../../../../../../src/plugins/data/public'; | ||
import { LogStream } from '../../../../../../../../../infra/public'; | ||
import { Agent } from '../../../../../types'; | ||
import { useStartServices } from '../../../../../hooks'; | ||
import { DatasetFilter } from './filter_dataset'; | ||
import { LogLevelFilter } from './filter_log_level'; | ||
import { LogQueryBar } from './query_bar'; | ||
import { buildQuery } from './build_query'; | ||
import { SelectLogLevel } from './select_log_level'; | ||
|
||
const WrapperFlexGroup = styled(EuiFlexGroup)` | ||
height: 100%; | ||
`; | ||
|
||
const DatePickerFlexItem = styled(EuiFlexItem)` | ||
max-width: 312px; | ||
`; | ||
|
||
export interface AgentLogsProps { | ||
agent: Agent; | ||
state: AgentLogsState; | ||
} | ||
|
||
export interface AgentLogsState { | ||
start: string; | ||
end: string; | ||
logLevels: string[]; | ||
datasets: string[]; | ||
query: string; | ||
} | ||
|
||
export const AgentLogsUrlStateHelper = createStateContainerReactHelpers(); | ||
|
||
export const AgentLogsUI: React.FunctionComponent<AgentLogsProps> = memo(({ agent, state }) => { | ||
const { data, application, http } = useStartServices(); | ||
const { update: updateState } = AgentLogsUrlStateHelper.useTransitions(); | ||
|
||
// Util to convert date expressions (returned by datepicker) to timestamps (used by LogStream) | ||
const getDateRangeTimestamps = useCallback( | ||
(timeRange: TimeRange) => { | ||
const { min, max } = data.query.timefilter.timefilter.calculateBounds(timeRange); | ||
return min && max | ||
? { | ||
startTimestamp: min.valueOf(), | ||
endTimestamp: max.valueOf(), | ||
} | ||
: undefined; | ||
}, | ||
[data.query.timefilter.timefilter] | ||
); | ||
|
||
const tryUpdateDateRange = useCallback( | ||
(timeRange: TimeRange) => { | ||
const timestamps = getDateRangeTimestamps(timeRange); | ||
if (timestamps) { | ||
updateState({ | ||
start: timeRange.from, | ||
end: timeRange.to, | ||
}); | ||
} | ||
}, | ||
[getDateRangeTimestamps, updateState] | ||
); | ||
|
||
const dateRangeTimestamps = useMemo( | ||
() => | ||
getDateRangeTimestamps({ | ||
from: state.start, | ||
to: state.end, | ||
}), | ||
[getDateRangeTimestamps, state.end, state.start] | ||
); | ||
|
||
// Query validation helper | ||
const isQueryValid = useCallback((testQuery: string) => { | ||
try { | ||
esKuery.fromKueryExpression(testQuery); | ||
return true; | ||
} catch (err) { | ||
return false; | ||
} | ||
}, []); | ||
|
||
// User query state | ||
const [draftQuery, setDraftQuery] = useState<string>(state.query); | ||
const [isDraftQueryValid, setIsDraftQueryValid] = useState<boolean>(isQueryValid(state.query)); | ||
const onUpdateDraftQuery = useCallback( | ||
(newDraftQuery: string, runQuery?: boolean) => { | ||
setDraftQuery(newDraftQuery); | ||
if (isQueryValid(newDraftQuery)) { | ||
setIsDraftQueryValid(true); | ||
if (runQuery) { | ||
updateState({ query: newDraftQuery }); | ||
} | ||
} else { | ||
setIsDraftQueryValid(false); | ||
} | ||
}, | ||
[isQueryValid, updateState] | ||
); | ||
|
||
// Build final log stream query from agent id, datasets, log levels, and user input | ||
const logStreamQuery = useMemo( | ||
() => | ||
buildQuery({ | ||
agentId: agent.id, | ||
datasets: state.datasets, | ||
logLevels: state.logLevels, | ||
userQuery: state.query, | ||
}), | ||
[agent.id, state.datasets, state.logLevels, state.query] | ||
); | ||
|
||
// Generate URL to pass page state to Logs UI | ||
const viewInLogsUrl = useMemo( | ||
() => | ||
http.basePath.prepend( | ||
url.format({ | ||
pathname: '/app/logs/stream', | ||
search: stringify( | ||
{ | ||
logPosition: encode({ | ||
start: state.start, | ||
end: state.end, | ||
streamLive: false, | ||
}), | ||
logFilter: encode({ | ||
expression: logStreamQuery, | ||
kind: 'kuery', | ||
}), | ||
}, | ||
{ sort: false, encode: false } | ||
), | ||
}) | ||
), | ||
[http.basePath, state.start, state.end, logStreamQuery] | ||
); | ||
|
||
const agentVersion = agent.local_metadata?.elastic?.agent?.version; | ||
const isLogLevelSelectionAvailable = useMemo(() => { | ||
if (!agentVersion) { | ||
return false; | ||
} | ||
const agentVersionWithPrerelease = semverCoerce(agentVersion)?.version; | ||
if (!agentVersionWithPrerelease) { | ||
return false; | ||
} | ||
return semverGte(agentVersionWithPrerelease, '7.11.0'); | ||
}, [agentVersion]); | ||
|
||
return ( | ||
<WrapperFlexGroup direction="column" gutterSize="m"> | ||
<EuiFlexItem grow={false}> | ||
<EuiFlexGroup gutterSize="m"> | ||
<EuiFlexItem> | ||
<LogQueryBar | ||
query={draftQuery} | ||
onUpdateQuery={onUpdateDraftQuery} | ||
isQueryValid={isDraftQueryValid} | ||
/> | ||
</EuiFlexItem> | ||
<EuiFlexItem grow={false}> | ||
<EuiFilterGroup> | ||
<DatasetFilter | ||
selectedDatasets={state.datasets} | ||
onToggleDataset={(dataset: string) => { | ||
const currentDatasets = [...state.datasets]; | ||
const datasetPosition = currentDatasets.indexOf(dataset); | ||
if (datasetPosition >= 0) { | ||
currentDatasets.splice(datasetPosition, 1); | ||
updateState({ datasets: currentDatasets }); | ||
} else { | ||
updateState({ datasets: [...state.datasets, dataset] }); | ||
} | ||
}} | ||
/> | ||
<LogLevelFilter | ||
selectedLevels={state.logLevels} | ||
onToggleLevel={(level: string) => { | ||
const currentLevels = [...state.logLevels]; | ||
const levelPosition = currentLevels.indexOf(level); | ||
if (levelPosition >= 0) { | ||
currentLevels.splice(levelPosition, 1); | ||
updateState({ logLevels: currentLevels }); | ||
} else { | ||
updateState({ logLevels: [...state.logLevels, level] }); | ||
} | ||
}} | ||
/> | ||
</EuiFilterGroup> | ||
</EuiFlexItem> | ||
<DatePickerFlexItem grow={false}> | ||
<EuiSuperDatePicker | ||
showUpdateButton={false} | ||
start={state.start} | ||
end={state.end} | ||
onTimeChange={({ start, end }) => { | ||
tryUpdateDateRange({ | ||
from: start, | ||
to: end, | ||
}); | ||
}} | ||
/> | ||
</DatePickerFlexItem> | ||
<EuiFlexItem grow={false}> | ||
<RedirectAppLinks application={application}> | ||
<EuiButtonEmpty href={viewInLogsUrl} iconType="popout" flush="both"> | ||
<FormattedMessage | ||
id="xpack.fleet.agentLogs.openInLogsUiLinkText" | ||
defaultMessage="Open in Logs" | ||
/> | ||
</EuiButtonEmpty> | ||
</RedirectAppLinks> | ||
</EuiFlexItem> | ||
</EuiFlexGroup> | ||
</EuiFlexItem> | ||
<EuiFlexItem> | ||
<EuiPanel paddingSize="none"> | ||
<LogStream | ||
height="100%" | ||
startTimestamp={dateRangeTimestamps!.startTimestamp} | ||
endTimestamp={dateRangeTimestamps!.endTimestamp} | ||
query={logStreamQuery} | ||
/> | ||
</EuiPanel> | ||
</EuiFlexItem> | ||
{isLogLevelSelectionAvailable && ( | ||
<EuiFlexItem grow={false}> | ||
<SelectLogLevel agent={agent} /> | ||
</EuiFlexItem> | ||
)} | ||
</WrapperFlexGroup> | ||
); | ||
}); |
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
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.
I do not think the bang operator is correct here, if I used this
start:now-1D
in the urlD
instead ofd
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.
good catch, looking into it..
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.
pushed up a fix, bang operator removed and it should handle invalid expressions better now (will reset back to default if the expression is invalid)