Realm Blog
Published in

Realm Blog

Realm Logs: Alert Logging System with Twilio

Motivation

  1. Through the Logs section of our graphical interface: From here we will be able to access the logs and we will be able to filter between the different origins, dates, etc.
  2. Using the Admin API Rest: this will allow us to request the logs of an application programmatically by accessing the logging endpoints of the Realm Admin API.
  3. Through the command interface tools or realm-cli
Example of Log view in Realm
Example of Admin API

Prerequisites

  1. Have already created a MongoDB Atlas account and an associated cluster. https://www.mongodb.com/cloud/atlas/register
  2. Have a Twilio account. https://www.twilio.com/

Overview

System design
  1. Schedule Trigger: This trigger will be in charge of monitoring our Admin API to request the error logs. Here it is important to define the monitoring time so that this time will be the delay that we will have between a new error that arises in our application and the alert message that we will receive. For this tutorial, the monitoring is indicated every 10 minutes.
  2. MongoDB collection for monitoring: We have created a new database called “Logging” where we will store the three collections we need for our alert monitoring system.
    2.1. Control collection: We will use this collection to store the access_token and the refresh_token needed to make the requests to our Admin API. The access_token is valid for half an hour, so the refresh_token will be automatically used to generate a new token every time it expires and it will be stored in this collection. We will be able to consult the last_modified property to consult the date of the last access_token renewal request.
    2.2. Error Collection: We will use this collection to store any error derived from the requests to our Admin API.
    2.3. Alert Collection: We will use this collection to store the last error log of our application that we have not yet processed and sent via SMS to our phone.
  3. Database Trigger: The function of this trigger is to monitor the Alert collection, every time a new document is introduced in this collection, this trigger will be in charge of sending an alert to the phone numbers configured through Twilio.

Enable Realm CLI

realm-cli --version

Create a Realm application

Screenshot of the create new Realm App widget

Generate an API Key

realm-cli login --api-key="<my api key>" --private-api-key="<my private api key>"
Create an API Key for realm-cli
Create an API Key for realm-cli
Terminal output of realm cli login command
Terminal output of realm-cli login command

Download our application using Realm-CLI

Screenshot of Export App section in Realm UI
realm-cli pull --remote <<APP_ID>>

Download the template project from GitHub

An important thing to consider

  1. When copying the downloaded GitHub project files to our Realm application, we must be careful not to delete the config.json file located in the /data_sources/mongodb-atlas/directory because the downloaded template project does not have it and copying and pasting this file may delete it.
  2. The name of the folder /data_sources/<<name>>will match the name of our Linked Data Sources Service Name. In the template project, this name is mongodb-atlas but if in your project the name is different, please update it.

Update the Realm application with the required data

Secrets

  • private_key: We must create a secret that will store our private key to be able to request an access_token to our Admin API. At this point, we can create a new API Key, following the steps described above in the section “Generate an API Key” or we can use the same API Key that we have generated for Realm CLI.
    To generate the next secret, we can create it directly from the graphical interface in the Value section of our Realm application or we can use realm-cli for this purpose.
    The command to create this secret with realm-cli will be the following:
realm-cli secret create --app=<APP_ID> --name=private_key --value=<PRIVATE_KEY>
  • Auth_Token_Twilio: We must create another secret with the name Auth_Token_Twilio, where we will store the Auth token that Twilio provides us when we create an account in their platform.
    For them we must execute the following command:
realm-cli secret create --app=<APP_ID> --name=Auth_Token_Twilio --value=<Auth token>
Screenshot of Twilio Dashboard

Values

Twilio_Numbers.json

{
"from_secret": false,
"name": "Twilio_Numbers",
"value": {
"from": "<<Twilio number>>",
"to": "<<Number>>"
}
}
vim values/Twilio_Numbers.json

Realm_App.json

vim values/Realm_App.json
{
"from_secret": false,
"name": "Realm_App",
"value": {
"appId": "<<app_id>>",
"groupId": "<<group_id>>"
}
}
  • The app_id corresponds to the id of the Realm application we are going to monitor.
  • The group_id or project_id corresponds to the id of the project where our Realm application is located.

Twilio_SID_Value.json:

{
"from_secret": false,
"name": "Twilio_SID_Value",
"value": {
"sid": "<<Twilio SID value>>"
}
}

public_key.json

{
"name": "public_key",
"value": "<<public key>>",
"from_secret": false
}

Publish changes to our application

realm-cli push --include-dependencies --remote <APP_ID>
Screenshot of terminal output after realm-cli push command

Things to consider

const client = require('twilio')(accountSid, authToken, {    lazyLoading: true  });

Wrapping up

SMS received by the Alert System

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Josman Pérez Expóstio

If I had to sum up my professional interests in one sentence, I could only say that I am passionate about technology.