Skip to content

Commit

Permalink
[ResponseOps][Alerting] Provide scaling metrics for Kibana Alerting (#…
Browse files Browse the repository at this point in the history
…143586)

* Adding scaling metrics

* Adding utilization tests

* Changing the key name

* Updating to use new api

* Updating task created counter

* Fixing tests

* Fixing tests

* Adding telemetry

* Changing telemetry field

* Updating telemetry schema

* Fixing failing test

* Fixed typos

* Update x-pack/plugins/task_manager/server/routes/background_task_utilization.test.ts

Co-authored-by: Ying Mao <[email protected]>

* Addressing pr feedback

* Updating to use configurable interval

* Updating metrics to be counts

Co-authored-by: Ying Mao <[email protected]>
  • Loading branch information
doakalexi and ymao1 authored Nov 3, 2022
1 parent 48ea029 commit 1934b59
Show file tree
Hide file tree
Showing 20 changed files with 1,728 additions and 13 deletions.
27 changes: 27 additions & 0 deletions x-pack/plugins/task_manager/server/lib/adhoc_task_counter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { AdHocTaskCounter } from './adhoc_task_counter';

describe('AdHocTaskCounter', () => {
const counter = new AdHocTaskCounter();

afterAll(() => {
counter.reset();
});

it('increments counter', async () => {
counter.increment(10);
await expect(counter.count).toEqual(10);
});

it('resets counter', async () => {
counter.increment(10);
counter.reset();
await expect(counter.count).toEqual(0);
});
});
36 changes: 36 additions & 0 deletions x-pack/plugins/task_manager/server/lib/adhoc_task_counter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

/**
* Keeps track of how many tasks have been created.
*
* @export
* @class AdHocTaskCounter
*
*/
export class AdHocTaskCounter {
/**
* Gets the number of created tasks.
*/
public get count() {
return this._count;
}

private _count: number;

constructor() {
this._count = 0;
}

public increment(by: number = 1) {
this._count += by;
}

public reset() {
this._count = 0;
}
}
39 changes: 39 additions & 0 deletions x-pack/plugins/task_manager/server/lib/intervals.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
secondsFromDate,
asInterval,
maxIntervalFromDate,
parseIntervalAsMinute,
} from './intervals';

let fakeTimer: sinon.SinonFakeTimers;
Expand Down Expand Up @@ -65,6 +66,44 @@ describe('taskIntervals', () => {
});
});

describe('parseIntervalAsMinute', () => {
test('it accepts intervals in the form `Nm`', () => {
expect(() => parseIntervalAsMinute(`${_.random(1, 1000)}m`)).not.toThrow();
});

test('it accepts intervals in the form `Ns`', () => {
expect(() => parseIntervalAsMinute(`${_.random(1, 1000)}s`)).not.toThrow();
});

test('it rejects 0 based intervals', () => {
expect(() => parseIntervalAsMinute('0m')).toThrow(
/Invalid interval "0m"\. Intervals must be of the form {number}m. Example: 5m/
);
expect(() => parseIntervalAsMinute('0s')).toThrow(
/Invalid interval "0s"\. Intervals must be of the form {number}m. Example: 5m/
);
});

test('it rejects intervals are not of the form `Nm` or `Ns`', () => {
expect(() => parseIntervalAsMinute(`5m 2s`)).toThrow(
/Invalid interval "5m 2s"\. Intervals must be of the form {number}m. Example: 5m/
);
expect(() => parseIntervalAsMinute(`hello`)).toThrow(
/Invalid interval "hello"\. Intervals must be of the form {number}m. Example: 5m/
);
});

test('returns an interval as m', () => {
expect(parseIntervalAsMinute('5s')).toEqual(5 / 60);
expect(parseIntervalAsMinute('15s')).toEqual(15 / 60);
expect(parseIntervalAsMinute('20m')).toEqual(20);
expect(parseIntervalAsMinute('61m')).toEqual(61);
expect(parseIntervalAsMinute('90m')).toEqual(90);
expect(parseIntervalAsMinute('2h')).toEqual(2 * 60);
expect(parseIntervalAsMinute('9d')).toEqual(9 * 60 * 24);
});
});

describe('parseIntervalAsMillisecond', () => {
test('it accepts intervals in the form `Nm`', () => {
expect(() => parseIntervalAsMillisecond(`${_.random(1, 1000)}m`)).not.toThrow();
Expand Down
4 changes: 4 additions & 0 deletions x-pack/plugins/task_manager/server/lib/intervals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ export const parseIntervalAsSecond = memoize((interval: Interval): number => {
return Math.round(parseIntervalAsMillisecond(interval) / 1000);
});

export const parseIntervalAsMinute = memoize((interval: Interval): number => {
return parseIntervalAsMillisecond(interval) / (1000 * 60);
});

export const parseIntervalAsMillisecond = memoize((interval: Interval): number => {
const numericAsStr: string = interval.slice(0, -1);
const numeric: number = parseInt(numericAsStr, 10);
Expand Down
Loading

0 comments on commit 1934b59

Please sign in to comment.