Skip to content
This repository was archived by the owner on Mar 26, 2024. It is now read-only.

Commit 1d076d7

Browse files
authored
Merge pull request #8 from microsoft/add-appinsights
Added app insights
2 parents 53e08a2 + 036718d commit 1d076d7

File tree

8 files changed

+164
-10
lines changed

8 files changed

+164
-10
lines changed

ado-extension.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"manifestVersion": 1,
33
"id": "android-app-size-diff-utils",
44
"name": "Android app size changes",
5-
"version": "0.0.27",
5+
"version": "0.0.28",
66
"publisher": "PraveenPendyala",
77
"targets": [
88
{

dist_gh_action/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

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

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "android-app-size-diff",
3-
"version": "0.0.27",
3+
"version": "0.0.28",
44
"description": "Azure DevOps task to measure the size in Android app size by looking at 2 given APKs and AABs",
55
"main": "index.js",
66
"scripts": {
@@ -37,6 +37,7 @@
3737
"@types/adm-zip": "^0.4.32",
3838
"@types/line-reader": "0.0.28",
3939
"adm-zip": "^0.4.13",
40+
"applicationinsights": "^1.6.0",
4041
"azure-pipelines-task-lib": "^2.9.3",
4142
"line-reader": "^0.4.0"
4243
},

src/adoTask/adoTaskRunner.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import { isUndefined } from 'util';
44

55
export class AdoCiCore implements CiCore {
66

7+
getCiName(): string {
8+
return 'Azure DevOps';
9+
}
10+
711
getInput(name: string): string {
812
const value = adoTask.getInput(name);
913
return isUndefined(value) ? "" : value;
@@ -24,6 +28,10 @@ export class AdoCiCore implements CiCore {
2428
logError(message: string) {
2529
return adoTask.error(message);
2630
}
31+
32+
markAsFailed(errorMessage: string) {
33+
return adoTask.setResult(adoTask.TaskResult.Failed, errorMessage);
34+
}
2735
}
2836

2937
export default class AdoTaskRunner {
@@ -32,7 +40,7 @@ export default class AdoTaskRunner {
3240
try {
3341
const adoCiCore = new AdoCiCore();
3442
const ciRunner = new CiRunner(adoCiCore);
35-
await ciRunner.run();
43+
await ciRunner.runWithTelemetry();
3644
}
3745
catch (err) {
3846
adoTask.setResult(adoTask.TaskResult.Failed, err.message);

src/apkAnalyzer/CiRunner.ts

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,15 @@ import ComparisionReportGenerator from '../apkAnalyzer/ComparisionReportGenerato
33
import { MarkdownReporter } from '../apkAnalyzer/reporter/MarkdownReporter';
44
import ComparisionReport from './model/ComparisionReport';
55
import ThresholdChecker from './ThresholdChecker';
6+
import * as appInsights from 'applicationinsights'
67

78
export interface CiCore {
9+
10+
/**
11+
* @returns the name of the CI
12+
*/
13+
getCiName(): string;
14+
815
/**
916
* Gets an input from the CI
1017
* @param name
@@ -21,6 +28,12 @@ export interface CiCore {
2128
logInfo(message: string): any;
2229
logWarning(message: string): any;
2330
logError(message: string): any;
31+
32+
/**
33+
* Marks the run as failure
34+
* @param errorMessage Error message
35+
*/
36+
markAsFailed(errorMessage: string): any;
2437
}
2538

2639
/**
@@ -29,13 +42,59 @@ export interface CiCore {
2942
export default class CiRunner {
3043
ciCore: CiCore; // either Github action core or ADO Task
3144
thresholdChecker: ThresholdChecker;
45+
telemetryClient: appInsights.TelemetryClient;
3246

3347
constructor(ciCore: CiCore) {
3448
this.ciCore = ciCore;
3549
this.thresholdChecker = new ThresholdChecker(ciCore);
50+
51+
// Configure and enable telemetry
52+
appInsights.setup('0ba004b8-ff05-41fa-a241-3f026d68fc3a') // Change this to your own instrumentation key
53+
.setAutoCollectExceptions(true)
54+
.setSendLiveMetrics(false)
55+
.start();
56+
this.telemetryClient = appInsights.defaultClient;
57+
}
58+
59+
public async runWithTelemetry() {
60+
// Send app start telemetry
61+
const startTime = new Date().getTime();
62+
const telemetryProperties = {
63+
ciName: this.ciCore.getCiName()
64+
}
65+
this.telemetryClient.trackEvent({
66+
name: 'AppStart',
67+
properties: telemetryProperties
68+
});
69+
70+
var result: any;
71+
try {
72+
result = await this.run();
73+
} catch (err) {
74+
// Send error telemetry
75+
this.telemetryClient.trackException({
76+
exception: err,
77+
properties: telemetryProperties
78+
});
79+
this.telemetryClient.flush()
80+
81+
throw err;
82+
}
83+
84+
// Send performance telemetry
85+
const endTime = new Date().getTime();
86+
const elapsedTime = endTime - startTime;
87+
this.telemetryClient.trackMetric({
88+
name: 'RunPerformance',
89+
value: elapsedTime,
90+
properties: telemetryProperties
91+
});
92+
this.telemetryClient.flush()
93+
94+
return result;
3695
}
3796

38-
public async run() {
97+
private async run() {
3998
const baseAppPath = this.ciCore.getInput('baseAppPath');
4099
const targetAppPath = this.ciCore.getInput('targetAppPath');
41100
const baseAppLabel = this.ciCore.getInput('baseAppLabel');
@@ -96,10 +155,10 @@ export default class CiRunner {
96155

97156
// Check if thresholds are adhered to
98157
if (!this.thresholdChecker.checkThresholds(comparisionReport)) {
99-
throw 'App size increased significantly in at least one metric!';
158+
this.ciCore.markAsFailed('App size increased significantly in at least one metric!');
100159
}
101-
102160
console.log(comparisionReport);
161+
103162
return comparisionReport;
104163
}
105164

src/githubAction/GithubActionRunner.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import { isUndefined } from 'util';
44

55
export class GithubCiCore implements CiCore {
66

7+
getCiName(): string {
8+
return 'GitHub';
9+
}
10+
711
getInput(name: string): string {
812
const value = ghAction.getInput(name);
913
return isUndefined(value) ? "" : value;
@@ -24,6 +28,10 @@ export class GithubCiCore implements CiCore {
2428
logError(message: string) {
2529
return ghAction.error(message);
2630
}
31+
32+
markAsFailed(errorMessage: string) {
33+
return ghAction.setFailed(errorMessage);
34+
}
2735
}
2836

2937
export default class GithubActionRunner {
@@ -32,7 +40,7 @@ export default class GithubActionRunner {
3240
try {
3341
const githubCore = new GithubCiCore();
3442
const ciRunner = new CiRunner(githubCore);
35-
await ciRunner.run();
43+
await ciRunner.runWithTelemetry();
3644
}
3745
catch (err) {
3846
ghAction.setFailed(err.message);

src/task.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"version": {
1111
"Major": 0,
1212
"Minor": 0,
13-
"Patch": 27
13+
"Patch": 28
1414
},
1515
"instanceNameFormat": "Android App size change - $(baseAppPath) vs $(targetAppPath)",
1616
"inputs": [

0 commit comments

Comments
 (0)