Skip to content

Commit

Permalink
chore(slo): refactor transform generators and managers dependency inj…
Browse files Browse the repository at this point in the history
…ection (#201031)
  • Loading branch information
kdelemme authored Nov 22, 2024
1 parent 455a075 commit 2046663
Show file tree
Hide file tree
Showing 23 changed files with 620 additions and 655 deletions.
100 changes: 51 additions & 49 deletions x-pack/plugins/observability_solution/slo/server/routes/slo/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import {
updateSLOParamsSchema,
} from '@kbn/slo-schema';
import { getOverviewParamsSchema } from '@kbn/slo-schema/src/rest_specs/routes/get_overview';
import type { IndicatorTypes } from '../../domain/models';
import { executeWithErrorHandler } from '../../errors';
import {
CreateSLO,
Expand Down Expand Up @@ -60,29 +59,10 @@ import { SloDefinitionClient } from '../../services/slo_definition_client';
import { getSloSettings, storeSloSettings } from '../../services/slo_settings';
import { DefaultSummarySearchClient } from '../../services/summary_search_client';
import { DefaultSummaryTransformGenerator } from '../../services/summary_transform_generator/summary_transform_generator';
import {
ApmTransactionDurationTransformGenerator,
ApmTransactionErrorRateTransformGenerator,
HistogramTransformGenerator,
KQLCustomTransformGenerator,
MetricCustomTransformGenerator,
SyntheticsAvailabilityTransformGenerator,
TimesliceMetricTransformGenerator,
TransformGenerator,
} from '../../services/transform_generators';
import { createTransformGenerators } from '../../services/transform_generators';
import { createSloServerRoute } from '../create_slo_server_route';
import { SLORoutesDependencies } from '../types';

const transformGenerators: Record<IndicatorTypes, TransformGenerator> = {
'sli.apm.transactionDuration': new ApmTransactionDurationTransformGenerator(),
'sli.apm.transactionErrorRate': new ApmTransactionErrorRateTransformGenerator(),
'sli.synthetics.availability': new SyntheticsAvailabilityTransformGenerator(),
'sli.kql.custom': new KQLCustomTransformGenerator(),
'sli.metric.custom': new MetricCustomTransformGenerator(),
'sli.histogram.custom': new HistogramTransformGenerator(),
'sli.metric.timeslice': new TimesliceMetricTransformGenerator(),
};

const assertPlatinumLicense = async (plugins: SLORoutesDependencies['plugins']) => {
const licensing = await plugins.licensing.start();
const hasCorrectLicense = (await licensing.getLicense()).hasAtLeast('platinum');
Expand Down Expand Up @@ -120,14 +100,18 @@ const createSLORoute = createSloServerRoute({
getSpaceId(plugins, request),
dataViews.dataViewsServiceFactory(soClient, esClient),
]);
const transformManager = new DefaultTransformManager(
transformGenerators,
scopedClusterClient,
logger,

const transformGenerators = createTransformGenerators(
spaceId,
dataViewsService,
sloContext.isServerless
);

const transformManager = new DefaultTransformManager(
transformGenerators,
scopedClusterClient,
logger
);
const summaryTransformManager = new DefaultSummaryTransformManager(
new DefaultSummaryTransformGenerator(),
scopedClusterClient,
Expand Down Expand Up @@ -168,14 +152,17 @@ const inspectSLORoute = createSloServerRoute({
const soClient = core.savedObjects.client;
const repository = new KibanaSavedObjectsSLORepository(soClient, logger);
const dataViewsService = await dataViews.dataViewsServiceFactory(soClient, esClient);
const transformManager = new DefaultTransformManager(
transformGenerators,
scopedClusterClient,
logger,

const transformGenerators = createTransformGenerators(
spaceId,
dataViewsService,
sloContext.isServerless
);
const transformManager = new DefaultTransformManager(
transformGenerators,
scopedClusterClient,
logger
);
const summaryTransformManager = new DefaultSummaryTransformManager(
new DefaultSummaryTransformGenerator(),
scopedClusterClient,
Expand Down Expand Up @@ -218,14 +205,17 @@ const updateSLORoute = createSloServerRoute({
const soClient = core.savedObjects.client;
const dataViewsService = await dataViews.dataViewsServiceFactory(soClient, esClient);
const repository = new KibanaSavedObjectsSLORepository(soClient, logger);
const transformManager = new DefaultTransformManager(
transformGenerators,
scopedClusterClient,
logger,

const transformGenerators = createTransformGenerators(
spaceId,
dataViewsService,
sloContext.isServerless
);
const transformManager = new DefaultTransformManager(
transformGenerators,
scopedClusterClient,
logger
);
const summaryTransformManager = new DefaultSummaryTransformManager(
new DefaultSummaryTransformGenerator(),
scopedClusterClient,
Expand Down Expand Up @@ -271,14 +261,16 @@ const deleteSLORoute = createSloServerRoute({

const dataViewsService = await dataViews.dataViewsServiceFactory(soClient, esClient);

const transformGenerators = createTransformGenerators(
spaceId,
dataViewsService,
sloContext.isServerless
);
const repository = new KibanaSavedObjectsSLORepository(soClient, logger);
const transformManager = new DefaultTransformManager(
transformGenerators,
scopedClusterClient,
logger,
spaceId,
dataViewsService,
sloContext.isServerless
logger
);

const summaryTransformManager = new DefaultSummaryTransformManager(
Expand Down Expand Up @@ -346,14 +338,18 @@ const enableSLORoute = createSloServerRoute({
const esClient = core.elasticsearch.client.asCurrentUser;
const dataViewsService = await dataViews.dataViewsServiceFactory(soClient, esClient);
const repository = new KibanaSavedObjectsSLORepository(soClient, logger);
const transformManager = new DefaultTransformManager(
transformGenerators,
scopedClusterClient,
logger,

const transformGenerators = createTransformGenerators(
spaceId,
dataViewsService,
sloContext.isServerless
);

const transformManager = new DefaultTransformManager(
transformGenerators,
scopedClusterClient,
logger
);
const summaryTransformManager = new DefaultSummaryTransformManager(
new DefaultSummaryTransformGenerator(),
scopedClusterClient,
Expand Down Expand Up @@ -388,14 +384,17 @@ const disableSLORoute = createSloServerRoute({
const esClient = core.elasticsearch.client.asCurrentUser;
const dataViewsService = await dataViews.dataViewsServiceFactory(soClient, esClient);
const repository = new KibanaSavedObjectsSLORepository(soClient, logger);
const transformManager = new DefaultTransformManager(
transformGenerators,
scopedClusterClient,
logger,

const transformGenerators = createTransformGenerators(
spaceId,
dataViewsService,
sloContext.isServerless
);
const transformManager = new DefaultTransformManager(
transformGenerators,
scopedClusterClient,
logger
);
const summaryTransformManager = new DefaultSummaryTransformManager(
new DefaultSummaryTransformGenerator(),
scopedClusterClient,
Expand Down Expand Up @@ -430,14 +429,17 @@ const resetSLORoute = createSloServerRoute({

const dataViewsService = await dataViews.dataViewsServiceFactory(soClient, esClient);
const repository = new KibanaSavedObjectsSLORepository(soClient, logger);
const transformManager = new DefaultTransformManager(
transformGenerators,
scopedClusterClient,
logger,

const transformGenerators = createTransformGenerators(
spaceId,
dataViewsService,
sloContext.isServerless
);
const transformManager = new DefaultTransformManager(
transformGenerators,
scopedClusterClient,
logger
);
const summaryTransformManager = new DefaultSummaryTransformManager(
new DefaultSummaryTransformGenerator(),
scopedClusterClient,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* 2.0.
*/

import { dataViewsService } from '@kbn/data-views-plugin/server/mocks';
import { ALL_VALUE } from '@kbn/slo-schema';
import { twoMinute } from '../fixtures/duration';
import {
Expand All @@ -13,15 +14,14 @@ import {
createSLOWithTimeslicesBudgetingMethod,
} from '../fixtures/slo';
import { ApmTransactionDurationTransformGenerator } from './apm_transaction_duration';
import { dataViewsService } from '@kbn/data-views-plugin/server/mocks';

const generator = new ApmTransactionDurationTransformGenerator();
const spaceId = 'custom-space';
const SPACE_ID = 'custom-space';
const generator = new ApmTransactionDurationTransformGenerator(SPACE_ID, dataViewsService);

describe('APM Transaction Duration Transform Generator', () => {
it('returns the expected transform params with every specified indicator params', async () => {
const slo = createSLO({ id: 'irrelevant', indicator: createAPMTransactionDurationIndicator() });
const transform = await generator.getTransformParams(slo, spaceId, dataViewsService);
const transform = await generator.getTransformParams(slo);

expect(transform).toMatchSnapshot();
});
Expand All @@ -31,7 +31,7 @@ describe('APM Transaction Duration Transform Generator', () => {
id: 'irrelevant',
indicator: createAPMTransactionDurationIndicator(),
});
const transform = await generator.getTransformParams(slo, spaceId, dataViewsService);
const transform = await generator.getTransformParams(slo);

expect(transform).toMatchSnapshot();
});
Expand All @@ -46,7 +46,7 @@ describe('APM Transaction Duration Transform Generator', () => {
timesliceWindow: twoMinute(),
},
});
const transform = await generator.getTransformParams(slo, spaceId, dataViewsService);
const transform = await generator.getTransformParams(slo);

expect(transform).toMatchSnapshot();
});
Expand All @@ -60,7 +60,7 @@ describe('APM Transaction Duration Transform Generator', () => {
transactionType: ALL_VALUE,
}),
});
const transform = await generator.getTransformParams(slo, spaceId, dataViewsService);
const transform = await generator.getTransformParams(slo);

expect(transform.source.query).toMatchSnapshot();
});
Expand All @@ -72,7 +72,7 @@ describe('APM Transaction Duration Transform Generator', () => {
index,
}),
});
const transform = await generator.getTransformParams(slo, spaceId, dataViewsService);
const transform = await generator.getTransformParams(slo);

expect(transform.source.index).toEqual(index);
});
Expand All @@ -84,7 +84,7 @@ describe('APM Transaction Duration Transform Generator', () => {
filter,
}),
});
const transform = await generator.getTransformParams(slo, spaceId, dataViewsService);
const transform = await generator.getTransformParams(slo);

expect(transform.source.query).toMatchSnapshot();
});
Expand All @@ -99,7 +99,7 @@ describe('APM Transaction Duration Transform Generator', () => {
}),
});

const transform = await generator.getTransformParams(slo, spaceId, dataViewsService);
const transform = await generator.getTransformParams(slo);

expect(transform.source.query).toMatchSnapshot();
expect(transform.pivot?.group_by).toMatchSnapshot();
Expand All @@ -115,7 +115,7 @@ describe('APM Transaction Duration Transform Generator', () => {
}),
});

const transform = await generator.getTransformParams(slo, spaceId, dataViewsService);
const transform = await generator.getTransformParams(slo);

expect(transform.source.query).toMatchSnapshot();
expect(transform.pivot?.group_by).toMatchSnapshot();
Expand All @@ -131,7 +131,7 @@ describe('APM Transaction Duration Transform Generator', () => {
}),
});

const transform = await generator.getTransformParams(slo, spaceId, dataViewsService);
const transform = await generator.getTransformParams(slo);

expect(transform.source.query).toMatchSnapshot();
expect(transform.pivot?.group_by).toMatchSnapshot();
Expand All @@ -147,7 +147,7 @@ describe('APM Transaction Duration Transform Generator', () => {
}),
});

const transform = await generator.getTransformParams(slo, spaceId, dataViewsService);
const transform = await generator.getTransformParams(slo);

expect(transform.source.query).toMatchSnapshot();
expect(transform.pivot?.group_by).toMatchSnapshot();
Expand All @@ -163,7 +163,7 @@ describe('APM Transaction Duration Transform Generator', () => {
},
});

const transform = await generator.getTransformParams(slo, spaceId, dataViewsService);
const transform = await generator.getTransformParams(slo);

// @ts-ignore
const rangeFilter = transform.source.query.bool.filter.find((f) => 'range' in f);
Expand Down
Loading

0 comments on commit 2046663

Please sign in to comment.