-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
server.ts
145 lines (129 loc) · 4.65 KB
/
server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* 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 expect from '@kbn/expect';
import type { FtrProviderContext } from '../ftr_provider_context';
import { assertLogContains, isExecutionContextLog, ANY } from '../test_utils';
function delay(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
export default function ({ getService }: FtrProviderContext) {
const retry = getService('retry');
const supertest = getService('supertest');
const log = getService('log');
async function waitForStatus(
id: string,
statuses: Set<string>,
waitMillis: number = 10000
): Promise<Record<string, any>> {
if (waitMillis < 0) {
expect().fail(`waiting for alert ${id} statuses ${Array.from(statuses)} timed out`);
}
const response = await supertest.get(`/api/alerting/rule/${id}`);
expect(response.status).to.eql(200);
const { status } = response.body.execution_status;
if (statuses.has(status)) return response.body.execution_status;
log.debug(
`waitForStatus(${Array.from(statuses)} for id:${id}): got ${JSON.stringify(
response.body.execution_status
)}, retrying`
);
const WaitForStatusIncrement = 500;
await delay(WaitForStatusIncrement);
return await waitForStatus(id, statuses, waitMillis - WaitForStatusIncrement);
}
describe('Server-side apps', () => {
it('propagates context for Task and Alerts', async () => {
const { body: createdAlert } = await supertest
.post('/api/alerting/rule')
.set('kbn-xsrf', 'true')
.send({
enabled: true,
name: 'abc',
tags: ['foo'],
rule_type_id: 'test.executionContext',
consumer: 'alertsFixture',
schedule: { interval: '3s' },
throttle: '20s',
actions: [],
params: {},
notify_when: 'onThrottleInterval',
})
.expect(200);
const alertId = createdAlert.id;
await waitForStatus(alertId, new Set(['ok']), 90_000);
await assertLogContains({
description:
'task manager execution context propagates to Elasticsearch via "x-opaque-id" header',
predicate: (record) =>
Boolean(
// exclude part with taskId
record.http?.request?.id?.includes(
`kibana:task%20manager:run%20alerting%3Atest.executionContext:`
)
),
retry,
});
await assertLogContains({
description:
'alerting execution context propagates to Elasticsearch via "x-opaque-id" header',
predicate: (record) =>
Boolean(
record.http?.request?.id?.includes(`alert:execute%20test.executionContext:${alertId}`)
),
retry,
});
await assertLogContains({
description: 'execution context propagates to Kibana logs',
predicate: (record) =>
isExecutionContextLog(record?.message, {
type: 'task manager',
name: 'run alerting:test.executionContext',
// @ts-expect-error. it accepts strings only
id: ANY,
description: 'run task',
child: {
type: 'alert',
name: 'execute test.executionContext',
id: alertId,
description: 'execute [test.executionContext] with name [abc] in [default] namespace',
},
}),
retry,
});
});
it('propagates context for Telemetry collection', async () => {
await supertest
.post('/api/telemetry/v2/clusters/_stats')
.set('kbn-xsrf', 'true')
.send({ unencrypted: false })
.expect(200);
await assertLogContains({
description:
'usage_collection execution context propagates to Elasticsearch via "x-opaque-id" header',
predicate: (record) =>
Boolean(
// exclude part with collector types
record.http?.request?.id?.includes(
`kibana:usage_collection:collector.fetch:application_usage`
)
),
retry,
});
await assertLogContains({
description: 'execution context propagates to Kibana logs',
predicate: (record) =>
isExecutionContextLog(record?.message, {
type: 'usage_collection',
name: 'collector.fetch',
id: 'application_usage',
description: 'Fetch method in the Collector "application_usage"',
}),
retry,
});
});
});
}