-
Notifications
You must be signed in to change notification settings - Fork 739
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 all commits
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
93 changes: 93 additions & 0 deletions
93
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,93 @@ | ||
| /*! | ||
| * 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, ToolkitError } 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() | ||
| try { | ||
| return this.liveTailClient.cwlClient.send(command, { | ||
| abortSignal: this.liveTailClient.abortController.signal, | ||
| }) | ||
| } catch (e) { | ||
| throw new ToolkitError('Encountered error while trying to start LiveTail session.') | ||
| } | ||
| } | ||
|
|
||
| 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, | ||
| }) | ||
| } | ||
| } | ||
48 changes: 48 additions & 0 deletions
48
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,48 @@ | ||
| /*! | ||
| * 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' | ||
| import { NestedMap } from '../../../shared/utilities/map' | ||
|
|
||
| export class LiveTailSessionRegistry extends NestedMap<vscode.Uri, LiveTailSession> { | ||
| static #instance: LiveTailSessionRegistry | ||
|
|
||
| public static get instance() { | ||
| return (this.#instance ??= new this()) | ||
| } | ||
|
|
||
| public constructor() { | ||
| super() | ||
| } | ||
|
|
||
| protected override hash(uri: vscode.Uri): string { | ||
| return uri.toString() | ||
| } | ||
|
|
||
| protected override get name(): string { | ||
| return LiveTailSessionRegistry.name | ||
| } | ||
|
|
||
| protected override get default(): LiveTailSession { | ||
| throw new ToolkitError('No LiveTailSession found for provided uri.') | ||
| } | ||
| } | ||
|
|
||
| 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
136 changes: 136 additions & 0 deletions
136
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,136 @@ | ||
| /*! | ||
| * 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' | ||
| import { CLOUDWATCH_LOGS_LIVETAIL_SCHEME } from '../../../../shared/constants' | ||
|
|
||
| /** | ||
| * Exposes protected methods so we can test them | ||
| */ | ||
| class TestLiveTailSessionRegistry extends LiveTailSessionRegistry { | ||
| constructor() { | ||
| super() | ||
| } | ||
|
|
||
| override hash(uri: vscode.Uri): string { | ||
| return super.hash(uri) | ||
| } | ||
|
|
||
| override get default(): LiveTailSession { | ||
| return super.default | ||
| } | ||
| } | ||
|
|
||
| describe('LiveTailRegistry', async function () { | ||
| const session = new LiveTailSession({ | ||
| logGroupName: 'test-log-group', | ||
| region: 'test-region', | ||
| }) | ||
|
|
||
| let liveTailSessionRegistry: TestLiveTailSessionRegistry | ||
|
|
||
| beforeEach(function () { | ||
| liveTailSessionRegistry = new TestLiveTailSessionRegistry() | ||
| }) | ||
|
|
||
| it('hash()', function () { | ||
| assert.deepStrictEqual(liveTailSessionRegistry.hash(session.uri), session.uri.toString()) | ||
| }) | ||
|
|
||
| it('default()', function () { | ||
| assert.throws(() => liveTailSessionRegistry.default) | ||
| }) | ||
| }) | ||
|
|
||
| describe('LiveTailSession URI', async function () { | ||
| const testLogGroupName = 'test-log-group' | ||
| const testRegion = 'test-region' | ||
| const expectedUriBase = `${CLOUDWATCH_LOGS_LIVETAIL_SCHEME}:${testRegion}:${testLogGroupName}` | ||
|
|
||
| it('is correct with no logStream filter, no filter pattern', function () { | ||
| const config: LiveTailSessionConfiguration = { | ||
| logGroupName: testLogGroupName, | ||
| region: testRegion, | ||
| } | ||
| const expectedUri = vscode.Uri.parse(expectedUriBase) | ||
| const uri = createLiveTailURIFromArgs(config) | ||
| assert.deepEqual(uri, expectedUri) | ||
| }) | ||
|
|
||
| it('is correct with no logStream filter, with filter pattern', function () { | ||
| const config: LiveTailSessionConfiguration = { | ||
| logGroupName: testLogGroupName, | ||
| region: testRegion, | ||
| logEventFilterPattern: 'test-filter', | ||
| } | ||
| const expectedUri = vscode.Uri.parse(`${expectedUriBase}:test-filter`) | ||
| const uri = createLiveTailURIFromArgs(config) | ||
| assert.deepEqual(uri, expectedUri) | ||
| }) | ||
|
|
||
| it('is correct with ALL logStream filter', function () { | ||
| const config: LiveTailSessionConfiguration = { | ||
| logGroupName: testLogGroupName, | ||
| region: testRegion, | ||
| logStreamFilter: { | ||
| type: 'all', | ||
| }, | ||
| } | ||
| const expectedUri = vscode.Uri.parse(`${expectedUriBase}:all`) | ||
| const uri = createLiveTailURIFromArgs(config) | ||
| assert.deepEqual(uri, expectedUri) | ||
| }) | ||
|
|
||
| it('is correct with prefix logStream filter', function () { | ||
| const config: LiveTailSessionConfiguration = { | ||
| logGroupName: testLogGroupName, | ||
| region: testRegion, | ||
| logStreamFilter: { | ||
| type: 'prefix', | ||
| filter: 'test-prefix', | ||
| }, | ||
| } | ||
| const expectedUri = vscode.Uri.parse(`${expectedUriBase}:prefix:test-prefix`) | ||
| const uri = createLiveTailURIFromArgs(config) | ||
| assert.deepEqual(uri, expectedUri) | ||
| }) | ||
|
|
||
| it('is correct with specific logStream filter', function () { | ||
| const config: LiveTailSessionConfiguration = { | ||
| logGroupName: testLogGroupName, | ||
| region: testRegion, | ||
| logStreamFilter: { | ||
| type: 'specific', | ||
| filter: 'test-stream', | ||
| }, | ||
| } | ||
| const expectedUri = vscode.Uri.parse(`${expectedUriBase}: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: testLogGroupName, | ||
| region: testRegion, | ||
| logStreamFilter: { | ||
| type: 'specific', | ||
| filter: 'test-stream', | ||
| }, | ||
| logEventFilterPattern: 'test-filter', | ||
| } | ||
| const expectedUri = vscode.Uri.parse(`${expectedUriBase}: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.
Uh oh!
There was an error while loading. Please reload this page.