Skip to content

Commit b3adf2c

Browse files
committed
[APM] Fix watcher integration (#51721)
Closes #51720.
1 parent 779d552 commit b3adf2c

File tree

5 files changed

+36
-11
lines changed

5 files changed

+36
-11
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
@@ -197,6 +197,7 @@ export class WatcherFlyout extends Component<
197197
) as string;
198198

199199
return createErrorGroupWatch({
200+
http: core.http,
200201
emails,
201202
schedule,
202203
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
@@ -8,22 +8,30 @@ import { isArray, isObject, isString } from 'lodash';
88
import mustache from 'mustache';
99
import uuid from 'uuid';
1010
import { StringMap } from '../../../../../../typings/common';
11-
// @ts-ignore
1211
import * as rest from '../../../../../services/rest/watcher';
1312
import { createErrorGroupWatch } from '../createErrorGroupWatch';
1413
import { esResponse } from './esResponse';
14+
import { HttpServiceBase } from 'kibana/public';
1515

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

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

2633
createWatchResponse = await createErrorGroupWatch({
34+
http: {} as HttpServiceBase,
2735
2836
schedule: {
2937
daily: {
@@ -37,19 +45,19 @@ describe('createErrorGroupWatch', () => {
3745
apmIndexPatternTitle: 'myIndexPattern'
3846
});
3947

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

46-
tmpl = renderMustache(rest.createWatch.mock.calls[0][1], templateCtx);
54+
tmpl = renderMustache(createWatchSpy.mock.calls[0][0].watch, templateCtx);
4755
});
4856

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

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

5563
it('should format slack message correctly', () => {
@@ -79,7 +87,7 @@ describe('createErrorGroupWatch', () => {
7987
});
8088

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

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,
@@ -18,7 +19,6 @@ import {
1819
SERVICE_NAME
1920
} from '../../../../../common/elasticsearch_fieldnames';
2021
import { StringMap } from '../../../../../typings/common';
21-
// @ts-ignore
2222
import { createWatch } from '../../../../services/rest/watcher';
2323

2424
function getSlackPathUrl(slackUrl?: string) {
@@ -36,6 +36,7 @@ export interface Schedule {
3636
}
3737

3838
interface Arguments {
39+
http: HttpServiceBase;
3940
emails: string[];
4041
schedule: Schedule;
4142
serviceName: string;
@@ -55,6 +56,7 @@ interface Actions {
5556
}
5657

5758
export async function createErrorGroupWatch({
59+
http,
5860
emails = [],
5961
schedule,
6062
serviceName,
@@ -251,6 +253,10 @@ export async function createErrorGroupWatch({
251253
};
252254
}
253255

254-
await createWatch(id, body);
256+
await createWatch({
257+
http,
258+
id,
259+
watch: body
260+
});
255261
return id;
256262
}

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

Lines changed: 2 additions & 1 deletion
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) {

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,19 @@
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}`,
1322
body: JSON.stringify({ type: 'json', id, watch })

0 commit comments

Comments
 (0)