Skip to content

Fix slack alert retries and text length limits #1353

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

Merged
merged 10 commits into from
Sep 25, 2024
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
10 changes: 9 additions & 1 deletion apps/webapp/app/models/orgIntegration.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,15 @@ export class OrgIntegrationRepository {
return new WebClient(
options?.forceBotToken
? secret.botAccessToken
: secret.userAccessToken ?? secret.botAccessToken
: secret.userAccessToken ?? secret.botAccessToken,
{
retryConfig: {
retries: 2,
randomize: true,
maxTimeout: 5000,
maxRetryTime: 10000,
},
}
) as AuthenticatedClientForIntegration<TService>;
}
default: {
Expand Down
75 changes: 58 additions & 17 deletions apps/webapp/app/v3/services/alerts/deliverAlert.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ type FoundAlert = Prisma.Result<
>;
};

class SkipRetryError extends Error {}

export class DeliverAlertService extends BaseService {
public async call(alertId: string) {
const alert: FoundAlert | null = await this._prisma.projectAlert.findFirst({
Expand Down Expand Up @@ -136,22 +138,34 @@ export class DeliverAlertService extends BaseService {
alert.failedAttempt = finishedAttempt;
}

switch (alert.channel.type) {
case "EMAIL": {
await this.#sendEmail(alert);
break;
}
case "SLACK": {
await this.#sendSlack(alert);
break;
}
case "WEBHOOK": {
await this.#sendWebhook(alert);
break;
try {
switch (alert.channel.type) {
case "EMAIL": {
await this.#sendEmail(alert);
break;
}
case "SLACK": {
await this.#sendSlack(alert);
break;
}
case "WEBHOOK": {
await this.#sendWebhook(alert);
break;
}
default: {
assertNever(alert.channel.type);
}
}
default: {
assertNever(alert.channel.type);
} catch (error) {
if (error instanceof SkipRetryError) {
logger.error("[DeliverAlert] Skipping retry", {
reason: error.message,
});

return;
}

throw error;
}

await this._prisma.projectAlert.update({
Expand Down Expand Up @@ -617,7 +631,7 @@ export class DeliverAlertService extends BaseService {
type: "section",
text: {
type: "mrkdwn",
text: `\`\`\`${error.stackTrace ?? error.message}\`\`\``,
text: this.#wrapInCodeBlock(error.stackTrace ?? error.message),
},
},
{
Expand Down Expand Up @@ -729,7 +743,7 @@ export class DeliverAlertService extends BaseService {
type: "section",
text: {
type: "mrkdwn",
text: `\`\`\`${error.stackTrace ?? error.message}\`\`\``,
text: this.#wrapInCodeBlock(error.stackTrace ?? error.message),
},
},
{
Expand Down Expand Up @@ -829,7 +843,7 @@ export class DeliverAlertService extends BaseService {
type: "section",
text: {
type: "mrkdwn",
text: `\`\`\`${preparedError.stack ?? preparedError.message}\`\`\``,
text: this.#wrapInCodeBlock(preparedError.stack ?? preparedError.message),
},
},
{
Expand Down Expand Up @@ -1010,6 +1024,14 @@ export class DeliverAlertService extends BaseService {
message,
});

if (error.data.error === "invalid_blocks") {
logger.error("[DeliverAlert] Slack invalid blocks", {
error,
});

throw new SkipRetryError("Slack invalid blocks");
}

throw new Error("Slack platform error");
}

Expand Down Expand Up @@ -1047,6 +1069,25 @@ export class DeliverAlertService extends BaseService {
};
}

#wrapInCodeBlock(text: string, maxLength = 3000) {
return `\`\`\`${this.#truncateSlackText(text, maxLength - 10)}\`\`\``;
}

#truncateSlackText(text: string, length = 3000) {
if (text.length > length) {
logger.debug("[DeliverAlert] Truncating slack text", {
length,
originalLength: text.length,
});

const truncationSuffix = "\n\ntruncated - check dashboard for complete error message";

return text.slice(0, length - truncationSuffix.length) + truncationSuffix;
}

return text;
}

static async enqueue(
alertId: string,
tx: PrismaClientOrTransaction,
Expand Down