How to instrument your Battlesnake with New Relic One
Tags: battlesnake, newrelic, obervability, javascript
Published: 2022-03-24 17:02:36


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](https://docs.newrelic.com/docs/apm/agents/manage-apm-agents/agent-data/custom-instrumentation/?utm_source=devto&utm_medium=community&utm_campaign=global-ever-green-custom-instrumentation) to help you instrument your BattleSnake with a variety of languages and tools.

  

## Instrumenting your BattleSnake

1.  First, you must have a New Relic account. If you have one already, go ahead and [log in to your account](https://login.newrelic.com/?utm_source=devto&utm_medium=community&utm_campaign=global-ever-green-login). 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)
    
2.  With your New Relic account ready, you can begin to instrument your Snakes! My Snake’s example code is located at [https://replit.com/](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.
    

![screenshot showing where the button is located](https://lh4.googleusercontent.com/fwupBftJRXc0G2KrZ3BBYqzq6qftdDMmocqCxN2A063Z2eMdR08u9spGNs2eLTQaliGwWrt0aEO-wxnuTao6-Xl5tPW2L01pcWNskuE86ffcn_hy--gy0K_KRg2r2lFNnNY-D7uL)

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.

3.  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](https://docs.newrelic.com/docs/apm/agents/nodejs-agent/installation-configuration/install-nodejs-agent-docker/?utm_source=devto&utm_medium=community&utm_campaign=global-ever-green-nodejs-agent-docker).
    

  

![screenshot display mentioned installation step](https://lh4.googleusercontent.com/xQHkqAHcOSPdYE_zeqJUehYmuGtZMioJ32TMXPR8zUH6UzwMQTW07b_V0gtMprfJ3yYmJt7vfWw6YUJKZq_U1cNtTkHs5jumpuXasaF0nJgOIoz0aumJjTPw9Mx6P8rrDYsSGZZG)

4.   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’

![screenshot display mentioned installation step](https://lh4.googleusercontent.com/90TACDjWloDAMm7yk9PRm-S5g6W9cS_xLlBRZP8wljhfNH0J7_kKuRdslj3A8w6D8EoJJ12jDUIQqu6DFS21GnIPtkcNe4M6ONdWRDeksEXXHhMrN1FR9woId5YEZ34i99oJ8d_r)

c.  Download the custom configuration file and put it on the root of your directory.
    

![screenshot display mentioned installation step](https://lh4.googleusercontent.com/ZjlSAKe9iCyhNlj0MjhzAgT28qFW60jLmSf_FsRhYKMusKG_0T7cLyb6CgrXJgU1JvQGviFw93j4GwrsXNm5JXCWCCGPmQ_cYYdpWrdsR2yevLZU-a38_aewOYrH9tKid8Zqo5Me)

d.  Add New Relic to your application, adding this piece of code to the first line of your BattleSnake main module: ‘require('newrelic');’
    
5.  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!
    
6.  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

1.  Now you'll navigate to the [New Relic BattleSnake quickstart](https://newrelic.com/instant-observability/battlesnake/63a65857-a25e-4654-9174-928e5894b3b9?utm_source=devto&utm_medium=community&utm_campaign=global-ever-green-battlesnake-quickstart), and select the + Install quickstart button.This will take you to the BattleSnake quickstart installation.
    
2.  Select Begin installation, and it will create two dashboards for you: Performance and Server Status.
    
3.  Extra step: [Add a `getAttributes` function](https://github.com/zstix/nr-battlesnake-attributes) to your `move` 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?
    
-   ![screenshot of dashboard](https://lh4.googleusercontent.com/cdCZPXI3O1L29IDcm6HCTnTk2mgFdqai9zRMD1INCL_t5nNybmGhZIQtv8d8J1-XNhKfNxVMa7hafxDZ_0_k_KxHCTZ5VxOIINdEU86kEpE-dJcYTodnKEYqeTcwypkR1I9hyPNJ)
    

The BattleSnake Server Status dashboard also shows you important server information like:

-   Time consumed by transactions
    
-   Moves per minute
    
-   Network Traffic
    
-   Request by endpoint
    

  

![screenshot display mentioned installation step](https://lh5.googleusercontent.com/pZRbq-oyWDxf4t-3jgldThpXoXDelw2PdwlU9iWQN65IDgrza5hkk45IvdErLrfX3bZQQzE9Z27G9NVqFoczQ2jmseiv0Zp6Rg4VEmMsYqQ0eMbmrV1tWh2-dyJnpMCL1hB0UO69)

  

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](https://github.com/newrelic-experimental/instrumenting-battlesnake-nodejs) 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](https://twitter.com/newrelic) !