Skip to content

Commit 1af551b

Browse files
[Telemetry] Remove from and to timestamps from usage stats APIs (#81579)
Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
1 parent 1e5060e commit 1af551b

File tree

27 files changed

+770
-782
lines changed

27 files changed

+770
-782
lines changed

src/plugins/telemetry/public/services/telemetry_service.test.ts

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,42 +18,30 @@
1818
*/
1919

2020
/* eslint-disable dot-notation */
21-
import { mockTelemetryService } from '../mocks';
22-
23-
const mockSubtract = jest.fn().mockImplementation(() => {
24-
return {
25-
toISOString: jest.fn(),
26-
};
27-
});
28-
29-
const mockClone = jest.fn().mockImplementation(() => {
30-
return {
31-
clone: mockClone,
32-
subtract: mockSubtract,
33-
toISOString: jest.fn(),
34-
};
35-
});
21+
const mockMomentValueOf = jest.fn();
3622

3723
jest.mock('moment', () => {
3824
return jest.fn().mockImplementation(() => {
3925
return {
40-
clone: mockClone,
41-
subtract: mockSubtract,
42-
toISOString: jest.fn(),
26+
valueOf: mockMomentValueOf,
4327
};
4428
});
4529
});
4630

31+
import { mockTelemetryService } from '../mocks';
32+
4733
describe('TelemetryService', () => {
4834
describe('fetchTelemetry', () => {
4935
it('calls expected URL with 20 minutes - now', async () => {
36+
const timestamp = Date.now();
37+
mockMomentValueOf.mockReturnValueOnce(timestamp);
5038
const telemetryService = mockTelemetryService();
39+
5140
await telemetryService.fetchTelemetry();
5241
expect(telemetryService['http'].post).toBeCalledWith('/api/telemetry/v2/clusters/_stats', {
53-
body: JSON.stringify({ unencrypted: false, timeRange: {} }),
42+
body: JSON.stringify({ unencrypted: false, timestamp }),
5443
});
55-
expect(mockClone).toBeCalled();
56-
expect(mockSubtract).toBeCalledWith(20, 'minutes');
44+
expect(mockMomentValueOf).toBeCalled();
5745
});
5846
});
5947

src/plugins/telemetry/public/services/telemetry_service.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,17 +121,10 @@ export class TelemetryService {
121121
};
122122

123123
public fetchTelemetry = async ({ unencrypted = false } = {}) => {
124-
const now = moment();
125124
return this.http.post('/api/telemetry/v2/clusters/_stats', {
126125
body: JSON.stringify({
127126
unencrypted,
128-
timeRange: {
129-
min: now
130-
.clone() // Need to clone it to avoid mutation (and max being the same value)
131-
.subtract(20, 'minutes')
132-
.toISOString(),
133-
max: now.toISOString(),
134-
},
127+
timestamp: moment().valueOf(),
135128
}),
136129
});
137130
};

src/plugins/telemetry/server/fetcher.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,7 @@ export class FetcherTask {
213213
private async fetchTelemetry() {
214214
return await this.telemetryCollectionManager!.getStats({
215215
unencrypted: false,
216-
start: moment().subtract(20, 'minutes').toISOString(),
217-
end: moment().toISOString(),
216+
timestamp: moment().valueOf(),
218217
});
219218
}
220219

src/plugins/telemetry/server/routes/telemetry_opt_in.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ export function registerTelemetryOptInRoutes({
8686
}
8787

8888
const statsGetterConfig: StatsGetterConfig = {
89-
start: moment().subtract(20, 'minutes').toISOString(),
90-
end: moment().toISOString(),
89+
timestamp: moment().valueOf(),
9190
unencrypted: false,
9291
};
9392

src/plugins/telemetry/server/routes/telemetry_opt_in_stats.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ export function registerTelemetryOptInStatsRoutes(
7272
const unencrypted = req.body.unencrypted;
7373

7474
const statsGetterConfig: StatsGetterConfig = {
75-
start: moment().subtract(20, 'minutes').toISOString(),
76-
end: moment().toISOString(),
75+
timestamp: moment().valueOf(),
7776
unencrypted,
7877
request: req,
7978
};

src/plugins/telemetry/server/routes/telemetry_usage_stats.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ const validate: TypeOptions<string | number>['validate'] = (value) => {
3232
}
3333
};
3434

35-
const dateSchema = schema.oneOf([schema.string({ validate }), schema.number({ validate })]);
36-
3735
export function registerTelemetryUsageStatsRoutes(
3836
router: IRouter,
3937
telemetryCollectionManager: TelemetryCollectionManagerPluginSetup,
@@ -45,25 +43,20 @@ export function registerTelemetryUsageStatsRoutes(
4543
validate: {
4644
body: schema.object({
4745
unencrypted: schema.boolean({ defaultValue: false }),
48-
timeRange: schema.object({
49-
min: dateSchema,
50-
max: dateSchema,
51-
}),
46+
timestamp: schema.oneOf([schema.string({ validate }), schema.number({ validate })]),
5247
}),
5348
},
5449
},
5550
async (context, req, res) => {
56-
const start = moment(req.body.timeRange.min).toISOString();
57-
const end = moment(req.body.timeRange.max).toISOString();
58-
const unencrypted = req.body.unencrypted;
51+
const { unencrypted, timestamp } = req.body;
5952

6053
try {
6154
const statsConfig: StatsGetterConfig = {
62-
unencrypted,
63-
start,
64-
end,
55+
timestamp: moment(timestamp).valueOf(),
6556
request: req,
57+
unencrypted,
6658
};
59+
6760
const stats = await telemetryCollectionManager.getStats(statsConfig);
6861
return res.ok({ body: stats });
6962
} catch (err) {

src/plugins/telemetry/server/telemetry_collection/get_local_stats.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@ function mockStatsCollectionConfig(clusterInfo: any, clusterStats: any, kibana:
8787
...createCollectorFetchContextMock(),
8888
esClient: mockGetLocalStats(clusterInfo, clusterStats),
8989
usageCollection: mockUsageCollection(kibana),
90-
start: '',
91-
end: '',
90+
timestamp: Date.now(),
9291
};
9392
}
9493

src/plugins/telemetry_collection_manager/server/plugin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ export class TelemetryCollectionManagerPlugin
144144
collectionSoService: SavedObjectsServiceStart,
145145
usageCollection: UsageCollectionSetup
146146
): StatsCollectionConfig {
147-
const { start, end, request } = config;
147+
const { timestamp, request } = config;
148148

149149
const callCluster = config.unencrypted
150150
? collection.esCluster.asScoped(request).callAsCurrentUser
@@ -157,7 +157,7 @@ export class TelemetryCollectionManagerPlugin
157157
const soClient = config.unencrypted
158158
? collectionSoService.getScopedClient(config.request)
159159
: collectionSoService.createInternalRepository();
160-
return { callCluster, start, end, usageCollection, esClient, soClient };
160+
return { callCluster, timestamp, usageCollection, esClient, soClient };
161161
}
162162

163163
private async getOptInStats(optInStatus: boolean, config: StatsGetterConfig) {

src/plugins/telemetry_collection_manager/server/types.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ export interface TelemetryOptInStats {
5656

5757
export interface BaseStatsGetterConfig {
5858
unencrypted: boolean;
59-
start: string;
60-
end: string;
59+
timestamp: number;
6160
request?: KibanaRequest;
6261
}
6362

@@ -77,8 +76,7 @@ export interface ClusterDetails {
7776
export interface StatsCollectionConfig {
7877
usageCollection: UsageCollectionSetup;
7978
callCluster: LegacyAPICaller;
80-
start: string | number;
81-
end: string | number;
79+
timestamp: number;
8280
esClient: ElasticsearchClient;
8381
soClient: SavedObjectsClientContract | ISavedObjectsRepository;
8482
}

test/api_integration/apis/telemetry/telemetry_local.js

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,12 @@ export default function ({ getService }) {
5353
});
5454

5555
it('should pull local stats and validate data types', async () => {
56-
const timeRange = {
57-
min: '2018-07-23T22:07:00Z',
58-
max: '2018-07-23T22:13:00Z',
59-
};
56+
const timestamp = '2018-07-23T22:13:00Z';
6057

6158
const { body } = await supertest
6259
.post('/api/telemetry/v2/clusters/_stats')
6360
.set('kbn-xsrf', 'xxx')
64-
.send({ timeRange, unencrypted: true })
61+
.send({ timestamp, unencrypted: true })
6562
.expect(200);
6663

6764
expect(body.length).to.be(1);
@@ -98,15 +95,12 @@ export default function ({ getService }) {
9895
});
9996

10097
it('should pull local stats and validate fields', async () => {
101-
const timeRange = {
102-
min: '2018-07-23T22:07:00Z',
103-
max: '2018-07-23T22:13:00Z',
104-
};
98+
const timestamp = '2018-07-23T22:13:00Z';
10599

106100
const { body } = await supertest
107101
.post('/api/telemetry/v2/clusters/_stats')
108102
.set('kbn-xsrf', 'xxx')
109-
.send({ timeRange, unencrypted: true })
103+
.send({ timestamp, unencrypted: true })
110104
.expect(200);
111105

112106
const stats = body[0];
@@ -156,10 +150,7 @@ export default function ({ getService }) {
156150
});
157151

158152
describe('application usage limits', () => {
159-
const timeRange = {
160-
min: '2018-07-23T22:07:00Z',
161-
max: '2018-07-23T22:13:00Z',
162-
};
153+
const timestamp = '2018-07-23T22:13:00Z';
163154

164155
function createSavedObject() {
165156
return supertest
@@ -191,7 +182,7 @@ export default function ({ getService }) {
191182
const { body } = await supertest
192183
.post('/api/telemetry/v2/clusters/_stats')
193184
.set('kbn-xsrf', 'xxx')
194-
.send({ timeRange, unencrypted: true })
185+
.send({ timestamp, unencrypted: true })
195186
.expect(200);
196187

197188
expect(body.length).to.be(1);
@@ -242,7 +233,7 @@ export default function ({ getService }) {
242233
const { body } = await supertest
243234
.post('/api/telemetry/v2/clusters/_stats')
244235
.set('kbn-xsrf', 'xxx')
245-
.send({ timeRange, unencrypted: true })
236+
.send({ timestamp, unencrypted: true })
246237
.expect(200);
247238

248239
expect(body.length).to.be(1);

0 commit comments

Comments
 (0)