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

feat(cwl): Emit telemetry when starting and stopping liveTail sessions #6047

Merged
merged 3 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 4 additions & 3 deletions packages/core/src/awsService/cloudWatchLogs/activation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,12 @@ export async function activate(context: vscode.ExtensionContext, configuration:
node instanceof LogGroupNode
? { regionName: node.regionCode, groupName: node.logGroup.logGroupName! }
: undefined
await tailLogGroup(liveTailRegistry, logGroupInfo)
const source = node ? (logGroupInfo ? 'ExplorerLogGroupNode' : 'ExplorerServiceNode') : 'Command'
await tailLogGroup(liveTailRegistry, source, logGroupInfo)
}),

Commands.register('aws.cwl.stopTailingLogGroup', async (document: vscode.TextDocument) => {
closeSession(document.uri, liveTailRegistry)
Commands.register('aws.cwl.stopTailingLogGroup', async (document: vscode.TextDocument, source: string) => {
closeSession(document.uri, liveTailRegistry, source)
}),

Commands.register('aws.cwl.clearDocument', async (document: vscode.TextDocument) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

import * as vscode from 'vscode'
import { telemetry } from '../../../shared/telemetry/telemetry'
import { TailLogGroupWizard } from '../wizard/tailLogGroupWizard'
import { CancellationError } from '../../../shared/utilities/timeoutUtils'
import { LiveTailSession, LiveTailSessionConfiguration } from '../registry/liveTailSession'
Expand All @@ -18,48 +19,68 @@ import { uriToKey } from '../cloudWatchLogsUtils'

export async function tailLogGroup(
registry: LiveTailSessionRegistry,
source: string,
logData?: { regionName: string; groupName: string }
): Promise<void> {
const wizard = new TailLogGroupWizard(logData)
const wizardResponse = await wizard.run()
if (!wizardResponse) {
throw new CancellationError('user')
}
const awsCredentials = await globals.awsContext.getCredentials()
if (awsCredentials === undefined) {
throw new ToolkitError('Failed to start LiveTail session: credentials are undefined.')
}
const liveTailSessionConfig: LiveTailSessionConfiguration = {
logGroupArn: wizardResponse.regionLogGroupSubmenuResponse.data,
logStreamFilter: wizardResponse.logStreamFilter,
logEventFilterPattern: wizardResponse.filterPattern,
region: wizardResponse.regionLogGroupSubmenuResponse.region,
awsCredentials: awsCredentials,
}
const session = new LiveTailSession(liveTailSessionConfig)
if (registry.has(uriToKey(session.uri))) {
await prepareDocument(session)
return
}
registry.set(uriToKey(session.uri), session)
await telemetry.cwlLiveTail_Start.run(async (span) => {
const wizard = new TailLogGroupWizard(logData)
const wizardResponse = await wizard.run()
if (!wizardResponse) {
throw new CancellationError('user')
}
const awsCredentials = await globals.awsContext.getCredentials()
if (awsCredentials === undefined) {
throw new ToolkitError('Failed to start LiveTail session: credentials are undefined.')
}
const liveTailSessionConfig: LiveTailSessionConfiguration = {
logGroupArn: wizardResponse.regionLogGroupSubmenuResponse.data,
logStreamFilter: wizardResponse.logStreamFilter,
logEventFilterPattern: wizardResponse.filterPattern,
region: wizardResponse.regionLogGroupSubmenuResponse.region,
awsCredentials: awsCredentials,
}
const session = new LiveTailSession(liveTailSessionConfig)
if (registry.has(uriToKey(session.uri))) {
await prepareDocument(session)
span.record({
livetailSessionAlreadyStarted: true,
source: source,
})
return
}
span.record({
source: source,
livetailSessionAlreadyStarted: false,
livetailHasLogEventFilterPattern: Boolean(wizardResponse.filterPattern),
livetailLogStreamFilterType: wizardResponse.logStreamFilter.type,
})

registry.set(uriToKey(session.uri), session)

const document = await prepareDocument(session)
const document = await prepareDocument(session)

hideShowStatusBarItemsOnActiveEditor(session, document)
registerTabChangeCallback(session, registry, document)
hideShowStatusBarItemsOnActiveEditor(session, document)
registerTabChangeCallback(session, registry, document)

const stream = await session.startLiveTailSession()
const stream = await session.startLiveTailSession()

await handleSessionStream(stream, document, session)
await handleSessionStream(stream, document, session)
})
}

export function closeSession(sessionUri: vscode.Uri, registry: LiveTailSessionRegistry) {
const session = registry.get(uriToKey(sessionUri))
if (session === undefined) {
throw new ToolkitError(`No LiveTail session found for URI: ${sessionUri.toString()}`)
}
session.stopLiveTailSession()
registry.delete(uriToKey(sessionUri))
export function closeSession(sessionUri: vscode.Uri, registry: LiveTailSessionRegistry, source: string) {
telemetry.cwlLiveTail_Stop.run((span) => {
const session = registry.get(uriToKey(sessionUri))
if (session === undefined) {
throw new ToolkitError(`No LiveTail session found for URI: ${sessionUri.toString()}`)
}
session.stopLiveTailSession()
registry.delete(uriToKey(sessionUri))
span.record({
source: source,
duration: session.getLiveTailSessionDuration(),
})
})
}

export async function clearDocument(textDocument: vscode.TextDocument) {
Expand Down Expand Up @@ -215,7 +236,7 @@ function registerTabChangeCallback(
vscode.window.tabGroups.onDidChangeTabs((tabEvent) => {
const isOpen = isLiveTailSessionOpenInAnyTab(session)
if (!isOpen) {
closeSession(session.uri, registry)
closeSession(session.uri, registry, 'ClosedEditors')
void clearDocument(document)
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class LiveTailCodeLensProvider implements vscode.CodeLensProvider {
const command: vscode.Command = {
title: 'Stop tailing',
command: 'aws.cwl.stopTailingLogGroup',
arguments: [document],
arguments: [document, 'codeLens'],
}
return new vscode.CodeLens(range, command)
}
Expand Down
51 changes: 51 additions & 0 deletions packages/core/src/shared/telemetry/vscodeTelemetry.json
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,21 @@
"name": "amazonqMessageDisplayedMs",
"type": "int",
"description": "Duration between the partner teams code receiving the message and when the message was finally displayed in ms"
},
{
"name": "livetailSessionAlreadyStarted",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of prefixing these with the product name like we have in the past, you can probably drop the product name as the prefix so that we can make these more general

"type": "boolean",
"description": "Session already open"
},
{
"name": "livetailHasLogEventFilterPattern",
"type": "boolean",
"description": "If LogEvent filter pattern is applied"
},
{
"name": "livetailLogStreamFilterType",
"type": "string",
"description": "Type of LogStream filter applied to session"
}
],
"metrics": [
Expand Down Expand Up @@ -1230,6 +1245,42 @@
}
],
"passive": true
},
{
"name": "cwlLiveTail_Start",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm still not sure why we can't just upstream these to commons

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just following the documentation here: https://github.com/aws/aws-toolkit-vscode/blob/master/docs/telemetry.md. I did this in VSCode so my local builds would generate the telemetry objects and I could bundle adding the metrics as well as using them in the same PR. If I need to update commons instead I can send a change for that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened PR to upstream to Commons. aws/aws-toolkit-common#916

"description": "When user starts a new LiveTail command",
"metadata": [
{
"type": "source",
"required": true
},
{
"type": "livetailSessionAlreadyStarted",
"required": true
},
{
"type": "livetailHasLogEventFilterPattern",
"required": false
},
{
"type": "livetailLogStreamFilterType",
"required": false
}
]
},
{
"name": "cwlLiveTail_Stop",
"description": "When user stops a liveTailSession",
"metadata": [
{
"type": "source",
"required": true
},
{
"type": "duration",
"required": true
}
]
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ describe('TailLogGroup', function () {
const testRegion = 'test-region'
const testMessage = 'test-message'
const testAwsAccountId = '1234'
const testSource = 'test-source'
const testAwsCredentials = {} as any as AWS.Credentials

let sandbox: sinon.SinonSandbox
Expand Down Expand Up @@ -93,7 +94,7 @@ describe('TailLogGroup', function () {
cloudwatchSettingsSpy = sandbox.stub(CloudWatchLogsSettings.prototype, 'get').callsFake(() => {
return 1
})
await tailLogGroup(registry, {
await tailLogGroup(registry, testSource, {
groupName: testLogGroup,
regionName: testRegion,
})
Expand Down Expand Up @@ -131,7 +132,7 @@ describe('TailLogGroup', function () {
return getTestWizardResponse()
})
await assert.rejects(async () => {
await tailLogGroup(registry, {
await tailLogGroup(registry, testSource, {
groupName: testLogGroup,
regionName: testRegion,
})
Expand All @@ -152,7 +153,7 @@ describe('TailLogGroup', function () {
})
registry.set(uriToKey(session.uri), session)

closeSession(session.uri, registry)
closeSession(session.uri, registry, testSource)
assert.strictEqual(0, registry.size)
assert.strictEqual(true, stopLiveTailSessionSpy.calledOnce)
assert.strictEqual(0, clock.countTimers())
Expand Down
Loading