[7.5] [APM] Fix watcher integration (#51721) (#51760)

Closes #51720.
This commit is contained in:
Dario Gieselaar 2019-11-26 20:43:48 +01:00 committed by GitHub
parent fbea18d90d
commit 811a558684
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 36 additions and 11 deletions

View file

@ -197,6 +197,7 @@ export class WatcherFlyout extends Component<
) as string;
return createErrorGroupWatch({
http: core.http,
emails,
schedule,
serviceName,

View file

@ -8,22 +8,30 @@ import { isArray, isObject, isString } from 'lodash';
import mustache from 'mustache';
import uuid from 'uuid';
import { StringMap } from '../../../../../../typings/common';
// @ts-ignore
import * as rest from '../../../../../services/rest/watcher';
import { createErrorGroupWatch } from '../createErrorGroupWatch';
import { esResponse } from './esResponse';
import { HttpServiceBase } from 'kibana/public';
// disable html escaping since this is also disabled in watcher\s mustache implementation
mustache.escape = value => value;
jest.mock('../../../../../services/rest/callApi', () => ({
callApi: () => Promise.resolve(null)
}));
describe('createErrorGroupWatch', () => {
let createWatchResponse: string;
let tmpl: any;
const createWatchSpy = jest
.spyOn(rest, 'createWatch')
.mockResolvedValue(undefined);
beforeEach(async () => {
jest.spyOn(uuid, 'v4').mockReturnValue(new Buffer('mocked-uuid'));
jest.spyOn(rest, 'createWatch').mockReturnValue(undefined);
createWatchResponse = await createErrorGroupWatch({
http: {} as HttpServiceBase,
emails: ['my@email.dk', 'mySecond@email.dk'],
schedule: {
daily: {
@ -37,19 +45,19 @@ describe('createErrorGroupWatch', () => {
apmIndexPatternTitle: 'myIndexPattern'
});
const watchBody = rest.createWatch.mock.calls[0][1];
const watchBody = createWatchSpy.mock.calls[0][0].watch;
const templateCtx = {
payload: esResponse,
metadata: watchBody.metadata
};
tmpl = renderMustache(rest.createWatch.mock.calls[0][1], templateCtx);
tmpl = renderMustache(createWatchSpy.mock.calls[0][0].watch, templateCtx);
});
afterEach(() => jest.restoreAllMocks());
it('should call createWatch with correct args', () => {
expect(rest.createWatch.mock.calls[0][0]).toBe('apm-mocked-uuid');
expect(createWatchSpy.mock.calls[0][0].id).toBe('apm-mocked-uuid');
});
it('should format slack message correctly', () => {
@ -79,7 +87,7 @@ describe('createErrorGroupWatch', () => {
});
it('should return watch id', async () => {
const id = rest.createWatch.mock.calls[0][0];
const id = createWatchSpy.mock.calls[0][0].id;
expect(createWatchResponse).toEqual(id);
});
});

View file

@ -8,6 +8,7 @@ import { i18n } from '@kbn/i18n';
import { isEmpty } from 'lodash';
import url from 'url';
import uuid from 'uuid';
import { HttpServiceBase } from 'kibana/public';
import {
ERROR_CULPRIT,
ERROR_EXC_HANDLED,
@ -18,7 +19,6 @@ import {
SERVICE_NAME
} from '../../../../../common/elasticsearch_fieldnames';
import { StringMap } from '../../../../../typings/common';
// @ts-ignore
import { createWatch } from '../../../../services/rest/watcher';
function getSlackPathUrl(slackUrl?: string) {
@ -36,6 +36,7 @@ export interface Schedule {
}
interface Arguments {
http: HttpServiceBase;
emails: string[];
schedule: Schedule;
serviceName: string;
@ -55,6 +56,7 @@ interface Actions {
}
export async function createErrorGroupWatch({
http,
emails = [],
schedule,
serviceName,
@ -251,6 +253,10 @@ export async function createErrorGroupWatch({
};
}
await createWatch(id, body);
await createWatch({
http,
id,
watch: body
});
return id;
}

View file

@ -9,10 +9,11 @@ import LRU from 'lru-cache';
import hash from 'object-hash';
import { HttpServiceBase, HttpFetchOptions } from 'kibana/public';
export type FetchOptions = HttpFetchOptions & {
export type FetchOptions = Omit<HttpFetchOptions, 'body'> & {
pathname: string;
forceCache?: boolean;
method?: string;
body?: any;
};
function fetchOptionsWithDebug(fetchOptions: FetchOptions) {

View file

@ -4,10 +4,19 @@
* you may not use this file except in compliance with the Elastic License.
*/
import { HttpServiceBase } from 'kibana/public';
import { callApi } from './callApi';
export async function createWatch(id, watch) {
return callApi({
export async function createWatch({
id,
watch,
http
}: {
http: HttpServiceBase;
id: string;
watch: any;
}) {
return callApi(http, {
method: 'PUT',
pathname: `/api/watcher/watch/${id}`,
body: JSON.stringify({ type: 'json', id, watch })