Skip to content

Commit

Permalink
Implement instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
adunkman committed Sep 2, 2021
1 parent b94aec1 commit e5b8715
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 1 deletion.
3 changes: 3 additions & 0 deletions bot/actions/closeMessage.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import newrelic from "newrelic";
import { AllMiddlewareArgs, BlockAction, ButtonAction, SlackActionMiddlewareArgs } from "@slack/bolt";

export const CLOSE_MESSAGE_ACTION_ID = "close_ephemeral_message";
Expand All @@ -6,6 +7,8 @@ export async function handleMessageClose(event: SlackActionMiddlewareArgs<BlockA
// We must acknowledge this event, or it will show as a UI error.
event.ack();

newrelic.incrementMetric("Actions/messageClose");

await event.respond({
delete_original: true
});
Expand Down
3 changes: 3 additions & 0 deletions bot/actions/menuClick.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import newrelic from "newrelic";
import { AllMiddlewareArgs, BlockAction, OverflowAction, SlackActionMiddlewareArgs } from "@slack/bolt";

export const OVERFLOW_MENU_CLICK_ACTION_ID = "overflow_menu_click";

export async function handleMenuClick(event: SlackActionMiddlewareArgs<BlockAction<OverflowAction>> & AllMiddlewareArgs) {
// We must acknowledge this event, or it will show as a UI error.
await event.ack();

newrelic.incrementMetric("Actions/menuClick");
}
5 changes: 4 additions & 1 deletion bot/actions/message.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import newrelic from "newrelic";
import { AllMiddlewareArgs, GenericMessageEvent, SlackEventMiddlewareArgs } from "@slack/bolt";
import { WebAPICallResult } from "@slack/web-api";
import { capitalize } from "../helpers/capitalize";
Expand All @@ -20,6 +21,7 @@ export async function handleMessage(event: SlackEventMiddlewareArgs<'message'> &

if (matches.length === 0) {
// This message contains only ignored items.
newrelic.incrementMetric("Actions/message/ignoredTerm");
return;
}

Expand All @@ -34,7 +36,8 @@ export async function handleMessage(event: SlackEventMiddlewareArgs<'message'> &
const random = Math.floor(Math.random() * trigger.alternatives.length);
const alternative = trigger.alternatives[random];
const why = (trigger.why || '').replace(/:TERM:/gi, capitalize(text));
return `Instead of saying “${text.toLowerCase()},” how about *${alternative}*? ${why}`;
newrelic.incrementMetric(`Actions/message/term/${text.toLowerCase()}`);
return `Instead of saying “${text.toLowerCase()},” how about *${alternative}*? You may be able to edit your message. ${why}`;
});

actions.push(event.client.chat.postEphemeral({
Expand Down
2 changes: 2 additions & 0 deletions bot/actions/messageEdited.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import newrelic from "newrelic";
import { AllMiddlewareArgs, GenericMessageEvent, MessageChangedEvent, SlackEventMiddlewareArgs } from "@slack/bolt";
import { config } from "../triggers/triggers";

Expand Down Expand Up @@ -42,6 +43,7 @@ export async function handleMessageEdited(event: SlackEventMiddlewareArgs<'messa
if (!message.text.match(config.allTriggersRegExp)) {
// The user has adjusted their language and it no longer matches our
// triggers.
newrelic.incrementMetric("Actions/messageEdited/termRemoved");
await event.client.reactions.remove({
name: config.emoji,
channel: messageChanged.channel,
Expand Down
6 changes: 6 additions & 0 deletions bot/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ const receiver = new ExpressReceiver({
logLevel: LogLevel.INFO,
installationStore: {
storeInstallation: async (installation) => {
newrelic.incrementMetric("InstallationStore/storeInstallation");

await Installation.create({
id: installation.isEnterpriseInstall
? installation.enterprise.id
Expand All @@ -47,13 +49,17 @@ const receiver = new ExpressReceiver({
});
},
fetchInstallation: async (query) => {
newrelic.incrementMetric("InstallationStore/fetchInstallation");

const installation = await Installation.findByPk(
query.isEnterpriseInstall ? query.enterpriseId : query.teamId
);

return installation.installationObject as InstallationType;
},
deleteInstallation: async (query) => {
newrelic.incrementMetric("InstallationStore/deleteInstallation");

const installation = await Installation.findByPk(
query.isEnterpriseInstall ? query.enterpriseId : query.teamId
);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"dependencies": {
"@slack/bolt": "^3.6.0",
"@types/js-yaml": "^4.0.3",
"@types/newrelic": "^7.0.2",
"@types/node": "^16.7.4",
"js-yaml": "^4.1.0",
"newrelic": "^8.2.0",
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,11 @@
resolved "https://registry.yarnpkg.com/@types/mime/-/mime-1.3.2.tgz#93e25bf9ee75fe0fd80b594bc4feb0e862111b5a"
integrity sha512-YATxVxgRqNH6nHEIsvg6k2Boc1JHI9ZbH5iWFFv/MTkchz3b1ieGDa5T0a9RznNdI0KhVbdbWSN+KWWrQZRxTw==

"@types/newrelic@^7.0.2":
version "7.0.2"
resolved "https://registry.yarnpkg.com/@types/newrelic/-/newrelic-7.0.2.tgz#232746fdaba0033e5b246727d5d836c64d5da9ad"
integrity sha512-+PPoS4y66YypF2tvTdJD2aXSGPpOWaDsJ5vPs/e3ejfrOB4bKSCrpH6nnxIeV12pv1upRsKfuXNFtaH1pGQ0MA==

"@types/node@*", "@types/node@>=12", "@types/node@>=12.0.0", "@types/node@^16.7.4":
version "16.7.4"
resolved "https://registry.yarnpkg.com/@types/node/-/node-16.7.4.tgz#68a9384694af63ceab8848e95d76d9a3519e84b6"
Expand Down

0 comments on commit e5b8715

Please sign in to comment.