Get started with application monitoring
Monibot collects, aggregates and monitors your application's metrics, for example:
- Number of served HTTP requests
- Database size
- Number of registrered users
- Anything that can be expressed in numbers
In this article, we will, as an example, monitor the number of registrered users for a hypothetical web app. We assume that the web app stores its users in some kind of a database. The database can be a SQL database, a JSON file, or some other persistent storage.
Create a metric entry in Monibot
- Log into your Monibot account. If you don't have one, register now, it's free.
- In the main menu, click on the 'Metrics' link. Here you'll see a 'New Metric' button.
- Click the 'New Metric' button. Enter a name, for example 'User Count'. You can chose any name here, it's used only for display.
- As metric type, select 'Gauge'. A gauge metric is a metric that can go up and down, much like a car's speedometer.
- Click 'Save'. Your metric is now ready and waits for data from your application.
Each new metric gets a unique ID, which Monibot shows on the screen. You will need that ID later.
Send metrics with Go
The Monibot Go SDK is a library for accessing the Monibot REST Api with Go. You can install it with the following command:
user@host:~/my-app$ go get github.com/cvilsmeier/monibot-go
In your code, make a function that sends the current user count to Monibot. Here is a sample code:
// file: main.go
package main
import (
"log"
"math/rand"
"os"
"time"
"github.com/cvilsmeier/monibot-go"
)
func main() {
// initialize monibot api
const apiKey = "c757bda424b92e4028659610f9071f03"
api := monibot.NewApi(apiKey)
// query database for current user count
userCount := loadUserCountFromDatabase()
// send user count to Monibot
const metricId = "90d0091348a79f137b489713c74713e4"
err := api.PostMetricSet(metricId, userCount)
if err != nil {
log.Fatal(err)
}
}
You can invoke this code manually, by issuing the follogin command:
user@host:~/my-app$ go run main.go
You can also put this command in your crontab, so that is is executed every 5 minutes:
user@host:~$ crontab -l MONIBOT_API_KEY=c757bda424b92e4028659610f9071f03 METRIC_ID=90d0091348a79f137b489713c74713e4 */5 * * * * cd /home/user/my-app && go run main.go
Send metrics with Python
You don't have to use Go for using Monibot. In fact, any HTTP library in any programming language can be used. Here's a python sample:
import requests
# hardcoded values for demonstration purposes only
apiKey = "c757bda424b92e4028659610f9071f03"
# load user count from DB
userCount = loadUserCountFromDatabase()
# send to Monibot
metricId = "90d0091348a79f137b489713c74713e4"
url = 'https://monibot.io/api/metric/'+metricId+'/set'
data = "value={}".format(userCount)
print("data: ", data)
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Bearer '+apiKey
}
res = requests.post(url, data=data, headers=headers)
print("res: ", res, res.text)