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

[APM] Use SavedObjectsClient from the request context for APM indices endpoints #49994

Merged
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
14 changes: 14 additions & 0 deletions x-pack/legacy/plugins/apm/common/apm_saved_object_constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* 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.
*/

// APM Services telemetry
export const APM_SERVICES_TELEMETRY_SAVED_OBJECT_TYPE =
'apm-services-telemetry';
export const APM_SERVICES_TELEMETRY_SAVED_OBJECT_ID = 'apm-services-telemetry';

// APM indices
export const APM_INDICES_SAVED_OBJECT_TYPE = 'apm-indices';
export const APM_INDICES_SAVED_OBJECT_ID = 'apm-indices';
5 changes: 4 additions & 1 deletion x-pack/legacy/plugins/apm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ export const apm: LegacyPluginInitializer = kibana => {
},
hacks: ['plugins/apm/hacks/toggle_app_link_in_nav'],
savedObjectSchemas: {
'apm-telemetry': {
'apm-services-telemetry': {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know I asked for this but I gotta ask: will this cause migration issues?

Copy link
Contributor Author

@ogupte ogupte Nov 12, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't trigger a migration, as it's a different object type, but it would remove saved objects of the old type. This won't be an issue as the object is ephemeral, and only reference when triggered by the usage collector.

isNamespaceAgnostic: true
},
'apm-indices': {
isNamespaceAgnostic: true
}
},
Expand Down
2 changes: 1 addition & 1 deletion x-pack/legacy/plugins/apm/mappings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"apm-telemetry": {
"apm-services-telemetry": {
"properties": {
"has_any_services": {
"type": "boolean"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
*/

import { SavedObjectAttributes } from 'src/core/server';
import { createApmTelementry, storeApmServicesTelemetry } from '../index';
import {
APM_TELEMETRY_DOC_ID,
createApmTelementry,
storeApmTelemetry
} from '../apm_telemetry';
APM_SERVICES_TELEMETRY_SAVED_OBJECT_TYPE,
APM_SERVICES_TELEMETRY_SAVED_OBJECT_ID
} from '../../../../common/apm_saved_object_constants';

describe('apm_telemetry', () => {
describe('createApmTelementry', () => {
Expand Down Expand Up @@ -44,7 +44,7 @@ describe('apm_telemetry', () => {
});
});

describe('storeApmTelemetry', () => {
describe('storeApmServicesTelemetry', () => {
let server: any;
let apmTelemetry: SavedObjectAttributes;
let savedObjectsClientInstance: any;
Expand Down Expand Up @@ -75,24 +75,24 @@ describe('apm_telemetry', () => {
});

it('should call savedObjectsClient create with the given ApmTelemetry object', () => {
storeApmTelemetry(server, apmTelemetry);
storeApmServicesTelemetry(server, apmTelemetry);
expect(savedObjectsClientInstance.create.mock.calls[0][1]).toBe(
apmTelemetry
);
});

it('should call savedObjectsClient create with the apm-telemetry document type and ID', () => {
storeApmTelemetry(server, apmTelemetry);
storeApmServicesTelemetry(server, apmTelemetry);
expect(savedObjectsClientInstance.create.mock.calls[0][0]).toBe(
'apm-telemetry'
APM_SERVICES_TELEMETRY_SAVED_OBJECT_TYPE
);
expect(savedObjectsClientInstance.create.mock.calls[0][2].id).toBe(
APM_TELEMETRY_DOC_ID
APM_SERVICES_TELEMETRY_SAVED_OBJECT_ID
);
});

it('should call savedObjectsClient create with overwrite: true', () => {
storeApmTelemetry(server, apmTelemetry);
storeApmServicesTelemetry(server, apmTelemetry);
expect(savedObjectsClientInstance.create.mock.calls[0][2].overwrite).toBe(
true
);
Expand Down

This file was deleted.

80 changes: 74 additions & 6 deletions x-pack/legacy/plugins/apm/server/lib/apm_telemetry/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,77 @@
* 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 { CoreSetup } from 'src/core/server';
import { isAgentName } from '../../../common/agent_name';
import { getInternalSavedObjectsClient } from '../helpers/saved_objects_client';
import {
APM_SERVICES_TELEMETRY_SAVED_OBJECT_TYPE,
APM_SERVICES_TELEMETRY_SAVED_OBJECT_ID
} from '../../../common/apm_saved_object_constants';
import { LegacySetup } from '../../new-platform/plugin';

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 storeApmServicesTelemetry(
server: Server,
apmTelemetry: SavedObjectAttributes
) {
try {
const internalSavedObjectsClient = getInternalSavedObjectsClient(server);
await internalSavedObjectsClient.create(
APM_SERVICES_TELEMETRY_SAVED_OBJECT_TYPE,
apmTelemetry,
{
id: APM_SERVICES_TELEMETRY_SAVED_OBJECT_ID,
overwrite: true
}
);
} catch (e) {
server.log(['error'], `Unable to save APM telemetry data: ${e.message}`);
}
}

interface LegacySetupWithUsageCollector extends LegacySetup {
server: LegacySetup['server'] & {
usage: {
collectorSet: {
makeUsageCollector: (options: unknown) => unknown;
register: (options: unknown) => unknown;
};
};
};
}

export function makeApmUsageCollector(
core: CoreSetup,
{ server }: LegacySetupWithUsageCollector
) {
const apmUsageCollector = server.usage.collectorSet.makeUsageCollector({
type: 'apm',
fetch: async () => {
const internalSavedObjectsClient = getInternalSavedObjectsClient(server);
try {
const apmTelemetrySavedObject = await internalSavedObjectsClient.get(
APM_SERVICES_TELEMETRY_SAVED_OBJECT_TYPE,
APM_SERVICES_TELEMETRY_SAVED_OBJECT_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
Original file line number Diff line number Diff line change
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)
}),
sorenlouv marked this conversation as resolved.
Show resolved Hide resolved
uiSettings.get('search:includeFrozen')
]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { getSavedObjectsClient } from './saved_objects_client';
import { getInternalSavedObjectsClient } from './saved_objects_client';

describe('saved_objects/client', () => {
describe('getSavedObjectsClient', () => {
Expand All @@ -31,25 +31,25 @@ describe('saved_objects/client', () => {
});

it('should use internal user "admin"', () => {
getSavedObjectsClient(server);
getInternalSavedObjectsClient(server);

expect(server.plugins.elasticsearch.getCluster).toHaveBeenCalledWith(
'admin'
);
});

it('should call getSavedObjectsRepository with a cluster using the internal user context', () => {
getSavedObjectsClient(server);
getInternalSavedObjectsClient(server);

expect(
server.savedObjects.getSavedObjectsRepository
).toHaveBeenCalledWith(callWithInternalUser);
});

it('should return a SavedObjectsClient initialized with the saved objects internal repository', () => {
const result = getSavedObjectsClient(server);
const internalSavedObjectsClient = getInternalSavedObjectsClient(server);

expect(result).toBe(savedObjectsClientInstance);
expect(internalSavedObjectsClient).toBe(savedObjectsClientInstance);
expect(server.savedObjects.SavedObjectsClient).toHaveBeenCalledWith(
internalRepository
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

import { Server } from 'hapi';

export function getSavedObjectsClient(server: Server, clusterName = 'admin') {
export function getInternalSavedObjectsClient(
server: Server,
clusterName = 'admin'
) {
const { SavedObjectsClient, getSavedObjectsRepository } = server.savedObjects;
const { callWithInternalUser } = server.plugins.elasticsearch.getCluster(
clusterName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ function getMockRequest() {
callWithInternalUser: callWithInternalUserSpy
})
}
},
savedObjects: {
getScopedSavedObjectsClient: () => ({ get: async () => false })
}
},
getUiSettingsService: () => ({ get: async () => false })
Expand Down
Original file line number Diff line number Diff line change
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
11 changes: 7 additions & 4 deletions x-pack/legacy/plugins/apm/server/lib/index_pattern/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@
* 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 { getInternalSavedObjectsClient } from '../helpers/saved_objects_client';
import apmIndexPattern from '../../../../../../../src/legacy/core_plugins/kibana/server/tutorials/apm/index_pattern.json';

export async function getAPMIndexPattern(server: Server) {
const config = server.config();
const apmIndexPatternTitle = config.get('apm_oss.indexPattern');
const savedObjectsClient = getSavedObjectsClient(server);
const internalSavedObjectsClient = getInternalSavedObjectsClient(server);
try {
return await savedObjectsClient.get('index-pattern', apmIndexPattern.id);
return await internalSavedObjectsClient.get(
'index-pattern',
apmIndexPattern.id
);
} catch (error) {
// if GET fails, then create a new index pattern saved object
return await savedObjectsClient.create(
return await internalSavedObjectsClient.create(
'index-pattern',
{
...apmIndexPattern.attributes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@ import { CoreSetup } from 'src/core/server';
import { CallCluster } from '../../../../../../../../src/legacy/core_plugins/elasticsearch';
import { getApmIndices } from '../apm_indices/get_apm_indices';
import { LegacySetup } from '../../../new-platform/plugin';
import { getInternalSavedObjectsClient } from '../../helpers/saved_objects_client';

export async function createApmAgentConfigurationIndex(
core: CoreSetup,
{ server }: LegacySetup
) {
try {
const indices = await getApmIndices(server);
const config = server.config();
const internalSavedObjectsClient = getInternalSavedObjectsClient(server);
const indices = await getApmIndices({
savedObjectsClient: internalSavedObjectsClient,
config
});
const index = indices['apm_oss.apmAgentConfigurationIndex'];
const { callWithInternalUser } = server.plugins.elasticsearch.getCluster(
'admin'
sorenlouv marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
Loading