Skip to content
This repository has been archived by the owner on Jan 15, 2025. It is now read-only.

Commit

Permalink
[#IOPID-106] add assertionRef to exception, enable sampling
Browse files Browse the repository at this point in the history
  • Loading branch information
gquadrati committed May 2, 2023
1 parent 957d2b4 commit aa4c051
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
55 changes: 55 additions & 0 deletions HandlePubKeyRevoke/__tests__/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,10 @@ describe("handleRevoke", () => {
expect(mockAppinsights.trackException).toHaveBeenCalled();
expect(mockAppinsights.trackException).toHaveBeenCalledWith(
expect.objectContaining({
tagOverrides: { samplingEnabled: "false" },
properties: expect.objectContaining({
detail: "PERMANENT",
assertionRef: "unknown",
name: "lollipop.pubKeys.revoke.failure",
retryCount: "1",
maxRetryCount: "5"
Expand Down Expand Up @@ -128,11 +130,13 @@ describe("handleRevoke", () => {
expect(mockAppinsights.trackException).toHaveBeenCalled();
expect(mockAppinsights.trackException).toHaveBeenCalledWith(
expect.objectContaining({
tagOverrides: { samplingEnabled: "true" },
properties: expect.objectContaining({
detail: "TRANSIENT",
fatal: "false",
isSuccess: "false",
modelId: "",
assertionRef: aValidAssertionRef,
name: "lollipop.pubKeys.revoke.failure",
retryCount: "1",
maxRetryCount: "5"
Expand All @@ -145,6 +149,47 @@ describe("handleRevoke", () => {
]);
});

it("GIVEN a valid revoke message WHEN findLastVersion fails the last retry THEN it should throw with a transient failure without sampling", async () => {
findLastVersionByModelIdMock.mockImplementationOnce(() =>
TE.left(toCosmosErrorResponse("Cannot reach cosmosDB"))
);
await expect(
handleRevoke(
{
...contextMock,
executionContext: {
retryContext: { retryCount: 4, maxRetryCount: 5 }
}
},
mockAppinsights as any,
lollipopKeysModelMock,
masterAlgo,
aValidRevokeInput
)
).rejects.toBeDefined();

expect(mockAppinsights.trackException).toHaveBeenCalled();
expect(mockAppinsights.trackException).toHaveBeenCalledWith(
expect.objectContaining({
tagOverrides: { samplingEnabled: "false" },
properties: expect.objectContaining({
detail: "TRANSIENT",
fatal: "false",
isSuccess: "false",
modelId: "",
assertionRef: aValidAssertionRef,
name: "lollipop.pubKeys.revoke.failure",
retryCount: "4",
maxRetryCount: "5"
})
})
);
expect(findLastVersionByModelIdMock).toHaveBeenCalledTimes(1);
expect(findLastVersionByModelIdMock).toHaveBeenCalledWith([
aValidRevokeInput.assertion_ref
]);
});

it("GIVEN a valid revoke message WHEN findLastVersion returns none THEN it should success without perform any upsert", async () => {
findLastVersionByModelIdMock.mockImplementationOnce(() => TE.right(O.none));
const result = await handleRevoke(
Expand Down Expand Up @@ -243,8 +288,10 @@ describe("handleRevoke", () => {
expect(mockAppinsights.trackException).toHaveBeenCalled();
expect(mockAppinsights.trackException).toHaveBeenCalledWith(
expect.objectContaining({
tagOverrides: { samplingEnabled: "false" },
properties: expect.objectContaining({
detail: "PERMANENT",
assertionRef: aValidAssertionRef,
name: "lollipop.pubKeys.revoke.failure",
retryCount: "1",
maxRetryCount: "5"
Expand Down Expand Up @@ -285,8 +332,10 @@ describe("handleRevoke", () => {
expect(mockAppinsights.trackException).toHaveBeenCalled();
expect(mockAppinsights.trackException).toHaveBeenCalledWith(
expect.objectContaining({
tagOverrides: { samplingEnabled: "false" },
properties: expect.objectContaining({
detail: "PERMANENT",
assertionRef: aValidAssertionRef,
name: "lollipop.pubKeys.revoke.failure",
retryCount: "1",
maxRetryCount: "5"
Expand Down Expand Up @@ -331,8 +380,10 @@ describe("handleRevoke", () => {
expect(mockAppinsights.trackException).toHaveBeenCalled();
expect(mockAppinsights.trackException).toHaveBeenCalledWith(
expect.objectContaining({
tagOverrides: { samplingEnabled: "true" },
properties: expect.objectContaining({
detail: "TRANSIENT",
assertionRef: aValidAssertionRef,
name: "lollipop.pubKeys.revoke.failure",
retryCount: "1",
maxRetryCount: "5"
Expand Down Expand Up @@ -371,10 +422,12 @@ describe("handleRevoke", () => {
expect(mockAppinsights.trackException).toHaveBeenCalled();
expect(mockAppinsights.trackException).toHaveBeenCalledWith(
expect.objectContaining({
tagOverrides: { samplingEnabled: "true" },
properties: expect.objectContaining({
detail: "TRANSIENT",
name: "lollipop.pubKeys.revoke.failure",
retryCount: "1",
assertionRef: aValidAssertionRef,
maxRetryCount: "5",
errorMessage: expect.stringContaining(
"Cannot find a master lollipopPubKey"
Expand Down Expand Up @@ -414,8 +467,10 @@ describe("handleRevoke", () => {
expect(mockAppinsights.trackException).toHaveBeenCalled();
expect(mockAppinsights.trackException).toHaveBeenCalledWith(
expect.objectContaining({
tagOverrides: { samplingEnabled: "true" },
properties: expect.objectContaining({
detail: "TRANSIENT",
assertionRef: aValidAssertionRef,
errorMessage: expect.stringContaining(
"Cannot decode a VALID master lollipopPubKey"
),
Expand Down
20 changes: 19 additions & 1 deletion HandlePubKeyRevoke/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,15 @@ const extractPubKeysToRevoke = (
)
);

/**
*
* @param context The function context
* @returns `true` if retryCount >= maxRetryCount-1, `false` otherwise
*/
const isLastRetry = (context: Context): boolean =>
(context.executionContext.retryContext?.retryCount ?? 0) >=
(context.executionContext.retryContext?.maxRetryCount ?? 0) - 1;

const revokePubKey = (lollipopKeysModel: LolliPOPKeysModel) => (
notPendingLollipopPubKey: NotPendingLolliPopPubKeys
): TE.TaskEither<CosmosErrors, RetrievedLolliPopPubKeys> =>
Expand Down Expand Up @@ -164,6 +173,12 @@ export const handleRevoke = (
trackException(telemetryClient, {
exception: new Error(error),
properties: {
assertionRef: pipe(
rawRevokeMessage,
RevokeAssertionRefInfo.decode,
E.map(message => message.assertion_ref),
E.getOrElse(() => "unknown")
),
detail: err.kind,
errorMessage: error,
fatal: PermanentFailure.is(err).toString(),
Expand All @@ -177,7 +192,10 @@ export const handleRevoke = (
context.executionContext.retryContext?.retryCount ?? "undefined"
)
},
tagOverrides: { samplingEnabled: "false" }
tagOverrides: {
samplingEnabled:
!isTransient || isLastRetry(context) ? "false" : "true"
}
});
context.log.error(error);
if (isTransient) {
Expand Down

0 comments on commit aa4c051

Please sign in to comment.