Skip to content

Commit

Permalink
add track event when createorupdateinstallation ends
Browse files Browse the repository at this point in the history
  • Loading branch information
Garma00 committed Jun 20, 2024
1 parent 9a5c94f commit 694c560
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { NotificationHubConfig } from "../../utils/notificationhubServicePartiti
import { envConfig } from "../../__mocks__/env-config.mock";
import { createActivity } from "../../utils/durable/activities";
import { NotificationHubsClient } from "@azure/notification-hubs";
import { TelemetryClient } from "applicationinsights";

const activityName = "any";

Expand Down Expand Up @@ -48,11 +49,15 @@ const mockBuildNHClient = jest
_ => (mockNotificationHubService as unknown) as NotificationHubsClient
);

const mockTelemetryClient = ({
trackEvent: jest.fn(() => {})
} as unknown) as TelemetryClient;

const handler = createActivity(
activityName,
ActivityInput, // FIXME: the editor marks it as type error, but tests compile correctly
ActivityResultSuccess,
getActivityBody(mockBuildNHClient)
getActivityBody(mockBuildNHClient, mockTelemetryClient)
);

describe("HandleNHCreateOrUpdateInstallationCallActivity", () => {
Expand Down
33 changes: 30 additions & 3 deletions HandleNHCreateOrUpdateInstallationCallActivity/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { pipe } from "fp-ts/lib/function";
import * as TE from "fp-ts/lib/TaskEither";
import * as t from "io-ts";
import { NotificationHubsClient } from "@azure/notification-hubs";
import { TelemetryClient } from "applicationinsights";
import { toString } from "../utils/conversions";

import { InstallationId } from "../generated/notifications/InstallationId";
Expand Down Expand Up @@ -36,7 +37,8 @@ const getPlatformFromPlatformEnum = (
): "apns" | "fcmv1" => (platformEnum === "apns" ? "apns" : "fcmv1");

export const getActivityBody = (
buildNHClient: (nhConfig: NotificationHubConfig) => NotificationHubsClient
buildNHClient: (nhConfig: NotificationHubConfig) => NotificationHubsClient,
telemetryClient: TelemetryClient
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
): ActivityBodyImpl => ({ input, logger }) => {
logger.info(`INSTALLATION_ID=${input.installationId}`);
Expand All @@ -50,8 +52,33 @@ export const getActivityBody = (
input.tags
),
TE.bimap(
e => retryActivity(logger, toString(e)),
() => ActivityResultSuccess.encode({ kind: "SUCCESS" })
e => {
telemetryClient.trackEvent({
name: "api.messages.notification.createOrUpdateInstallation.failure",
properties: {
installationId: input.installationId,
isSuccess: "false",
notificationHubName: input.notificationHubConfig.AZURE_NH_HUB_NAME,
platform: input.platform,
reason: e.message
},
tagOverrides: { samplingEnabled: "false" }
});
return retryActivity(logger, toString(e));
},
() => {
telemetryClient.trackEvent({
name: "api.messages.notification.createOrUpdateInstallation.success",
properties: {
installationId: input.installationId,
isSuccess: "true",
notificationHubName: input.notificationHubConfig.AZURE_NH_HUB_NAME,
platform: input.platform
},
tagOverrides: { samplingEnabled: "false" }
});
return ActivityResultSuccess.encode({ kind: "SUCCESS" });
}
)
);
};
8 changes: 7 additions & 1 deletion HandleNHCreateOrUpdateInstallationCallActivity/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { RetryOptions } from "durable-functions";
import { initTelemetryClient } from "../utils/appinsights";
import { getConfigOrThrow } from "../utils/config";

import { createActivity } from "../utils/durable/activities";
import * as o from "../utils/durable/orchestrators";
Expand All @@ -13,6 +15,10 @@ export { ActivityInput, ActivityResultSuccess } from "./handler";

export const activityName = "HandleNHCreateOrUpdateInstallationCallActivity";

const config = getConfigOrThrow();

const telemetryClient = initTelemetryClient(config);

/**
* Build a `CreateOrUpdateActivity` to be called by an Orchestrator
*
Expand All @@ -32,7 +38,7 @@ const activityFunctionHandler = createActivity(
activityName,
ActivityInput,
ActivityResultSuccess,
getActivityBody(buildNHClient)
getActivityBody(buildNHClient, telemetryClient)
);

export default activityFunctionHandler;
4 changes: 1 addition & 3 deletions utils/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,7 @@ export const getInstallationFromInstallationId = (
E.mapLeft(
() =>
new Error(
`Error while retrieving the installation with installationId: ${installationId} | ${JSON.stringify(
error
)}`
`Error while retrieving the installation with installationId: ${installationId}`
)
),
E.map(() => O.none),
Expand Down

0 comments on commit 694c560

Please sign in to comment.