Skip to content

Commit

Permalink
🌊 Refactor APIs to follow Elasticsearch conventions (#204671)
Browse files Browse the repository at this point in the history
## Summary

This PR refactors the API by creating a new packaged called
`@kbn/streams-schema` where you can find all the Zod types along with
some type guards. I've also updated all the API's and calls to use the
new schemas.

---------

Co-authored-by: kibanamachine <[email protected]>
Co-authored-by: Joe Reuter <[email protected]>
  • Loading branch information
3 people authored Dec 23, 2024
1 parent 34bc507 commit 5ae53d4
Show file tree
Hide file tree
Showing 90 changed files with 1,486 additions and 613 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,7 @@ x-pack/packages/kbn-ai-assistant @elastic/search-kibana
x-pack/packages/kbn-alerting-comparators @elastic/response-ops
x-pack/packages/kbn-alerting-state-types @elastic/response-ops
x-pack/packages/kbn-random-sampling @elastic/kibana-visualizations
x-pack/packages/kbn-streams-schema @elastic/streams-program-team
x-pack/packages/kbn-synthetics-private-location @elastic/obs-ux-management-team
x-pack/packages/maps/vector_tile_utils @elastic/kibana-presentation
x-pack/packages/observability/observability_utils/observability_utils_browser @elastic/observability-ui
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,7 @@
"@kbn/std": "link:packages/kbn-std",
"@kbn/streams-app-plugin": "link:x-pack/solutions/observability/plugins/streams_app",
"@kbn/streams-plugin": "link:x-pack/solutions/observability/plugins/streams",
"@kbn/streams-schema": "link:x-pack/packages/kbn-streams-schema",
"@kbn/synthetics-plugin": "link:x-pack/solutions/observability/plugins/synthetics",
"@kbn/synthetics-private-location": "link:x-pack/packages/kbn-synthetics-private-location",
"@kbn/task-manager-fixture-plugin": "link:x-pack/test/alerting_api_integration/common/plugins/task_manager_fixture",
Expand Down
2 changes: 2 additions & 0 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -1878,6 +1878,8 @@
"@kbn/streams-app-plugin/*": ["x-pack/solutions/observability/plugins/streams_app/*"],
"@kbn/streams-plugin": ["x-pack/solutions/observability/plugins/streams"],
"@kbn/streams-plugin/*": ["x-pack/solutions/observability/plugins/streams/*"],
"@kbn/streams-schema": ["x-pack/packages/kbn-streams-schema"],
"@kbn/streams-schema/*": ["x-pack/packages/kbn-streams-schema/*"],
"@kbn/synthetics-e2e": ["x-pack/solutions/observability/plugins/synthetics/e2e"],
"@kbn/synthetics-e2e/*": ["x-pack/solutions/observability/plugins/synthetics/e2e/*"],
"@kbn/synthetics-plugin": ["x-pack/solutions/observability/plugins/synthetics"],
Expand Down
3 changes: 3 additions & 0 deletions x-pack/packages/kbn-streams-schema/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# @kbn/streams-schema

This shared package contains the Zod schema definition for the Streams project.
10 changes: 10 additions & 0 deletions x-pack/packages/kbn-streams-schema/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* 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 './src/apis';
export * from './src/models';
export * from './src/helpers';
12 changes: 12 additions & 0 deletions x-pack/packages/kbn-streams-schema/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* 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.
*/

module.exports = {
preset: '@kbn/test',
rootDir: '../../..',
roots: ['<rootDir>/x-pack/packages/kbn-streams-schema'],
};
8 changes: 8 additions & 0 deletions x-pack/packages/kbn-streams-schema/kibana.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"type": "shared-common",
"id": "@kbn/streams-schema",
"owner": "@elastic/streams-program-team",
"group": "observability",
"visibility": "shared"
}

7 changes: 7 additions & 0 deletions x-pack/packages/kbn-streams-schema/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "@kbn/streams-schema",
"description": "Streams Zod schema definition and common models shared between public and server.",
"private": true,
"version": "1.0.0",
"license": "Elastic License 2.0"
}

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

9 changes: 9 additions & 0 deletions x-pack/packages/kbn-streams-schema/src/apis/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* 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 './read_streams_response';
export * from './list_streams_response';
Original file line number Diff line number Diff line change
@@ -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 { z } from '@kbn/zod';
import { streamDefintionSchema } from '../models';

export const listStreamsResponseSchema = z.object({
streams: z.array(streamDefintionSchema),
});

export type ListStreamsResponse = z.infer<typeof listStreamsResponseSchema>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* 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 { readStreamResponse } from '../fixtures/read_streams_response';
import { readStreamResponseSchema } from './read_streams_response';

describe('ReadStreamResponse', () => {
it('should successfully parse', () => {
expect(readStreamResponseSchema.parse(readStreamResponse)).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* 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 { z } from '@kbn/zod';
import { readStreamDefinitonSchema } from '../models';

export const readStreamResponseSchema = z.object({
streams: z.array(readStreamDefinitonSchema),
});

export type ReadStreamResponse = z.infer<typeof readStreamResponseSchema>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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 { ingestStream } from './ingest_stream';

export const ingestReadStream = {
...ingestStream,
inherited_fields: {
'@timestamp': {
type: 'date',
from: 'logs',
},
message: {
type: 'match_only_text',
from: 'logs',
},
},
};
14 changes: 14 additions & 0 deletions x-pack/packages/kbn-streams-schema/src/fixtures/ingest_stream.ts
Original file line number Diff line number Diff line change
@@ -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 { ingestStreamConfig } from './ingest_stream_config';

export const ingestStream = {
name: 'logs.nginx',
elasticsearch_assets: [],
stream: ingestStreamConfig,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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 const ingestStreamConfig = {
ingest: {
processing: [
{
config: {
grok: {
field: 'message',
patterns: ['%{TIMESTAMP_ISO8601:event.timestamp} %{GREEDY:rest}'],
},
},
condition: {
field: 'log.level',
operator: 'eq',
value: 'error',
},
},
],
routing: [
{
name: 'logs.errors',
condition: {
field: 'log.level',
operator: 'eq',
value: 'error',
},
},
],
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* 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 { ingestReadStream } from './ingest_read_stream';
import { wiredReadStream } from './wired_read_stream';

export const readStreamResponse = {
streams: [wiredReadStream, ingestReadStream],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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 { wiredStream } from './wired_stream';

export const wiredReadStream = {
...wiredStream,
inherited_fields: {
'@timestamp': {
type: 'date',
from: 'logs',
},
message: {
type: 'match_only_text',
from: 'logs',
},
},
};
14 changes: 14 additions & 0 deletions x-pack/packages/kbn-streams-schema/src/fixtures/wired_stream.ts
Original file line number Diff line number Diff line change
@@ -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 { wiredStreamConfig } from './wired_stream_config';

export const wiredStream = {
name: 'logs.nginx',
elasticsearch_assets: [],
stream: wiredStreamConfig,
};
Loading

0 comments on commit 5ae53d4

Please sign in to comment.