-
Notifications
You must be signed in to change notification settings - Fork 738
feat(cwl): Create LiveTailSession object and registry. Adds MaxLine configuration #5738
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
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
packages/core/src/awsService/cloudWatchLogs/registry/liveTailSession.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| /*! | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| import * as vscode from 'vscode' | ||
| import { CloudWatchLogsClient, StartLiveTailCommand, StartLiveTailCommandOutput } from '@aws-sdk/client-cloudwatch-logs' | ||
| import { LogStreamFilterResponse } from '../liveTailLogStreamSubmenu' | ||
| import { CloudWatchLogsSettings } from '../cloudWatchLogsUtils' | ||
| import { Settings } from '../../../shared' | ||
| import { createLiveTailURIFromArgs } from './liveTailSessionRegistry' | ||
|
|
||
| export type LiveTailSessionConfiguration = { | ||
| logGroupName: string | ||
| logStreamFilter?: LogStreamFilterResponse | ||
| logEventFilterPattern?: string | ||
| region: string | ||
| } | ||
|
|
||
| export type LiveTailSessionClient = { | ||
| cwlClient: CloudWatchLogsClient | ||
| abortController: AbortController | ||
| } | ||
|
|
||
| export class LiveTailSession { | ||
| private liveTailClient: LiveTailSessionClient | ||
| private _logGroupName: string | ||
| private logStreamFilter?: LogStreamFilterResponse | ||
| private logEventFilterPattern?: string | ||
| private _maxLines: number | ||
| private _uri: vscode.Uri | ||
|
|
||
| static settings = new CloudWatchLogsSettings(Settings.instance) | ||
|
|
||
| public constructor(configuration: LiveTailSessionConfiguration) { | ||
| this._logGroupName = configuration.logGroupName | ||
| this.logStreamFilter = configuration.logStreamFilter | ||
| this.liveTailClient = { | ||
| cwlClient: new CloudWatchLogsClient({ region: configuration.region }), | ||
| abortController: new AbortController(), | ||
| } | ||
| this._maxLines = LiveTailSession.settings.get('liveTailMaxEvents', 10000) | ||
| this._uri = createLiveTailURIFromArgs(configuration) | ||
| } | ||
|
|
||
| public get maxLines() { | ||
| return this._maxLines | ||
| } | ||
|
|
||
| public get uri() { | ||
| return this._uri | ||
| } | ||
|
|
||
| public get logGroupName() { | ||
| return this._logGroupName | ||
| } | ||
|
|
||
| public startLiveTailSession(): Promise<StartLiveTailCommandOutput> { | ||
keeganirby marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const command = this.buildStartLiveTailCommand() | ||
| return this.liveTailClient.cwlClient.send(command, { | ||
| abortSignal: this.liveTailClient.abortController.signal, | ||
| }) | ||
| } | ||
|
|
||
| public stopLiveTailSession() { | ||
| this.liveTailClient.abortController.abort() | ||
| this.liveTailClient.cwlClient.destroy() | ||
| } | ||
|
|
||
| private buildStartLiveTailCommand(): StartLiveTailCommand { | ||
| let logStreamNamePrefix = undefined | ||
| let logStreamName = undefined | ||
| if (this.logStreamFilter) { | ||
| if (this.logStreamFilter.type === 'prefix') { | ||
| logStreamNamePrefix = this.logStreamFilter.filter | ||
| logStreamName = undefined | ||
| } else if (this.logStreamFilter.type === 'specific') { | ||
| logStreamName = this.logStreamFilter.filter | ||
| logStreamNamePrefix = undefined | ||
| } | ||
| } | ||
|
|
||
| return new StartLiveTailCommand({ | ||
| logGroupIdentifiers: [this.logGroupName], | ||
| logStreamNamePrefixes: logStreamNamePrefix ? [logStreamNamePrefix] : undefined, | ||
| logStreamNames: logStreamName ? [logStreamName] : undefined, | ||
| logEventFilterPattern: this.logEventFilterPattern ? this.logEventFilterPattern : undefined, | ||
| }) | ||
| } | ||
| } | ||
60 changes: 60 additions & 0 deletions
60
packages/core/src/awsService/cloudWatchLogs/registry/liveTailSessionRegistry.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| /*! | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| import * as vscode from 'vscode' | ||
| import { CLOUDWATCH_LOGS_LIVETAIL_SCHEME } from '../../../shared/constants' | ||
| import { LiveTailSession, LiveTailSessionConfiguration } from './liveTailSession' | ||
| import { ToolkitError } from '../../../shared' | ||
|
|
||
| export class LiveTailSessionRegistry { | ||
keeganirby marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| static #instance: LiveTailSessionRegistry | ||
|
|
||
| public static get instance() { | ||
| return (this.#instance ??= new this()) | ||
| } | ||
|
|
||
| public constructor(private readonly registry: Map<string, LiveTailSession> = new Map()) {} | ||
|
|
||
| public registerLiveTailSession(session: LiveTailSession) { | ||
| if (this.doesRegistryContainLiveTailSession(session.uri)) { | ||
| throw new ToolkitError(`There is already a LiveTail session registered with uri: ${session.uri}`) | ||
| } | ||
| this.registry.set(this.uriToKey(session.uri), session) | ||
| } | ||
|
|
||
| public getLiveTailSessionFromUri(uri: vscode.Uri): LiveTailSession { | ||
| const session = this.registry.get(this.uriToKey(uri)) | ||
| if (!session) { | ||
| throw new ToolkitError(`No LiveTail session registered for uri: ${uri} found.`) | ||
| } | ||
| return session | ||
| } | ||
|
|
||
| public removeLiveTailSessionFromRegistry(uri: vscode.Uri) { | ||
| this.registry.delete(this.uriToKey(uri)) | ||
| } | ||
|
|
||
| public doesRegistryContainLiveTailSession(uri: vscode.Uri): boolean { | ||
| return this.registry.has(this.uriToKey(uri)) | ||
| } | ||
|
|
||
| private uriToKey(uri: vscode.Uri): string { | ||
| return uri.toString() | ||
| } | ||
| } | ||
|
|
||
| export function createLiveTailURIFromArgs(sessionData: LiveTailSessionConfiguration): vscode.Uri { | ||
| let uriStr = `${CLOUDWATCH_LOGS_LIVETAIL_SCHEME}:${sessionData.region}:${sessionData.logGroupName}` | ||
|
|
||
| if (sessionData.logStreamFilter) { | ||
| if (sessionData.logStreamFilter.type !== 'all') { | ||
| uriStr += `:${sessionData.logStreamFilter.type}:${sessionData.logStreamFilter.filter}` | ||
| } else { | ||
| uriStr += `:${sessionData.logStreamFilter.type}` | ||
| } | ||
| } | ||
| uriStr += sessionData.logEventFilterPattern ? `:${sessionData.logEventFilterPattern}` : '' | ||
|
|
||
| return vscode.Uri.parse(uriStr) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
packages/core/src/test/awsService/cloudWatchLogs/registry/liveTailRegistry.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| /*! | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| import * as vscode from 'vscode' | ||
| import assert from 'assert' | ||
| import { | ||
| LiveTailSession, | ||
| LiveTailSessionConfiguration, | ||
| } from '../../../../awsService/cloudWatchLogs/registry/liveTailSession' | ||
| import { | ||
| createLiveTailURIFromArgs, | ||
| LiveTailSessionRegistry, | ||
| } from '../../../../awsService/cloudWatchLogs/registry/liveTailSessionRegistry' | ||
|
|
||
| describe('LiveTailRegistry', async function () { | ||
| const session = new LiveTailSession({ | ||
| logGroupName: 'test-log-group', | ||
| region: 'test-region', | ||
| }) | ||
|
|
||
| let liveTailSessionRegistry: LiveTailSessionRegistry | ||
|
|
||
| beforeEach(function () { | ||
| liveTailSessionRegistry = new LiveTailSessionRegistry() | ||
| }) | ||
|
|
||
| it('returns LiveTailSession after setting it', async function () { | ||
| liveTailSessionRegistry.registerLiveTailSession(session) | ||
| const retrievedSession = liveTailSessionRegistry.getLiveTailSessionFromUri(session.uri) | ||
| assert.strictEqual(retrievedSession, session) | ||
| }) | ||
|
|
||
| it('contains returns true after setting session', async function () { | ||
| liveTailSessionRegistry.registerLiveTailSession(session) | ||
| const doesContain = liveTailSessionRegistry.doesRegistryContainLiveTailSession(session.uri) | ||
| assert.strictEqual(doesContain, true) | ||
| }) | ||
|
|
||
| it('contains returns false if session not set', async function () { | ||
| const doesContain = liveTailSessionRegistry.doesRegistryContainLiveTailSession(session.uri) | ||
| assert.strictEqual(doesContain, false) | ||
| }) | ||
|
|
||
| it('removeLiveTailSessionFromRegistry removes session from registry ', async function () { | ||
| liveTailSessionRegistry.registerLiveTailSession(session) | ||
| assert.strictEqual(liveTailSessionRegistry.doesRegistryContainLiveTailSession(session.uri), true) | ||
| liveTailSessionRegistry.removeLiveTailSessionFromRegistry(session.uri) | ||
| assert.strictEqual(liveTailSessionRegistry.doesRegistryContainLiveTailSession(session.uri), false) | ||
| }) | ||
|
|
||
| it('throws registering the same session twice', async function () { | ||
| liveTailSessionRegistry.registerLiveTailSession(session) | ||
| assert.throws(() => liveTailSessionRegistry.registerLiveTailSession(session)) | ||
| }) | ||
|
|
||
| it('throws cant find session', async function () { | ||
| assert.throws(() => liveTailSessionRegistry.getLiveTailSessionFromUri(session.uri)) | ||
| }) | ||
| }) | ||
|
|
||
| describe('LiveTailSession URI', async function () { | ||
| it('is correct with no logStream filter, no filter pattern', function () { | ||
| const config: LiveTailSessionConfiguration = { | ||
| logGroupName: 'test-log-group', | ||
| region: 'test-region', | ||
| } | ||
| const expectedUri = vscode.Uri.parse('aws-cwl-lt:test-region:test-log-group') | ||
keeganirby marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const uri = createLiveTailURIFromArgs(config) | ||
| assert.deepEqual(uri, expectedUri) | ||
| }) | ||
|
|
||
| it('is correct with no logStream filter, with filter pattern', function () { | ||
| const config: LiveTailSessionConfiguration = { | ||
| logGroupName: 'test-log-group', | ||
| region: 'test-region', | ||
| logEventFilterPattern: 'test-filter', | ||
| } | ||
| const expectedUri = vscode.Uri.parse('aws-cwl-lt:test-region:test-log-group:test-filter') | ||
| const uri = createLiveTailURIFromArgs(config) | ||
| assert.deepEqual(uri, expectedUri) | ||
| }) | ||
|
|
||
| it('is correct with ALL logStream filter', function () { | ||
| const config: LiveTailSessionConfiguration = { | ||
| logGroupName: 'test-log-group', | ||
| region: 'test-region', | ||
| logStreamFilter: { | ||
| type: 'all', | ||
| }, | ||
| } | ||
| const expectedUri = vscode.Uri.parse('aws-cwl-lt:test-region:test-log-group:all') | ||
| const uri = createLiveTailURIFromArgs(config) | ||
| assert.deepEqual(uri, expectedUri) | ||
| }) | ||
|
|
||
| it('is correct with prefix logStream filter', function () { | ||
| const config: LiveTailSessionConfiguration = { | ||
| logGroupName: 'test-log-group', | ||
| region: 'test-region', | ||
| logStreamFilter: { | ||
| type: 'prefix', | ||
| filter: 'test-prefix', | ||
| }, | ||
| } | ||
| const expectedUri = vscode.Uri.parse('aws-cwl-lt:test-region:test-log-group:prefix:test-prefix') | ||
| const uri = createLiveTailURIFromArgs(config) | ||
| assert.deepEqual(uri, expectedUri) | ||
| }) | ||
|
|
||
| it('is correct with specific logStream filter', function () { | ||
| const config: LiveTailSessionConfiguration = { | ||
| logGroupName: 'test-log-group', | ||
| region: 'test-region', | ||
| logStreamFilter: { | ||
| type: 'specific', | ||
| filter: 'test-stream', | ||
| }, | ||
| } | ||
| const expectedUri = vscode.Uri.parse('aws-cwl-lt:test-region:test-log-group:specific:test-stream') | ||
| const uri = createLiveTailURIFromArgs(config) | ||
| assert.deepEqual(uri, expectedUri) | ||
| }) | ||
|
|
||
| it('is correct with specific logStream filter and filter pattern', function () { | ||
| const config: LiveTailSessionConfiguration = { | ||
| logGroupName: 'test-log-group', | ||
| region: 'test-region', | ||
| logStreamFilter: { | ||
| type: 'specific', | ||
| filter: 'test-stream', | ||
| }, | ||
| logEventFilterPattern: 'test-filter', | ||
| } | ||
| const expectedUri = vscode.Uri.parse('aws-cwl-lt:test-region:test-log-group:specific:test-stream:test-filter') | ||
| const uri = createLiveTailURIFromArgs(config) | ||
| assert.deepEqual(uri, expectedUri) | ||
| }) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we able to update this to a slightly newer version? It looks like it created a bunch of churn in the package-lock.json since i'm guessing newer versions of the aws-sdk use different versions of smithy components, etc?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes there is a newer version, will push with a revision!