Skip to content

Commit

Permalink
[ObsUX] Fix timestamp for anomaly alert test (#169255)
Browse files Browse the repository at this point in the history
Closes #160769

### What was done
The anomaly alert test was failing with timeout because was not
generating alerts, the data and spikes were added to far away in time,
so when the job is created doesn't take into account data that far in
the past
We fixed the timerange of the data generated and the spikes.

BEFORE:

<img width="1196" alt="image"
src="https://github.com/elastic/kibana/assets/31922082/d14665f3-28be-437b-b9ca-f5f7d449e5f1">

AFTER:

<img width="1196" alt="image"
src="https://github.com/elastic/kibana/assets/31922082/683cb592-173b-4952-b7db-697e3dfb8ebd">
  • Loading branch information
MiriamAparicio authored Oct 23, 2023
1 parent 0fd5ff5 commit 17c78db
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ export function registerAnomalyRuleType({
range: {
timestamp: {
gte: dateStart,
format: 'epoch_millis',
},
},
},
Expand Down
41 changes: 19 additions & 22 deletions x-pack/test/apm_api_integration/tests/alerts/anomaly_alert.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* 2.0.
*/

import moment from 'moment';
import { ApmRuleType } from '@kbn/apm-plugin/common/rules/apm_rule_types';
import { apm, timerange } from '@kbn/apm-synthtrace-client';
import expect from '@kbn/expect';
Expand All @@ -23,34 +24,32 @@ export default function ApiTest({ getService }: FtrProviderContext) {
const logger = getService('log');

const synthtraceEsClient = getService('synthtraceEsClient');
// FLAKY https://github.com/elastic/kibana/issues/160298
registry.when.skip(
registry.when(
'fetching service anomalies with a trial license',
{ config: 'trial', archives: [] },
() => {
const start = '2021-01-01T00:00:00.000Z';
const end = '2021-01-08T00:15:00.000Z';
const start = moment().subtract(1, 'days').valueOf();
const end = moment().valueOf();

const spikeStart = new Date('2021-01-07T23:15:00.000Z').getTime();
const spikeEnd = new Date('2021-01-08T00:15:00.000Z').getTime();

const NORMAL_DURATION = 100;
const NORMAL_RATE = 1;
const spikeStart = moment().subtract(15, 'minutes').valueOf();
const spikeEnd = moment().valueOf();

let ruleId: string;

before(async () => {
await cleanup();

const serviceA = apm
.service({ name: 'a', environment: 'production', agentName: 'java' })
.instance('a');

const events = timerange(new Date(start).getTime(), new Date(end).getTime())
const events = timerange(start, end)
.interval('1m')
.rate(1)
.generator((timestamp) => {
const isInSpike = timestamp >= spikeStart && timestamp < spikeEnd;
const count = isInSpike ? 4 : NORMAL_RATE;
const duration = isInSpike ? 1000 : NORMAL_DURATION;
const count = isInSpike ? 4 : 1;
const duration = isInSpike ? 1000 : 100;
const outcome = isInSpike ? 'failure' : 'success';

return [
Expand All @@ -65,26 +64,25 @@ export default function ApiTest({ getService }: FtrProviderContext) {
});

await synthtraceEsClient.index(events);

await createAndRunApmMlJobs({ es, ml, environments: ['production'] });
});

after(async () => {
await cleanup();
});

async function cleanup() {
try {
await synthtraceEsClient.clean();
await deleteRuleById({ supertest, ruleId });
await ml.cleanMlIndices();
} catch (e) {
logger.info('Could not delete rule by id', e);
}
});
}

describe('with ml jobs', () => {
before(async () => {
await createAndRunApmMlJobs({ es, ml, environments: ['production'] });
});

after(async () => {
await ml.cleanMlIndices();
});

it('checks if alert is active', async () => {
const createdRule = await createApmRule({
supertest,
Expand All @@ -97,7 +95,6 @@ export default function ApiTest({ getService }: FtrProviderContext) {
},
ruleTypeId: ApmRuleType.Anomaly,
});

ruleId = createdRule.id;
if (!ruleId) {
expect(ruleId).to.not.eql(undefined);
Expand Down

0 comments on commit 17c78db

Please sign in to comment.