-
Notifications
You must be signed in to change notification settings - Fork 8.5k
[Endpoint][EPM] Retrieve Index Pattern from Ingest Manager #63016
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
Changes from all commits
b69c2ad
7c7eaee
c9f0507
b82762b
cb45dfe
9e4f6e3
8cbfa42
a1f2bd0
f13231a
0c9946d
0140395
6c7b7b4
745c15d
661f860
b839152
cf1a071
9ff1e7b
8272634
3599f46
6c22146
6457c0f
3fd2dc1
4d2e8ce
e94629f
89dd822
337ab45
9ced2c8
4dc5854
b669294
cc4a355
1aa9388
dd033d4
efdcc2d
2829689
6facd1f
ec0a649
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| /* | ||
| * 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 { schema } from '@kbn/config-schema'; | ||
|
|
||
| export const indexPatternGetParamsSchema = schema.object({ datasetPath: schema.string() }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| /* | ||
| * 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 { Logger, LoggerFactory, RequestHandlerContext } from 'kibana/server'; | ||
| import { ESIndexPatternService } from '../../ingest_manager/server'; | ||
| import { EndpointAppConstants } from '../common/types'; | ||
|
|
||
| export interface IndexPatternRetriever { | ||
| getIndexPattern(ctx: RequestHandlerContext, datasetPath: string): Promise<string>; | ||
| getEventIndexPattern(ctx: RequestHandlerContext): Promise<string>; | ||
| getMetadataIndexPattern(ctx: RequestHandlerContext): Promise<string>; | ||
| } | ||
|
|
||
| /** | ||
| * This class is used to retrieve an index pattern. It should be used in the server side code whenever | ||
| * an index pattern is needed to query data within ES. The index pattern is constructed by the Ingest Manager | ||
| * based on the contents of the Endpoint Package in the Package Registry. | ||
| */ | ||
| export class IngestIndexPatternRetriever implements IndexPatternRetriever { | ||
| private static endpointPackageName = 'endpoint'; | ||
| private static metadataDataset = 'metadata'; | ||
| private readonly log: Logger; | ||
| constructor(private readonly service: ESIndexPatternService, loggerFactory: LoggerFactory) { | ||
| this.log = loggerFactory.get('index-pattern-retriever'); | ||
| } | ||
|
|
||
| /** | ||
| * Retrieves the index pattern for querying events within elasticsearch. | ||
| * | ||
| * @param ctx a RequestHandlerContext from a route handler | ||
| * @returns a string representing the index pattern (e.g. `events-endpoint-*`) | ||
| */ | ||
| async getEventIndexPattern(ctx: RequestHandlerContext) { | ||
| return await this.getIndexPattern(ctx, EndpointAppConstants.EVENT_DATASET); | ||
| } | ||
|
|
||
| /** | ||
| * Retrieves the index pattern for querying endpoint metadata within elasticsearch. | ||
| * | ||
| * @param ctx a RequestHandlerContext from a route handler | ||
| * @returns a string representing the index pattern (e.g. `metrics-endpoint-*`) | ||
| */ | ||
| async getMetadataIndexPattern(ctx: RequestHandlerContext) { | ||
|
Contributor
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. would you mind adding a quick doc comment to public methods? maybe I think these types of comments will help a lot, esp. considering the size of the org |
||
| return await this.getIndexPattern(ctx, IngestIndexPatternRetriever.metadataDataset); | ||
| } | ||
|
|
||
| /** | ||
| * Retrieves the index pattern for a specific dataset for querying endpoint data. | ||
| * | ||
| * @param ctx a RequestHandlerContext from a route handler | ||
| * @param datasetPath a string of the path being used for a dataset within the Endpoint Package | ||
| * (e.g. `events`, `metadata`) | ||
| * @returns a string representing the index pattern (e.g. `metrics-endpoint-*`) | ||
| */ | ||
| async getIndexPattern(ctx: RequestHandlerContext, datasetPath: string) { | ||
| try { | ||
| const pattern = await this.service.getESIndexPattern( | ||
|
Contributor
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. Should (can?) any of these call be cached?
Contributor
Author
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. Yeah, I haven't created an issue for it yet but we'll create a cache so we don't have to do so many calls to the saved objects.
Contributor
Author
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. |
||
| ctx.core.savedObjects.client, | ||
| IngestIndexPatternRetriever.endpointPackageName, | ||
| datasetPath | ||
| ); | ||
|
|
||
| if (!pattern) { | ||
| const msg = `Unable to retrieve the index pattern for dataset: ${datasetPath}`; | ||
| this.log.warn(msg); | ||
| throw new Error(msg); | ||
| } | ||
| return pattern; | ||
| } catch (error) { | ||
| const errMsg = `Error occurred while retrieving pattern for: ${datasetPath} error: ${error}`; | ||
| this.log.warn(errMsg); | ||
| throw new Error(errMsg); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| /** | ||
| * Creates a mock IndexPatternRetriever for use in tests. | ||
| * | ||
| * @param indexPattern a string index pattern to return when any of the mock's public methods are called. | ||
| * @returns the same string passed in via `indexPattern` | ||
| */ | ||
| export const createMockIndexPatternRetriever = (indexPattern: string) => { | ||
jonathan-buttner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const mockGetFunc = jest.fn().mockResolvedValue(indexPattern); | ||
| return { | ||
| getIndexPattern: mockGetFunc, | ||
| getEventIndexPattern: mockGetFunc, | ||
| getMetadataIndexPattern: mockGetFunc, | ||
| }; | ||
| }; | ||
|
|
||
| export const MetadataIndexPattern = 'metrics-endpoint-*'; | ||
|
|
||
| /** | ||
| * Creates a mock IndexPatternRetriever for use in tests that returns `metrics-endpoint-*` | ||
| */ | ||
| export const createMockMetadataIndexPatternRetriever = () => { | ||
| return createMockIndexPatternRetriever(MetadataIndexPattern); | ||
| }; | ||
|
|
||
| /** | ||
| * Creates a mock IndexPatternService for use in tests that need to interact with the Ingest Manager's | ||
| * ESIndexPatternService. | ||
| * | ||
| * @param indexPattern a string index pattern to return when called by a test | ||
| * @returns the same value as `indexPattern` parameter | ||
| */ | ||
| export const createMockIndexPatternService = (indexPattern: string) => { | ||
| return { | ||
| esIndexPatternService: { | ||
| getESIndexPattern: jest.fn().mockResolvedValue(indexPattern), | ||
| }, | ||
| }; | ||
| }; | ||
Uh oh!
There was an error while loading. Please reload this page.