Skip to content

Commit

Permalink
[X-Pack][Monitoring][Telemetry] Ensure 24h time range when fetching K…
Browse files Browse the repository at this point in the history
…ibana usage stats (#55171)

Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
afharo and elasticmachine authored Jan 27, 2020
1 parent 72a8da2 commit 4c2d901
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { getUsageStats, combineStats, rollUpTotals } from '../get_kibana_stats';
import { getUsageStats, combineStats, rollUpTotals, ensureTimeSpan } from '../get_kibana_stats';
import expect from '@kbn/expect';

describe('Get Kibana Stats', () => {
Expand Down Expand Up @@ -542,4 +542,50 @@ describe('Get Kibana Stats', () => {
expect(rollUpTotals(rollUp, addOn, 'my_field')).to.eql({ total: 4 });
});
});

describe('Ensure minimum time difference', () => {
it('should return start and end as is when none are provided', () => {
const { start, end } = ensureTimeSpan(undefined, undefined);
expect(start).to.be.undefined;
expect(end).to.be.undefined;
});

it('should return start and end as is when only end is provided', () => {
const initialEnd = '2020-01-01T00:00:00Z';
const { start, end } = ensureTimeSpan(undefined, initialEnd);
expect(start).to.be.undefined;
expect(end).to.be.equal(initialEnd);
});

it('should return start and end as is because they are already 24h away', () => {
const initialStart = '2019-12-31T00:00:00Z';
const initialEnd = '2020-01-01T00:00:00Z';
const { start, end } = ensureTimeSpan(initialStart, initialEnd);
expect(start).to.be.equal(initialStart);
expect(end).to.be.equal(initialEnd);
});

it('should return start and end as is because they are already 24h+ away', () => {
const initialStart = '2019-12-31T00:00:00Z';
const initialEnd = '2020-01-01T01:00:00Z';
const { start, end } = ensureTimeSpan(initialStart, initialEnd);
expect(start).to.be.equal(initialStart);
expect(end).to.be.equal(initialEnd);
});

it('should modify start to a date 24h before end', () => {
const initialStart = '2020-01-01T00:00:00.000Z';
const initialEnd = '2020-01-01T01:00:00.000Z';
const { start, end } = ensureTimeSpan(initialStart, initialEnd);
expect(start).to.be.equal('2019-12-31T01:00:00.000Z');
expect(end).to.be.equal(initialEnd);
});

it('should modify start to a date 24h before now', () => {
const initialStart = new Date().toISOString();
const { start, end } = ensureTimeSpan(initialStart, undefined);
expect(start).to.not.be.equal(initialStart);
expect(end).to.be.undefined;
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
* you may not use this file except in compliance with the Elastic License.
*/

import moment from 'moment';
import { get, isEmpty, omit } from 'lodash';
import { KIBANA_SYSTEM_ID } from '../../common/constants';
import { KIBANA_SYSTEM_ID, TELEMETRY_COLLECTION_INTERVAL } from '../../common/constants';
import { fetchHighLevelStats, handleHighLevelStatsResponse } from './get_high_level_stats';

export function rollUpTotals(rolledUp, addOn, field) {
Expand Down Expand Up @@ -88,17 +89,42 @@ export function combineStats(highLevelStats, usageStats = {}) {
}, {});
}

/**
* Ensure the start and end dates are, at least, TELEMETRY_COLLECTION_INTERVAL apart
* because, otherwise, we are sending telemetry with empty Kibana usage data.
*
* @param {date} [start] The start time from which to get the telemetry data
* @param {date} [end] The end time from which to get the telemetry data
*/
export function ensureTimeSpan(start, end) {
// We only care if we have a start date, because that's the limit that might make us lose the document
if (start) {
const duration = moment.duration(TELEMETRY_COLLECTION_INTERVAL, 'milliseconds');
// If end exists, we need to ensure they are, at least, TELEMETRY_COLLECTION_INTERVAL apart.
// Otherwise start should be, at least, TELEMETRY_COLLECTION_INTERVAL apart from now
let safeStart = moment().subtract(duration);
if (end) {
safeStart = moment(end).subtract(duration);
}
if (safeStart.isBefore(start)) {
return { start: safeStart.toISOString(), end };
}
}
return { start, end };
}

/*
* Monkey-patch the modules from get_high_level_stats and add in the
* specialized usage data that comes with kibana stats (kibana_stats.usage).
*/
export async function getKibanaStats(server, callCluster, clusterUuids, start, end) {
const { start: safeStart, end: safeEnd } = ensureTimeSpan(start, end);
const rawStats = await fetchHighLevelStats(
server,
callCluster,
clusterUuids,
start,
end,
safeStart,
safeEnd,
KIBANA_SYSTEM_ID
);
const highLevelStats = handleHighLevelStatsResponse(rawStats, KIBANA_SYSTEM_ID);
Expand Down

0 comments on commit 4c2d901

Please sign in to comment.