forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Asset Manager] services endpoint (elastic#160294)
Closes elastic#159641 Implements `/assets/services` endpoint that returns service assets found in the configured source (signals or assets indices). Consumer can provide a `parent` query to filter the returned services. While the _assets_ mode supports any kind of parent/depth thanks to its common interface, the _signals_ mode only supports host parent for the moment. 1. pull this branch and point it at an oblt-cli created cluster that uses cross-cluster search to read from the edge cluster 2. add the following[1] to your kibana.yml file 3. hit `/api/asset-manager/assets/services?from=<from>&to=<to>&(parent=<host>)?`. services should be returned. Add/remove parent query string to filter services only running on specific host. 4. update `lockedSource: assets` to read from assets generated by implicit collection 5. repeat 3. [1] ``` xpack.assetManager: alphaEnabled: true sourceIndices: metrics: remote_cluster:metricbeat*,remote_cluster:metrics-* logs: remote_cluster:filebeat*,remote_cluster:logs-* traces: remote_cluster:traces-* serviceMetrics: remote_cluster:metrics-apm* serviceLogs: remote_cluster:logs-apm* lockedSource: signals ``` --------- Co-authored-by: Kibana Machine <[email protected]>
- Loading branch information
1 parent
a96785c
commit 16e3c34
Showing
11 changed files
with
321 additions
and
18 deletions.
There are no files selected for viewing
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
46 changes: 46 additions & 0 deletions
46
x-pack/plugins/asset_manager/server/lib/accessors/services/get_services_by_assets.ts
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,46 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { Asset } from '../../../../common/types_api'; | ||
import { GetServicesOptionsInjected } from '.'; | ||
import { getAssets } from '../../get_assets'; | ||
import { getAllRelatedAssets } from '../../get_all_related_assets'; | ||
|
||
export async function getServicesByAssets( | ||
options: GetServicesOptionsInjected | ||
): Promise<{ services: Asset[] }> { | ||
if (options.parent) { | ||
return getServicesByParent(options); | ||
} | ||
|
||
const services = await getAssets({ | ||
esClient: options.esClient, | ||
filters: { | ||
kind: 'service', | ||
from: options.from, | ||
to: options.to, | ||
}, | ||
}); | ||
|
||
return { services }; | ||
} | ||
|
||
async function getServicesByParent( | ||
options: GetServicesOptionsInjected | ||
): Promise<{ services: Asset[] }> { | ||
const { descendants } = await getAllRelatedAssets(options.esClient, { | ||
from: options.from, | ||
to: options.to, | ||
maxDistance: 5, | ||
kind: ['service'], | ||
size: 100, | ||
relation: 'descendants', | ||
ean: options.parent!, | ||
}); | ||
|
||
return { services: descendants as Asset[] }; | ||
} |
38 changes: 38 additions & 0 deletions
38
x-pack/plugins/asset_manager/server/lib/accessors/services/get_services_by_signals.ts
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,38 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { Asset } from '../../../../common/types_api'; | ||
import { GetServicesOptionsInjected } from '.'; | ||
import { collectServices } from '../../collectors/services'; | ||
|
||
export async function getServicesBySignals( | ||
options: GetServicesOptionsInjected | ||
): Promise<{ services: Asset[] }> { | ||
const filters = []; | ||
|
||
if (options.parent) { | ||
filters.push({ | ||
bool: { | ||
should: [ | ||
{ term: { 'host.name': options.parent } }, | ||
{ term: { 'host.hostname': options.parent } }, | ||
], | ||
minimum_should_match: 1, | ||
}, | ||
}); | ||
} | ||
|
||
const { assets } = await collectServices({ | ||
client: options.esClient, | ||
from: options.from, | ||
to: options.to, | ||
sourceIndices: options.sourceIndices, | ||
filters, | ||
}); | ||
|
||
return { services: assets }; | ||
} |
22 changes: 22 additions & 0 deletions
22
x-pack/plugins/asset_manager/server/lib/accessors/services/index.ts
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,22 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { AccessorOptions, OptionsWithInjectedValues } from '..'; | ||
|
||
export interface GetServicesOptions extends AccessorOptions { | ||
from: string; | ||
to: string; | ||
parent?: string; | ||
} | ||
export type GetServicesOptionsInjected = OptionsWithInjectedValues<GetServicesOptions>; | ||
|
||
export interface ServiceIdentifier { | ||
'asset.ean': string; | ||
'asset.id': string; | ||
'asset.name'?: string; | ||
'service.environment'?: string; | ||
} |
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
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
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
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
70 changes: 70 additions & 0 deletions
70
x-pack/plugins/asset_manager/server/routes/assets/services.ts
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,70 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import * as rt from 'io-ts'; | ||
import datemath from '@kbn/datemath'; | ||
import { | ||
dateRt, | ||
inRangeFromStringRt, | ||
datemathStringRt, | ||
createRouteValidationFunction, | ||
createLiteralValueFromUndefinedRT, | ||
} from '@kbn/io-ts-utils'; | ||
import { RequestHandlerContext } from '@kbn/core-http-request-handler-context-server'; | ||
import { debug } from '../../../common/debug_log'; | ||
import { SetupRouteOptions } from '../types'; | ||
import { ASSET_MANAGER_API_BASE } from '../../constants'; | ||
import { getEsClientFromContext } from '../utils'; | ||
|
||
const sizeRT = rt.union([inRangeFromStringRt(1, 100), createLiteralValueFromUndefinedRT(10)]); | ||
const assetDateRT = rt.union([dateRt, datemathStringRt]); | ||
const getServiceAssetsQueryOptionsRT = rt.exact( | ||
rt.partial({ | ||
from: assetDateRT, | ||
to: assetDateRT, | ||
size: sizeRT, | ||
parent: rt.string, | ||
}) | ||
); | ||
|
||
export type GetServiceAssetsQueryOptions = rt.TypeOf<typeof getServiceAssetsQueryOptionsRT>; | ||
|
||
export function servicesRoutes<T extends RequestHandlerContext>({ | ||
router, | ||
assetAccessor, | ||
}: SetupRouteOptions<T>) { | ||
// GET /assets/services | ||
router.get<unknown, GetServiceAssetsQueryOptions, unknown>( | ||
{ | ||
path: `${ASSET_MANAGER_API_BASE}/assets/services`, | ||
validate: { | ||
query: createRouteValidationFunction(getServiceAssetsQueryOptionsRT), | ||
}, | ||
}, | ||
async (context, req, res) => { | ||
const { from = 'now-24h', to = 'now', parent } = req.query || {}; | ||
const esClient = await getEsClientFromContext(context); | ||
|
||
try { | ||
const response = await assetAccessor.getServices({ | ||
from: datemath.parse(from)!.toISOString(), | ||
to: datemath.parse(to)!.toISOString(), | ||
parent, | ||
esClient, | ||
}); | ||
|
||
return res.ok({ body: response }); | ||
} catch (error: unknown) { | ||
debug('Error while looking up SERVICE asset records', error); | ||
return res.customError({ | ||
statusCode: 500, | ||
body: { message: 'Error while looking up service asset records - ' + `${error}` }, | ||
}); | ||
} | ||
} | ||
); | ||
} |
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.