Skip to content

Commit 074f24e

Browse files
authored
[APM] Fix watcher integration (#51721)
Closes #51720.
1 parent 1fb779b commit 074f24e

File tree

5 files changed

+38
-15
lines changed

5 files changed

+38
-15
lines changed

x-pack/legacy/plugins/apm/public/components/app/ServiceDetails/ServiceIntegrations/WatcherFlyout.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ export class WatcherFlyout extends Component<
191191
) as string;
192192

193193
return createErrorGroupWatch({
194+
http: core.http,
194195
emails,
195196
schedule,
196197
serviceName,

x-pack/legacy/plugins/apm/public/components/app/ServiceDetails/ServiceIntegrations/__test__/createErrorGroupWatch.test.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,30 @@
77
import { isArray, isObject, isString } from 'lodash';
88
import mustache from 'mustache';
99
import uuid from 'uuid';
10-
// @ts-ignore
1110
import * as rest from '../../../../../services/rest/watcher';
1211
import { createErrorGroupWatch } from '../createErrorGroupWatch';
1312
import { esResponse } from './esResponse';
13+
import { HttpServiceBase } from 'kibana/public';
1414

1515
// disable html escaping since this is also disabled in watcher\s mustache implementation
1616
mustache.escape = value => value;
1717

18+
jest.mock('../../../../../services/rest/callApi', () => ({
19+
callApi: () => Promise.resolve(null)
20+
}));
21+
1822
describe('createErrorGroupWatch', () => {
1923
let createWatchResponse: string;
2024
let tmpl: any;
25+
const createWatchSpy = jest
26+
.spyOn(rest, 'createWatch')
27+
.mockResolvedValue(undefined);
28+
2129
beforeEach(async () => {
2230
jest.spyOn(uuid, 'v4').mockReturnValue(new Buffer('mocked-uuid'));
23-
jest.spyOn(rest, 'createWatch').mockReturnValue(undefined);
2431

2532
createWatchResponse = await createErrorGroupWatch({
33+
http: {} as HttpServiceBase,
2634
2735
schedule: {
2836
daily: {
@@ -36,19 +44,19 @@ describe('createErrorGroupWatch', () => {
3644
apmIndexPatternTitle: 'myIndexPattern'
3745
});
3846

39-
const watchBody = rest.createWatch.mock.calls[0][1];
47+
const watchBody = createWatchSpy.mock.calls[0][0].watch;
4048
const templateCtx = {
4149
payload: esResponse,
4250
metadata: watchBody.metadata
4351
};
4452

45-
tmpl = renderMustache(rest.createWatch.mock.calls[0][1], templateCtx);
53+
tmpl = renderMustache(createWatchSpy.mock.calls[0][0].watch, templateCtx);
4654
});
4755

4856
afterEach(() => jest.restoreAllMocks());
4957

5058
it('should call createWatch with correct args', () => {
51-
expect(rest.createWatch.mock.calls[0][0]).toBe('apm-mocked-uuid');
59+
expect(createWatchSpy.mock.calls[0][0].id).toBe('apm-mocked-uuid');
5260
});
5361

5462
it('should format slack message correctly', () => {
@@ -78,7 +86,7 @@ describe('createErrorGroupWatch', () => {
7886
});
7987

8088
it('should return watch id', async () => {
81-
const id = rest.createWatch.mock.calls[0][0];
89+
const id = createWatchSpy.mock.calls[0][0].id;
8290
expect(createWatchResponse).toEqual(id);
8391
});
8492
});

x-pack/legacy/plugins/apm/public/components/app/ServiceDetails/ServiceIntegrations/createErrorGroupWatch.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { i18n } from '@kbn/i18n';
88
import { isEmpty } from 'lodash';
99
import url from 'url';
1010
import uuid from 'uuid';
11+
import { HttpServiceBase } from 'kibana/public';
1112
import {
1213
ERROR_CULPRIT,
1314
ERROR_EXC_HANDLED,
@@ -17,7 +18,6 @@ import {
1718
PROCESSOR_EVENT,
1819
SERVICE_NAME
1920
} from '../../../../../common/elasticsearch_fieldnames';
20-
// @ts-ignore
2121
import { createWatch } from '../../../../services/rest/watcher';
2222

2323
function getSlackPathUrl(slackUrl?: string) {
@@ -35,6 +35,7 @@ export interface Schedule {
3535
}
3636

3737
interface Arguments {
38+
http: HttpServiceBase;
3839
emails: string[];
3940
schedule: Schedule;
4041
serviceName: string;
@@ -54,6 +55,7 @@ interface Actions {
5455
}
5556

5657
export async function createErrorGroupWatch({
58+
http,
5759
emails = [],
5860
schedule,
5961
serviceName,
@@ -250,6 +252,10 @@ export async function createErrorGroupWatch({
250252
};
251253
}
252254

253-
await createWatch(id, body);
255+
await createWatch({
256+
http,
257+
id,
258+
watch: body
259+
});
254260
return id;
255261
}

x-pack/legacy/plugins/apm/public/services/rest/callApi.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ import LRU from 'lru-cache';
99
import hash from 'object-hash';
1010
import { HttpServiceBase, HttpFetchOptions } from 'kibana/public';
1111

12-
export type FetchOptions = HttpFetchOptions & {
12+
export type FetchOptions = Omit<HttpFetchOptions, 'body'> & {
1313
pathname: string;
1414
forceCache?: boolean;
1515
method?: string;
16+
body?: any;
1617
};
1718

1819
function fetchOptionsWithDebug(fetchOptions: FetchOptions) {
@@ -26,9 +27,7 @@ function fetchOptionsWithDebug(fetchOptions: FetchOptions) {
2627
const body = isGet
2728
? {}
2829
: {
29-
body: JSON.stringify(
30-
fetchOptions.body || ({} as HttpFetchOptions['body'])
31-
)
30+
body: JSON.stringify(fetchOptions.body || {})
3231
};
3332

3433
return {

x-pack/legacy/plugins/apm/public/services/rest/watcher.js renamed to x-pack/legacy/plugins/apm/public/services/rest/watcher.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,21 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7+
import { HttpServiceBase } from 'kibana/public';
78
import { callApi } from './callApi';
89

9-
export async function createWatch(id, watch) {
10-
return callApi({
10+
export async function createWatch({
11+
id,
12+
watch,
13+
http
14+
}: {
15+
http: HttpServiceBase;
16+
id: string;
17+
watch: any;
18+
}) {
19+
return callApi(http, {
1120
method: 'PUT',
1221
pathname: `/api/watcher/watch/${id}`,
13-
body: JSON.stringify({ type: 'json', id, watch })
22+
body: { type: 'json', id, watch }
1423
});
1524
}

0 commit comments

Comments
 (0)