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

Added app insights #8

Merged
merged 1 commit into from
Jan 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion ado-extension.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifestVersion": 1,
"id": "android-app-size-diff-utils",
"name": "Android app size changes",
"version": "0.0.27",
"version": "0.0.28",
"publisher": "PraveenPendyala",
"targets": [
{
Expand Down
2 changes: 1 addition & 1 deletion dist_gh_action/index.js

Large diffs are not rendered by default.

80 changes: 79 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "android-app-size-diff",
"version": "0.0.27",
"version": "0.0.28",
"description": "Azure DevOps task to measure the size in Android app size by looking at 2 given APKs and AABs",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -37,6 +37,7 @@
"@types/adm-zip": "^0.4.32",
"@types/line-reader": "0.0.28",
"adm-zip": "^0.4.13",
"applicationinsights": "^1.6.0",
"azure-pipelines-task-lib": "^2.9.3",
"line-reader": "^0.4.0"
},
Expand Down
10 changes: 9 additions & 1 deletion src/adoTask/adoTaskRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { isUndefined } from 'util';

export class AdoCiCore implements CiCore {

getCiName(): string {
return 'Azure DevOps';
}

getInput(name: string): string {
const value = adoTask.getInput(name);
return isUndefined(value) ? "" : value;
Expand All @@ -24,6 +28,10 @@ export class AdoCiCore implements CiCore {
logError(message: string) {
return adoTask.error(message);
}

markAsFailed(errorMessage: string) {
return adoTask.setResult(adoTask.TaskResult.Failed, errorMessage);
}
}

export default class AdoTaskRunner {
Expand All @@ -32,7 +40,7 @@ export default class AdoTaskRunner {
try {
const adoCiCore = new AdoCiCore();
const ciRunner = new CiRunner(adoCiCore);
await ciRunner.run();
await ciRunner.runWithTelemetry();
}
catch (err) {
adoTask.setResult(adoTask.TaskResult.Failed, err.message);
Expand Down
65 changes: 62 additions & 3 deletions src/apkAnalyzer/CiRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@ import ComparisionReportGenerator from '../apkAnalyzer/ComparisionReportGenerato
import { MarkdownReporter } from '../apkAnalyzer/reporter/MarkdownReporter';
import ComparisionReport from './model/ComparisionReport';
import ThresholdChecker from './ThresholdChecker';
import * as appInsights from 'applicationinsights'

export interface CiCore {

/**
* @returns the name of the CI
*/
getCiName(): string;

/**
* Gets an input from the CI
* @param name
Expand All @@ -21,6 +28,12 @@ export interface CiCore {
logInfo(message: string): any;
logWarning(message: string): any;
logError(message: string): any;

/**
* Marks the run as failure
* @param errorMessage Error message
*/
markAsFailed(errorMessage: string): any;
}

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

constructor(ciCore: CiCore) {
this.ciCore = ciCore;
this.thresholdChecker = new ThresholdChecker(ciCore);

// Configure and enable telemetry
appInsights.setup('0ba004b8-ff05-41fa-a241-3f026d68fc3a') // Change this to your own instrumentation key
.setAutoCollectExceptions(true)
.setSendLiveMetrics(false)
.start();
this.telemetryClient = appInsights.defaultClient;
}

public async runWithTelemetry() {
// Send app start telemetry
const startTime = new Date().getTime();
const telemetryProperties = {
ciName: this.ciCore.getCiName()
}
this.telemetryClient.trackEvent({
name: 'AppStart',
properties: telemetryProperties
});

var result: any;
try {
result = await this.run();
} catch (err) {
// Send error telemetry
this.telemetryClient.trackException({
exception: err,
properties: telemetryProperties
});
this.telemetryClient.flush()

throw err;
}

// Send performance telemetry
const endTime = new Date().getTime();
const elapsedTime = endTime - startTime;
this.telemetryClient.trackMetric({
name: 'RunPerformance',
value: elapsedTime,
properties: telemetryProperties
});
this.telemetryClient.flush()

return result;
}

public async run() {
private async run() {
const baseAppPath = this.ciCore.getInput('baseAppPath');
const targetAppPath = this.ciCore.getInput('targetAppPath');
const baseAppLabel = this.ciCore.getInput('baseAppLabel');
Expand Down Expand Up @@ -96,10 +155,10 @@ export default class CiRunner {

// Check if thresholds are adhered to
if (!this.thresholdChecker.checkThresholds(comparisionReport)) {
throw 'App size increased significantly in at least one metric!';
this.ciCore.markAsFailed('App size increased significantly in at least one metric!');
}

console.log(comparisionReport);

return comparisionReport;
}

Expand Down
10 changes: 9 additions & 1 deletion src/githubAction/GithubActionRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { isUndefined } from 'util';

export class GithubCiCore implements CiCore {

getCiName(): string {
return 'GitHub';
}

getInput(name: string): string {
const value = ghAction.getInput(name);
return isUndefined(value) ? "" : value;
Expand All @@ -24,6 +28,10 @@ export class GithubCiCore implements CiCore {
logError(message: string) {
return ghAction.error(message);
}

markAsFailed(errorMessage: string) {
return ghAction.setFailed(errorMessage);
}
}

export default class GithubActionRunner {
Expand All @@ -32,7 +40,7 @@ export default class GithubActionRunner {
try {
const githubCore = new GithubCiCore();
const ciRunner = new CiRunner(githubCore);
await ciRunner.run();
await ciRunner.runWithTelemetry();
}
catch (err) {
ghAction.setFailed(err.message);
Expand Down
2 changes: 1 addition & 1 deletion src/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"version": {
"Major": 0,
"Minor": 0,
"Patch": 27
"Patch": 28
},
"instanceNameFormat": "Android App size change - $(baseAppPath) vs $(targetAppPath)",
"inputs": [
Expand Down