Observability is an extremely important practice for you having an idea of what is happening in your app and receive updates from new interactions.
We will see how to send new messages to a discord channel, this allows us to monitor interactions in production of our application, such as a new api call, a new login, or even a new record in the database.
In this example we will monitor a route and at each call to it you want to send a message to discord with some data.
If you do not create a project yet, just run yarn init -y
to create a new.
Install some dependencies:
yarn add express axios nodemon
Create a discord channel that will be used to monitor and receive new updates. After creating it, we need to create a new webhook for this channel. Go to Server Settings > Integrations > Webhooks > New Webhook, then select the created channel and copy the link.
Go back to code... create a config.js
file to define our variables:
export const config = {
API_PORT: 4000,
DISCORD_CHANNEL_WEBHOOK: <your discord channel webhook>,
};
Create a new express server with a simple route:
// index.js
const express = require('express')
const { config } = require('./config')
const axios = require('axios')
const app = express()
app.get('/observability', async (req, res) => {
await axios.post(config.DISCORD_CHANNEL_WEBHOOK, { content: 'Hello World' }, {
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
}
})
res.send('observability done successfully')
})
app.listen(config.API_PORT, () => {
console.log(`Server listening on port ${config.API_PORT}`)
})
Will be made a POST for this discord channel webhook with the content.
Let's run the code, open the terminal and run nodemon index.js
Now, if you access this url localhost:4000/observability
you will see it:
And if you check your discord channel it has something like this:
And that's it, we have a log and observability system, feel free to run this webhook at any time in your code that you want to follow the status in production.
See the repository using typescript here: observability-with-discord
See more in my zettelkasten
Foto de Marten Newhall na Unsplash
Top comments (0)