Skip to content

Commit

Permalink
fix(slo): Override transform _id to ensure uniqness (#198610)
Browse files Browse the repository at this point in the history
(cherry picked from commit 98db4d6)
  • Loading branch information
kdelemme committed Nov 1, 2024
1 parent 83ac80c commit 9255fae
Show file tree
Hide file tree
Showing 9 changed files with 248 additions and 166 deletions.
31 changes: 30 additions & 1 deletion x-pack/packages/kbn-slo-schema/src/schema/slo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import * as t from 'io-ts';
import { Either } from 'fp-ts/Either';
import { allOrAnyStringOrArray, dateType } from './common';
import { durationType } from './duration';
import { indicatorSchema } from './indicators';
Expand Down Expand Up @@ -36,7 +37,35 @@ const groupBySchema = allOrAnyStringOrArray;

const optionalSettingsSchema = t.partial({ ...settingsSchema.props });
const tagsSchema = t.array(t.string);
const sloIdSchema = t.string;
// id cannot contain special characters and spaces
const sloIdSchema = new t.Type<string, string, unknown>(
'sloIdSchema',
t.string.is,
(input, context): Either<t.Errors, string> => {
if (typeof input === 'string') {
const valid = isValidId(input);
if (!valid) {
return t.failure(
input,
context,
'Invalid slo id, must be between 8 and 48 characters and contain only letters, numbers, hyphens, and underscores'
);
}

return t.success(input);
} else {
return t.failure(input, context);
}
},
t.identity
);

function isValidId(id: string): boolean {
const MIN_ID_LENGTH = 8;
const MAX_ID_LENGTH = 48;
const validLength = MIN_ID_LENGTH <= id.length && id.length <= MAX_ID_LENGTH;
return validLength && /^[a-z0-9-_]+$/.test(id);
}

const sloDefinitionSchema = t.type({
id: sloIdSchema,
Expand Down
2 changes: 1 addition & 1 deletion x-pack/packages/kbn-slo-schema/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
],
"kbn_references": [
"@kbn/std",
"@kbn/io-ts-utils"
"@kbn/io-ts-utils",
],
"exclude": [
"target/**/*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ export const getSLOPipelineTemplate = (slo: SLODefinition) => ({
id: getSLOPipelineId(slo.id, slo.revision),
description: `Ingest pipeline for SLO rollup data [id: ${slo.id}, revision: ${slo.revision}]`,
processors: [
{
set: {
field: '_id',
value: `{{{_id}}}-${slo.id}-${slo.revision}`,
},
},
{
set: {
field: 'event.ingested',
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.

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.

Loading

0 comments on commit 9255fae

Please sign in to comment.