Skip to content

Commit 1e3fdce

Browse files
keeganirbyKeegan Irby
andauthored
feat(cwl): Emit telemetry when starting and stopping liveTail sessions (#6047)
## Problem Cloudwatch wants to monitor usage metrics of the LiveTail integration ## Solution When starting a session emit telemetry for: * Session already started (this case happens when session is already running, and customer sends a new command that matches the already running session) * Has LogEventFilter * LogStream filter type * Source of the command (command palette, explorer) When closing a session: * Session duration * source of cancellation (ex: CodeLens, ClosingEditors) --- <!--- REMINDER: Ensure that your PR meets the guidelines in CONTRIBUTING.md --> License: I confirm that my contribution is made under the terms of the Apache 2.0 license. --------- Co-authored-by: Keegan Irby <[email protected]>
1 parent 30580b4 commit 1e3fdce

File tree

6 files changed

+72
-47
lines changed

6 files changed

+72
-47
lines changed

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"generateNonCodeFiles": "npm run generateNonCodeFiles -w packages/ --if-present"
4040
},
4141
"devDependencies": {
42-
"@aws-toolkits/telemetry": "^1.0.282",
42+
"@aws-toolkits/telemetry": "^1.0.284",
4343
"@playwright/browser-chromium": "^1.43.1",
4444
"@types/he": "^1.2.3",
4545
"@types/vscode": "^1.68.0",

packages/core/src/awsService/cloudWatchLogs/activation.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,12 @@ export async function activate(context: vscode.ExtensionContext, configuration:
120120
node instanceof LogGroupNode
121121
? { regionName: node.regionCode, groupName: node.logGroup.logGroupName! }
122122
: undefined
123-
await tailLogGroup(liveTailRegistry, logGroupInfo)
123+
const source = node ? (logGroupInfo ? 'ExplorerLogGroupNode' : 'ExplorerServiceNode') : 'Command'
124+
await tailLogGroup(liveTailRegistry, source, logGroupInfo)
124125
}),
125126

126-
Commands.register('aws.cwl.stopTailingLogGroup', async (document: vscode.TextDocument) => {
127-
closeSession(document.uri, liveTailRegistry)
127+
Commands.register('aws.cwl.stopTailingLogGroup', async (document: vscode.TextDocument, source: string) => {
128+
closeSession(document.uri, liveTailRegistry, source)
128129
}),
129130

130131
Commands.register('aws.cwl.clearDocument', async (document: vscode.TextDocument) => {

packages/core/src/awsService/cloudWatchLogs/commands/tailLogGroup.ts

Lines changed: 58 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
import * as vscode from 'vscode'
7+
import { telemetry } from '../../../shared/telemetry/telemetry'
78
import { TailLogGroupWizard } from '../wizard/tailLogGroupWizard'
89
import { CancellationError } from '../../../shared/utilities/timeoutUtils'
910
import { LiveTailSession, LiveTailSessionConfiguration } from '../registry/liveTailSession'
@@ -18,48 +19,70 @@ import { uriToKey } from '../cloudWatchLogsUtils'
1819

1920
export async function tailLogGroup(
2021
registry: LiveTailSessionRegistry,
22+
source: string,
2123
logData?: { regionName: string; groupName: string }
2224
): Promise<void> {
23-
const wizard = new TailLogGroupWizard(logData)
24-
const wizardResponse = await wizard.run()
25-
if (!wizardResponse) {
26-
throw new CancellationError('user')
27-
}
28-
const awsCredentials = await globals.awsContext.getCredentials()
29-
if (awsCredentials === undefined) {
30-
throw new ToolkitError('Failed to start LiveTail session: credentials are undefined.')
31-
}
32-
const liveTailSessionConfig: LiveTailSessionConfiguration = {
33-
logGroupArn: wizardResponse.regionLogGroupSubmenuResponse.data,
34-
logStreamFilter: wizardResponse.logStreamFilter,
35-
logEventFilterPattern: wizardResponse.filterPattern,
36-
region: wizardResponse.regionLogGroupSubmenuResponse.region,
37-
awsCredentials: awsCredentials,
38-
}
39-
const session = new LiveTailSession(liveTailSessionConfig)
40-
if (registry.has(uriToKey(session.uri))) {
41-
await prepareDocument(session)
42-
return
43-
}
44-
registry.set(uriToKey(session.uri), session)
25+
await telemetry.cwlLiveTail_Start.run(async (span) => {
26+
const wizard = new TailLogGroupWizard(logData)
27+
const wizardResponse = await wizard.run()
28+
if (!wizardResponse) {
29+
throw new CancellationError('user')
30+
}
31+
const awsCredentials = await globals.awsContext.getCredentials()
32+
if (awsCredentials === undefined) {
33+
throw new ToolkitError('Failed to start LiveTail session: credentials are undefined.')
34+
}
35+
const liveTailSessionConfig: LiveTailSessionConfiguration = {
36+
logGroupArn: wizardResponse.regionLogGroupSubmenuResponse.data,
37+
logStreamFilter: wizardResponse.logStreamFilter,
38+
logEventFilterPattern: wizardResponse.filterPattern,
39+
region: wizardResponse.regionLogGroupSubmenuResponse.region,
40+
awsCredentials: awsCredentials,
41+
}
42+
const session = new LiveTailSession(liveTailSessionConfig)
43+
if (registry.has(uriToKey(session.uri))) {
44+
await prepareDocument(session)
45+
span.record({
46+
result: 'Succeeded',
47+
sessionAlreadyStarted: true,
48+
source: source,
49+
})
50+
return
51+
}
4552

46-
const document = await prepareDocument(session)
53+
registry.set(uriToKey(session.uri), session)
4754

48-
hideShowStatusBarItemsOnActiveEditor(session, document)
49-
registerTabChangeCallback(session, registry, document)
55+
const document = await prepareDocument(session)
5056

51-
const stream = await session.startLiveTailSession()
57+
hideShowStatusBarItemsOnActiveEditor(session, document)
58+
registerTabChangeCallback(session, registry, document)
5259

53-
await handleSessionStream(stream, document, session)
60+
const stream = await session.startLiveTailSession()
61+
span.record({
62+
source: source,
63+
result: 'Succeeded',
64+
sessionAlreadyStarted: false,
65+
hasLogEventFilterPattern: Boolean(wizardResponse.filterPattern),
66+
logStreamFilterType: wizardResponse.logStreamFilter.type,
67+
})
68+
await handleSessionStream(stream, document, session)
69+
})
5470
}
5571

56-
export function closeSession(sessionUri: vscode.Uri, registry: LiveTailSessionRegistry) {
57-
const session = registry.get(uriToKey(sessionUri))
58-
if (session === undefined) {
59-
throw new ToolkitError(`No LiveTail session found for URI: ${sessionUri.toString()}`)
60-
}
61-
session.stopLiveTailSession()
62-
registry.delete(uriToKey(sessionUri))
72+
export function closeSession(sessionUri: vscode.Uri, registry: LiveTailSessionRegistry, source: string) {
73+
telemetry.cwlLiveTail_Stop.run((span) => {
74+
const session = registry.get(uriToKey(sessionUri))
75+
if (session === undefined) {
76+
throw new ToolkitError(`No LiveTail session found for URI: ${sessionUri.toString()}`)
77+
}
78+
session.stopLiveTailSession()
79+
registry.delete(uriToKey(sessionUri))
80+
span.record({
81+
result: 'Succeeded',
82+
source: source,
83+
duration: session.getLiveTailSessionDuration(),
84+
})
85+
})
6386
}
6487

6588
export async function clearDocument(textDocument: vscode.TextDocument) {
@@ -215,7 +238,7 @@ function registerTabChangeCallback(
215238
vscode.window.tabGroups.onDidChangeTabs((tabEvent) => {
216239
const isOpen = isLiveTailSessionOpenInAnyTab(session)
217240
if (!isOpen) {
218-
closeSession(session.uri, registry)
241+
closeSession(session.uri, registry, 'ClosedEditors')
219242
void clearDocument(document)
220243
}
221244
})

packages/core/src/awsService/cloudWatchLogs/document/liveTailCodeLensProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export class LiveTailCodeLensProvider implements vscode.CodeLensProvider {
3838
const command: vscode.Command = {
3939
title: 'Stop tailing',
4040
command: 'aws.cwl.stopTailingLogGroup',
41-
arguments: [document],
41+
arguments: [document, 'codeLens'],
4242
}
4343
return new vscode.CodeLens(range, command)
4444
}

packages/core/src/test/awsService/cloudWatchLogs/commands/tailLogGroup.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ describe('TailLogGroup', function () {
2727
const testRegion = 'test-region'
2828
const testMessage = 'test-message'
2929
const testAwsAccountId = '1234'
30+
const testSource = 'test-source'
3031
const testAwsCredentials = {} as any as AWS.Credentials
3132

3233
let sandbox: sinon.SinonSandbox
@@ -93,7 +94,7 @@ describe('TailLogGroup', function () {
9394
cloudwatchSettingsSpy = sandbox.stub(CloudWatchLogsSettings.prototype, 'get').callsFake(() => {
9495
return 1
9596
})
96-
await tailLogGroup(registry, {
97+
await tailLogGroup(registry, testSource, {
9798
groupName: testLogGroup,
9899
regionName: testRegion,
99100
})
@@ -131,7 +132,7 @@ describe('TailLogGroup', function () {
131132
return getTestWizardResponse()
132133
})
133134
await assert.rejects(async () => {
134-
await tailLogGroup(registry, {
135+
await tailLogGroup(registry, testSource, {
135136
groupName: testLogGroup,
136137
regionName: testRegion,
137138
})
@@ -152,7 +153,7 @@ describe('TailLogGroup', function () {
152153
})
153154
registry.set(uriToKey(session.uri), session)
154155

155-
closeSession(session.uri, registry)
156+
closeSession(session.uri, registry, testSource)
156157
assert.strictEqual(0, registry.size)
157158
assert.strictEqual(true, stopLiveTailSessionSpy.calledOnce)
158159
assert.strictEqual(0, clock.countTimers())

0 commit comments

Comments
 (0)