[6.7] [Telemetry] collect xpack.cloud details (#31180) (#31704)

* [Telemetry] collect xpack.cloud details (#31180)

* collect xpack.cloud details

* add elasticsearch cluster uuid

* remove cloud ID from telemetry stats

* remove esUUID from telemtry stats

* add stack_stats.kibana.plugins.cloud.isCloudEnabled
This commit is contained in:
Ahmad Bamieh 2019-02-24 15:09:17 +02:00 committed by GitHub
parent 4e0b72d035
commit a5537ee3b4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 110 additions and 0 deletions

View file

@ -0,0 +1,7 @@
/*
* 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.
*/
export const KIBANA_CLOUD_STATS_TYPE = 'cloud';

View file

@ -0,0 +1,58 @@
/*
* 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.
*/
import sinon from 'sinon';
import {
createCollectorFetch,
getCloudUsageCollector,
KibanaHapiServer,
} from './get_cloud_usage_collector';
const CLOUD_ID_STAGING =
'staging:dXMtZWFzdC0xLmF3cy5mb3VuZC5pbyRjZWM2ZjI2MWE3NGJmMjRjZTMzYmI4ODExYjg0Mjk0ZiRjNmMyY2E2ZDA0MjI0OWFmMGNjN2Q3YTllOTYyNTc0Mw==';
const CLOUD_ID =
'dXMtZWFzdC0xLmF3cy5mb3VuZC5pbyRjZWM2ZjI2MWE3NGJmMjRjZTMzYmI4ODExYjg0Mjk0ZiRjNmMyY2E2ZDA0MjI0OWFmMGNjN2Q3YTllOTYyNTc0Mw==';
const getMockServer = (cloudId?: string) => ({
usage: { collectorSet: { makeUsageCollector: sinon.stub() } },
config() {
return {
get(path: string) {
switch (path) {
case 'xpack.cloud':
return { id: cloudId };
default:
throw Error(`server.config().get(${path}) should not be called by this collector.`);
}
},
};
},
});
describe('Cloud usage collector', () => {
describe('collector', () => {
it('returns `isCloudEnabled: false` if `xpack.cloud.id` is not defined', async () => {
const collector = await createCollectorFetch(getMockServer())();
expect(collector.isCloudEnabled).toBe(false);
});
it('returns `isCloudEnabled: true` if `xpack.cloud.id` is defined', async () => {
const stagingCollector = await createCollectorFetch(getMockServer(CLOUD_ID))();
const collector = await createCollectorFetch(getMockServer(CLOUD_ID_STAGING))();
expect(collector.isCloudEnabled).toBe(true);
expect(stagingCollector.isCloudEnabled).toBe(true);
});
});
});
describe('getCloudUsageCollector', () => {
it('returns calls `collectorSet.makeUsageCollector`', () => {
const mockServer = getMockServer();
getCloudUsageCollector((mockServer as any) as KibanaHapiServer);
const { makeUsageCollector } = mockServer.usage.collectorSet;
expect(makeUsageCollector.calledOnce).toBe(true);
});
});

View file

@ -0,0 +1,42 @@
/*
* 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.
*/
import { Server } from 'hapi';
import { KIBANA_CLOUD_STATS_TYPE } from './constants';
export interface UsageStats {
isCloudEnabled: boolean;
}
export interface KibanaHapiServer extends Server {
usage: {
collectorSet: {
makeUsageCollector: any;
};
};
}
export function createCollectorFetch(server: any) {
return async function fetchUsageStats(): Promise<UsageStats> {
const { id } = server.config().get(`xpack.cloud`);
return {
isCloudEnabled: !!id,
};
};
}
/*
* @param {Object} server
* @return {Object} kibana usage stats type collection object
*/
export function getCloudUsageCollector(server: KibanaHapiServer) {
const { collectorSet } = server.usage;
return collectorSet.makeUsageCollector({
type: KIBANA_CLOUD_STATS_TYPE,
fetch: createCollectorFetch(server),
});
}

View file

@ -4,6 +4,8 @@
* you may not use this file except in compliance with the Elastic License.
*/
import { getCloudUsageCollector } from './get_cloud_usage_collector';
export const cloud = kibana => {
return new kibana.Plugin({
id: 'cloud',
@ -38,6 +40,7 @@ export const cloud = kibana => {
server.expose('config', {
isCloudEnabled: !!config.id
});
server.usage.collectorSet.register(getCloudUsageCollector(server));
}
});
};