Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#172884826] Automatic download event tracking #60

Merged
merged 37 commits into from
Jul 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
cec9bc9
user data download orchestrator
Jul 6, 2020
e358919
better modeling
Jul 6, 2020
81f2f56
sub orchestrator
Jul 7, 2020
9821096
timer
Jul 7, 2020
78a2063
lint fix
Jul 7, 2020
c0b2a50
exlude DELETE choices
Jul 7, 2020
52d17ed
lint fix
Jul 7, 2020
bb6c548
Update UserDataDownloadSubOrchestrator/handler.ts
balanza Jul 8, 2020
2e5ce86
fix delay
Jul 8, 2020
3db3333
pause after wip
Jul 8, 2020
a145811
reviewed orchestrator workflow
Jul 9, 2020
9cccdf1
fix lint
Jul 9, 2020
f30eb1e
Update UserDataDownloadSubOrchestrator/handler.ts
balanza Jul 9, 2020
79087fa
Update UserDataDownloadSubOrchestrator/__tests__/handler.test.ts
balanza Jul 9, 2020
63b8af6
Update UserDataDownloadOrchestrator/__tests__/handler.test.ts
balanza Jul 9, 2020
56f26a6
refactor
Jul 9, 2020
e72e61b
Merge branch '172884826-automatic-download' of github.com:pagopa/io-f…
Jul 9, 2020
8e28f3f
remove delay
Jul 10, 2020
7977438
fix lint
Jul 10, 2020
d1e0a4a
env vars
Jul 10, 2020
4226fb9
await all
Jul 13, 2020
479a359
fix lint
Jul 13, 2020
a3ea395
use id
Jul 13, 2020
849b7c9
fix lint
Jul 13, 2020
e50ba7f
use global storage for messages
Jul 13, 2020
e07b84d
readme fixes and small refactor
Jul 14, 2020
8033bd0
revert readme change
Jul 14, 2020
d6bc106
add env into readme
Jul 14, 2020
97b8e9b
fix lint
Jul 14, 2020
f6d40a5
fix entrypoint
Jul 14, 2020
579bbbd
fix path
Jul 14, 2020
fcd72d0
fix typos
Jul 14, 2020
bb1878b
fix decode
Jul 14, 2020
b0bf438
Merge branch '172884826-automatic-download' of https://github.com/pag…
Jul 14, 2020
a3dc118
adding ai events
Jul 16, 2020
1dbaafb
Merge branch 'master' into 172884826-automatic-download
Jul 16, 2020
439bc19
Merge branch 'master' into 172884826-automatic-download-event-tracking
Jul 16, 2020
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
29 changes: 29 additions & 0 deletions UserDataDownloadOrchestrator/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ActivityResultSuccess as ExtractUserDataActivityResultSuccess } from ".
import { ActivityResultSuccess as SendUserDataDownloadMessageActivityResultSuccess } from "../SendUserDataDownloadMessageActivity/handler";
import { ActivityResultSuccess as SetUserDataProcessingStatusActivityResultSuccess } from "../SetUserDataProcessingStatusActivity/handler";
import { ProcessableUserDataDownload } from "../UserDataProcessingTrigger";
import { trackEvent, trackException } from "../utils/appinsights";

const logPrefix = "UserDataDownloadOrchestrator";

Expand Down Expand Up @@ -142,8 +143,28 @@ export const handler = function*(
status: UserDataProcessingStatusEnum.CLOSED
});
});

trackEvent({
// tslint:disable-next-line: no-duplicate-string
name: "user.data.download",
properties: {
userDataProcessingId: currentUserDataProcessing.userDataProcessingId
},
tagOverrides: {
"ai.operation.id": currentUserDataProcessing.userDataProcessingId,
"ai.operation.parentId": currentUserDataProcessing.userDataProcessingId
}
});

return OrchestratorSuccess.encode({ kind: "SUCCESS" });
} catch (error) {
trackException({
exception: new Error(error),
properties: {
name: "user.data.download",
userDataProcessingId: currentUserDataProcessing.userDataProcessingId
}
});
context.log.error(
`${logPrefix}|ERROR|Failed processing user data for download: ${error.message}`
);
Expand All @@ -153,6 +174,14 @@ export const handler = function*(
nextStatus: UserDataProcessingStatusEnum.FAILED
})
).getOrElseL(err => {
trackException({
exception: new Error(readableReport(err)),
properties: {
name: "user.data.download",
type: "unhandled exception when trying to set document as FAILED",
userDataProcessingId: currentUserDataProcessing.userDataProcessingId
}
});
throw new Error(
`Activity SetUserDataProcessingStatusActivity (status=FAILED) failed: ${readableReport(
err
Expand Down
4 changes: 4 additions & 0 deletions __mocks__/applicationinsights.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const defaultClient = {
trackEvent: jest.fn(),
trackException: jest.fn()
};
41 changes: 41 additions & 0 deletions utils/appinsights.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import * as ai from "applicationinsights";
import {
EventTelemetry,
ExceptionTelemetry
} from "applicationinsights/out/Declarations/Contracts";
import { fromNullable } from "fp-ts/lib/Option";
import { tryCatch } from "fp-ts/lib/Option";
import { initAppInsights } from "italia-ts-commons/lib/appinsights";
import { IntegerFromString } from "italia-ts-commons/lib/numbers";
import { NonEmptyString } from "italia-ts-commons/lib/strings";

// the internal function runtime has MaxTelemetryItem per second set to 20 by default
// @see https://github.com/Azure/azure-functions-host/blob/master/src/WebJobs.Script/Config/ApplicationInsightsLoggerOptionsSetup.cs#L29
const DEFAULT_SAMPLING_PERCENTAGE = 20;

// Avoid to initialize Application Insights more than once
export const initTelemetryClient = (env = process.env) =>
ai.defaultClient
? ai.defaultClient
: NonEmptyString.decode(env.APPINSIGHTS_INSTRUMENTATIONKEY).fold(
_ => undefined,
k =>
initAppInsights(k, {
disableAppInsights: env.APPINSIGHTS_DISABLE === "true",
samplingPercentage: IntegerFromString.decode(
env.APPINSIGHTS_SAMPLING_PERCENTAGE
).getOrElse(DEFAULT_SAMPLING_PERCENTAGE)
})
);

export const trackEvent = (event: EventTelemetry) => {
fromNullable(initTelemetryClient()).map(_ =>
tryCatch(() => _.trackEvent(event))
);
};

export const trackException = (event: ExceptionTelemetry) => {
fromNullable(initTelemetryClient()).map(_ =>
tryCatch(() => _.trackException(event))
);
};