chore(slo): refactor types (#140856)

This commit is contained in:
Kevin Delemme 2022-09-23 11:46:25 -04:00 committed by GitHub
parent 1b496c5eaa
commit 51c56345aa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 176 additions and 102 deletions

View file

@ -18,7 +18,7 @@ import {
TransformGenerator,
} from '../../services/slo/transform_generators';
import { SLITypes } from '../../types/models';
import { createSLOParamsSchema, deleteSLOParamsSchema } from '../../types/schema';
import { createSLOParamsSchema, deleteSLOParamsSchema } from '../../types/rest_specs';
import { createObservabilityServerRoute } from '../create_observability_server_route';
const transformGenerators: Record<SLITypes, TransformGenerator> = {

View file

@ -11,7 +11,7 @@ import { SLO } from '../../types/models';
import { ResourceInstaller } from './resource_installer';
import { SLORepository } from './slo_repository';
import { TransformManager } from './transform_manager';
import { CreateSLOParams, CreateSLOResponse } from '../../types/schema';
import { CreateSLOParams, CreateSLOResponse } from '../../types/rest_specs';
export class CreateSLO {
constructor(

View file

@ -7,7 +7,7 @@
import uuid from 'uuid';
import { SLI, SLO } from '../../../types/models';
import { CreateSLOParams } from '../../../types/schema';
import { CreateSLOParams } from '../../../types/rest_specs';
const commonSLO: Omit<CreateSLOParams, 'indicator'> = {
name: 'irrelevant',

View file

@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`APM Transaction Duration Transform Generator does not include the query filter when params are 'ALL' 1`] = `
exports[`APM Transaction Duration Transform Generator does not include the query filter when params are '*' 1`] = `
Object {
"bool": Object {
"filter": Array [

View file

@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`APM Transaction Error Rate Transform Generator does not include the query filter when params are 'ALL' 1`] = `
exports[`APM Transaction Error Rate Transform Generator does not include the query filter when params are '*' 1`] = `
Object {
"bool": Object {
"filter": Array [

View file

@ -25,13 +25,13 @@ describe('APM Transaction Duration Transform Generator', () => {
});
});
it("does not include the query filter when params are 'ALL'", async () => {
it("does not include the query filter when params are '*'", async () => {
const anSLO = createSLO(
createAPMTransactionDurationIndicator({
environment: 'ALL',
service: 'ALL',
transaction_name: 'ALL',
transaction_type: 'ALL',
environment: '*',
service: '*',
transaction_name: '*',
transaction_type: '*',
})
);
const transform = generator.getTransformParams(anSLO, 'my-namespace');

View file

@ -10,6 +10,7 @@ import {
MappingRuntimeFieldType,
TransformPutTransformRequest,
} from '@elastic/elasticsearch/lib/api/types';
import { ALL_VALUE } from '../../../types/schema';
import {
getSLODestinationIndexName,
getSLOIngestPipelineName,
@ -21,7 +22,6 @@ import {
apmTransactionDurationSLOSchema,
APMTransactionDurationSLO,
} from '../../../types/models';
import { ALL_VALUE } from '../../../types/schema';
import { TransformGenerator } from '.';
const APM_SOURCE_INDEX = 'metrics-apm*';

View file

@ -32,13 +32,13 @@ describe('APM Transaction Error Rate Transform Generator', () => {
expect(transform.pivot?.aggregations).toMatchSnapshot();
});
it("does not include the query filter when params are 'ALL'", async () => {
it("does not include the query filter when params are '*'", async () => {
const anSLO = createSLO(
createAPMTransactionErrorRateIndicator({
environment: 'ALL',
service: 'ALL',
transaction_name: 'ALL',
transaction_type: 'ALL',
environment: '*',
service: '*',
transaction_name: '*',
transaction_type: '*',
})
);
const transform = generator.getTransformParams(anSLO, 'my-namespace');

View file

@ -10,6 +10,7 @@ import {
MappingRuntimeFieldType,
TransformPutTransformRequest,
} from '@elastic/elasticsearch/lib/api/types';
import { ALL_VALUE } from '../../../types/schema';
import { getSLOTransformTemplate } from '../../../assets/transform_templates/slo_transform_template';
import { TransformGenerator } from '.';
import {
@ -22,7 +23,6 @@ import {
APMTransactionErrorRateSLO,
SLO,
} from '../../../types/models';
import { ALL_VALUE } from '../../../types/schema';
const APM_SOURCE_INDEX = 'metrics-apm*';
const ALLOWED_STATUS_CODES = ['2xx', '3xx', '4xx', '5xx'];

View file

@ -6,47 +6,51 @@
*/
import * as t from 'io-ts';
import {
apmTransactionDurationIndicatorSchema,
apmTransactionErrorRateIndicatorSchema,
indicatorSchema,
indicatorTypesSchema,
rollingTimeWindowSchema,
commonSLOSchema,
} from '../schema';
const baseSLOSchema = t.type({
id: t.string,
name: t.string,
description: t.string,
time_window: rollingTimeWindowSchema,
indicator: indicatorSchema,
budgeting_method: t.literal('occurrences'),
objective: t.type({
target: t.number,
const sloSchema = t.intersection([
commonSLOSchema,
t.type({
id: t.string,
}),
});
]);
export const apmTransactionErrorRateSLOSchema = t.intersection([
baseSLOSchema,
const apmTransactionErrorRateSLOSchema = t.intersection([
sloSchema,
t.type({ indicator: apmTransactionErrorRateIndicatorSchema }),
]);
export const apmTransactionDurationSLOSchema = t.intersection([
baseSLOSchema,
const apmTransactionDurationSLOSchema = t.intersection([
sloSchema,
t.type({ indicator: apmTransactionDurationIndicatorSchema }),
]);
const storedSLOSchema = t.intersection([
baseSLOSchema,
sloSchema,
t.type({ created_at: t.string, updated_at: t.string }),
]);
export type SLO = t.TypeOf<typeof baseSLOSchema>;
export type APMTransactionErrorRateSLO = t.TypeOf<typeof apmTransactionErrorRateSLOSchema>;
export type APMTransactionDurationSLO = t.TypeOf<typeof apmTransactionDurationSLOSchema>;
type SLO = t.TypeOf<typeof sloSchema>;
type APMTransactionErrorRateSLO = t.TypeOf<typeof apmTransactionErrorRateSLOSchema>;
type APMTransactionDurationSLO = t.TypeOf<typeof apmTransactionDurationSLOSchema>;
export type SLI = t.TypeOf<typeof indicatorSchema>;
export type SLITypes = t.TypeOf<typeof indicatorTypesSchema>;
type SLI = t.TypeOf<typeof indicatorSchema>;
type SLITypes = t.TypeOf<typeof indicatorTypesSchema>;
export type StoredSLO = t.TypeOf<typeof storedSLOSchema>;
type StoredSLO = t.TypeOf<typeof storedSLOSchema>;
export { apmTransactionDurationSLOSchema, apmTransactionErrorRateSLOSchema };
export type {
SLO,
APMTransactionErrorRateSLO,
APMTransactionDurationSLO,
SLI,
SLITypes,
StoredSLO,
};

View file

@ -0,0 +1,8 @@
/*
* 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.
*/
export * from './slo';

View file

@ -0,0 +1,38 @@
/*
* 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 * as t from 'io-ts';
import { commonSLOSchema } from '../schema';
const createSLOBodySchema = t.intersection([
commonSLOSchema,
t.partial({
settings: t.partial({
destination_index: t.string,
}),
}),
]);
const createSLOParamsSchema = t.type({
body: createSLOBodySchema,
});
const createSLOResponseSchema = t.type({
id: t.string,
});
const deleteSLOParamsSchema = t.type({
path: t.type({
id: t.string,
}),
});
type CreateSLOParams = t.TypeOf<typeof createSLOBodySchema>;
type CreateSLOResponse = t.TypeOf<typeof createSLOResponseSchema>;
export { createSLOParamsSchema, deleteSLOParamsSchema };
export type { CreateSLOParams, CreateSLOResponse };

View file

@ -0,0 +1,14 @@
/*
* 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 * as t from 'io-ts';
const ALL_VALUE = '*';
const allOrAnyString = t.union([t.literal(ALL_VALUE), t.string]);
export { allOrAnyString, ALL_VALUE };

View file

@ -6,3 +6,5 @@
*/
export * from './slo';
export * from './common';
export * from './indicators';

View file

@ -0,0 +1,60 @@
/*
* 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 * as t from 'io-ts';
import { allOrAnyString } from './common';
const apmTransactionDurationIndicatorTypeSchema = t.literal('slo.apm.transaction_duration');
const apmTransactionDurationIndicatorSchema = t.type({
type: apmTransactionDurationIndicatorTypeSchema,
params: t.type({
environment: allOrAnyString,
service: allOrAnyString,
transaction_type: allOrAnyString,
transaction_name: allOrAnyString,
'threshold.us': t.number,
}),
});
const apmTransactionErrorRateIndicatorTypeSchema = t.literal('slo.apm.transaction_error_rate');
const apmTransactionErrorRateIndicatorSchema = t.type({
type: apmTransactionErrorRateIndicatorTypeSchema,
params: t.intersection([
t.type({
environment: allOrAnyString,
service: allOrAnyString,
transaction_type: allOrAnyString,
transaction_name: allOrAnyString,
}),
t.partial({
good_status_codes: t.array(
t.union([t.literal('2xx'), t.literal('3xx'), t.literal('4xx'), t.literal('5xx')])
),
}),
]),
});
const indicatorTypesSchema = t.union([
apmTransactionDurationIndicatorTypeSchema,
apmTransactionErrorRateIndicatorTypeSchema,
]);
const indicatorSchema = t.union([
apmTransactionDurationIndicatorSchema,
apmTransactionErrorRateIndicatorSchema,
]);
export {
apmTransactionDurationIndicatorSchema,
apmTransactionDurationIndicatorTypeSchema,
apmTransactionErrorRateIndicatorSchema,
apmTransactionErrorRateIndicatorTypeSchema,
indicatorSchema,
indicatorTypesSchema,
};

View file

@ -7,78 +7,26 @@
import * as t from 'io-ts';
export const ALL_VALUE = 'ALL';
const allOrAnyString = t.union([t.literal(ALL_VALUE), t.string]);
import { indicatorSchema } from './indicators';
const apmTransactionDurationIndicatorTypeSchema = t.literal('slo.apm.transaction_duration');
export const apmTransactionDurationIndicatorSchema = t.type({
type: apmTransactionDurationIndicatorTypeSchema,
params: t.type({
environment: allOrAnyString,
service: allOrAnyString,
transaction_type: allOrAnyString,
transaction_name: allOrAnyString,
'threshold.us': t.number,
}),
});
const apmTransactionErrorRateIndicatorTypeSchema = t.literal('slo.apm.transaction_error_rate');
export const apmTransactionErrorRateIndicatorSchema = t.type({
type: apmTransactionErrorRateIndicatorTypeSchema,
params: t.intersection([
t.type({
environment: allOrAnyString,
service: allOrAnyString,
transaction_type: allOrAnyString,
transaction_name: allOrAnyString,
}),
t.partial({
good_status_codes: t.array(
t.union([t.literal('2xx'), t.literal('3xx'), t.literal('4xx'), t.literal('5xx')])
),
}),
]),
});
export const rollingTimeWindowSchema = t.type({
const rollingTimeWindowSchema = t.type({
duration: t.string,
is_rolling: t.literal(true),
});
export const indicatorTypesSchema = t.union([
apmTransactionDurationIndicatorTypeSchema,
apmTransactionErrorRateIndicatorTypeSchema,
]);
const budgetingMethodSchema = t.literal('occurrences');
export const indicatorSchema = t.union([
apmTransactionDurationIndicatorSchema,
apmTransactionErrorRateIndicatorSchema,
]);
const objectiveSchema = t.type({
target: t.number,
});
const createSLOBodySchema = t.type({
const commonSLOSchema = t.type({
name: t.string,
description: t.string,
indicator: indicatorSchema,
time_window: rollingTimeWindowSchema,
budgeting_method: t.literal('occurrences'),
objective: t.type({
target: t.number,
}),
budgeting_method: budgetingMethodSchema,
objective: objectiveSchema,
});
const createSLOResponseSchema = t.type({
id: t.string,
});
export type CreateSLOParams = t.TypeOf<typeof createSLOBodySchema>;
export type CreateSLOResponse = t.TypeOf<typeof createSLOResponseSchema>;
export const createSLOParamsSchema = t.type({
body: createSLOBodySchema,
});
export const deleteSLOParamsSchema = t.type({
path: t.type({
id: t.string,
}),
});
export { commonSLOSchema, rollingTimeWindowSchema, budgetingMethodSchema, objectiveSchema };