Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.16] fix(slo): Override transform _id to ensure uniqness (#198610) #198699

Merged
merged 1 commit into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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