Skip to content

Commit

Permalink
emit telemetry on start/close live tail
Browse files Browse the repository at this point in the history
  • Loading branch information
Keegan Irby committed Nov 18, 2024
1 parent 500f94b commit 927abe7
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 42 deletions.
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",
"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",
"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

0 comments on commit 927abe7

Please sign in to comment.