Skip to content

Commit

Permalink
Migrate existing insights
Browse files Browse the repository at this point in the history
  • Loading branch information
dgieselaar committed Jul 23, 2023
1 parent 3432888 commit 556b2de
Show file tree
Hide file tree
Showing 42 changed files with 287 additions and 1,499 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export function ErrorSampleContextualInsight({
<ContextualInsight
messages={messages}
title={i18n.translate(
'xpack.apm.errorGroupCoPilotPrompt.explainErrorTitle',
'xpack.apm.errorGroupContextualInsight.explainErrorTitle',
{ defaultMessage: "What's this error?" }
)}
/>
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/infra/kibana.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"lens",
"logsShared",
"observability",
"observabilityAIAssistant",
"observabilityShared",
"ruleRegistry",
"security",
Expand All @@ -36,6 +37,7 @@
"requiredBundles": [
"unifiedSearch",
"observability",
"observabilityAIAssistant",
"licenseManagement",
"kibanaUtils",
"kibanaReact",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import React, { FC, useEffect, useState } from 'react';
import React, { FC, useEffect, useMemo, useState } from 'react';
import { pick, orderBy } from 'lodash';
import moment from 'moment';

Expand All @@ -15,10 +15,15 @@ import { FormattedMessage } from '@kbn/i18n-react';
import { DataView } from '@kbn/data-views-plugin/common';
import { LogRateAnalysisContent, type LogRateAnalysisResultsData } from '@kbn/aiops-plugin/public';
import { Rule } from '@kbn/alerting-plugin/common';
import { CoPilotPrompt, TopAlert, useCoPilot } from '@kbn/observability-plugin/public';
import { TopAlert } from '@kbn/observability-plugin/public';
import {
ContextualInsight,
useObservabilityAIAssistant,
type Message,
MessageRole,
} from '@kbn/observability-ai-assistant-plugin/public';
import { QueryDslQueryContainer } from '@elastic/elasticsearch/lib/api/types';
import { i18n } from '@kbn/i18n';
import { CoPilotPromptId } from '@kbn/observability-plugin/common';
import { ALERT_END } from '@kbn/rule-data-utils';
import { Color, colorTransformer } from '../../../../../../common/color_palette';
import { useKibanaContextForPlugin } from '../../../../../hooks/use_kibana';
Expand Down Expand Up @@ -169,9 +174,54 @@ export const LogRateAnalysis: FC<AlertDetailsLogRateAnalysisSectionProps> = ({ r
setLogSpikeParams(significantFieldValues ? { significantFieldValues } : undefined);
};

const coPilotService = useCoPilot();
const aiAssistant = useObservabilityAIAssistant();

const hasLogSpikeParams = logSpikeParams && logSpikeParams.significantFieldValues?.length > 0;

const messages = useMemo<Message[] | undefined>(() => {
if (!logSpikeParams) {
return undefined;
}
const header = 'Field name,Field value,Doc count,p-value';
const rows = logSpikeParams.significantFieldValues
.map((item) => Object.values(item).join(','))
.join('\n');

const content = `You are an observability expert using Elastic Observability Suite on call being consulted about a log threshold alert that got triggered by a spike of log messages. Your job is to take immediate action and proceed with both urgency and precision.
"Log Rate Analysis" is an AIOps feature that uses advanced statistical methods to identify reasons for increases in log rates. It makes it easy to find and investigate causes of unusual spikes by using the analysis workflow view.
You are using "Log Rate Analysis" and ran the statistical analysis on the log messages which occured during the alert.
You received the following analysis results from "Log Rate Analysis" which list statistically significant co-occuring field/value combinations sorted from most significant (lower p-values) to least significant (higher p-values) that contribute to the log messages spike:
${header}
${rows}
Based on the above analysis results and your observability expert knowledge, output the following:
Analyse the type of these logs and explain their usual purpose (1 paragraph).
Based on the type of these logs do a root cause analysis on why the field and value combinations from the anlaysis results are causing this spike in logs (2 parapraphs).
Recommend concrete remediations to resolve the root cause (3 bullet points).
Do not repeat the given instructions in your output.`;

const now = new Date().toString();

return [
{
'@timestamp': now,
message: {
role: MessageRole.System,
content: `You are logs-gpt, a helpful assistant for logs-based observability. Answer as
concisely as possible.`,
},
},
{
'@timestamp': now,
message: {
content,
role: MessageRole.User,
},
},
];
}, [logSpikeParams]);

if (!dataView || !esSearchQuery) return null;

return (
Expand Down Expand Up @@ -215,15 +265,9 @@ export const LogRateAnalysis: FC<AlertDetailsLogRateAnalysisSectionProps> = ({ r
</EuiFlexItem>
</EuiFlexGroup>
<EuiFlexGroup direction="column" gutterSize="m">
{coPilotService?.isEnabled() && hasLogSpikeParams ? (
{aiAssistant.isEnabled() && hasLogSpikeParams ? (
<EuiFlexItem grow={false}>
<CoPilotPrompt
coPilot={coPilotService}
title={explainLogSpikeTitle}
params={logSpikeParams}
promptId={CoPilotPromptId.ExplainLogSpike}
feedbackEnabled={false}
/>
<ContextualInsight title={explainLogSpikeTitle} messages={messages} />
</EuiFlexItem>
) : null}
</EuiFlexGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { EuiTitle } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { getPaddedAlertTimeRange } from '@kbn/observability-alert-details';
import { get, identity } from 'lodash';
import { CoPilotContextProvider } from '@kbn/observability-plugin/public';
import { ObservabilityAIAssistantProvider } from '@kbn/observability-ai-assistant-plugin/public';
import { useLogView } from '@kbn/logs-shared-plugin/public';
import { useKibanaContextForPlugin } from '../../../../hooks/use_kibana';
import {
Expand All @@ -44,7 +44,8 @@ const AlertDetailsAppSection = ({
alert,
setAlertSummaryFields,
}: AlertDetailsAppSectionProps) => {
const { observability, logsShared } = useKibanaContextForPlugin().services;
const { observability, logsShared, observabilityAIAssistant } =
useKibanaContextForPlugin().services;
const theme = useTheme();
const timeRange = getPaddedAlertTimeRange(alert.fields[ALERT_START]!, alert.fields[ALERT_END]);
const alertEnd = alert.fields[ALERT_END] ? moment(alert.fields[ALERT_END]).valueOf() : undefined;
Expand Down Expand Up @@ -242,14 +243,14 @@ const AlertDetailsAppSection = ({
};

return (
<CoPilotContextProvider value={observability.getCoPilotService()}>
<ObservabilityAIAssistantProvider value={observabilityAIAssistant}>
<EuiFlexGroup direction="column" data-test-subj="logsThresholdAlertDetailsPage">
{getLogRatioChart()}
{getLogCountChart()}
{getLogRateAnalysisSection()}
{getLogsHistoryChart()}
</EuiFlexGroup>
</CoPilotContextProvider>
</ObservabilityAIAssistantProvider>
);
};

Expand Down
14 changes: 8 additions & 6 deletions x-pack/plugins/infra/public/apps/common_providers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import { AppMountParameters, CoreStart } from '@kbn/core/public';
import React from 'react';
import { EuiThemeProvider } from '@kbn/kibana-react-plugin/common';
import { KibanaContextProvider, KibanaThemeProvider } from '@kbn/kibana-react-plugin/public';
import { CoPilotContextProvider } from '@kbn/observability-plugin/public';
import { CoPilotService } from '@kbn/observability-plugin/public/typings/co_pilot';
import {
ObservabilityAIAssistantProvider,
ObservabilityAIAssistantPluginStart,
} from '@kbn/observability-ai-assistant-plugin/public';
import { Storage } from '@kbn/kibana-utils-plugin/public';
import { NavigationWarningPromptProvider } from '@kbn/observability-shared-plugin/public';
import { TriggersAndActionsUIPublicPluginStart } from '@kbn/triggers-actions-ui-plugin/public';
Expand All @@ -24,13 +26,13 @@ export const CommonInfraProviders: React.FC<{
appName: string;
storage: Storage;
triggersActionsUI: TriggersAndActionsUIPublicPluginStart;
observabilityCopilot: CoPilotService;
observabilityAIAssistant: ObservabilityAIAssistantPluginStart;
setHeaderActionMenu: AppMountParameters['setHeaderActionMenu'];
theme$: AppMountParameters['theme$'];
}> = ({
children,
triggersActionsUI,
observabilityCopilot,
observabilityAIAssistant,
setHeaderActionMenu,
appName,
storage,
Expand All @@ -42,11 +44,11 @@ export const CommonInfraProviders: React.FC<{
<TriggersActionsProvider triggersActionsUI={triggersActionsUI}>
<EuiThemeProvider darkMode={darkMode}>
<DataUIProviders appName={appName} storage={storage}>
<CoPilotContextProvider value={observabilityCopilot}>
<ObservabilityAIAssistantProvider value={observabilityAIAssistant}>
<HeaderActionMenuProvider setHeaderActionMenu={setHeaderActionMenu} theme$={theme$}>
<NavigationWarningPromptProvider>{children}</NavigationWarningPromptProvider>
</HeaderActionMenuProvider>
</CoPilotContextProvider>
</ObservabilityAIAssistantProvider>
</DataUIProviders>
</EuiThemeProvider>
</TriggersActionsProvider>
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/infra/public/apps/logs_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const LogsApp: React.FC<{
storage={storage}
theme$={theme$}
triggersActionsUI={plugins.triggersActionsUi}
observabilityCopilot={plugins.observability.getCoPilotService()}
observabilityAIAssistant={plugins.observabilityAIAssistant}
>
<Router history={history}>
<KbnUrlStateStorageFromRouterProvider
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/infra/public/apps/metrics_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const MetricsApp: React.FC<{
storage={storage}
theme$={theme$}
triggersActionsUI={plugins.triggersActionsUi}
observabilityCopilot={plugins.observability.getCoPilotService()}
observabilityAIAssistant={plugins.observabilityAIAssistant}
>
<SourceProvider sourceId="default">
<Router history={history}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ const ProcessesTableBody = ({ items, currentTime }: TableBodyProps) => (
{column.render ? column.render(item[column.field], currentTime) : item[column.field]}
</EuiTableRowCell>
));
return <ProcessRow cells={cells} item={item} key={`row-${i}`} supportCopilot={true} />;
return <ProcessRow cells={cells} item={item} key={`row-${i}`} supportAIAssistant={true} />;
})}
</>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,54 @@ import {
EuiSpacer,
} from '@elastic/eui';
import { euiStyled } from '@kbn/kibana-react-plugin/common';
import { useCoPilot, CoPilotPrompt } from '@kbn/observability-plugin/public';
import { CoPilotPromptId } from '@kbn/observability-plugin/common';
import useToggle from 'react-use/lib/useToggle';
import {
useObservabilityAIAssistant,
type Message,
MessageRole,
ContextualInsight,
} from '@kbn/observability-ai-assistant-plugin/public';
import { Process } from './types';
import { ProcessRowCharts } from './process_row_charts';

interface Props {
cells: React.ReactNode[];
item: Process;
supportCopilot?: boolean;
supportAIAssistant?: boolean;
}
export const CopilotProcessRow = ({ command }: { command: string }) => {
const coPilotService = useCoPilot();
const explainProcessParams = useMemo(() => {
return command ? { command } : undefined;
export const ContextualInsightProcessRow = ({ command }: { command: string }) => {
const aiAssistant = useObservabilityAIAssistant();
const explainProcessMessages = useMemo<Message[] | undefined>(() => {
if (!command) {
return undefined;
}
const now = new Date().toISOString();
return [
{
'@timestamp': now,
message: {
role: MessageRole.System,
content: '',
},
},
{
'@timestamp': now,
message: {
role: MessageRole.User,
content: '',
},
},
];
}, [command]);
return (
<>
{coPilotService?.isEnabled() && explainProcessParams ? (
{aiAssistant.isEnabled() && explainProcessMessages ? (
<EuiFlexGroup>
<EuiFlexItem>
<EuiFlexItem grow={false}>
<CoPilotPrompt
coPilot={coPilotService}
<ContextualInsight
title={explainProcessMessageTitle}
params={explainProcessParams}
promptId={CoPilotPromptId.InfraExplainProcess}
feedbackEnabled={true}
messages={explainProcessMessages}
/>
</EuiFlexItem>
</EuiFlexItem>
Expand All @@ -59,7 +79,7 @@ export const CopilotProcessRow = ({ command }: { command: string }) => {
);
};

export const ProcessRow = ({ cells, item, supportCopilot = false }: Props) => {
export const ProcessRow = ({ cells, item, supportAIAssistant = false }: Props) => {
const [isExpanded, toggle] = useToggle(false);

return (
Expand Down Expand Up @@ -135,7 +155,7 @@ export const ProcessRow = ({ cells, item, supportCopilot = false }: Props) => {
</EuiFlexItem>
<ProcessRowCharts command={item.command} />
</EuiFlexGrid>
{supportCopilot && <CopilotProcessRow command={item.command} />}
{supportAIAssistant && <ContextualInsightProcessRow command={item.command} />}
</ExpandedRowDescriptionList>
</ExpandedRowCell>
)}
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/infra/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
} from '@kbn/logs-shared-plugin/public';
import { FieldFormatsSetup, FieldFormatsStart } from '@kbn/field-formats-plugin/public';
import { LicensingPluginSetup, LicensingPluginStart } from '@kbn/licensing-plugin/public';
import { ObservabilityAIAssistantPluginStart } from '@kbn/observability-ai-assistant-plugin/public';
import type { UnwrapPromise } from '../common/utility_types';
import type {
SourceProviderProps,
Expand Down Expand Up @@ -104,6 +105,7 @@ export interface InfraClientStartDeps {
ml: MlPluginStart;
observability: ObservabilityPublicStart;
observabilityShared: ObservabilitySharedPluginStart;
observabilityAIAssistant: ObservabilityAIAssistantPluginStart;
osquery?: unknown; // OsqueryPluginStart - can't be imported due to cyclic dependency;
share: SharePluginStart;
spaces: SpacesPluginStart;
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/infra/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"@kbn/discover-plugin",
"@kbn/observability-alert-details",
"@kbn/observability-shared-plugin",
"@kbn/observability-ai-assistant-plugin",
"@kbn/ui-theme",
"@kbn/ml-anomaly-utils",
"@kbn/aiops-plugin",
Expand Down
3 changes: 1 addition & 2 deletions x-pack/plugins/logs_shared/kibana.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
"server": true,
"browser": true,
"configPath": ["xpack", "logs_shared"],
"requiredPlugins": ["data", "dataViews", "usageCollection", "observabilityShared"],
"requiredPlugins": ["data", "dataViews", "usageCollection", "observabilityShared", "observabilityAIAssistant"],
"optionalPlugins": ["observability"],
"requiredBundles": [
"observability",
"kibanaUtils",
"kibanaReact",
],
Expand Down
Loading

0 comments on commit 556b2de

Please sign in to comment.