Skip to content

Commit

Permalink
#patch added newrelic logging
Browse files Browse the repository at this point in the history
  • Loading branch information
jgaribsin committed Mar 9, 2024
1 parent 79a2384 commit b93e4d6
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 14 deletions.
6 changes: 4 additions & 2 deletions src/api-wrapper/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
getSectorName,
} from './mapping';
import {existsSync, mkdirSync, writeFileSync} from 'fs';
import {compressFile, dayjs, writeGzipJson} from '../handlers';
import {compressFile, dayjs, logger, writeGzipJson} from '../handlers';
import path from 'path';
import {getAllPlanets} from './planets';
import axios from 'axios';
Expand Down Expand Up @@ -68,7 +68,9 @@ for (const season of seasons.seasons) {
export async function getData() {
const season = seasons.current;
const fileTimestamp = dayjs().format('DD_MM_YYYYTHH_mm_ssZZ[UTC]');
console.log(`Fetching data for season ${season} at ${fileTimestamp}`);
logger.info(`Fetching data for season ${season} at ${fileTimestamp}`, {
type: 'info',
});

if (!existsSync(path.join('api_responses', String(season))))
mkdirSync(path.join('api_responses', String(season)), {recursive: true});
Expand Down
16 changes: 13 additions & 3 deletions src/events/onInteraction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import {
ownerCmds,
planetAutoCmds,
} from '../commands';
import {checkPerms, commandErrorEmbed, ownerCommandEmbed} from '../handlers';
import {
checkPerms,
commandErrorEmbed,
logger,
ownerCommandEmbed,
} from '../handlers';
import {go as search} from 'fuzzysort';
import {mappedNames} from '../api-wrapper';

Expand Down Expand Up @@ -75,7 +80,12 @@ const onInteraction = async (interaction: Interaction) => {
await commandHash[command](interaction);

const time = `${Date.now() - start}ms`;
console.log(`Executed command /${command} in ${time}`);
logger.info(`Executed command /${command} in ${time}`, {
time,
command,
type: 'command',
user: interaction.user.tag,
});

return;
} catch (err) {
Expand All @@ -84,7 +94,7 @@ const onInteraction = async (interaction: Interaction) => {
// TODO: handle other error types explicitly eg. discordjs

// Log error and additional info for debugging
console.log(error.message, {
logger.info(error.message, {
command: interaction.commandName,
args: interaction.options.data,
user: interaction.user.tag,
Expand Down
22 changes: 16 additions & 6 deletions src/events/onReady.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {commandHash, commandList, presenceCmds} from '../commands';
import {config, isProd} from '../config';
import {getData, mappedNames} from '../api-wrapper';
import {db, eq, persistentMessages} from '../db';
import {warStatusEmbeds} from '../handlers';
import {logger, warStatusEmbeds} from '../handlers';

// bot client token, for use with discord API
const BOT_TOKEN = config.BOT_TOKEN;
Expand All @@ -23,7 +23,9 @@ const onReady = async (client: Client) => {

const rest = new REST().setToken(BOT_TOKEN);

console.log(`Serving ${serverCount} servers as ${client.user?.tag}`);
logger.info(`Serving ${serverCount} servers as ${client.user?.tag}`, {
type: 'startup',
});

// two non-constant value for timing functions
let start = Date.now();
Expand All @@ -34,10 +36,14 @@ const onReady = async (client: Client) => {
await rest.put(Routes.applicationCommands(clientId), {
body: commandData,
});
console.log(`Commands loaded: ${Object.keys(commandHash).join(', ')}`);
logger.info(`Commands loaded: ${Object.keys(commandHash).join(', ')}`, {
type: 'startup',
});

time = `${Date.now() - start}ms`;
console.log(`Loaded ${commandData.length} commands in ${time}`);
logger.info(`Loaded ${commandData.length} commands in ${time}`, {
type: 'startup',
});

// get api data on startup
await getData().then(data => {
Expand All @@ -47,7 +53,9 @@ const onReady = async (client: Client) => {

// retrieve encounters and load them as autocomplete suggestions
time = `${Date.now() - start}ms`;
console.log(`Loaded ${mappedNames.planets.length} planets in ${time}`);
logger.info(`Loaded ${mappedNames.planets.length} planets in ${time}`, {
type: 'startup',
});

// cron schedule to update messages every hour
schedule('0 * * * *', async () => {
Expand Down Expand Up @@ -127,7 +135,9 @@ const onReady = async (client: Client) => {
// await all message edits completion
await Promise.all(promises);
time = `${Date.now() - start}ms`;
console.log(`Updated ${messages.length} messages in ${time}`);
logger.info(`Updated ${messages.length} messages in ${time}`, {
type: 'info',
});
});

// cron schedule to update api data every 10 seconds
Expand Down
12 changes: 9 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {config} from './config';
import {onInteraction, onReady} from './events';
import {client} from './handlers';
import {client, logger} from './handlers';

const {BOT_TOKEN} = config;

Expand All @@ -13,13 +13,19 @@ async function main() {
if (guild.available) {
// retrieve new server count
const serverCount = (await client.guilds.fetch()).size;
console.log(`Client joined guild #${serverCount}: ${guild.name}`);
logger.info(`Client joined guild #${serverCount}: ${guild.name}`),
{
type: 'info',
};
}
});

// log server kick
client.on('guildDelete', guild => {
if (guild.available) console.log(`Client removed from: ${guild.name}`);
if (guild.available)
logger.info(`Client removed from: ${guild.name}`, {
type: 'info',
});
});

// handle user interactions (eg. commands)
Expand Down

0 comments on commit b93e4d6

Please sign in to comment.