Skip to content

Commit

Permalink
[Telemetry] collect xpack.cloud details (elastic#31180) (elastic#31701)
Browse files Browse the repository at this point in the history
* collect xpack.cloud details

* add elasticsearch cluster uuid

* remove cloud ID from telemetry stats

* remove esUUID from telemtry stats
  • Loading branch information
Bamieh authored Feb 21, 2019
1 parent c645acd commit 5556a6e
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
7 changes: 7 additions & 0 deletions x-pack/plugins/cloud/constants.ts
Original file line number Diff line number Diff line change
@@ -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';
58 changes: 58 additions & 0 deletions x-pack/plugins/cloud/get_cloud_usage_collector.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
42 changes: 42 additions & 0 deletions x-pack/plugins/cloud/get_cloud_usage_collector.ts
Original file line number Diff line number Diff line change
@@ -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),
});
}
3 changes: 3 additions & 0 deletions x-pack/plugins/cloud/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -38,6 +40,7 @@ export const cloud = kibana => {
server.expose('config', {
isCloudEnabled: !!config.id
});
server.usage.collectorSet.register(getCloudUsageCollector(server));
}
});
};

0 comments on commit 5556a6e

Please sign in to comment.