-
Notifications
You must be signed in to change notification settings - Fork 494
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
feat(lambda): Connect CW Live Tail to Lambda functions #6423
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,13 +28,13 @@ import { getLogger } from '../../shared/logger/logger' | |
import { ToolkitError } from '../../shared' | ||
import { LiveTailCodeLensProvider } from './document/liveTailCodeLensProvider' | ||
|
||
export const liveTailRegistry = LiveTailSessionRegistry.instance | ||
export const liveTailCodeLensProvider = new LiveTailCodeLensProvider(liveTailRegistry) | ||
export async function activate(context: vscode.ExtensionContext, configuration: Settings): Promise<void> { | ||
const registry = LogDataRegistry.instance | ||
const liveTailRegistry = LiveTailSessionRegistry.instance | ||
|
||
const documentProvider = new LogDataDocumentProvider(registry) | ||
const liveTailDocumentProvider = new LiveTailDocumentProvider() | ||
const liveTailCodeLensProvider = new LiveTailCodeLensProvider(liveTailRegistry) | ||
context.subscriptions.push( | ||
vscode.languages.registerCodeLensProvider( | ||
{ | ||
|
@@ -150,7 +150,7 @@ export async function activate(context: vscode.ExtensionContext, configuration: | |
) | ||
} | ||
|
||
function getFunctionLogGroupName(configuration: any) { | ||
export function getFunctionLogGroupName(configuration: any) { | ||
const logGroupPrefix = '/aws/lambda/' | ||
return configuration.logGroupName || logGroupPrefix + configuration.FunctionName | ||
return configuration.LoggingConfig?.LogGroup || logGroupPrefix + configuration.FunctionName | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this because SDK update? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's related. The code that was removed in the other file used to set up this |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
*/ | ||
|
||
import * as vscode from 'vscode' | ||
import { Lambda } from 'aws-sdk' | ||
import { deleteLambda } from './commands/deleteLambda' | ||
import { uploadLambdaCommand } from './commands/uploadLambda' | ||
import { LambdaFunctionNode } from './explorer/lambdaFunctionNode' | ||
|
@@ -18,6 +19,11 @@ import { copyLambdaUrl } from './commands/copyLambdaUrl' | |
import { ResourceNode } from '../awsService/appBuilder/explorer/nodes/resourceNode' | ||
import { isTreeNode, TreeNode } from '../shared/treeview/resourceTreeDataProvider' | ||
import { getSourceNode } from '../shared/utilities/treeNodeUtils' | ||
import { tailLogGroup } from '../awsService/cloudWatchLogs/commands/tailLogGroup' | ||
import { liveTailRegistry, liveTailCodeLensProvider } from '../awsService/cloudWatchLogs/activation' | ||
import { getFunctionLogGroupName } from '../awsService/cloudWatchLogs/activation' | ||
import { ToolkitError, isError } from '../shared' | ||
import { LogStreamFilterResponse } from '../awsService/cloudWatchLogs/wizard/liveTailLogStreamSubmenu' | ||
|
||
/** | ||
* Activates Lambda components. | ||
|
@@ -76,6 +82,40 @@ export async function activate(context: ExtContext): Promise<void> { | |
|
||
Commands.register('aws.launchDebugConfigForm', async (node: ResourceNode) => | ||
registerSamDebugInvokeVueCommand(context.extensionContext, { resource: node }) | ||
) | ||
), | ||
|
||
Commands.register('aws.appBuilder.tailLogs', async (node: LambdaFunctionNode | TreeNode) => { | ||
let functionConfiguration: Lambda.FunctionConfiguration | ||
try { | ||
const sourceNode = getSourceNode<LambdaFunctionNode>(node) | ||
functionConfiguration = sourceNode.configuration | ||
const logGroupInfo = { | ||
regionName: sourceNode.regionCode, | ||
groupName: getFunctionLogGroupName(functionConfiguration), | ||
} | ||
|
||
const source = isTreeNode(node) ? 'AppBuilder' : 'AwsExplorerLambdaNode' | ||
// Show all log streams without having to choose | ||
const logStreamFilterData: LogStreamFilterResponse = { type: 'all' } | ||
await tailLogGroup( | ||
liveTailRegistry, | ||
source, | ||
liveTailCodeLensProvider, | ||
logGroupInfo, | ||
logStreamFilterData | ||
) | ||
} catch (err) { | ||
if (isError(err as Error, 'ResourceNotFoundException', "LogGroup doesn't exist.")) { | ||
// If we caught this error, then we know `functionConfiguration` actually has a value | ||
throw ToolkitError.chain( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will we get this issue when we deploy the function for the first time, and hit taillog before invoking it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For I feel this should be a valid use case There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, and that's how the Live Tail API works. It needs an existing log group. We could eventually do something like try once and if it fails we retry (but we might keep retrying forever), but in practice it's better to surface the error, so customers understand it better and retry when needed. Live Tail use case is to "see and filter logs", so it's less likely that people want to use it a lot for a brand new function that doesn't have any invokes yet. |
||
err, | ||
`Unable to fetch logs. Log group for function '${functionConfiguration!.FunctionName}' does not exist. ` + | ||
'Invoking your function at least once will create the log group.' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice hint! Very helpful to surface hints like this. |
||
) | ||
} else { | ||
throw err | ||
} | ||
} | ||
}) | ||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"type": "Feature", | ||
"description": "Support starting CloudWatch Logs Live Tail sessions for a specific Lambda function's log group, without having to find which log group manually, but just starting from the function directly." | ||
valerena marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
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.
We should probably use the same wording (and actually the same
AWS.foo.bar
id, instead of multiple) in all places:aws-toolkit-vscode/packages/core/package.nls.json
Lines 181 to 182 in 8d48a3c
However, not a blocker for this PR. Needs a decision about which wording to use in both places.
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.
The difference here is that the one you linked shows "when you have a log group selected" (therefore the wording is "tail log group" as in "tail this log group"), while this new one is when you have a function selected. Maybe this could be "Tail function logs" instead of just "Tail logs". The inspiration was the other existing string right above "Search Logs" (with its corresponding "Search Log Group"), which is a similar behavior, but that starts a search instead of using live tail.
Let me know if this difference makes sense or if we should still try to unify the wording.
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.
Yup I noticed that too, and I like the brevity of "Logs" instead of "Log Group". For now let's leave it as you have in this PR.