in src/push/kibana_api.ts [152:196]
export async function createMonitorsLegacy({
schemas,
keepStale,
options,
}: {
schemas: MonitorSchema[];
keepStale: boolean;
options: PushOptions;
}) {
const schema: LegacyAPISchema = {
project: options.id,
keep_stale: keepStale,
monitors: schemas,
};
const url = generateURL(options, 'legacy');
const { body, statusCode } = await sendRequest({
url,
method: 'PUT',
auth: options.auth,
body: JSON.stringify(schema),
});
const resBody = await handleError(statusCode, url, body);
const allchunks = [];
for await (const data of resBody) {
allchunks.push(Buffer.from(data));
}
const chunks = safeNDJSONParse(Buffer.concat(allchunks).toString('utf-8'));
// Its kind of hacky for now where Kibana streams the response by
// writing the data as NDJSON events (data can be interleaved), we
// distinguish the final data by checking if the event was a progress vs complete event
for (const chunk of chunks) {
if (typeof chunk === 'string') {
// Ignore the progress from Kibana as we chunk the requests
continue;
}
const { failedMonitors, failedStaleMonitors } = chunk;
if (failedMonitors && failedMonitors.length > 0) {
throw formatFailedMonitors(failedMonitors);
}
if (failedStaleMonitors.length > 0) {
throw formatStaleMonitors(failedStaleMonitors);
}
}
}