Skip to content

Commit

Permalink
Include filter in logs shared locators query.
Browse files Browse the repository at this point in the history
  • Loading branch information
awahab07 committed Dec 22, 2023
1 parent 83a1b23 commit 87b45c9
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 35 deletions.
11 changes: 0 additions & 11 deletions x-pack/plugins/infra/common/locators/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ import {
LogViewReference,
ResolvedLogView,
LogsLocatorParams,
NodeLogsLocatorParams,
} from '@kbn/logs-shared-plugin/common';
import { flowRight } from 'lodash';
import { findInventoryFields } from '@kbn/metrics-data-access-plugin/common';
import type { InfraClientCoreSetup } from '../../public/types';
import { MESSAGE_FIELD, TIMESTAMP_FIELD } from '../constants';
import type { TimeRange } from '../time';
Expand All @@ -33,15 +31,6 @@ interface LocationToDiscoverParams {
logView?: LogViewReference;
}

export const createNodeLogsQuery = (params: NodeLogsLocatorParams) => {
const { nodeType, nodeId, filter } = params;

const nodeFilter = `${findInventoryFields(nodeType).id}: ${nodeId}`;
const query = filter ? `(${nodeFilter}) and (${filter})` : nodeFilter;

return query;
};

export const createSearchString = ({
time,
timeRange,
Expand Down
8 changes: 6 additions & 2 deletions x-pack/plugins/infra/common/locators/node_logs_locator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
*/

import type { LocatorDefinition, LocatorPublic } from '@kbn/share-plugin/public';
import { INFRA_NODE_LOGS_LOCATOR_ID, NodeLogsLocatorParams } from '@kbn/logs-shared-plugin/common';
import {
INFRA_NODE_LOGS_LOCATOR_ID,
NodeLogsLocatorParams,
createNodeLogsQuery,
} from '@kbn/logs-shared-plugin/common';
import type { InfraLogsLocatorDependencies } from './logs_locator';

export type InfraNodeLogsLocator = LocatorPublic<NodeLogsLocatorParams>;
Expand All @@ -19,7 +23,7 @@ export class InfraNodeLogsLocatorDefinition implements LocatorDefinition<NodeLog
constructor(protected readonly deps: InfraNodeLogsLocatorDependencies) {}

public readonly getLocation = async (params: NodeLogsLocatorParams) => {
const { createNodeLogsQuery, createSearchString } = await import('./helpers');
const { createSearchString } = await import('./helpers');

const query = createNodeLogsQuery(params);

Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/logs_shared/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,4 @@ export {
getLogsLocatorsFromUrlService,
} from './locators';
export type { LogsLocatorParams, NodeLogsLocatorParams, TraceLogsLocatorParams } from './locators';
export { createNodeLogsQuery } from './locators/helpers';
34 changes: 21 additions & 13 deletions x-pack/plugins/logs_shared/common/locators/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,33 @@
*/

import moment, { DurationInputObject } from 'moment';
import { findInventoryFields } from '@kbn/metrics-data-access-plugin/common';
import { LogsLocatorParams, NodeLogsLocatorParams, TraceLogsLocatorParams } from './types';

export type NodeType = 'host' | 'pod' | 'container' | 'awsEC2' | 'awsS3' | 'awsSQS' | 'awsRDS';
export const getLogsQuery = (params: LogsLocatorParams) => {
const { filter } = params;

const NodeTypeMapping: Record<NodeType, string> = {
host: 'host.name',
container: 'container.id',
pod: 'kubernetes.pod.uid',
awsEC2: 'awsEC2',
awsS3: 'awsS3',
awsSQS: 'awsSQS',
awsRDS: 'awsRDS',
return filter ? { language: 'kuery', query: filter } : undefined;
};

export const getNodeQuery = (type: NodeType, id: string) => {
return { language: 'kuery', query: `${NodeTypeMapping[type]}: ${id}` };
export const createNodeLogsQuery = (params: NodeLogsLocatorParams) => {
const { nodeType, nodeId, filter } = params;

const nodeFilter = `${findInventoryFields(nodeType).id}: ${nodeId}`;
return filter ? `(${nodeFilter}) and (${filter})` : nodeFilter;
};

export const getNodeQuery = (params: NodeLogsLocatorParams) => {
return { language: 'kuery', query: createNodeLogsQuery(params) };
};

export const getTraceQuery = (traceId: string) => {
return { language: 'kuery', query: `trace.id:"${traceId}" OR (not trace.id:* AND "${traceId}")` };
export const getTraceQuery = (params: TraceLogsLocatorParams) => {
const { traceId, filter } = params;

const traceFilter = `trace.id:"${traceId}" OR (not trace.id:* AND "${traceId}")`;
const query = filter ? `(${traceFilter}) and (${filter})` : traceFilter;

return { language: 'kuery', query };
};

const defaultTimeRangeFromPositionOffset: DurationInputObject = { hours: 1 };
Expand Down
3 changes: 2 additions & 1 deletion x-pack/plugins/logs_shared/common/locators/logs_locator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { LocatorClient } from '@kbn/share-plugin/common/url_service';

import { INFRA_LOGS_LOCATOR_ID } from './infra';
import { LogsLocatorParams } from './types';
import { getTimeRangeEndFromTime, getTimeRangeStartFromTime } from './helpers';
import { getLogsQuery, getTimeRangeEndFromTime, getTimeRangeStartFromTime } from './helpers';

export const LOGS_LOCATOR_ID = 'LOGS_LOCATOR';

Expand All @@ -30,6 +30,7 @@ export class LogsLocatorDefinition implements LocatorDefinition<LogsLocatorParam
this.locators.get<AllDatasetsLocatorParams>(ALL_DATASETS_LOCATOR_ID)!;
const { time } = params;
return allDatasetsLocator.getLocation({
query: getLogsQuery(params),
...(time
? {
timeRange: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ export class NodeLogsLocatorDefinition implements LocatorDefinition<NodeLogsLoca

const allDatasetsLocator =
this.locators.get<AllDatasetsLocatorParams>(ALL_DATASETS_LOCATOR_ID)!;
const { nodeId, nodeType, time } = params;
const { time } = params;
return allDatasetsLocator.getLocation({
query: getNodeQuery(nodeType, nodeId),
query: getNodeQuery(params),
...(time
? {
timeRange: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,19 @@ export class TraceLogsLocatorDefinition implements LocatorDefinition<TraceLogsLo
constructor(private readonly locators: LocatorClient) {}

public readonly getLocation = async (params: TraceLogsLocatorParams) => {
const { traceId, time } = params;

const infraLogsLocator = this.locators.get<LogsLocatorParams>(INFRA_LOGS_LOCATOR_ID);
if (infraLogsLocator) {
return infraLogsLocator.getLocation({
...params,
filter: getTraceQuery(traceId).query,
filter: getTraceQuery(params).query,
});
}

const { time } = params;
const allDatasetsLocator =
this.locators.get<AllDatasetsLocatorParams>(ALL_DATASETS_LOCATOR_ID)!;
return allDatasetsLocator.getLocation({
query: getTraceQuery(traceId),
query: getTraceQuery(params),
...(time
? {
timeRange: {
Expand Down
3 changes: 2 additions & 1 deletion x-pack/plugins/logs_shared/kibana.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"usageCollection",
"observabilityShared",
"observabilityAIAssistant",
"share"
"share",
"metricsDataAccess"
],
"requiredBundles": ["kibanaUtils", "kibanaReact"],
"extraPublicDirs": ["common"]
Expand Down
3 changes: 2 additions & 1 deletion x-pack/plugins/logs_shared/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@kbn/ui-actions-plugin",
"@kbn/observability-ai-assistant-plugin",
"@kbn/deeplinks-observability",
"@kbn/share-plugin"
"@kbn/share-plugin",
"@kbn/metrics-data-access-plugin"
]
}

0 comments on commit 87b45c9

Please sign in to comment.