Remove the parameter timestamp in /api/telemetry/v2/clusters/_stats (#83791)

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Alejandro Fernández Haro 2020-11-23 13:09:16 +01:00 committed by GitHub
parent ade7278cf2
commit df4f4758fa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 14 additions and 62 deletions

View file

@ -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();
});
});

View file

@ -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';
@ -124,7 +123,6 @@ export class TelemetryService {
return this.http.post('/api/telemetry/v2/clusters/_stats', {
body: JSON.stringify({
unencrypted,
timestamp: moment().valueOf(),
}),
});
};

View file

@ -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';
@ -86,7 +85,6 @@ export function registerTelemetryOptInRoutes({
}
const statsGetterConfig: StatsGetterConfig = {
timestamp: moment().valueOf(),
unencrypted: false,
};

View file

@ -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';
@ -72,7 +71,6 @@ export function registerTelemetryOptInStatsRoutes(
const unencrypted = req.body.unencrypted;
const statsGetterConfig: StatsGetterConfig = {
timestamp: moment().valueOf(),
unencrypted,
request: req,
};

View file

@ -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,
@ -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,
};

View file

@ -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
@ -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) {

View file

@ -56,7 +56,6 @@ export interface TelemetryOptInStats {
export interface BaseStatsGetterConfig {
unencrypted: boolean;
timestamp: number;
request?: KibanaRequest;
}
@ -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

View file

@ -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);
@ -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];
@ -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')
@ -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);
@ -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);

View file

@ -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 };

View file

@ -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);
@ -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);

View file

@ -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);
@ -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];

View file

@ -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')

View file

@ -4,7 +4,6 @@
* you may not use this file except in compliance with the Elastic License.
*/
import moment from 'moment';
import expect from '@kbn/expect/expect.js';
import { FtrProviderContext } from '../../ftr_provider_context';
import { DATES } from './constants';
@ -64,7 +63,6 @@ export default ({ getPageObjects, getService }: FtrProviderContext) => {
.set(COMMON_REQUEST_HEADERS)
.set('Accept', 'application/json')
.send({
timestamp: moment().toISOString(),
unencrypted: true,
})
.expect(200)
@ -85,7 +83,6 @@ export default ({ getPageObjects, getService }: FtrProviderContext) => {
.set(COMMON_REQUEST_HEADERS)
.set('Accept', 'application/json')
.send({
timestamp: moment().toISOString(),
unencrypted: true,
})
.expect(200)

View file

@ -5,7 +5,6 @@
*/
import expect from '@kbn/expect';
import moment from 'moment';
import { DATES } from './constants';
import { FtrProviderContext } from '../../ftr_provider_context';
@ -118,7 +117,6 @@ export default ({ getPageObjects, getService }: FtrProviderContext) => {
.set(COMMON_REQUEST_HEADERS)
.set('Accept', 'application/json')
.send({
timestamp: moment().toISOString(),
unencrypted: true,
})
.expect(200)

View file

@ -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;