Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Build an Event-Driven Uptime Monitor in Go 🚀

Posted on Oct 20 This guide shows you how to build an uptime monitoring tool using Go and Encore. Get your entire application running in the cloud in 30 minutes!🚀 What's on deck:✨ Final result:Demo app: Try the appWhen we're done, we'll have a backend with this event-driven architecture:In this diagram (automatically generated by Encore) you can see individual services as white boxes, and Pub/Sub topics as black boxes.To make it easier to follow along, we've laid out a trail of croissants to guide your way.Whenever you see a 🥐 it means there's something for you to do!Install the Encore CLI to run your local environment:🥐 Create your new app from this starter branch with a ready-to-go frontend to use:🥐 Check that your frontend works by running your app locally.You should see this: This means Encore has started your local environment and created local infrastructure for Pub/Sub and Databases.Then visit http://localhost:4000/frontend/ to see the frontend.The functionality won't work yet, since we haven't yet built the backend yet.– Let's do that now!Let's start by creating the functionality to check if a website is currently up or down.Later we'll store this result in a database so we can detect when the status changes andsend alerts.🥐 Create a service named monitor containing a file named ping.go. With Encore, you do this by creating a Go package:🥐 Add an API endpoint named Ping that takes a URL as input and returns a response indicating whether the site is up or down.With Encore you do this by creating a function and adding the //encore:api annotation to it.Paste this into the ping.go file:🥐 Let's try it! Make sure you have Docker installed and running, then run encore run in your terminal and you should see the service start up.🥐 Now open up the Local Dev Dashboard running at http://localhost:9400 and try callingthe monitor.Ping endpoint, passing in google.com as the URL.If you prefer to use the terminal instead run curl http://localhost:4000/ping/google.com in a new terminal instead. Either way, you should see the response:You can also try with httpstat.us/400 and some-non-existing-url.com and it should respond with {"up": false}.(It's always a good idea to test the negative case as well.)🥐 Let's write an automated test so we don't break this endpoint over time. Create the file monitor/ping_test.go and add this code:🥐 Run encore test ./... to check that it all works as expected. You should see something like this:🎉 It works. Well done!Next, we want to keep track of a list of websites to monitor.Since most of these APIs will be simple CRUD (Create/Read/Update/Delete) endpoints, let's build this service using GORM, an ORM library that makes building CRUD endpoints really simple.🥐 Create a new service named site with a SQL database. To do so, create a new directory site in the application root with migrations folder inside that folder:🥐 Add a database migration file inside that folder, named 1_create_tables.up.sql. The file name is important (it must look something like 1_.up.sql) as Encore uses the file name to automatically run migrations.Add the following contents:🥐 Next, install the GORM library and PostgreSQL driver:Now let's create the site service itself. To do this we'll use Encore's support for dependency injection to inject the GORM database connection.🥐 Create site/service.go and add this code:🥐 With that, we're now ready to create our CRUD endpoints. Create the following files:site/get.go:site/add.go:site/list.go:site/delete.go:🥐 Restart encore run to cause the site database to be created, and then call the site.Add endpoint:In order to notify when a website goes down or comes back up, we need to track the previous state it was in.To do so, let's add a database to the monitor service as well.🥐 Create the directory monitor/migrations and the file monitor/migrations/1_create_tables.up.sql:We'll insert a database row every time we check if a site is up.🥐 Add a new endpoint Check to the monitor service, that takes in a Site ID, pings the site, and inserts a database row in the checks table.For this service we'll use Encore's sqldb package instead of GORM (in order to showcase both approaches).Add this to monitor/check.go:🥐 Restart encore run to cause the monitor database to be created, and then call the new monitor.Check endpoint:🥐 Inspect the database to make sure everything worked:You should see this:If that's what you see, everything's working great!🎉We now want to regularly check all the tracked sites so we canimmediately respond in case any of them go down.We'll create a new CheckAll API endpoint in the monitor service that will list all the tracked sites and check all of them.🥐 Let's extract some of the functionality we wrote for theCheck endpoint into a separate function.In monitor/check.go it should look like so:Now we're ready to create our new CheckAll endpoint.🥐 Create the new CheckAll endpoint inside monitor/check.go:This uses an [errgroup (https://pkg.go.dev/golang.org/x/sync/errgroup) to check up to 8 sites concurrently, aborting early if we encounter any error. (Note that a website being down is not treated as an error.)🥐 Run go get golang.org/x/sync/errgroup to install that dependency.🥐 Now that we have a CheckAll endpoint, define a cron job to automatically call it every 5 minutes.Add this to monitor/check.go:Note: For ease of development, cron jobs are not triggered when running the application locally, but work when deploying the application to a cloud environment.To try out your uptime monitor for real, let's deploy it to Encore's development cloud.Encore comes with built-in CI/CD, and the deployment process is as simple as a git push encore.(You can also integrate with GitHub to activate per Pull Request Preview Environments, learn more in the CI/CD docs.)🥐 Deploy your app by running:Encore will now build and test your app, provision the needed infrastructure, and deploy your application to the cloud.After triggering the deployment, you will see a URL where you can view its progress in Encore's Cloud Dashboard. It will look something like: https://app.encore.dev/$APP_ID/deploys/...From there you can also see metrics, traces, link your app to a GitHub repo to get automatic deploys on new commits, and connect your own AWS or GCP account to use for production deployment.🥐 When the deploy has finished, you can try out your uptime monitor by going to:https://staging-$APP_ID.encr.app/frontend.You now have an Uptime Monitor running in the cloud, well done!✨An uptime monitoring system isn't very useful if it doesn'tactually notify you when a site goes down.To do so let's add a [Pub/Sub topic (https://encore.dev/docs/primitives/pubsub)on which we'll publish a message every time a site transitions from being up to being down, or vice versa.🥐 Define the topic using Encore's Pub/Sub package in a new file, monitor/alerts.go:Now let's publish a message on the TransitionTopic if a site's up/down state differs from the previous measurement.🥐 Create a getPreviousMeasurement function in alerts.go to report the last up/down state:🥐 Now add a function in alerts.go to conditionally publish a message if the up/down state differs:🥐 Finally modify the check function in check.go to call the publishOnTransition function:Now the monitoring system will publish messages on the TransitionTopic whenever a monitored site transitions from up->down or from down->up.However, it doesn't know or care who actually listens to these messages. The truth is right now nobody does. So let's fix that by adding a Pub/Sub subscriber that posts these events to Slack.🥐 Start by creating a Slack service slack/slack.go containing the following:🥐 Now go to a Slack community of your choice (where you have permission to create a new Incoming Webhook). If you don't have any, join the Encore Slack and ask in #help and we're happy to help out.🥐 Once you have the Webhook URL, save it as a secret using Encore's built-in secrets manager:🥐 Test the slack.Notify endpoint by calling it via cURL:You should see the Testing Slack webhook message appear in the Slack channel you designated for the webhook.🥐 It's now time to add a Pub/Sub subscriber to automatically notify Slack when a monitored site goes up or down. Add the following to slack/slack.go:You're now ready to deploy your finished Uptime Monitor, complete with a Slack integration!🥐 As before, deploying your app to the cloud is as simple as running:You now have a fully featured, production-ready, Uptime Monitoring system running in the cloud. Well done! ✨You've now built a fully functioning uptime monitoring system, accomplishing a remarkable amount with very little code:All of this in just a bit over 300 lines of code!🤯If you want to build something more, check out Encore's templates library for inspiration.If you need help or want to chat, come join the developer's hangout on Slack.Templates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well Confirm For further actions, you may consider blocking this person and/or reporting abuse Zafar Urakov - Sep 25 Rama Reksotinoyo - Oct 8 Aniket - Sep 25 Matheus - Oct 8 If you want to stop spending time on boring tasks and start being productive and creative, come check out Encore. It's free. Once suspended, encore will not be able to comment or publish posts until their suspension is removed. Once unsuspended, encore will be able to comment and publish posts again. Once unpublished, all posts by encore will become hidden and only accessible to themselves. If encore is not suspended, they can still re-publish their posts from their dashboard. Note: Once unpublished, this post will become invisible to the public and only accessible to marcuskohlberg. They can still re-publish the post if they are not suspended. Thanks for keeping DEV Community safe. Here is what you can do to flag encore: encore consistently posts content that violates DEV Community's code of conduct because it is harassing, offensive or spammy. Unflagging encore will restore default visibility to their posts. DEV Community — A constructive and inclusive social network for software developers. With you every step of your journey. Built on Forem — the open source software that powers DEV and other inclusive communities.Made with love and Ruby on Rails. DEV Community © 2016 - 2023. We're a place where coders share, stay up-to-date and grow their careers.



This post first appeared on VedVyas Articles, please read the originial post: here

Share the post

Build an Event-Driven Uptime Monitor in Go 🚀

×

Subscribe to Vedvyas Articles

Get updates delivered right to your inbox!

Thank you for your subscription

×