Skip to content

Commit

Permalink
refactor: enhance logging for Slack API and IncidentService, improve …
Browse files Browse the repository at this point in the history
…code formatting and readability
  • Loading branch information
simlarsen committed Mar 4, 2025
1 parent 0acee70 commit 8472967
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 27 deletions.
3 changes: 3 additions & 0 deletions Common/Server/API/SlackAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,9 @@ export default class SlackAPI {
"/slack/interactive",
SlackAuthorization.isAuthorizedSlackRequest,
async (req: ExpressRequest, res: ExpressResponse) => {
logger.debug("Slack Interactive Request: ");
logger.debug(JSON.stringify(JSON.parse(req.body.payload), null, 2));

const authResult: SlackRequest = await SlackAuthAction.isAuthorized({
req: req,
});
Expand Down
51 changes: 24 additions & 27 deletions Common/Server/Services/IncidentService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,16 +342,16 @@ export class Service extends DatabaseService<Model> {

logger.debug(
"Mutex acquired - IncidentService.incident-create " +
projectId.toString() +
" at " +
OneUptimeDate.getCurrentDateAsFormattedString(),
projectId.toString() +
" at " +
OneUptimeDate.getCurrentDateAsFormattedString(),
);
} catch (err) {
logger.debug(
"Mutex acquire failed - IncidentService.incident-create " +
projectId.toString() +
" at " +
OneUptimeDate.getCurrentDateAsFormattedString(),
projectId.toString() +
" at " +
OneUptimeDate.getCurrentDateAsFormattedString(),
);
logger.error(err);
}
Expand Down Expand Up @@ -429,16 +429,16 @@ export class Service extends DatabaseService<Model> {
await Semaphore.release(mutex);
logger.debug(
"Mutex released - IncidentService.incident-create " +
projectId.toString() +
" at " +
OneUptimeDate.getCurrentDateAsFormattedString(),
projectId.toString() +
" at " +
OneUptimeDate.getCurrentDateAsFormattedString(),
);
} catch (err) {
logger.debug(
"Mutex release failed - IncidentService.incident-create " +
projectId.toString() +
" at " +
OneUptimeDate.getCurrentDateAsFormattedString(),
projectId.toString() +
" at " +
OneUptimeDate.getCurrentDateAsFormattedString(),
);
logger.error(err);
}
Expand All @@ -447,7 +447,6 @@ export class Service extends DatabaseService<Model> {
const createdByUserId: ObjectID | undefined | null =
createdItem.createdByUserId || createdItem.createdByUser?.id;


// send message to workspaces - slack, teams, etc.
const workspaceResult: {
channelsCreated: Array<WorkspaceChannel>;
Expand Down Expand Up @@ -534,9 +533,9 @@ ${createdItem.remediationNotes || "No remediation notes provided."}
createdItem.changeMonitorStatusToId,
true, // notifyMonitorOwners
createdItem.rootCause ||
"Status was changed because incident " +
createdItem.id.toString() +
" was created.",
"Status was changed because incident " +
createdItem.id.toString() +
" was created.",
createdItem.createdStateLog,
onCreate.createBy.props,
);
Expand All @@ -560,8 +559,6 @@ ${createdItem.remediationNotes || "No remediation notes provided."}
},
});



// add owners.

if (
Expand All @@ -573,9 +570,9 @@ ${createdItem.remediationNotes || "No remediation notes provided."}
createdItem.projectId,
createdItem.id,
(onCreate.createBy.miscDataProps["ownerUsers"] as Array<ObjectID>) ||
[],
[],
(onCreate.createBy.miscDataProps["ownerTeams"] as Array<ObjectID>) ||
[],
[],
false,
onCreate.createBy.props,
);
Expand Down Expand Up @@ -876,10 +873,10 @@ ${onUpdate.updateBy.data.remediationNotes || "No remediation notes provided."}
feedInfoInMarkdown += `\n\n**Labels**:
${labels
.map((label: Label) => {
return `- ${label.name}`;
})
.join("\n")}
.map((label: Label) => {
return `- ${label.name}`;
})
.join("\n")}
`;

shouldAddIncidentFeed = true;
Expand Down Expand Up @@ -1037,7 +1034,7 @@ ${incidentSeverity.name}
if (
latestState &&
latestState.monitorStatusId?.toString() ===
resolvedMonitorState.id!.toString()
resolvedMonitorState.id!.toString()
) {
// already on this state. Skip.
continue;
Expand Down Expand Up @@ -1150,7 +1147,7 @@ ${incidentSeverity.name}
lastIncidentStatusTimeline &&
lastIncidentStatusTimeline.incidentStateId &&
lastIncidentStatusTimeline.incidentStateId.toString() ===
incidentStateId.toString()
incidentStateId.toString()
) {
return;
}
Expand Down Expand Up @@ -1368,7 +1365,7 @@ ${incidentSeverity.name}
timeToResolveMetric.description = "Time taken to resolve the incident";
timeToResolveMetric.value = OneUptimeDate.getDifferenceInSeconds(
resolvedIncidentStateTimeline?.startsAt ||
OneUptimeDate.getCurrentDate(),
OneUptimeDate.getCurrentDate(),
incidentStartsAt,
);
timeToResolveMetric.unit = "seconds";
Expand Down
5 changes: 5 additions & 0 deletions Common/Server/Utils/Workspace/Slack/Actions/Auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export default class SlackAuthAction {

// if there are no actions then return.
if (!payload["actions"] || (payload["actions"] as JSONArray).length === 0) {
logger.debug("No actions found in payload. Returning unauthorized.");
return {
isAuthorized: false,
};
Expand Down Expand Up @@ -117,6 +118,9 @@ export default class SlackAuthAction {
const projectId: ObjectID | undefined = projectAuth.projectId;

if (!projectId) {
logger.debug(
"Project ID not found in project auth. Returning unauthorized.",
);
return {
isAuthorized: false,
};
Expand Down Expand Up @@ -162,6 +166,7 @@ export default class SlackAuthAction {
});

// clear response.
logger.debug("User auth not found. Returning unauthorized.");
return {
isAuthorized: false,
};
Expand Down

0 comments on commit 8472967

Please sign in to comment.