How to instrument your Battlesnake with New Relic One
Pachi š„
Posted on March 24, 2022
BattleSnake players are always looking for fun and creative ways to make their slithery digital companions more competitive. But how well does your BattleSnake function? The strategy of āeat that foodā works, but you can improve it. Iāll tell you how to monitor your BattleSnake performance, server, and web application in real time with New Relic, so you can help your snake live its best life. ? I will be working with Node.js but New Relic has great documentation to help you instrument your BattleSnake with a variety of languages and tools.
Instrumenting your BattleSnake
First, you must have a New Relic account. If you have one already, go ahead and log in to your account. If you do not have an account yet, you can s[ign up for New Relic here. It is free and thereās no trial period so you can keep using your account forever.]https://login.newrelic.com/?utm_source=devto&utm_medium=community&utm_campaign=global-ever-green-login)
With your New Relic account ready, you can begin to instrument your Snakes! My Snakeās example code is located at https://replit.com/. On the New Relic One home page, select the + Add more data button on the top right, and choose how you will be adding data.
My Snake is in JavaScript, so I will go with App Monitoring > Node.js, but once you're on this page you can see that you have a variety of options, so go ahead and choose what's best for your BattleSnake and select Begin Installation.
- Follow the installation instructions.
- On the Node.js agent option it will give you four options as you can see on the screenshot below. In this example, the package manager options will be used. If you are using the Node.js agent for Docker, you can find the documentation here.
- Following the installations steps we will:
a. Name your application. Use a unique name that makes sense to you, to help you find it in the future.
b. Install the agent running this piece of code on your BattleSnakeās terminal:
ānpm install newrelic --saveā
c. Download the custom configuration file and put it on the root of your directory.
d. Add New Relic to your application, adding this piece of code to the first line of your BattleSnake main module: ārequire('newrelic');ā
Now when you start your BattleSnake, it will start sending data to your New Relic One. Go ahead and play a game to generate some data!
As simple as that your BattleSnake is instrumented and you are collecting its data! But, what to do with this data? Analyze your Snake performance of course!
BattleSnake dashboards
Now you'll navigate to the New Relic BattleSnake quickstart, and select the + Install quickstart button.This will take you to the BattleSnake quickstart installation.
Select Begin installation, and it will create two dashboards for you: Performance and Server Status.
Extra step: Add a
getAttributes
function to yourmove
request handler, so that data is sent to New Relic One.
Now you have two dashboards that will read your BattleSnake data and show you lots of cool information.
In the Performance dashboard you can see:
Competitions Status: How is your Snake doing against its opponents?
Survival: How many turns is it surviving?
Growth: How long was your longest Snake?
The BattleSnake Server Status dashboard also shows you important server information like:
Time consumed by transactions
Moves per minute
Network Traffic
Request by endpoint
And these dashboards are customizable, so if there is more or less information you want to see, go ahead and customize it.
While these dashboards are fun to look at, you may be asking yourself why you would go through this effort for a couple of pretty screens? Great question!
Keeping your response time under the 500ms limit is a must for your BattleSnake, and checking their performance you can see where you could improve your code to make sure they never pass the time limit.
Watching your baby snake and your skills over time is the best feeling, and you can use the dashboards to keep track of how your Snake average length improves over time,
You can customize your dashboards to get more information on your opponents and find out which snake is defeating yours more often! Then you can replay the games to learn their tactics and build your counterattack!
Track your win and losses, as well as your win percentage.
Those are just some ideas, but as I mentioned, you can customize your dashboard to your snake's content, and the possibilities are endless.
Next Steps
Interested in more? I have created a GitHub repository that you can use to deploy an instrumented BattleSkane with Heroku with one click. It is in experimental mode, so feel free to add your contribution to that.
We would love to see how you use the power of data and observability to make your snake a winner, please share with us on Twitter how you are using New Relic !
Posted on March 24, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.