Skip to content

Commit

Permalink
[Monitor management] Update to use new add monitor endpoint (#129447)
Browse files Browse the repository at this point in the history
Co-authored-by: Abdul Zahid <[email protected]>
(cherry picked from commit 39cf5d5)
  • Loading branch information
shahzad31 committed Apr 6, 2022
1 parent dbbbfea commit a3973a8
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -306,23 +306,19 @@ export type EncryptedSyntheticsMonitorWithId = t.TypeOf<
typeof EncryptedSyntheticsMonitorWithIdCodec
>;

export const MonitorManagementListResultCodec = t.intersection([
t.type({
monitors: t.array(
t.interface({
id: t.string,
attributes: EncryptedSyntheticsMonitorCodec,
updated_at: t.string,
})
),
page: t.number,
perPage: t.number,
total: t.union([t.number, t.null]),
}),
t.partial({
syncErrors: ServiceLocationErrors,
}),
]);
export const MonitorManagementListResultCodec = t.type({
monitors: t.array(
t.interface({
id: t.string,
attributes: EncryptedSyntheticsMonitorCodec,
updated_at: t.string,
})
),
page: t.number,
perPage: t.number,
total: t.union([t.number, t.null]),
syncErrors: t.union([ServiceLocationErrors, t.null]),
});

export type MonitorManagementListResult = t.TypeOf<typeof MonitorManagementListResultCodec>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ describe('useInlineErrors', function () {
{
error: { monitorList: null, serviceLocations: null, enablement: null },
enablement: null,
list: { monitors: [], page: 1, perPage: 10, total: null },
list: { monitors: [], page: 1, perPage: 10, total: null, syncErrors: null },
loading: { monitorList: false, serviceLocations: false, enablement: false },
locations: [],
syntheticsService: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe('useInlineErrorsCount', function () {
'heartbeat-8*,heartbeat-7*,synthetics-*',
{
error: { monitorList: null, serviceLocations: null, enablement: null },
list: { monitors: [], page: 1, perPage: 10, total: null },
list: { monitors: [], page: 1, perPage: 10, total: null, syncErrors: null },
enablement: null,
loading: { monitorList: false, serviceLocations: false, enablement: false },
locations: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ describe('useExpViewTimeRange', function () {
page: 1,
total: 0,
monitors: [],
syncErrors: null,
},
locations: [],
enablement: null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export const InvalidMonitors = ({
page: pageState.pageIndex,
perPage: pageState.pageSize,
total: invalidTotal ?? 0,
syncErrors: null,
},
enablement: null,
error: { monitorList: null, serviceLocations: null, enablement: null },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,15 @@ export const MonitorAsyncError = () => {
/>
</p>
<ul>
{Object.values(syncErrors).map((e) => {
{Object.values(syncErrors ?? {}).map((e) => {
return (
<li key={e.locationId}>{`${
locations.find((location) => location.id === e.locationId)?.label
} - ${STATUS_LABEL}: ${e.error.status}; ${REASON_LABEL}: ${e.error.reason}.`}</li>
<li key={e.locationId}>
{`${
locations.find((location) => location.id === e.locationId)?.label
} - ${STATUS_LABEL}: ${e.error?.status ?? NOT_AVAILABLE_LABEL}; ${REASON_LABEL}: ${
e.error?.reason ?? NOT_AVAILABLE_LABEL
}`}
</li>
);
})}
</ul>
Expand All @@ -67,6 +71,13 @@ const STATUS_LABEL = i18n.translate(
}
);

const NOT_AVAILABLE_LABEL = i18n.translate(
'xpack.uptime.monitorManagement.monitorSync.failure.notAvailable',
{
defaultMessage: 'Not available',
}
);

const DISMISS_LABEL = i18n.translate(
'xpack.uptime.monitorManagement.monitorSync.failure.dismissLabel',
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ describe('<MonitorManagementList />', () => {
page: 1,
total: 6,
monitors,
syncErrors: null,
},
locations: [],
enablement: null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export const mockState: AppState = {
perPage: 10,
total: null,
monitors: [],
syncErrors: null,
},
locations: [],
loading: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export class ServiceAPIClient {
}

async put(data: ServiceData) {
return this.callAPI('POST', data);
return this.callAPI('PUT', data);
}

async delete(data: ServiceData) {
Expand Down Expand Up @@ -170,6 +170,9 @@ export class ServiceAPIClient {
catchError((err) => {
pushErrors.push({ locationId: id, error: err.response?.data });
this.logger.error(err);
if (err.response?.data?.reason) {
this.logger.error(err.response?.data?.reason);
}
// we don't want to throw an unhandled exception here
return of(true);
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ const SYNTHETICS_SERVICE_SYNC_MONITORS_TASK_TYPE =
const SYNTHETICS_SERVICE_SYNC_MONITORS_TASK_ID = 'UPTIME:SyntheticsService:sync-task';
const SYNTHETICS_SERVICE_SYNC_INTERVAL_DEFAULT = '5m';

type SyntheticsConfig = SyntheticsMonitorWithId & {
fields_under_root?: boolean;
fields?: { config_id: string; run_once?: boolean; test_run_id?: string };
};

export class SyntheticsService {
private logger: Logger;
private readonly server: UptimeServerSetup;
Expand Down Expand Up @@ -218,14 +223,32 @@ export class SyntheticsService {
};
}

async pushConfigs(
configs?: Array<
SyntheticsMonitorWithId & {
fields_under_root?: boolean;
fields?: { config_id: string };
}
>
) {
async addConfig(config: SyntheticsConfig) {
const monitors = this.formatConfigs([config]);

this.apiKey = await this.getApiKey();

if (!this.apiKey) {
return null;
}

const data = {
monitors,
output: await this.getOutput(this.apiKey),
};

this.logger.debug(`1 monitor will be pushed to synthetics service.`);

try {
this.syncErrors = await this.apiClient.post(data);
return this.syncErrors;
} catch (e) {
this.logger.error(e);
throw e;
}
}

async pushConfigs(configs?: SyntheticsConfig[]) {
const monitors = this.formatConfigs(configs || (await this.getMonitorConfigs()));
if (monitors.length === 0) {
this.logger.debug('No monitor found which can be pushed to service.');
Expand All @@ -246,21 +269,15 @@ export class SyntheticsService {
this.logger.debug(`${monitors.length} monitors will be pushed to synthetics service.`);

try {
return await this.apiClient.post(data);
this.syncErrors = await this.apiClient.put(data);
return this.syncErrors;
} catch (e) {
this.logger.error(e);
throw e;
}
}

async runOnceConfigs(
configs?: Array<
SyntheticsMonitorWithId & {
fields_under_root?: boolean;
fields?: { run_once: boolean; config_id: string };
}
>
) {
async runOnceConfigs(configs?: SyntheticsConfig[]) {
const monitors = this.formatConfigs(configs || (await this.getMonitorConfigs()));
if (monitors.length === 0) {
return;
Expand All @@ -284,15 +301,7 @@ export class SyntheticsService {
}
}

async triggerConfigs(
request?: KibanaRequest,
configs?: Array<
SyntheticsMonitorWithId & {
fields_under_root?: boolean;
fields?: { config_id: string; test_run_id: string };
}
>
) {
async triggerConfigs(request?: KibanaRequest, configs?: SyntheticsConfig[]) {
const monitors = this.formatConfigs(configs || (await this.getMonitorConfigs()));
if (monitors.length === 0) {
return;
Expand Down Expand Up @@ -328,7 +337,11 @@ export class SyntheticsService {
monitors: this.formatConfigs(configs),
output: await this.getOutput(this.apiKey),
};
return await this.apiClient.delete(data);
const result = await this.apiClient.delete(data);
if (this.syncErrors && this.syncErrors?.length > 0) {
this.syncErrors = await this.pushConfigs();
}
return result;
}

async deleteAllConfigs() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,14 @@ export const addSyntheticsMonitorRoute: UMRestApiRouteFactory = () => ({

const { syntheticsService } = server;

const errors = await syntheticsService.pushConfigs([
{
...monitor,
id: newMonitor.id,
fields: {
config_id: newMonitor.id,
},
fields_under_root: true,
const errors = await syntheticsService.addConfig({
...monitor,
id: newMonitor.id,
fields: {
config_id: newMonitor.id,
},
]);
fields_under_root: true,
});

sendTelemetryEvents(
server.logger,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
import expect from '@kbn/expect';
import { isRight } from 'fp-ts/lib/Either';
import { FtrProviderContext } from '../../../ftr_provider_context';
import { MonitorSummariesResultType } from '../../../../../plugins/uptime/common/runtime_types';
import {
MonitorSummariesResult,
MonitorSummariesResultType,
} from '../../../../../plugins/uptime/common/runtime_types';
import { API_URLS } from '../../../../../plugins/uptime/common/constants';

interface ExpectedMonitorStatesPage {
Expand Down Expand Up @@ -40,7 +43,8 @@ const checkMonitorStatesResponse = ({
const decoded = MonitorSummariesResultType.decode(response);
expect(isRight(decoded)).to.be.ok();
if (isRight(decoded)) {
const { summaries, prevPagePagination, nextPagePagination } = decoded.right;
const { summaries, prevPagePagination, nextPagePagination } =
decoded.right as MonitorSummariesResult;
expect(summaries).to.have.length(size);
expect(summaries?.map((s) => s.monitor_id)).to.eql(statesIds);
expect(
Expand Down

0 comments on commit a3973a8

Please sign in to comment.