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 6 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
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
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');
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@
* under the License.
*/

import { get, omit } from 'lodash';
// @ts-ignore
import { getClusterInfo } from './get_cluster_info';
import { getClusterInfo, ESClusterInfo } from './get_cluster_info';
import { getClusterStats } from './get_cluster_stats';
// @ts-ignore
import { getKibana, handleKibanaStats, KibanaUsageStats } from './get_kibana';
import { StatsGetter } from '../collection_manager';

Expand All @@ -33,35 +30,32 @@ import { StatsGetter } from '../collection_manager';
* @param {Object} clusterInfo Cluster info (GET /)
* @param {Object} clusterStats Cluster stats (GET /_cluster/stats)
* @param {Object} kibana The Kibana Usage stats
* @return {Object} A combined object containing the different responses.
*/
export function handleLocalStats(
server: any,
clusterInfo: any,
clusterStats: any,
{ cluster_name, cluster_uuid, version }: ESClusterInfo,
{ _nodes, cluster_name: clusterName, ...clusterStats }: any,
kibana: KibanaUsageStats
) {
return {
timestamp: new Date().toISOString(),
cluster_uuid: get(clusterInfo, 'cluster_uuid'),
cluster_name: get(clusterInfo, 'cluster_name'),
version: get(clusterInfo, 'version.number'),
cluster_stats: omit(clusterStats, '_nodes', 'cluster_name'),
cluster_uuid,
cluster_name,
version: version.number,
cluster_stats: clusterStats,
collection: 'local',
stack_stats: {
kibana: handleKibanaStats(server, kibana),
},
};
}

export type TelemetryLocalStats = ReturnType<typeof handleLocalStats>;

/**
* Get statistics for all products joined by Elasticsearch cluster.
*
* @param {Object} server The Kibana server instance used to call ES as the internal user
* @param {function} callCluster The callWithInternalUser handler (exposed for testing)
* @return {Promise} The object containing the current Elasticsearch cluster's telemetry.
*/
export const getLocalStats: StatsGetter = async (clustersDetails, config) => {
export const getLocalStats: StatsGetter<TelemetryLocalStats> = async (clustersDetails, config) => {
const { server, callCluster, usageCollection } = config;

return await Promise.all(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* under the License.
*/

// @ts-ignore
export { getLocalStats } from './get_local_stats';
export { getClusterUuids } from './get_cluster_stats';
export { registerCollection } from './register_collection';

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
Expand Up @@ -7,4 +7,5 @@
/**
* The timeout used by each request, whenever a timeout can be specified.
*/

export const TIMEOUT = '30s';
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* 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 { getStatsWithXpack } from './get_stats_with_xpack';

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-like 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 getStatsWithXpack([{ 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 getStatsWithXpack([{ clusterUuid: '1234' }], {
callCluster,
usageCollection,
server,
} as any);
expect(stats[0].license).toStrictEqual({ type: 'basic' });
expect(stats.map(({ timestamp, ...rest }) => rest)).toMatchSnapshot();
});
});
Loading