Skip to content

Commit 08571af

Browse files
Vandita2020Vandita Patidar
authored andcommitted
feat(lambda): adding metrics and unit test for serverlessland (aws#6719)
## Problem Adding metrics and unit test for Serverless Land Project ## Solution - CreateWizard :) Path ✔ creates project with Python runtime and CDK (725ms) Error Handling ✔ handles empty project name (521ms) - MetadataManager getInstance ✔ should create a new instance when none exists ✔ should return the same instance when called multiple times initialize ✔ should initialize metadata manager and return instance getMetadataPath ✔ should return correct metadata path getPatterns ✔ handles different pattern data types getRuntimes ✔ returns empty array when pattern not found ✔ returns unique runtimes for valid pattern getUrl ✔ returns empty string when pattern not found ✔ returns correct URL for valid pattern getIacOptions ✔ returns empty array when pattern not found ✔ returns unique IAC options for valid pattern getAssetName ✔ returns empty string when pattern not found ✔ returns correct asset name for matching implementation ✔ returns empty string when no matching implementation found - createNewServerlessLandProject ✔ should complete project creation successfully ✔ should handle wizard cancellation downloadPatternCode ✔ successfully downloads pattern code ✔ handles download failure openReadmeFile ✔ successfully opens README file ✔ handles missing README file ✔ handles error with opening README file getProjectUri ✔ returns Uri when file exists ✔ handles missing project directory --- - Treat all work as PUBLIC. Private `feature/x` branches will not be squash-merged at release time. - Your code changes must meet the guidelines in [CONTRIBUTING.md](https://github.com/aws/aws-toolkit-vscode/blob/master/CONTRIBUTING.md#guidelines). - License: I confirm that my contribution is made under the terms of the Apache 2.0 license. --------- Co-authored-by: Vandita Patidar <[email protected]>
1 parent 7be8bcd commit 08571af

File tree

8 files changed

+541
-25
lines changed

8 files changed

+541
-25
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,9 @@ async function registerAppBuilderCommands(context: ExtContext): Promise<void> {
202202
}
203203
}),
204204
Commands.register({ id: 'aws.toolkit.lambda.createServerlessLandProject', autoconnect: false }, async () => {
205-
await createNewServerlessLandProject(context)
205+
await telemetry.lambda_createServerlessLandProject.run(async () => {
206+
await createNewServerlessLandProject(context)
207+
})
206208
})
207209
)
208210
}

packages/core/src/awsService/appBuilder/serverlessLand/main.ts

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ import * as nls from 'vscode-nls'
77
const localize = nls.loadMessageBundle()
88
import * as path from 'path'
99
import * as vscode from 'vscode'
10-
import { getTelemetryReason, getTelemetryResult } from '../../../shared/errors'
1110
import { getLogger } from '../../../shared/logger/logger'
1211
import { checklogs } from '../../../shared/localizedText'
13-
import { Result, telemetry } from '../../../shared/telemetry/telemetry'
12+
import { telemetry } from '../../../shared/telemetry/telemetry'
1413
import { CreateServerlessLandWizardForm, CreateServerlessLandWizard } from './wizard'
1514
import { ExtContext } from '../../../shared/extensions'
1615
import { addFolderToWorkspace } from '../../../shared/utilities/workspaceUtils'
@@ -37,17 +36,13 @@ const serverlessLandRepo = 'serverless-patterns'
3736
* 6. Handles errors and emits telemetry
3837
*/
3938
export async function createNewServerlessLandProject(extContext: ExtContext): Promise<void> {
40-
let createResult: Result = 'Succeeded'
41-
let reason: string | undefined
4239
let metadataManager: MetadataManager
4340

4441
try {
4542
metadataManager = MetadataManager.getInstance()
4643
// Launch the project creation wizard
4744
const config = await launchProjectCreationWizard(extContext)
4845
if (!config) {
49-
createResult = 'Cancelled'
50-
reason = 'userCancelled'
5146
return
5247
}
5348
const assetName = metadataManager.getAssetName(config.pattern, config.runtime, config.iac)
@@ -61,9 +56,12 @@ export async function createNewServerlessLandProject(extContext: ExtContext): Pr
6156
},
6257
true
6358
)
59+
telemetry.record({
60+
templateName: assetName,
61+
runtimeString: config.runtime,
62+
iac: config.iac,
63+
})
6464
} catch (err) {
65-
createResult = getTelemetryResult(err)
66-
reason = getTelemetryReason(err)
6765
getLogger().error(
6866
localize(
6967
'AWS.serverlessland.initWizard.general.error',
@@ -72,17 +70,10 @@ export async function createNewServerlessLandProject(extContext: ExtContext): Pr
7270
)
7371
)
7472
getLogger().error('Error creating new Serverless Land Application: %O', err as Error)
75-
} finally {
76-
// add telemetry
77-
// TODO: Will add telemetry once the implementation gets completed
78-
telemetry.sam_init.emit({
79-
result: createResult,
80-
reason: reason,
81-
})
8273
}
8374
}
8475

85-
async function launchProjectCreationWizard(
76+
export async function launchProjectCreationWizard(
8677
extContext: ExtContext
8778
): Promise<CreateServerlessLandWizardForm | undefined> {
8879
const awsContext = extContext.awsContext
@@ -95,7 +86,7 @@ async function launchProjectCreationWizard(
9586
}).run()
9687
}
9788

98-
async function downloadPatternCode(config: CreateServerlessLandWizardForm, assetName: string): Promise<void> {
89+
export async function downloadPatternCode(config: CreateServerlessLandWizardForm, assetName: string): Promise<void> {
9990
const fullAssetName = assetName + '.zip'
10091
const location = vscode.Uri.joinPath(config.location, config.name)
10192
try {
@@ -108,7 +99,7 @@ async function downloadPatternCode(config: CreateServerlessLandWizardForm, asset
10899
}
109100
}
110101

111-
async function openReadmeFile(config: CreateServerlessLandWizardForm): Promise<void> {
102+
export async function openReadmeFile(config: CreateServerlessLandWizardForm): Promise<void> {
112103
try {
113104
const readmeUri = await getProjectUri(config, readmeFile)
114105
if (!readmeUri) {
@@ -123,12 +114,12 @@ async function openReadmeFile(config: CreateServerlessLandWizardForm): Promise<v
123114
}
124115
}
125116

126-
async function getProjectUri(
117+
export async function getProjectUri(
127118
config: Pick<CreateServerlessLandWizardForm, 'location' | 'name'>,
128119
file: string
129120
): Promise<vscode.Uri | undefined> {
130121
if (!file) {
131-
throw Error('expected "file" parameter to have at least one item')
122+
throw new ToolkitError('expected "file" parameter to have at least one item')
132123
}
133124
const cfnTemplatePath = path.resolve(config.location.fsPath, config.name, file)
134125
if (await fs.exists(cfnTemplatePath)) {

packages/core/src/awsService/appBuilder/serverlessLand/metadata.json

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"patterns": {
3-
"s3-lambda-resizing-sam": {
3+
"Image Resizing": {
44
"name": "Resizing image",
55
"description": "Lambda, S3 • Python, Javascript, Java, .NET • SAM",
66
"implementation": [
@@ -26,7 +26,7 @@
2626
}
2727
]
2828
},
29-
"apigw-rest-api-lambda-sam": {
29+
"Hello World Lambda with API": {
3030
"name": "Rest API",
3131
"description": "Lambda, API Gateway • Python, Javascript, Java, .NET • SAM",
3232
"implementation": [
@@ -51,6 +51,32 @@
5151
"assetName": "apigw-rest-api-lambda-dotnet"
5252
}
5353
]
54+
},
55+
"Process SQS Records with Lambda": {
56+
"name": "Rest API",
57+
"description": "Lambda, SQS • Python, Javascript, Java, .NET • SAM",
58+
"implementation": [
59+
{
60+
"iac": "sam",
61+
"runtime": "python",
62+
"assetName": "sqs-lambda-python-sam"
63+
},
64+
{
65+
"iac": "sam",
66+
"runtime": "javascript",
67+
"assetName": "sqs-lambda-nodejs-sam"
68+
},
69+
{
70+
"iac": "sam",
71+
"runtime": "java",
72+
"assetName": "sqs-lambda-java-sam"
73+
},
74+
{
75+
"iac": "sam",
76+
"runtime": "dotnet",
77+
"assetName": "sqs-lambda-dotnet-sam"
78+
}
79+
]
5480
}
5581
}
5682
}

packages/core/src/awsService/appBuilder/serverlessLand/metadataManager.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55
import * as nodefs from 'fs' // eslint-disable-line no-restricted-imports
6+
import * as path from 'path'
67
import { ToolkitError } from '../../../shared/errors'
78
import globals from '../../../shared/extensionGlobals'
89

@@ -48,7 +49,7 @@ export class MetadataManager {
4849
}
4950

5051
public getMetadataPath(): string {
51-
return globals.context.asAbsolutePath('dist/src/serverlessLand/metadata.json')
52+
return globals.context.asAbsolutePath(path.join('dist', 'src', 'serverlessLand', 'metadata.json'))
5253
}
5354

5455
/**

packages/core/src/awsService/appBuilder/serverlessLand/wizard.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ export interface CreateServerlessLandWizardForm {
2222
pattern: string
2323
runtime: string
2424
iac: string
25-
assetName: string
2625
}
2726

2827
function promptPattern(metadataManager: MetadataManager) {

0 commit comments

Comments
 (0)