Skip to content

Commit

Permalink
Updated APM Indices endpoints to use the SavedObjectsClient from the
Browse files Browse the repository at this point in the history
legacy request context, and set the apm-indices schema object to be
namspace-agnostic.
  • Loading branch information
ogupte committed Nov 11, 2019
1 parent ab5f411 commit 3b55d11
Show file tree
Hide file tree
Showing 13 changed files with 117 additions and 123 deletions.
3 changes: 3 additions & 0 deletions x-pack/legacy/plugins/apm/index.ts
Expand Up @@ -50,6 +50,9 @@ export const apm: LegacyPluginInitializer = kibana => {
savedObjectSchemas: {
'apm-telemetry': {
isNamespaceAgnostic: true
},
'apm-indices': {
isNamespaceAgnostic: true
}
},
mappings
Expand Down
Expand Up @@ -9,7 +9,7 @@ import {
APM_TELEMETRY_DOC_ID,
createApmTelementry,
storeApmTelemetry
} from '../apm_telemetry';
} from '../index';

describe('apm_telemetry', () => {
describe('createApmTelementry', () => {
Expand Down

This file was deleted.

60 changes: 54 additions & 6 deletions x-pack/legacy/plugins/apm/server/lib/apm_telemetry/index.ts
Expand Up @@ -4,9 +4,57 @@
* you may not use this file except in compliance with the Elastic License.
*/

export {
storeApmTelemetry,
createApmTelementry,
APM_TELEMETRY_DOC_ID
} from './apm_telemetry';
export { makeApmUsageCollector } from './make_apm_usage_collector';
import { Server } from 'hapi';
import { countBy } from 'lodash';
import { SavedObjectAttributes } from 'src/core/server';
import { InternalCoreSetup } from 'src/core/server';
import { isAgentName } from '../../../common/agent_name';
import { getSavedObjectsClient } from '../helpers/saved_objects_client';
export const APM_TELEMETRY_DOC_ID = 'apm-telemetry';

export function createApmTelementry(
agentNames: string[] = []
): SavedObjectAttributes {
const validAgentNames = agentNames.filter(isAgentName);
return {
has_any_services: validAgentNames.length > 0,
services_per_agent: countBy(validAgentNames)
};
}

export async function storeApmTelemetry(
server: Server,
apmTelemetry: SavedObjectAttributes
) {
try {
const savedObjectsClient = getSavedObjectsClient(server);
await savedObjectsClient.create('apm-telemetry', apmTelemetry, {
id: APM_TELEMETRY_DOC_ID,
overwrite: true
});
} catch (e) {
server.log(['error'], `Unable to save APM telemetry data: ${e.message}`);
}
}

export function makeApmUsageCollector(core: InternalCoreSetup) {
const { server } = core.http;

const apmUsageCollector = server.usage.collectorSet.makeUsageCollector({
type: 'apm',
fetch: async () => {
const savedObjectsClient = getSavedObjectsClient(server);
try {
const apmTelemetrySavedObject = await savedObjectsClient.get(
'apm-telemetry',
APM_TELEMETRY_DOC_ID
);
return apmTelemetrySavedObject.attributes;
} catch (err) {
return createApmTelementry();
}
},
isReady: () => true
});
server.usage.collectorSet.register(apmUsageCollector);
}

This file was deleted.

6 changes: 5 additions & 1 deletion x-pack/legacy/plugins/apm/server/lib/helpers/es_client.ts
Expand Up @@ -66,8 +66,12 @@ async function getParamsForSearchRequest(
apmOptions?: APMOptions
) {
const uiSettings = req.getUiSettingsService();
const { server } = req;
const [indices, includeFrozen] = await Promise.all([
getApmIndices(req.server),
getApmIndices({
config: server.config(),
savedObjectsClient: server.savedObjects.getScopedSavedObjectsClient(req)
}),
uiSettings.get('search:includeFrozen')
]);

Expand Down
Expand Up @@ -30,6 +30,9 @@ function getMockRequest() {
elasticsearch: {
getCluster: () => ({ callWithRequest: callWithRequestSpy })
}
},
savedObjects: {
getScopedSavedObjectsClient: () => ({ get: async () => false })
}
},
getUiSettingsService: () => ({ get: async () => false })
Expand Down
Expand Up @@ -31,10 +31,13 @@ export type Setup = PromiseReturnType<typeof setupRequest>;
export async function setupRequest(req: Legacy.Request) {
const query = (req.query as unknown) as APMRequestQuery;
const { server } = req;
const savedObjectsClient = server.savedObjects.getScopedSavedObjectsClient(
req
);
const config = server.config();
const [uiFiltersES, indices] = await Promise.all([
decodeUiFilters(server, query.uiFilters),
getApmIndices(server)
getApmIndices({ config, savedObjectsClient })
]);

return {
Expand Down
Expand Up @@ -7,13 +7,16 @@
import { InternalCoreSetup } from 'src/core/server';
import { CallCluster } from '../../../../../../../../src/legacy/core_plugins/elasticsearch';
import { getApmIndices } from '../apm_indices/get_apm_indices';
import { getSavedObjectsClient } from '../../helpers/saved_objects_client';

export async function createApmAgentConfigurationIndex(
core: InternalCoreSetup
) {
try {
const { server } = core.http;
const indices = await getApmIndices(server);
const config = server.config();
const savedObjectsClient = getSavedObjectsClient(server);
const indices = await getApmIndices({ savedObjectsClient, config });
const index = indices['apm_oss.apmAgentConfigurationIndex'];
const { callWithInternalUser } = server.plugins.elasticsearch.getCluster(
'admin'
Expand Down
Expand Up @@ -4,11 +4,9 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { Server } from 'hapi';
import { merge } from 'lodash';
import { KibanaConfig } from 'src/legacy/server/kbn_server';
import { getSavedObjectsClient } from '../../helpers/saved_objects_client';
import { Setup } from '../../helpers/setup_request';
import { Server } from 'hapi';
import { PromiseReturnType } from '../../../../typings/common';

export interface ApmIndicesConfig {
Expand All @@ -26,8 +24,13 @@ export type ApmIndicesName = keyof ApmIndicesConfig;
export const APM_INDICES_SAVED_OBJECT_TYPE = 'apm-indices';
export const APM_INDICES_SAVED_OBJECT_ID = 'apm-indices';

async function getApmIndicesSavedObject(server: Server) {
const savedObjectsClient = getSavedObjectsClient(server, 'data');
export type ScopedSavedObjectsClient = ReturnType<
Server['savedObjects']['getScopedSavedObjectsClient']
>;

async function getApmIndicesSavedObject(
savedObjectsClient: ScopedSavedObjectsClient
) {
const apmIndices = await savedObjectsClient.get<Partial<ApmIndicesConfig>>(
APM_INDICES_SAVED_OBJECT_TYPE,
APM_INDICES_SAVED_OBJECT_ID
Expand All @@ -53,13 +56,21 @@ function getApmIndicesConfig(config: KibanaConfig): ApmIndicesConfig {
};
}

export async function getApmIndices(server: Server) {
export async function getApmIndices({
savedObjectsClient,
config
}: {
savedObjectsClient: ScopedSavedObjectsClient;
config: KibanaConfig;
}) {
try {
const apmIndicesSavedObject = await getApmIndicesSavedObject(server);
const apmIndicesConfig = getApmIndicesConfig(server.config());
const apmIndicesSavedObject = await getApmIndicesSavedObject(
savedObjectsClient
);
const apmIndicesConfig = getApmIndicesConfig(config);
return merge({}, apmIndicesConfig, apmIndicesSavedObject);
} catch (error) {
return getApmIndicesConfig(server.config());
return getApmIndicesConfig(config);
}
}

Expand All @@ -74,16 +85,15 @@ const APM_UI_INDICES: ApmIndicesName[] = [
];

export async function getApmIndexSettings({
setup,
server
config,
savedObjectsClient
}: {
setup: Setup;
server: Server;
config: KibanaConfig;
savedObjectsClient: ScopedSavedObjectsClient;
}) {
const { config } = setup;
let apmIndicesSavedObject: PromiseReturnType<typeof getApmIndicesSavedObject>;
try {
apmIndicesSavedObject = await getApmIndicesSavedObject(server);
apmIndicesSavedObject = await getApmIndicesSavedObject(savedObjectsClient);
} catch (error) {
if (error.output && error.output.statusCode === 404) {
apmIndicesSavedObject = {};
Expand Down
Expand Up @@ -4,19 +4,17 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { Server } from 'hapi';
import { getSavedObjectsClient } from '../../helpers/saved_objects_client';
import {
ApmIndicesConfig,
APM_INDICES_SAVED_OBJECT_TYPE,
APM_INDICES_SAVED_OBJECT_ID
APM_INDICES_SAVED_OBJECT_ID,
ScopedSavedObjectsClient
} from './get_apm_indices';

export async function saveApmIndices(
server: Server,
savedObjectsClient: ScopedSavedObjectsClient,
apmIndicesSavedObject: Partial<ApmIndicesConfig>
) {
const savedObjectsClient = getSavedObjectsClient(server, 'data');
return await savedObjectsClient.create(
APM_INDICES_SAVED_OBJECT_TYPE,
apmIndicesSavedObject,
Expand Down
3 changes: 1 addition & 2 deletions x-pack/legacy/plugins/apm/server/new-platform/plugin.ts
Expand Up @@ -6,14 +6,13 @@

import { InternalCoreSetup } from 'src/core/server';
import { makeApmUsageCollector } from '../lib/apm_telemetry';
import { CoreSetupWithUsageCollector } from '../lib/apm_telemetry/make_apm_usage_collector';
import { createApmAgentConfigurationIndex } from '../lib/settings/agent_configuration/create_agent_config_index';
import { createApmApi } from '../routes/create_apm_api';

export class Plugin {
public setup(core: InternalCoreSetup) {
createApmApi().init(core);
createApmAgentConfigurationIndex(core);
makeApmUsageCollector(core as CoreSetupWithUsageCollector);
makeApmUsageCollector(core);
}
}
22 changes: 14 additions & 8 deletions x-pack/legacy/plugins/apm/server/routes/settings/apm_indices.ts
Expand Up @@ -5,7 +5,6 @@
*/

import * as t from 'io-ts';
import { setupRequest } from '../../lib/helpers/setup_request';
import { createRoute } from '../create_route';
import {
getApmIndices,
Expand All @@ -18,9 +17,11 @@ export const apmIndexSettingsRoute = createRoute(core => ({
method: 'GET',
path: '/api/apm/settings/apm-index-settings',
handler: async req => {
const { server } = core.http;
const setup = await setupRequest(req);
return await getApmIndexSettings({ setup, server });
const config = req.server.config();
const savedObjectsClient = req.server.savedObjects.getScopedSavedObjectsClient(
req
);
return await getApmIndexSettings({ config, savedObjectsClient });
}
}));

Expand All @@ -29,8 +30,11 @@ export const apmIndicesRoute = createRoute(core => ({
method: 'GET',
path: '/api/apm/settings/apm-indices',
handler: async req => {
const { server } = core.http;
return await getApmIndices(server);
const config = req.server.config();
const savedObjectsClient = req.server.savedObjects.getScopedSavedObjectsClient(
req
);
return await getApmIndices({ config, savedObjectsClient });
}
}));

Expand All @@ -50,7 +54,9 @@ export const saveApmIndicesRoute = createRoute(core => ({
})
},
handler: async (req, { body }) => {
const { server } = core.http;
return await saveApmIndices(server, body);
const savedObjectsClient = req.server.savedObjects.getScopedSavedObjectsClient(
req
);
return await saveApmIndices(savedObjectsClient, body);
}
}));

0 comments on commit 3b55d11

Please sign in to comment.