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

[Telemetry] Separate the license retrieval from the stats in the usage collectors #57332

Merged
merged 13 commits into from
Feb 25, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ export interface StatsCollectionConfig {

export type StatsGetterConfig = UnencryptedStatsGetterConfig | EncryptedStatsGetterConfig;
export type ClusterDetailsGetter = (config: StatsCollectionConfig) => Promise<ClusterDetails[]>;
export type StatsGetter = (
export type StatsGetter<T = unknown> = (
afharo marked this conversation as resolved.
Show resolved Hide resolved
clustersDetails: ClusterDetails[],
config: StatsCollectionConfig
) => Promise<any[]>;
) => Promise<T[]>;

interface CollectionConfig {
title: string;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
/*
* 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.
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import expect from '@kbn/expect';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { getAggregatedStats } from './get_aggregated_stats';

const kibana = {
kibana: {
great: 'googlymoogly',
versions: [{ version: '8675309', count: 1 }],
},
kibana_stats: {
os: {
platform: 'rocky',
platformRelease: 'iv',
},
},
localization: {
locale: 'en',
labelsCount: 0,
integrities: {},
},
sun: { chances: 5 },
clouds: { chances: 95 },
rain: { chances: 2 },
snow: { chances: 0 },
};

const getMockServer = (getCluster = jest.fn()) => ({
log(tags: string[], message: string) {
// eslint-disable-next-line no-console
console.log({ tags, message });
},
config() {
return {
get(item: string) {
switch (item) {
case 'pkg.version':
return '8675309-snapshot';
default:
throw Error(`unexpected config.get('${item}') received.`);
}
},
};
},
plugins: {
elasticsearch: { getCluster },
},
});

const mockUsageCollection = (kibanaUsage = kibana) => ({
bulkFetch: () => kibanaUsage,
toObject: (data: any) => data,
});

describe('Telemetry Collection: Get Aggregated Stats', () => {
test('OSS telemetry (no license nor X-Pack telemetry)', async () => {
const callCluster = jest.fn(async (method: string, options: { path?: string }) => {
switch (method) {
case 'transport.request':
if (options.path === '/_license' || options.path === '/_xpack/usage') {
// eslint-disable-next-line no-throw-literal
throw { statusCode: 404 };
}
return {};
case 'info':
return { cluster_uuid: 'test', cluster_name: 'test', version: { number: '8.0.0' } };
default:
return {};
}
});
const usageCollection = mockUsageCollection();
const server = getMockServer();

const stats = await getAggregatedStats([{ clusterUuid: '1234' }], {
callCluster,
usageCollection,
server,
} as any);
expect(stats[0].license).toBe(undefined);
expect(stats.map(({ timestamp, ...rest }) => rest)).toMatchSnapshot();
});

test('X-Pack telemetry (license + X-Pack)', async () => {
const callCluster = jest.fn(async (method: string, options: { path?: string }) => {
switch (method) {
case 'transport.request':
if (options.path === '/_license') {
return {
license: { type: 'basic' },
};
}
if (options.path === '/_xpack/usage') {
return {};
}
case 'info':
return { cluster_uuid: 'test', cluster_name: 'test', version: { number: '8.0.0' } };
default:
return {};
}
});
const usageCollection = mockUsageCollection();
const server = getMockServer();

const stats = await getAggregatedStats([{ clusterUuid: '1234' }], {
callCluster,
usageCollection,
server,
} as any);
expect(stats[0].license).toStrictEqual({ type: 'basic' });
expect(stats.map(({ timestamp, ...rest }) => rest)).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { StatsGetter } from '../collection_manager';
import { getLocalStats, TelemetryLocalStats } from './get_local_stats';
import { getXPackLicense, getXPackUsage, ESLicense } from './get_xpack';

export type TelemetryAggregatedStats = TelemetryLocalStats & {
license?: ESLicense;
stack_stats: { xpack?: object };
};

export const getAggregatedStats: StatsGetter<TelemetryAggregatedStats> = async function(
clustersDetails,
config
) {
const { callCluster } = config;
const clustersLocalStats = await getLocalStats(clustersDetails, config);
const license = await getXPackLicense(callCluster);
const xpack = await getXPackUsage(callCluster).catch(() => undefined); // We want to still report something even when this method fails.

return clustersLocalStats.map(localStats => {
if (license) {
if (!xpack) {
throw new Error(`It has a license but no xpack usage!`);
}
return {
...localStats,
license,
stack_stats: { ...localStats.stack_stats, xpack },
};
}

return localStats;
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,32 @@
* under the License.
*/

import { CallCluster } from 'src/legacy/core_plugins/elasticsearch';

// This can be removed when the ES client improves the types
export interface ESClusterInfo {
cluster_uuid: string;
cluster_name: string;
version: {
number: string;
build_flavor: string;
build_type: string;
build_hash: string;
build_date: string;
build_snapshot?: boolean;
lucene_version: string;
minimum_wire_compatibility_version: string;
minimum_index_compatibility_version: string;
};
}

/**
* Get the cluster info from the connected cluster.
*
* This is the equivalent to GET /
*
* @param {function} callCluster The callWithInternalUser handler (exposed for testing)
* @return {Promise} The response from Elasticsearch.
*/
export function getClusterInfo(callCluster) {
return callCluster('info');
export function getClusterInfo(callCluster: CallCluster) {
return callCluster<ESClusterInfo>('info');
}
Loading