mirror of
https://github.com/elastic/kibana.git
synced 2025-04-23 17:28:26 -04:00
# Backport This will backport the following commits from `main` to `8.x`: - [chore(slo): factorize error handler (#209671)](https://github.com/elastic/kibana/pull/209671) <!--- Backport version: 9.6.6 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sorenlouv/backport) <!--BACKPORT [{"author":{"name":"Kevin Delemme","email":"kevin.delemme@elastic.co"},"sourceCommit":{"committedDate":"2025-02-06T19:04:37Z","message":"chore(slo): factorize error handler (#209671)","sha":"375528dcfe03912fb491fa8477b10dcaa3fa7f7c","branchLabelMapping":{"^v9.1.0$":"main","^v8.19.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","backport:skip","v9.0.0","Team:obs-ux-management","v9.1.0"],"title":"chore(slo): factorize error handler","number":209671,"url":"https://github.com/elastic/kibana/pull/209671","mergeCommit":{"message":"chore(slo): factorize error handler (#209671)","sha":"375528dcfe03912fb491fa8477b10dcaa3fa7f7c"}},"sourceBranch":"main","suggestedTargetBranches":["9.0"],"targetPullRequestStates":[{"branch":"9.0","label":"v9.0.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v9.1.0","branchLabelMappingKey":"^v9.1.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/209671","number":209671,"mergeCommit":{"message":"chore(slo): factorize error handler (#209671)","sha":"375528dcfe03912fb491fa8477b10dcaa3fa7f7c"}}]}] BACKPORT-->
This commit is contained in:
parent
970b56957e
commit
46ef01bd41
28 changed files with 1059 additions and 1098 deletions
|
@ -11,7 +11,6 @@ import {
|
|||
groupBySchema,
|
||||
groupingsSchema,
|
||||
groupSummarySchema,
|
||||
historicalSummarySchema,
|
||||
metaSchema,
|
||||
objectiveSchema,
|
||||
sloSettingsSchema,
|
||||
|
@ -22,7 +21,6 @@ import {
|
|||
type Objective = t.TypeOf<typeof objectiveSchema>;
|
||||
type Status = t.TypeOf<typeof statusSchema>;
|
||||
type DateRange = t.TypeOf<typeof dateRangeSchema>;
|
||||
type HistoricalSummary = t.TypeOf<typeof historicalSummarySchema>;
|
||||
type Summary = t.TypeOf<typeof summarySchema>;
|
||||
type Groupings = t.TypeOf<typeof groupingsSchema>;
|
||||
type Meta = t.TypeOf<typeof metaSchema>;
|
||||
|
@ -35,7 +33,6 @@ export type {
|
|||
Objective,
|
||||
DateRange,
|
||||
Groupings,
|
||||
HistoricalSummary,
|
||||
Meta,
|
||||
Status,
|
||||
Summary,
|
||||
|
|
|
@ -1,37 +0,0 @@
|
|||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License
|
||||
* 2.0; you may not use this file except in compliance with the Elastic License
|
||||
* 2.0.
|
||||
*/
|
||||
|
||||
import { Boom, badRequest, conflict, forbidden, notFound } from '@hapi/boom';
|
||||
import { SLOError, SecurityException, SLOIdConflict, SLONotFound } from './errors';
|
||||
|
||||
function handleSLOError(error: SLOError): Boom {
|
||||
if (error instanceof SLONotFound) {
|
||||
return notFound(error.message);
|
||||
}
|
||||
|
||||
if (error instanceof SLOIdConflict) {
|
||||
return conflict(error.message);
|
||||
}
|
||||
|
||||
if (error instanceof SecurityException) {
|
||||
return forbidden(error.message);
|
||||
}
|
||||
|
||||
return badRequest(error.message);
|
||||
}
|
||||
|
||||
export async function executeWithErrorHandler(fn: () => Promise<any>): Promise<any> {
|
||||
try {
|
||||
return await fn();
|
||||
} catch (error) {
|
||||
if (error instanceof SLOError) {
|
||||
throw handleSLOError(error);
|
||||
}
|
||||
|
||||
throw error;
|
||||
}
|
||||
}
|
|
@ -6,4 +6,3 @@
|
|||
*/
|
||||
|
||||
export * from './errors';
|
||||
export * from './handler';
|
||||
|
|
|
@ -5,6 +5,42 @@
|
|||
* 2.0.
|
||||
*/
|
||||
import { createServerRouteFactory } from '@kbn/server-route-repository';
|
||||
import { Boom, forbidden, notFound, conflict, badRequest } from '@hapi/boom';
|
||||
import { CreateServerRouteFactory } from '@kbn/server-route-repository-utils/src/typings';
|
||||
import { SLORouteHandlerResources } from './types';
|
||||
import { SLOError, SLONotFound, SLOIdConflict, SecurityException } from '../errors';
|
||||
|
||||
export const createSloServerRoute = createServerRouteFactory<SLORouteHandlerResources>();
|
||||
function handleSLOError(error: SLOError): Boom {
|
||||
if (error instanceof SLONotFound) {
|
||||
return notFound(error.message);
|
||||
}
|
||||
|
||||
if (error instanceof SLOIdConflict) {
|
||||
return conflict(error.message);
|
||||
}
|
||||
|
||||
if (error instanceof SecurityException) {
|
||||
return forbidden(error.message);
|
||||
}
|
||||
|
||||
return badRequest(error.message);
|
||||
}
|
||||
|
||||
const createPlainSloServerRoute = createServerRouteFactory<SLORouteHandlerResources>();
|
||||
|
||||
export const createSloServerRoute: CreateServerRouteFactory<
|
||||
SLORouteHandlerResources,
|
||||
undefined
|
||||
> = ({ handler, ...config }) => {
|
||||
return createPlainSloServerRoute({
|
||||
...config,
|
||||
handler: (options) => {
|
||||
return handler(options).catch((error) => {
|
||||
if (error instanceof SLOError) {
|
||||
throw handleSLOError(error);
|
||||
}
|
||||
throw error;
|
||||
});
|
||||
},
|
||||
});
|
||||
};
|
||||
|
|
|
@ -6,18 +6,17 @@
|
|||
*/
|
||||
|
||||
import { createSLOParamsSchema } from '@kbn/slo-schema';
|
||||
import { createSloServerRoute } from '../create_slo_server_route';
|
||||
import { assertPlatinumLicense } from './utils/assert_platinum_license';
|
||||
import { getSpaceId } from './utils/get_space_id';
|
||||
import { createTransformGenerators } from '../../services/transform_generators';
|
||||
import { DefaultSummaryTransformGenerator } from '../../services/summary_transform_generator/summary_transform_generator';
|
||||
import { executeWithErrorHandler } from '../../errors';
|
||||
import {
|
||||
CreateSLO,
|
||||
DefaultSummaryTransformManager,
|
||||
DefaultTransformManager,
|
||||
KibanaSavedObjectsSLORepository,
|
||||
} from '../../services';
|
||||
import { DefaultSummaryTransformGenerator } from '../../services/summary_transform_generator/summary_transform_generator';
|
||||
import { createTransformGenerators } from '../../services/transform_generators';
|
||||
import { createSloServerRoute } from '../create_slo_server_route';
|
||||
import { assertPlatinumLicense } from './utils/assert_platinum_license';
|
||||
import { getSpaceId } from './utils/get_space_id';
|
||||
|
||||
export const createSLORoute = createSloServerRoute({
|
||||
endpoint: 'POST /api/observability/slos 2023-10-31',
|
||||
|
@ -74,6 +73,6 @@ export const createSLORoute = createSloServerRoute({
|
|||
userId
|
||||
);
|
||||
|
||||
return await executeWithErrorHandler(() => createSLO.execute(params.body));
|
||||
return await createSLO.execute(params.body);
|
||||
},
|
||||
});
|
||||
|
|
|
@ -6,7 +6,6 @@
|
|||
*/
|
||||
|
||||
import { deleteSLOInstancesParamsSchema } from '@kbn/slo-schema';
|
||||
import { executeWithErrorHandler } from '../../errors';
|
||||
import { DeleteSLOInstances } from '../../services';
|
||||
import { createSloServerRoute } from '../create_slo_server_route';
|
||||
import { assertPlatinumLicense } from './utils/assert_platinum_license';
|
||||
|
@ -26,7 +25,7 @@ export const deleteSloInstancesRoute = createSloServerRoute({
|
|||
const esClient = (await context.core).elasticsearch.client.asCurrentUser;
|
||||
const deleteSloInstances = new DeleteSLOInstances(esClient);
|
||||
|
||||
await executeWithErrorHandler(() => deleteSloInstances.execute(params.body));
|
||||
await deleteSloInstances.execute(params.body);
|
||||
return response.noContent();
|
||||
},
|
||||
});
|
||||
|
|
|
@ -6,7 +6,6 @@
|
|||
*/
|
||||
|
||||
import { deleteSLOParamsSchema } from '@kbn/slo-schema';
|
||||
import { executeWithErrorHandler } from '../../errors';
|
||||
import {
|
||||
DefaultSummaryTransformManager,
|
||||
DefaultTransformManager,
|
||||
|
@ -72,7 +71,7 @@ export const deleteSLORoute = createSloServerRoute({
|
|||
rulesClient
|
||||
);
|
||||
|
||||
await executeWithErrorHandler(() => deleteSLO.execute(params.path.id));
|
||||
await deleteSLO.execute(params.path.id);
|
||||
return response.noContent();
|
||||
},
|
||||
});
|
||||
|
|
|
@ -6,7 +6,6 @@
|
|||
*/
|
||||
|
||||
import { manageSLOParamsSchema } from '@kbn/slo-schema';
|
||||
import { executeWithErrorHandler } from '../../errors';
|
||||
import {
|
||||
DefaultSummaryTransformManager,
|
||||
DefaultTransformManager,
|
||||
|
@ -60,7 +59,7 @@ export const disableSLORoute = createSloServerRoute({
|
|||
|
||||
const manageSLO = new ManageSLO(repository, transformManager, summaryTransformManager);
|
||||
|
||||
await executeWithErrorHandler(() => manageSLO.disable(params.path.id));
|
||||
await manageSLO.disable(params.path.id);
|
||||
return response.noContent();
|
||||
},
|
||||
});
|
||||
|
|
|
@ -6,7 +6,6 @@
|
|||
*/
|
||||
|
||||
import { manageSLOParamsSchema } from '@kbn/slo-schema';
|
||||
import { executeWithErrorHandler } from '../../errors';
|
||||
import {
|
||||
DefaultSummaryTransformManager,
|
||||
DefaultTransformManager,
|
||||
|
@ -60,7 +59,7 @@ export const enableSLORoute = createSloServerRoute({
|
|||
|
||||
const manageSLO = new ManageSLO(repository, transformManager, summaryTransformManager);
|
||||
|
||||
await executeWithErrorHandler(() => manageSLO.enable(params.path.id));
|
||||
await manageSLO.enable(params.path.id);
|
||||
|
||||
return response.noContent();
|
||||
},
|
||||
|
|
|
@ -6,7 +6,6 @@
|
|||
*/
|
||||
|
||||
import { fetchSLOHealthParamsSchema } from '@kbn/slo-schema';
|
||||
import { executeWithErrorHandler } from '../../errors';
|
||||
import { GetSLOHealth, KibanaSavedObjectsSLORepository } from '../../services';
|
||||
import { createSloServerRoute } from '../create_slo_server_route';
|
||||
import { assertPlatinumLicense } from './utils/assert_platinum_license';
|
||||
|
@ -31,6 +30,6 @@ export const fetchSloHealthRoute = createSloServerRoute({
|
|||
|
||||
const getSLOHealth = new GetSLOHealth(esClient, scopedClusterClient, repository);
|
||||
|
||||
return await executeWithErrorHandler(() => getSLOHealth.execute(params.body));
|
||||
return await getSLOHealth.execute(params.body);
|
||||
},
|
||||
});
|
||||
|
|
|
@ -6,8 +6,7 @@
|
|||
*/
|
||||
|
||||
import { fetchHistoricalSummaryParamsSchema } from '@kbn/slo-schema';
|
||||
import { executeWithErrorHandler } from '../../errors';
|
||||
import { DefaultHistoricalSummaryClient } from '../../services/historical_summary_client';
|
||||
import { HistoricalSummaryClient } from '../../services/historical_summary_client';
|
||||
import { createSloServerRoute } from '../create_slo_server_route';
|
||||
import { assertPlatinumLicense } from './utils/assert_platinum_license';
|
||||
|
||||
|
@ -24,8 +23,8 @@ export const fetchHistoricalSummary = createSloServerRoute({
|
|||
await assertPlatinumLicense(plugins);
|
||||
|
||||
const esClient = (await context.core).elasticsearch.client.asCurrentUser;
|
||||
const historicalSummaryClient = new DefaultHistoricalSummaryClient(esClient);
|
||||
const historicalSummaryClient = new HistoricalSummaryClient(esClient);
|
||||
|
||||
return await executeWithErrorHandler(() => historicalSummaryClient.fetch(params.body));
|
||||
return await historicalSummaryClient.fetch(params.body);
|
||||
},
|
||||
});
|
||||
|
|
|
@ -6,7 +6,6 @@
|
|||
*/
|
||||
|
||||
import { findSloDefinitionsParamsSchema } from '@kbn/slo-schema';
|
||||
import { executeWithErrorHandler } from '../../errors';
|
||||
import { KibanaSavedObjectsSLORepository } from '../../services';
|
||||
import { FindSLODefinitions } from '../../services/find_slo_definitions';
|
||||
import { createSloServerRoute } from '../create_slo_server_route';
|
||||
|
@ -28,6 +27,6 @@ export const findSloDefinitionsRoute = createSloServerRoute({
|
|||
const repository = new KibanaSavedObjectsSLORepository(soClient, logger);
|
||||
const findSloDefinitions = new FindSLODefinitions(repository);
|
||||
|
||||
return await executeWithErrorHandler(() => findSloDefinitions.execute(params?.query ?? {}));
|
||||
return await findSloDefinitions.execute(params?.query ?? {});
|
||||
},
|
||||
});
|
||||
|
|
|
@ -6,7 +6,6 @@
|
|||
*/
|
||||
|
||||
import { findSLOGroupsParamsSchema } from '@kbn/slo-schema';
|
||||
import { executeWithErrorHandler } from '../../errors';
|
||||
import { FindSLOGroups } from '../../services';
|
||||
import { createSloServerRoute } from '../create_slo_server_route';
|
||||
import { assertPlatinumLicense } from './utils/assert_platinum_license';
|
||||
|
@ -29,6 +28,6 @@ export const findSLOGroupsRoute = createSloServerRoute({
|
|||
const coreContext = context.core;
|
||||
const esClient = (await coreContext).elasticsearch.client.asCurrentUser;
|
||||
const findSLOGroups = new FindSLOGroups(esClient, soClient, logger, spaceId);
|
||||
return await executeWithErrorHandler(() => findSLOGroups.execute(params?.query ?? {}));
|
||||
return await findSLOGroups.execute(params?.query ?? {});
|
||||
},
|
||||
});
|
||||
|
|
|
@ -6,7 +6,6 @@
|
|||
*/
|
||||
|
||||
import { findSLOParamsSchema } from '@kbn/slo-schema';
|
||||
import { executeWithErrorHandler } from '../../errors';
|
||||
import { FindSLO, KibanaSavedObjectsSLORepository } from '../../services';
|
||||
import { DefaultSummarySearchClient } from '../../services/summary_search_client/summary_search_client';
|
||||
import { createSloServerRoute } from '../create_slo_server_route';
|
||||
|
@ -33,6 +32,6 @@ export const findSLORoute = createSloServerRoute({
|
|||
|
||||
const findSLO = new FindSLO(repository, summarySearchClient);
|
||||
|
||||
return await executeWithErrorHandler(() => findSLO.execute(params?.query ?? {}));
|
||||
return await findSLO.execute(params?.query ?? {});
|
||||
},
|
||||
});
|
||||
|
|
|
@ -6,7 +6,6 @@
|
|||
*/
|
||||
|
||||
import { getSLOGroupingsParamsSchema } from '@kbn/slo-schema';
|
||||
import { executeWithErrorHandler } from '../../errors';
|
||||
import { KibanaSavedObjectsSLORepository } from '../../services';
|
||||
import { GetSLOGroupings } from '../../services/get_slo_groupings';
|
||||
import { SloDefinitionClient } from '../../services/slo_definition_client';
|
||||
|
@ -38,8 +37,6 @@ export const getSLOGroupingsRoute = createSloServerRoute({
|
|||
|
||||
const getSLOGroupings = new GetSLOGroupings(definitionClient, esClient, settings, spaceId);
|
||||
|
||||
return await executeWithErrorHandler(() =>
|
||||
getSLOGroupings.execute(params.path.id, params.query)
|
||||
);
|
||||
return await getSLOGroupings.execute(params.path.id, params.query);
|
||||
},
|
||||
});
|
||||
|
|
|
@ -6,7 +6,6 @@
|
|||
*/
|
||||
|
||||
import { getSLOParamsSchema } from '@kbn/slo-schema';
|
||||
import { executeWithErrorHandler } from '../../errors';
|
||||
import {
|
||||
DefaultBurnRatesClient,
|
||||
DefaultSummaryClient,
|
||||
|
@ -40,8 +39,6 @@ export const getSLORoute = createSloServerRoute({
|
|||
const definitionClient = new SloDefinitionClient(repository, esClient, logger);
|
||||
const getSLO = new GetSLO(definitionClient, summaryClient);
|
||||
|
||||
return await executeWithErrorHandler(() =>
|
||||
getSLO.execute(params.path.id, spaceId, params.query)
|
||||
);
|
||||
return await getSLO.execute(params.path.id, spaceId, params.query);
|
||||
},
|
||||
});
|
||||
|
|
|
@ -6,7 +6,6 @@
|
|||
*/
|
||||
|
||||
import { getSLOBurnRatesParamsSchema } from '@kbn/slo-schema';
|
||||
import { executeWithErrorHandler } from '../../errors';
|
||||
import { getBurnRates } from '../../services/get_burn_rates';
|
||||
import { createSloServerRoute } from '../create_slo_server_route';
|
||||
import { assertPlatinumLicense } from './utils/assert_platinum_license';
|
||||
|
@ -30,19 +29,17 @@ export const getSloBurnRates = createSloServerRoute({
|
|||
const soClient = (await context.core).savedObjects.client;
|
||||
const { instanceId, windows, remoteName } = params.body;
|
||||
|
||||
return await executeWithErrorHandler(() =>
|
||||
getBurnRates({
|
||||
instanceId,
|
||||
spaceId,
|
||||
windows,
|
||||
remoteName,
|
||||
sloId: params.path.id,
|
||||
services: {
|
||||
soClient,
|
||||
esClient,
|
||||
logger,
|
||||
},
|
||||
})
|
||||
);
|
||||
return await getBurnRates({
|
||||
instanceId,
|
||||
spaceId,
|
||||
windows,
|
||||
remoteName,
|
||||
sloId: params.path.id,
|
||||
services: {
|
||||
soClient,
|
||||
esClient,
|
||||
logger,
|
||||
},
|
||||
});
|
||||
},
|
||||
});
|
||||
|
|
|
@ -5,7 +5,6 @@
|
|||
* 2.0.
|
||||
*/
|
||||
|
||||
import { executeWithErrorHandler } from '../../errors';
|
||||
import { getSloSettings } from '../../services/slo_settings';
|
||||
import { createSloServerRoute } from '../create_slo_server_route';
|
||||
import { assertPlatinumLicense } from './utils/assert_platinum_license';
|
||||
|
@ -23,6 +22,6 @@ export const getSloSettingsRoute = createSloServerRoute({
|
|||
|
||||
const soClient = (await context.core).savedObjects.client;
|
||||
|
||||
return await executeWithErrorHandler(() => getSloSettings(soClient));
|
||||
return await getSloSettings(soClient);
|
||||
},
|
||||
});
|
||||
|
|
|
@ -6,7 +6,6 @@
|
|||
*/
|
||||
|
||||
import { getOverviewParamsSchema } from '@kbn/slo-schema/src/rest_specs/routes/get_overview';
|
||||
import { executeWithErrorHandler } from '../../errors';
|
||||
import { GetSLOsOverview } from '../../services/get_slos_overview';
|
||||
import { createSloServerRoute } from '../create_slo_server_route';
|
||||
import { assertPlatinumLicense } from './utils/assert_platinum_license';
|
||||
|
@ -44,6 +43,6 @@ export const getSLOsOverview = createSloServerRoute({
|
|||
racClient
|
||||
);
|
||||
|
||||
return await executeWithErrorHandler(() => slosOverview.execute(params?.query ?? {}));
|
||||
return await slosOverview.execute(params?.query ?? {});
|
||||
},
|
||||
});
|
||||
|
|
|
@ -5,7 +5,6 @@
|
|||
* 2.0.
|
||||
*/
|
||||
|
||||
import { executeWithErrorHandler } from '../../errors';
|
||||
import { GetSLOSuggestions } from '../../services/get_slo_suggestions';
|
||||
import { createSloServerRoute } from '../create_slo_server_route';
|
||||
import { assertPlatinumLicense } from './utils/assert_platinum_license';
|
||||
|
@ -23,6 +22,6 @@ export const getSLOSuggestionsRoute = createSloServerRoute({
|
|||
|
||||
const soClient = (await context.core).savedObjects.client;
|
||||
const getSLOSuggestions = new GetSLOSuggestions(soClient);
|
||||
return await executeWithErrorHandler(() => getSLOSuggestions.execute());
|
||||
return await getSLOSuggestions.execute();
|
||||
},
|
||||
});
|
||||
|
|
|
@ -6,7 +6,6 @@
|
|||
*/
|
||||
|
||||
import { createSLOParamsSchema } from '@kbn/slo-schema';
|
||||
import { executeWithErrorHandler } from '../../errors';
|
||||
import {
|
||||
CreateSLO,
|
||||
DefaultSummaryTransformManager,
|
||||
|
@ -71,6 +70,6 @@ export const inspectSLORoute = createSloServerRoute({
|
|||
username
|
||||
);
|
||||
|
||||
return await executeWithErrorHandler(() => createSLO.inspect(params.body));
|
||||
return await createSLO.inspect(params.body);
|
||||
},
|
||||
});
|
||||
|
|
|
@ -10,7 +10,6 @@ import {
|
|||
putSLOServerlessSettingsParamsSchema,
|
||||
putSLOSettingsParamsSchema,
|
||||
} from '@kbn/slo-schema';
|
||||
import { executeWithErrorHandler } from '../../errors';
|
||||
import { storeSloSettings } from '../../services/slo_settings';
|
||||
import { createSloServerRoute } from '../create_slo_server_route';
|
||||
import { assertPlatinumLicense } from './utils/assert_platinum_license';
|
||||
|
@ -29,8 +28,6 @@ export const putSloSettings = (isServerless?: boolean) =>
|
|||
await assertPlatinumLicense(plugins);
|
||||
|
||||
const soClient = (await context.core).savedObjects.client;
|
||||
return await executeWithErrorHandler(() =>
|
||||
storeSloSettings(soClient, params.body as PutSLOSettingsParams)
|
||||
);
|
||||
return await storeSloSettings(soClient, params.body as PutSLOSettingsParams);
|
||||
},
|
||||
});
|
||||
|
|
|
@ -6,7 +6,6 @@
|
|||
*/
|
||||
|
||||
import { resetSLOParamsSchema } from '@kbn/slo-schema';
|
||||
import { executeWithErrorHandler } from '../../errors';
|
||||
import {
|
||||
DefaultSummaryTransformManager,
|
||||
DefaultTransformManager,
|
||||
|
@ -70,6 +69,6 @@ export const resetSLORoute = createSloServerRoute({
|
|||
basePath
|
||||
);
|
||||
|
||||
return await executeWithErrorHandler(() => resetSLO.execute(params.path.id));
|
||||
return await resetSLO.execute(params.path.id);
|
||||
},
|
||||
});
|
||||
|
|
|
@ -6,7 +6,6 @@
|
|||
*/
|
||||
|
||||
import { updateSLOParamsSchema } from '@kbn/slo-schema';
|
||||
import { executeWithErrorHandler } from '../../errors';
|
||||
import {
|
||||
DefaultSummaryTransformManager,
|
||||
DefaultTransformManager,
|
||||
|
@ -73,6 +72,6 @@ export const updateSLORoute = createSloServerRoute({
|
|||
userId
|
||||
);
|
||||
|
||||
return await executeWithErrorHandler(() => updateSLO.execute(params.path.id, params.body));
|
||||
return await updateSLO.execute(params.path.id, params.body);
|
||||
},
|
||||
});
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -12,7 +12,7 @@ import { DateRange, SLODefinition } from '../domain/models';
|
|||
import { oneMinute, oneMonth, sevenDays, thirtyDays } from './fixtures/duration';
|
||||
import { createSLO } from './fixtures/slo';
|
||||
import {
|
||||
DefaultHistoricalSummaryClient,
|
||||
HistoricalSummaryClient,
|
||||
getFixedIntervalAndBucketsPerDay,
|
||||
} from './historical_summary_client';
|
||||
|
||||
|
@ -168,7 +168,7 @@ describe('FetchHistoricalSummary', () => {
|
|||
groupBy: ALL_VALUE,
|
||||
});
|
||||
esClientMock.msearch.mockResolvedValueOnce(generateEsResponseForRollingSLO(slo));
|
||||
const client = new DefaultHistoricalSummaryClient(esClientMock);
|
||||
const client = new HistoricalSummaryClient(esClientMock);
|
||||
|
||||
const results = await client.fetch({
|
||||
list: [
|
||||
|
@ -185,7 +185,7 @@ describe('FetchHistoricalSummary', () => {
|
|||
});
|
||||
|
||||
results[0].data.forEach((dailyResult) =>
|
||||
expect(dailyResult).toMatchSnapshot({ date: expect.any(Date) })
|
||||
expect(dailyResult).toMatchSnapshot({ date: expect.any(String) })
|
||||
);
|
||||
});
|
||||
|
||||
|
@ -201,7 +201,7 @@ describe('FetchHistoricalSummary', () => {
|
|||
};
|
||||
|
||||
esClientMock.msearch.mockResolvedValueOnce(generateEsResponseForRollingSLO(slo, range));
|
||||
const client = new DefaultHistoricalSummaryClient(esClientMock);
|
||||
const client = new HistoricalSummaryClient(esClientMock);
|
||||
|
||||
const results = await client.fetch({
|
||||
list: [
|
||||
|
@ -219,7 +219,7 @@ describe('FetchHistoricalSummary', () => {
|
|||
});
|
||||
|
||||
results[0].data.forEach((dailyResult) =>
|
||||
expect(dailyResult).toMatchSnapshot({ date: expect.any(Date) })
|
||||
expect(dailyResult).toMatchSnapshot({ date: expect.any(String) })
|
||||
);
|
||||
});
|
||||
});
|
||||
|
@ -233,7 +233,7 @@ describe('FetchHistoricalSummary', () => {
|
|||
groupBy: ALL_VALUE,
|
||||
});
|
||||
esClientMock.msearch.mockResolvedValueOnce(generateEsResponseForRollingSLO(slo));
|
||||
const client = new DefaultHistoricalSummaryClient(esClientMock);
|
||||
const client = new HistoricalSummaryClient(esClientMock);
|
||||
|
||||
const results = await client.fetch({
|
||||
list: [
|
||||
|
@ -250,7 +250,7 @@ describe('FetchHistoricalSummary', () => {
|
|||
});
|
||||
|
||||
results[0].data.forEach((dailyResult) =>
|
||||
expect(dailyResult).toMatchSnapshot({ date: expect.any(Date) })
|
||||
expect(dailyResult).toMatchSnapshot({ date: expect.any(String) })
|
||||
);
|
||||
expect(results[0].data).toHaveLength(180);
|
||||
});
|
||||
|
@ -267,7 +267,7 @@ describe('FetchHistoricalSummary', () => {
|
|||
to: new Date('2023-01-13T15:00:00.000Z'),
|
||||
};
|
||||
esClientMock.msearch.mockResolvedValueOnce(generateEsResponseForRollingSLO(slo, range));
|
||||
const client = new DefaultHistoricalSummaryClient(esClientMock);
|
||||
const client = new HistoricalSummaryClient(esClientMock);
|
||||
|
||||
const results = await client.fetch({
|
||||
list: [
|
||||
|
@ -285,7 +285,7 @@ describe('FetchHistoricalSummary', () => {
|
|||
});
|
||||
|
||||
results[0].data.forEach((dailyResult) =>
|
||||
expect(dailyResult).toMatchSnapshot({ date: expect.any(Date) })
|
||||
expect(dailyResult).toMatchSnapshot({ date: expect.any(String) })
|
||||
);
|
||||
});
|
||||
});
|
||||
|
@ -301,7 +301,7 @@ describe('FetchHistoricalSummary', () => {
|
|||
objective: { target: 0.95, timesliceTarget: 0.9, timesliceWindow: oneMinute() },
|
||||
});
|
||||
esClientMock.msearch.mockResolvedValueOnce(generateEsResponseForMonthlyCalendarAlignedSLO());
|
||||
const client = new DefaultHistoricalSummaryClient(esClientMock);
|
||||
const client = new HistoricalSummaryClient(esClientMock);
|
||||
|
||||
const results = await client.fetch({
|
||||
list: [
|
||||
|
@ -318,7 +318,7 @@ describe('FetchHistoricalSummary', () => {
|
|||
});
|
||||
|
||||
results[0].data.forEach((dailyResult) =>
|
||||
expect(dailyResult).toMatchSnapshot({ date: expect.any(Date) })
|
||||
expect(dailyResult).toMatchSnapshot({ date: expect.any(String) })
|
||||
);
|
||||
expect(results[0].data).toHaveLength(108);
|
||||
});
|
||||
|
@ -335,7 +335,7 @@ describe('FetchHistoricalSummary', () => {
|
|||
objective: { target: 0.95 },
|
||||
});
|
||||
esClientMock.msearch.mockResolvedValueOnce(generateEsResponseForMonthlyCalendarAlignedSLO());
|
||||
const client = new DefaultHistoricalSummaryClient(esClientMock);
|
||||
const client = new HistoricalSummaryClient(esClientMock);
|
||||
|
||||
const results = await client.fetch({
|
||||
list: [
|
||||
|
@ -352,7 +352,7 @@ describe('FetchHistoricalSummary', () => {
|
|||
});
|
||||
|
||||
results[0].data.forEach((dailyResult) =>
|
||||
expect(dailyResult).toMatchSnapshot({ date: expect.any(Date) })
|
||||
expect(dailyResult).toMatchSnapshot({ date: expect.any(String) })
|
||||
);
|
||||
|
||||
expect(results[0].data).toHaveLength(108);
|
||||
|
@ -366,7 +366,7 @@ describe('FetchHistoricalSummary', () => {
|
|||
groupBy: 'host',
|
||||
});
|
||||
esClientMock.msearch.mockResolvedValueOnce(generateEsResponseForRollingSLO(slo));
|
||||
const client = new DefaultHistoricalSummaryClient(esClientMock);
|
||||
const client = new HistoricalSummaryClient(esClientMock);
|
||||
|
||||
const results = await client.fetch({
|
||||
list: [
|
||||
|
@ -388,7 +388,7 @@ describe('FetchHistoricalSummary', () => {
|
|||
).toEqual({ term: { 'slo.instanceId': 'host-abc' } });
|
||||
|
||||
results[0].data.forEach((dailyResult) =>
|
||||
expect(dailyResult).toMatchSnapshot({ date: expect.any(Date) })
|
||||
expect(dailyResult).toMatchSnapshot({ date: expect.any(String) })
|
||||
);
|
||||
expect(results[0].data).toHaveLength(180);
|
||||
});
|
||||
|
|
|
@ -13,20 +13,19 @@ import {
|
|||
calendarAlignedTimeWindowSchema,
|
||||
DurationUnit,
|
||||
FetchHistoricalSummaryParams,
|
||||
fetchHistoricalSummaryResponseSchema,
|
||||
FetchHistoricalSummaryResponse,
|
||||
HistoricalSummaryResponse,
|
||||
occurrencesBudgetingMethodSchema,
|
||||
rollingTimeWindowSchema,
|
||||
timeslicesBudgetingMethodSchema,
|
||||
toMomentUnitOfTime,
|
||||
} from '@kbn/slo-schema';
|
||||
import { assertNever } from '@kbn/std';
|
||||
import * as t from 'io-ts';
|
||||
import moment from 'moment';
|
||||
import { SLI_DESTINATION_INDEX_PATTERN } from '../../common/constants';
|
||||
import {
|
||||
DateRange,
|
||||
GroupBy,
|
||||
HistoricalSummary,
|
||||
Objective,
|
||||
SLOId,
|
||||
TimeWindow,
|
||||
|
@ -53,16 +52,10 @@ interface DailyAggBucket {
|
|||
};
|
||||
}
|
||||
|
||||
export type HistoricalSummaryResponse = t.TypeOf<typeof fetchHistoricalSummaryResponseSchema>;
|
||||
|
||||
export interface HistoricalSummaryClient {
|
||||
fetch(list: FetchHistoricalSummaryParams): Promise<HistoricalSummaryResponse>;
|
||||
}
|
||||
|
||||
export class DefaultHistoricalSummaryClient implements HistoricalSummaryClient {
|
||||
export class HistoricalSummaryClient {
|
||||
constructor(private esClient: ElasticsearchClient) {}
|
||||
|
||||
async fetch(params: FetchHistoricalSummaryParams): Promise<HistoricalSummaryResponse> {
|
||||
async fetch(params: FetchHistoricalSummaryParams): Promise<FetchHistoricalSummaryResponse> {
|
||||
const dateRangeBySlo = params.list.reduce<
|
||||
Record<SLOId, { range: DateRange; queryRange: DateRange }>
|
||||
>((acc, { sloId, timeWindow, range }) => {
|
||||
|
@ -89,7 +82,7 @@ export class DefaultHistoricalSummaryClient implements HistoricalSummaryClient {
|
|||
]
|
||||
);
|
||||
|
||||
const historicalSummary: HistoricalSummaryResponse = [];
|
||||
const historicalSummary: FetchHistoricalSummaryResponse = [];
|
||||
if (searches.length === 0) {
|
||||
return historicalSummary;
|
||||
}
|
||||
|
@ -169,10 +162,10 @@ export class DefaultHistoricalSummaryClient implements HistoricalSummaryClient {
|
|||
function handleResultForCalendarAlignedAndOccurrences(
|
||||
objective: Objective,
|
||||
buckets: DailyAggBucket[]
|
||||
): HistoricalSummary[] {
|
||||
): HistoricalSummaryResponse[] {
|
||||
const initialErrorBudget = 1 - objective.target;
|
||||
|
||||
return buckets.map((bucket: DailyAggBucket): HistoricalSummary => {
|
||||
return buckets.map((bucket: DailyAggBucket) => {
|
||||
const good = bucket.cumulative_good?.value ?? 0;
|
||||
const total = bucket.cumulative_total?.value ?? 0;
|
||||
const sliValue = computeSLI(good, total);
|
||||
|
@ -180,7 +173,7 @@ function handleResultForCalendarAlignedAndOccurrences(
|
|||
const errorBudget = toErrorBudget(initialErrorBudget, consumedErrorBudget, true);
|
||||
|
||||
return {
|
||||
date: new Date(bucket.key_as_string),
|
||||
date: bucket.key_as_string,
|
||||
errorBudget,
|
||||
sliValue,
|
||||
status: computeSummaryStatus(objective, sliValue, errorBudget),
|
||||
|
@ -192,11 +185,11 @@ function handleResultForCalendarAlignedAndTimeslices(
|
|||
objective: Objective,
|
||||
buckets: DailyAggBucket[],
|
||||
dateRange: { range: DateRange; queryRange: DateRange }
|
||||
): HistoricalSummary[] {
|
||||
): HistoricalSummaryResponse[] {
|
||||
const initialErrorBudget = 1 - objective.target;
|
||||
const totalSlices = getSlicesFromDateRange(dateRange.range, objective.timesliceWindow!);
|
||||
|
||||
return buckets.map((bucket: DailyAggBucket): HistoricalSummary => {
|
||||
return buckets.map((bucket: DailyAggBucket) => {
|
||||
const good = bucket.cumulative_good?.value ?? 0;
|
||||
const total = bucket.cumulative_total?.value ?? 0;
|
||||
const sliValue = computeSLI(good, total, totalSlices);
|
||||
|
@ -204,7 +197,7 @@ function handleResultForCalendarAlignedAndTimeslices(
|
|||
const errorBudget = toErrorBudget(initialErrorBudget, consumedErrorBudget);
|
||||
|
||||
return {
|
||||
date: new Date(bucket.key_as_string),
|
||||
date: bucket.key_as_string,
|
||||
errorBudget,
|
||||
sliValue,
|
||||
status: computeSummaryStatus(objective, sliValue, errorBudget),
|
||||
|
@ -216,7 +209,7 @@ function handleResultForRollingAndOccurrences(
|
|||
objective: Objective,
|
||||
buckets: DailyAggBucket[],
|
||||
dateRange: { range: DateRange; queryRange: DateRange }
|
||||
): HistoricalSummary[] {
|
||||
): HistoricalSummaryResponse[] {
|
||||
const initialErrorBudget = 1 - objective.target;
|
||||
|
||||
return buckets
|
||||
|
@ -225,7 +218,7 @@ function handleResultForRollingAndOccurrences(
|
|||
moment(bucket.key_as_string).isSameOrAfter(dateRange.range.from) &&
|
||||
moment(bucket.key_as_string).isSameOrBefore(dateRange.range.to)
|
||||
)
|
||||
.map((bucket: DailyAggBucket): HistoricalSummary => {
|
||||
.map((bucket: DailyAggBucket) => {
|
||||
const good = bucket.cumulative_good?.value ?? 0;
|
||||
const total = bucket.cumulative_total?.value ?? 0;
|
||||
|
||||
|
@ -234,7 +227,7 @@ function handleResultForRollingAndOccurrences(
|
|||
const errorBudget = toErrorBudget(initialErrorBudget, consumedErrorBudget);
|
||||
|
||||
return {
|
||||
date: new Date(bucket.key_as_string),
|
||||
date: bucket.key_as_string,
|
||||
errorBudget,
|
||||
sliValue,
|
||||
status: computeSummaryStatus(objective, sliValue, errorBudget),
|
||||
|
@ -247,7 +240,7 @@ function handleResultForRollingAndTimeslices(
|
|||
timeWindow: TimeWindow,
|
||||
buckets: DailyAggBucket[],
|
||||
dateRange: { range: DateRange; queryRange: DateRange }
|
||||
): HistoricalSummary[] {
|
||||
): HistoricalSummaryResponse[] {
|
||||
const initialErrorBudget = 1 - objective.target;
|
||||
|
||||
const totalSlices = Math.ceil(
|
||||
|
@ -260,7 +253,7 @@ function handleResultForRollingAndTimeslices(
|
|||
moment(bucket.key_as_string).isSameOrAfter(dateRange.range.from) &&
|
||||
moment(bucket.key_as_string).isSameOrBefore(dateRange.range.to)
|
||||
)
|
||||
.map((bucket: DailyAggBucket): HistoricalSummary => {
|
||||
.map((bucket: DailyAggBucket) => {
|
||||
const good = bucket.cumulative_good?.value ?? 0;
|
||||
const total = bucket.cumulative_total?.value ?? 0;
|
||||
const sliValue = computeSLI(good, total, totalSlices);
|
||||
|
@ -268,7 +261,7 @@ function handleResultForRollingAndTimeslices(
|
|||
const errorBudget = toErrorBudget(initialErrorBudget, consumedErrorBudget);
|
||||
|
||||
return {
|
||||
date: new Date(bucket.key_as_string),
|
||||
date: bucket.key_as_string,
|
||||
errorBudget,
|
||||
sliValue,
|
||||
status: computeSummaryStatus(objective, sliValue, errorBudget),
|
||||
|
|
|
@ -104,6 +104,7 @@
|
|||
"@kbn/core-test-helpers-kbn-server",
|
||||
"@kbn/security-plugin",
|
||||
"@kbn/response-ops-rule-form",
|
||||
"@kbn/fields-metadata-plugin"
|
||||
"@kbn/fields-metadata-plugin",
|
||||
"@kbn/server-route-repository-utils",
|
||||
]
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue