Skip to content
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

[Asset Manager] update services endpoint to use apm data access plugin #164750

Merged
merged 6 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions x-pack/plugins/asset_manager/kibana.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"optionalPlugins": [
],
"requiredPlugins": [
"apmDataAccess"
],
"browser": false,
"server": true,
Expand Down
4 changes: 4 additions & 0 deletions x-pack/plugins/asset_manager/server/lib/accessors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@
*/

import { ElasticsearchClient } from '@kbn/core-elasticsearch-server';
import { APMDataAccessConfig } from '@kbn/apm-data-access-plugin/server';
import { SavedObjectsClientContract } from '@kbn/core/server';
import { AssetManagerConfig } from '../../types';

export interface InjectedValues {
sourceIndices: AssetManagerConfig['sourceIndices'];
getApmIndices: (soClient: SavedObjectsClientContract) => Promise<APMDataAccessConfig['indices']>;
}

export type OptionsWithInjectedValues<T extends object> = T & InjectedValues;

export interface AccessorOptions {
esClient: ElasticsearchClient;
soClient: SavedObjectsClientContract;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ export async function getServicesBySignals(
});
}

const apmIndices = await options.getApmIndices(options.soClient);
const { assets } = await collectServices({
client: options.esClient,
from: options.from,
to: options.to,
sourceIndices: options.sourceIndices,
apmIndices,
filters,
});

Expand Down
4 changes: 4 additions & 0 deletions x-pack/plugins/asset_manager/server/lib/asset_accessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* 2.0.
*/

import { APMDataAccessConfig } from '@kbn/apm-data-access-plugin/server';
import { SavedObjectsClientContract } from '@kbn/core/server';
import { Asset } from '../../common/types_api';
import { AssetManagerConfig } from '../types';
import { OptionsWithInjectedValues } from './accessors';
Expand All @@ -18,6 +20,7 @@ import { getServicesBySignals } from './accessors/services/get_services_by_signa
interface AssetAccessorClassOptions {
sourceIndices: AssetManagerConfig['sourceIndices'];
source: AssetManagerConfig['lockedSource'];
getApmIndices: (soClient: SavedObjectsClientContract) => Promise<APMDataAccessConfig['indices']>;
}

export class AssetAccessor {
Expand All @@ -27,6 +30,7 @@ export class AssetAccessor {
return {
...options,
sourceIndices: this.options.sourceIndices,
getApmIndices: this.options.getApmIndices,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ export async function collectContainers({
sourceIndices,
afterKey,
}: CollectorOptions) {
const { metrics, logs, traces } = sourceIndices;
const { metrics, logs } = sourceIndices;
const dsl: estypes.SearchRequest = {
index: [traces, logs, metrics],
index: [logs, metrics],
size: QUERY_MAX_SIZE,
collapse: {
field: 'container.id',
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/asset_manager/server/lib/collectors/hosts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ export async function collectHosts({
sourceIndices,
afterKey,
}: CollectorOptions) {
const { metrics, logs, traces } = sourceIndices;
const { metrics, logs } = sourceIndices;
const dsl: estypes.SearchRequest = {
index: [metrics, logs, traces],
index: [metrics, logs],
size: QUERY_MAX_SIZE,
collapse: { field: 'host.hostname' },
sort: [{ 'host.hostname': 'asc' }],
Expand Down
7 changes: 7 additions & 0 deletions x-pack/plugins/asset_manager/server/lib/collectors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import { estypes } from '@elastic/elasticsearch';
import type { APMIndices } from '@kbn/apm-data-access-plugin/server';
import { ElasticsearchClient } from '@kbn/core/server';
import { AssetManagerConfig } from '../../types';
import { Asset } from '../../../common/types_api';
Expand All @@ -23,6 +24,12 @@ export interface CollectorOptions {
filters?: estypes.QueryDslQueryContainer[];
}

type OmitSourceIndices<T> = Omit<T, 'sourceIndices'>;

export type ServicesCollectorOptions = OmitSourceIndices<CollectorOptions> & {
apmIndices: APMIndices;
};

export interface CollectorResult {
assets: Asset[];
afterKey?: estypes.SortResults;
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/asset_manager/server/lib/collectors/pods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import { Asset } from '../../../common/types_api';
import { CollectorOptions, QUERY_MAX_SIZE } from '.';

export async function collectPods({ client, from, to, sourceIndices, afterKey }: CollectorOptions) {
const { metrics, logs, traces } = sourceIndices;
const { metrics, logs } = sourceIndices;
const dsl: estypes.SearchRequest = {
index: [metrics, logs, traces],
index: [metrics, logs],
size: QUERY_MAX_SIZE,
collapse: {
field: 'kubernetes.pod.uid',
Expand Down
10 changes: 5 additions & 5 deletions x-pack/plugins/asset_manager/server/lib/collectors/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@

import { estypes } from '@elastic/elasticsearch';
import { Asset } from '../../../common/types_api';
import { CollectorOptions, QUERY_MAX_SIZE } from '.';
import { ServicesCollectorOptions, QUERY_MAX_SIZE } from '.';

export async function collectServices({
client,
from,
to,
sourceIndices,
apmIndices,
afterKey,
filters = [],
}: CollectorOptions) {
const { traces, serviceMetrics, serviceLogs } = sourceIndices;
}: ServicesCollectorOptions) {
const { transaction, error, metric } = apmIndices;
const musts: estypes.QueryDslQueryContainer[] = [
...filters,
{
Expand All @@ -28,7 +28,7 @@ export async function collectServices({
];

const dsl: estypes.SearchRequest = {
index: [traces, serviceMetrics, serviceLogs],
index: [transaction, error, metric],
size: 0,
_source: false,
query: {
Expand Down
12 changes: 10 additions & 2 deletions x-pack/plugins/asset_manager/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { setupRoutes } from './routes';
import { assetsIndexTemplateConfig } from './templates/assets_template';
import { AssetManagerConfig, configSchema } from './types';
import { AssetAccessor } from './lib/asset_accessor';
import { AssetManagerPluginSetupDependencies, AssetManagerPluginStartDependencies } from './types';

export type AssetManagerServerPluginSetup = ReturnType<AssetManagerServerPlugin['setup']>;
export type AssetManagerServerPluginStart = ReturnType<AssetManagerServerPlugin['start']>;
Expand All @@ -29,7 +30,13 @@ export const config: PluginConfigDescriptor<AssetManagerConfig> = {
};

export class AssetManagerServerPlugin
implements Plugin<AssetManagerServerPluginSetup, AssetManagerServerPluginStart>
implements
Plugin<
AssetManagerServerPluginSetup,
AssetManagerServerPluginStart,
AssetManagerPluginSetupDependencies,
AssetManagerPluginStartDependencies
>
{
public config: AssetManagerConfig;
public logger: Logger;
Expand All @@ -39,7 +46,7 @@ export class AssetManagerServerPlugin
this.logger = context.logger.get();
}

public setup(core: CoreSetup) {
public setup(core: CoreSetup, plugins: AssetManagerPluginSetupDependencies) {
// Check for config value and bail out if not "alpha-enabled"
if (!this.config.alphaEnabled) {
this.logger.info('Asset manager plugin [tech preview] is NOT enabled');
Expand All @@ -51,6 +58,7 @@ export class AssetManagerServerPlugin
const assetAccessor = new AssetAccessor({
source: this.config.lockedSource,
sourceIndices: this.config.sourceIndices,
getApmIndices: plugins.apmDataAccess.getApmIndices,
});

const router = core.http.createRouter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@ export function servicesRoutes<T extends RequestHandlerContext>({
async (context, req, res) => {
const { from = 'now-24h', to = 'now', parent } = req.query || {};
const esClient = await getEsClientFromContext(context);

const coreContext = await context.core;
const soClient = coreContext.savedObjects.client;
try {
const response = await assetAccessor.getServices({
from: datemath.parse(from)!.toISOString(),
to: datemath.parse(to)!.toISOString(),
parent,
esClient,
soClient,
});

return res.ok({ body: response });
Expand Down
17 changes: 11 additions & 6 deletions x-pack/plugins/asset_manager/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

import { schema, TypeOf } from '@kbn/config-schema';
import { ElasticsearchClient } from '@kbn/core/server';
import {
ApmDataAccessPluginSetup,
ApmDataAccessPluginStart,
} from '@kbn/apm-data-access-plugin/server';

export interface ElasticsearchAccessorOptions {
esClient: ElasticsearchClient;
Expand All @@ -15,9 +19,6 @@ export interface ElasticsearchAccessorOptions {
export const INDEX_DEFAULTS = {
metrics: 'metricbeat-*,metrics-*',
logs: 'filebeat-*,logs-*',
traces: 'traces-*',
serviceMetrics: 'metrics-apm*',
serviceLogs: 'logs-apm*',
};

export const configSchema = schema.object({
Expand All @@ -30,9 +31,6 @@ export const configSchema = schema.object({
{
metrics: schema.string({ defaultValue: INDEX_DEFAULTS.metrics }),
logs: schema.string({ defaultValue: INDEX_DEFAULTS.logs }),
traces: schema.string({ defaultValue: INDEX_DEFAULTS.traces }),
serviceMetrics: schema.string({ defaultValue: INDEX_DEFAULTS.serviceMetrics }),
serviceLogs: schema.string({ defaultValue: INDEX_DEFAULTS.serviceLogs }),
},
{ defaultValue: INDEX_DEFAULTS }
),
Expand All @@ -47,3 +45,10 @@ export const configSchema = schema.object({
});

export type AssetManagerConfig = TypeOf<typeof configSchema>;

export interface AssetManagerPluginSetupDependencies {
apmDataAccess: ApmDataAccessPluginSetup;
}
export interface AssetManagerPluginStartDependencies {
apmDataAccess: ApmDataAccessPluginStart;
}
3 changes: 2 additions & 1 deletion x-pack/plugins/asset_manager/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@kbn/io-ts-utils",
"@kbn/core-elasticsearch-server",
"@kbn/core-http-request-handler-context-server",
"@kbn/datemath"
"@kbn/datemath",
"@kbn/apm-data-access-plugin"
]
}