Skip to content

Commit

Permalink
[7.x] Remove the parameter timestamp in `/api/telemetry/v2/clusters…
Browse files Browse the repository at this point in the history
…/_stats` (#83791) (#84065)

Co-authored-by: Kibana Machine <[email protected]>
# Conflicts:
#	x-pack/test/functional/apps/infra/home_page.ts
#	x-pack/test/functional/apps/infra/logs_source_configuration.ts
  • Loading branch information
afharo authored Nov 23, 2020
1 parent 775f543 commit e5433f0
Show file tree
Hide file tree
Showing 13 changed files with 14 additions and 57 deletions.
15 changes: 2 additions & 13 deletions src/plugins/telemetry/public/services/telemetry_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,20 @@
* under the License.
*/

// ESLint disabled dot-notation we can access the private key telemetryService['http']
/* eslint-disable dot-notation */
const mockMomentValueOf = jest.fn();

jest.mock('moment', () => {
return jest.fn().mockImplementation(() => {
return {
valueOf: mockMomentValueOf,
};
});
});

import { mockTelemetryService } from '../mocks';

describe('TelemetryService', () => {
describe('fetchTelemetry', () => {
it('calls expected URL with 20 minutes - now', async () => {
const timestamp = Date.now();
mockMomentValueOf.mockReturnValueOnce(timestamp);
const telemetryService = mockTelemetryService();

await telemetryService.fetchTelemetry();
expect(telemetryService['http'].post).toBeCalledWith('/api/telemetry/v2/clusters/_stats', {
body: JSON.stringify({ unencrypted: false, timestamp }),
body: JSON.stringify({ unencrypted: false }),
});
expect(mockMomentValueOf).toBeCalled();
});
});

Expand Down
2 changes: 0 additions & 2 deletions src/plugins/telemetry/public/services/telemetry_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* under the License.
*/

import moment from 'moment';
import { i18n } from '@kbn/i18n';
import { CoreStart } from 'kibana/public';
import { TelemetryPluginConfig } from '../plugin';
Expand Down Expand Up @@ -124,7 +123,6 @@ export class TelemetryService {
return this.http.post('/api/telemetry/v2/clusters/_stats', {
body: JSON.stringify({
unencrypted,
timestamp: moment().valueOf(),
}),
});
};
Expand Down
2 changes: 0 additions & 2 deletions src/plugins/telemetry/server/routes/telemetry_opt_in.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* under the License.
*/

import moment from 'moment';
import { Observable } from 'rxjs';
import { take } from 'rxjs/operators';
import { schema } from '@kbn/config-schema';
Expand Down Expand Up @@ -86,7 +85,6 @@ export function registerTelemetryOptInRoutes({
}

const statsGetterConfig: StatsGetterConfig = {
timestamp: moment().valueOf(),
unencrypted: false,
};

Expand Down
2 changes: 0 additions & 2 deletions src/plugins/telemetry/server/routes/telemetry_opt_in_stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

// @ts-ignore
import fetch from 'node-fetch';
import moment from 'moment';

import { IRouter } from 'kibana/server';
import { schema } from '@kbn/config-schema';
Expand Down Expand Up @@ -72,7 +71,6 @@ export function registerTelemetryOptInStatsRoutes(
const unencrypted = req.body.unencrypted;

const statsGetterConfig: StatsGetterConfig = {
timestamp: moment().valueOf(),
unencrypted,
request: req,
};
Expand Down
12 changes: 1 addition & 11 deletions src/plugins/telemetry/server/routes/telemetry_usage_stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,13 @@
* under the License.
*/

import moment from 'moment';
import { schema } from '@kbn/config-schema';
import { TypeOptions } from '@kbn/config-schema/target/types/types';
import { IRouter } from 'kibana/server';
import {
TelemetryCollectionManagerPluginSetup,
StatsGetterConfig,
} from 'src/plugins/telemetry_collection_manager/server';

const validate: TypeOptions<string | number>['validate'] = (value) => {
if (!moment(value).isValid()) {
return `${value} is not a valid date`;
}
};

export function registerTelemetryUsageStatsRoutes(
router: IRouter,
telemetryCollectionManager: TelemetryCollectionManagerPluginSetup,
Expand All @@ -43,16 +35,14 @@ export function registerTelemetryUsageStatsRoutes(
validate: {
body: schema.object({
unencrypted: schema.boolean({ defaultValue: false }),
timestamp: schema.oneOf([schema.string({ validate }), schema.number({ validate })]),
}),
},
},
async (context, req, res) => {
const { unencrypted, timestamp } = req.body;
const { unencrypted } = req.body;

try {
const statsConfig: StatsGetterConfig = {
timestamp: moment(timestamp).valueOf(),
request: req,
unencrypted,
};
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/telemetry_collection_manager/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export class TelemetryCollectionManagerPlugin
collectionSoService: SavedObjectsServiceStart,
usageCollection: UsageCollectionSetup
): StatsCollectionConfig {
const { timestamp, request } = config;
const { request } = config;

const callCluster = config.unencrypted
? collection.esCluster.asScoped(request).callAsCurrentUser
Expand All @@ -160,7 +160,7 @@ export class TelemetryCollectionManagerPlugin
// Provide the kibanaRequest so opted-in plugins can scope their custom clients only if the request is not encrypted
const kibanaRequest = config.unencrypted ? request : void 0;

return { callCluster, timestamp, usageCollection, esClient, soClient, kibanaRequest };
return { callCluster, usageCollection, esClient, soClient, kibanaRequest };
}

private async getOptInStats(optInStatus: boolean, config: StatsGetterConfig) {
Expand Down
2 changes: 0 additions & 2 deletions src/plugins/telemetry_collection_manager/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ export interface TelemetryOptInStats {

export interface BaseStatsGetterConfig {
unencrypted: boolean;
timestamp: number;
request?: KibanaRequest;
}

Expand All @@ -76,7 +75,6 @@ export interface ClusterDetails {
export interface StatsCollectionConfig {
usageCollection: UsageCollectionSetup;
callCluster: LegacyAPICaller;
timestamp: number;
esClient: ElasticsearchClient;
soClient: SavedObjectsClientContract | ISavedObjectsRepository;
kibanaRequest: KibanaRequest | undefined; // intentionally `| undefined` to enforce providing the parameter
Expand Down
14 changes: 4 additions & 10 deletions test/api_integration/apis/telemetry/telemetry_local.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,10 @@ export default function ({ getService }) {
});

it('should pull local stats and validate data types', async () => {
const timestamp = '2018-07-23T22:13:00Z';

const { body } = await supertest
.post('/api/telemetry/v2/clusters/_stats')
.set('kbn-xsrf', 'xxx')
.send({ timestamp, unencrypted: true })
.send({ unencrypted: true })
.expect(200);

expect(body.length).to.be(1);
Expand Down Expand Up @@ -95,12 +93,10 @@ export default function ({ getService }) {
});

it('should pull local stats and validate fields', async () => {
const timestamp = '2018-07-23T22:13:00Z';

const { body } = await supertest
.post('/api/telemetry/v2/clusters/_stats')
.set('kbn-xsrf', 'xxx')
.send({ timestamp, unencrypted: true })
.send({ unencrypted: true })
.expect(200);

const stats = body[0];
Expand Down Expand Up @@ -150,8 +146,6 @@ export default function ({ getService }) {
});

describe('application usage limits', () => {
const timestamp = '2018-07-23T22:13:00Z';

function createSavedObject() {
return supertest
.post('/api/saved_objects/application_usage_transactional')
Expand Down Expand Up @@ -182,7 +176,7 @@ export default function ({ getService }) {
const { body } = await supertest
.post('/api/telemetry/v2/clusters/_stats')
.set('kbn-xsrf', 'xxx')
.send({ timestamp, unencrypted: true })
.send({ unencrypted: true })
.expect(200);

expect(body.length).to.be(1);
Expand Down Expand Up @@ -233,7 +227,7 @@ export default function ({ getService }) {
const { body } = await supertest
.post('/api/telemetry/v2/clusters/_stats')
.set('kbn-xsrf', 'xxx')
.send({ timestamp, unencrypted: true })
.send({ unencrypted: true })
.expect(200);

expect(body.length).to.be(1);
Expand Down
4 changes: 1 addition & 3 deletions x-pack/dev-tools/api_debug/apis/telemetry/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
* you may not use this file except in compliance with the Elastic License.
*/

import moment from 'moment';

export const name = 'telemetry';
export const description = 'Get the clusters stats from the Kibana server';
export const method = 'POST';
export const path = '/api/telemetry/v2/clusters/_stats';

export const body = { timeRange: moment().valueOf(), unencrypted: true };
export const body = { unencrypted: true };
4 changes: 2 additions & 2 deletions x-pack/test/api_integration/apis/telemetry/telemetry.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export default function ({ getService }) {
const { body } = await supertest
.post('/api/telemetry/v2/clusters/_stats')
.set('kbn-xsrf', 'xxx')
.send({ timestamp, unencrypted: true })
.send({ unencrypted: true })
.expect(200);

expect(body).length(4);
Expand All @@ -100,7 +100,7 @@ export default function ({ getService }) {
const { body } = await supertest
.post('/api/telemetry/v2/clusters/_stats')
.set('kbn-xsrf', 'xxx')
.send({ timestamp, unencrypted: true })
.send({ unencrypted: true })
.expect(200);

expect(body).length(2);
Expand Down
8 changes: 2 additions & 6 deletions x-pack/test/api_integration/apis/telemetry/telemetry_local.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,10 @@ export default function ({ getService }) {
});

it('should pull local stats and validate data types', async () => {
const timestamp = '2018-07-23T22:13:00Z';

const { body } = await supertest
.post('/api/telemetry/v2/clusters/_stats')
.set('kbn-xsrf', 'xxx')
.send({ timestamp, unencrypted: true })
.send({ unencrypted: true })
.expect(200);

expect(body.length).to.be(1);
Expand Down Expand Up @@ -102,12 +100,10 @@ export default function ({ getService }) {
});

it('should pull local stats and validate fields', async () => {
const timestamp = '2018-07-23T22:13:00Z';

const { body } = await supertest
.post('/api/telemetry/v2/clusters/_stats')
.set('kbn-xsrf', 'xxx')
.send({ timestamp, unencrypted: true })
.send({ unencrypted: true })
.expect(200);

const stats = body[0];
Expand Down
1 change: 0 additions & 1 deletion x-pack/test/api_integration/services/usage_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ export function UsageAPIProvider({ getService }: FtrProviderContext) {
*/
async getTelemetryStats(payload: {
unencrypted?: boolean;
timestamp: number | string;
}): Promise<ReturnType<TelemetryCollectionManagerPlugin['getStats']>> {
const { body } = await supertest
.post('/api/telemetry/v2/clusters/_stats')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export default function ({ getService }: FtrProviderContext) {
it('collects the expected data', async () => {
const telemetryStats = (await usageAPI.getTelemetryStats({
unencrypted: true,
timestamp: Date.now(),
})) as any;

const taggingStats = telemetryStats[0].stack_stats.kibana.plugins.saved_objects_tagging;
Expand Down

0 comments on commit e5433f0

Please sign in to comment.