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

[Maps] Fix regression preventing maps telemetry from populating & remove task manager logic #52834

Merged
merged 28 commits into from
Jan 6, 2020
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
2cf4d79
Remove task logic. Remove server refs and revise for np. Migrate a fe…
Dec 12, 2019
5a466b0
Remove unused reference
Dec 12, 2019
41f65fa
Merge remote-tracking branch 'upstream/master' into remove-task-logic…
Dec 13, 2019
310485c
Update mappings
Dec 13, 2019
45756f4
Merge remote-tracking branch 'upstream/master' into remove-task-logic…
Dec 16, 2019
4cb5e31
Merge remote-tracking branch 'upstream/master' into remove-task-logic…
Dec 17, 2019
92e07b5
Test usage collector register
Dec 17, 2019
1196755
Update api integration tests to include maps now that telemetry is 'n…
Dec 17, 2019
60c7ab8
Update integration test to use stack stats
Dec 17, 2019
1157510
Update integration test to look for 'maps-telemetry' instead of 'maps'
Dec 18, 2019
41329d0
Merge remote-tracking branch 'upstream/master' into remove-task-logic…
Dec 18, 2019
a64b67f
Update jest test to reflect calls to register
Dec 18, 2019
adb52cd
Follow the same pattern as other int tests and test reliable nested a…
Dec 19, 2019
60cfdf2
Back out np-related changes for separate PR
Dec 19, 2019
cdb926c
Merge remote-tracking branch 'upstream/master' into remove-task-logic…
Dec 19, 2019
58b1a57
Merge remote-tracking branch 'upstream/master' into remove-task-logic…
Dec 20, 2019
b885448
Merge remote-tracking branch 'upstream/master' into remove-task-logic…
Dec 23, 2019
00180ef
timeCaptured hasn't changed but for some reason stopped working. Gett…
Dec 23, 2019
98ecd90
Merge remote-tracking branch 'upstream/master' into remove-task-logic…
Dec 26, 2019
6734535
Back out file shuffling for separate PR
Dec 26, 2019
756707b
Remove mappings updates (handled in separate PR)
Dec 26, 2019
422b363
Merge remote-tracking branch 'upstream/master' into remove-task-logic…
Dec 26, 2019
a2f3e6d
Merge branch 'master' into remove-task-logic-from-maps-telemetry
elasticmachine Dec 30, 2019
fc9780b
Merge remote-tracking branch 'upstream/master' into remove-task-logic…
Jan 6, 2020
bdaacc1
Review feedback. Move telemetry type constant to constants file
Jan 6, 2020
cbc902a
Merge branch 'remove-task-logic-from-maps-telemetry' of github.com:aa…
Jan 6, 2020
1ab2bfc
Consolidate imports
Jan 6, 2020
97730c8
Linting fix
Jan 6, 2020
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/legacy/plugins/maps/common/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const EMS_TILES_VECTOR_TILE_PATH = 'ems/tiles/vector/tile';
export const MAP_SAVED_OBJECT_TYPE = 'map';
export const APP_ID = 'maps';
export const APP_ICON = 'gisApp';
export const TELEMETRY_TYPE = 'maps-telemetry';

export const MAP_APP_PATH = `app/${APP_ID}`;
export const GIS_API_PATH = `api/${APP_ID}`;
Expand Down
20 changes: 13 additions & 7 deletions x-pack/legacy/plugins/maps/server/maps_telemetry/maps_telemetry.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@
*/

import _ from 'lodash';
import { EMS_FILE, ES_GEO_FIELD_TYPE, MAP_SAVED_OBJECT_TYPE } from '../../common/constants';
import {
EMS_FILE,
ES_GEO_FIELD_TYPE,
MAP_SAVED_OBJECT_TYPE,
TELEMETRY_TYPE
} from '../../common/constants';

function getSavedObjectsClient(server, callCluster) {
function getSavedObjectsClient(server) {
const { SavedObjectsClient, getSavedObjectsRepository } = server.savedObjects;
const callCluster = server.plugins.elasticsearch.getCluster('admin').callWithInternalUser;
const internalRepository = getSavedObjectsRepository(callCluster);
return new SavedObjectsClient(internalRepository);
}
Expand Down Expand Up @@ -79,7 +85,7 @@ export function buildMapsTelemetry({ mapSavedObjects, indexPatternSavedObjects,
// Total count of maps
mapsTotalCount: mapsCount,
// Time of capture
timeCaptured: new Date(),
timeCaptured: new Date().toISOString(),
attributesPerMap: {
// Count of data sources per map
dataSourcesCount: {
Expand Down Expand Up @@ -115,16 +121,16 @@ async function getIndexPatternSavedObjects(savedObjectsClient) {
return _.get(indexPatternSavedObjects, 'saved_objects', []);
}

export async function getMapsTelemetry(server, callCluster) {
const savedObjectsClient = getSavedObjectsClient(server, callCluster);
export async function getMapsTelemetry(server) {
const savedObjectsClient = getSavedObjectsClient(server);
const mapSavedObjects = await getMapSavedObjects(savedObjectsClient);
const indexPatternSavedObjects = await getIndexPatternSavedObjects(savedObjectsClient);
const settings = {
showMapVisualizationTypes: server.config().get('xpack.maps.showMapVisualizationTypes'),
};
const mapsTelemetry = buildMapsTelemetry({ mapSavedObjects, indexPatternSavedObjects, settings });
return await savedObjectsClient.create('maps-telemetry', mapsTelemetry, {
id: 'maps-telemetry',
return await savedObjectsClient.create(TELEMETRY_TYPE, mapsTelemetry, {
id: TELEMETRY_TYPE,
overwrite: true,
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,85 +4,19 @@
* you may not use this file except in compliance with the Elastic License.
*/

import _ from 'lodash';
import { TASK_ID, scheduleTask, registerMapsTelemetryTask } from './telemetry_task';
import { getMapsTelemetry } from './maps_telemetry';
import { TELEMETRY_TYPE } from '../../common/constants';

export function initTelemetryCollection(usageCollection, server) {
registerMapsTelemetryTask(server);
scheduleTask(server);
registerMapsUsageCollector(usageCollection, server);
}

async function isTaskManagerReady(server) {
const result = await fetch(server);
return result !== null;
}

async function fetch(server) {
let docs;
const taskManager = server.plugins.task_manager;

if (!taskManager) {
return null;
}

try {
({ docs } = await taskManager.fetch({
query: {
bool: {
filter: {
term: {
_id: `task:${TASK_ID}`,
},
},
},
},
}));
} catch (err) {
const errMessage = err && err.message ? err.message : err.toString();
/*
* The usage service WILL to try to fetch from this collector before the task manager has been initialized, because the task manager
* has to wait for all plugins to initialize first.
* It's fine to ignore it as next time around it will be initialized (or it will throw a different type of error)
*/
if (errMessage.indexOf('NotInitialized') >= 0) {
return null;
} else {
throw err;
}
if (!usageCollection) {
return;
}

return docs;
}

export function buildCollectorObj(server) {
let isCollectorReady = false;
async function determineIfTaskManagerIsReady() {
let isReady = false;
try {
isReady = await isTaskManagerReady(server);
} catch (err) {} // eslint-disable-line

if (isReady) {
isCollectorReady = true;
} else {
setTimeout(determineIfTaskManagerIsReady, 500);
}
}
determineIfTaskManagerIsReady();

return {
type: 'maps',
isReady: () => isCollectorReady,
fetch: async () => {
const docs = await fetch(server);
return _.get(docs, '[0].state.stats');
},
};
}
const mapsUsageCollector = usageCollection.makeUsageCollector({
type: TELEMETRY_TYPE,
isReady: () => true,
fetch: async () => await getMapsTelemetry(server),
});

export function registerMapsUsageCollector(usageCollection, server) {
const collectorObj = buildCollectorObj(server);
const mapsUsageCollector = usageCollection.makeUsageCollector(collectorObj);
usageCollection.registerCollector(mapsUsageCollector);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,60 +4,32 @@
* you may not use this file except in compliance with the Elastic License.
*/

import sinon from 'sinon';
import { getMockCallWithInternal, getMockKbnServer, getMockTaskFetch } from '../test_utils';
import { buildCollectorObj } from './maps_usage_collector';
import { initTelemetryCollection } from './maps_usage_collector';

describe('buildCollectorObj#fetch', () => {
let mockKbnServer;
let makeUsageCollectorStub;
let registerStub;
let usageCollection;

beforeEach(() => {
mockKbnServer = getMockKbnServer();
makeUsageCollectorStub = jest.fn();
registerStub = jest.fn();
usageCollection = {
makeUsageCollector: makeUsageCollectorStub,
registerCollector: registerStub,
};
});

test('can return empty stats', async () => {
thomasneirynck marked this conversation as resolved.
Show resolved Hide resolved
const { type, fetch } = buildCollectorObj(mockKbnServer);
expect(type).toBe('maps');
const fetchResult = await fetch();
expect(fetchResult).toEqual({});
});

test('provides known stats', async () => {
const mockTaskFetch = getMockTaskFetch([
{
state: {
runs: 2,
stats: { wombat_sightings: { total: 712, max: 84, min: 7, avg: 63 } },
},
},
]);
mockKbnServer = getMockKbnServer(getMockCallWithInternal(), mockTaskFetch);

const { type, fetch } = buildCollectorObj(mockKbnServer);
expect(type).toBe('maps');
const fetchResult = await fetch();
expect(fetchResult).toEqual({ wombat_sightings: { total: 712, max: 84, min: 7, avg: 63 } });
});

describe('Error handling', () => {
test('Silently handles Task Manager NotInitialized', async () => {
const mockTaskFetch = sinon.stub();
mockTaskFetch.rejects(
new Error('NotInitialized taskManager is still waiting for plugins to load')
);
mockKbnServer = getMockKbnServer(getMockCallWithInternal(), mockTaskFetch);

const { fetch } = buildCollectorObj(mockKbnServer);
await expect(fetch()).resolves.toBe(undefined);
});
// In real life, the CollectorSet calls fetch and handles errors
test('defers the errors', async () => {
const mockTaskFetch = sinon.stub();
mockTaskFetch.rejects(new Error('Sad violin'));
mockKbnServer = getMockKbnServer(getMockCallWithInternal(), mockTaskFetch);
test('makes and registers maps usage collector', async () => {
const serverPlaceholder = {};
initTelemetryCollection(usageCollection, serverPlaceholder);

const { fetch } = buildCollectorObj(mockKbnServer);
await expect(fetch()).rejects.toMatchObject(new Error('Sad violin'));
expect(registerStub).toHaveBeenCalledTimes(1);
expect(makeUsageCollectorStub).toHaveBeenCalledTimes(1);
expect(makeUsageCollectorStub).toHaveBeenCalledWith({
type: expect.any(String),
isReady: expect.any(Function),
fetch: expect.any(Function),
});
});
});
125 changes: 0 additions & 125 deletions x-pack/legacy/plugins/maps/server/maps_telemetry/telemetry_task.js

This file was deleted.

Loading