Your data. Anywhere you go.

New Relic for iOS or Android


Download on the App Store    Android App on Google play


New Relic Insights App for iOS


Download on the App Store


Learn more

Close icon

Relic Solution: Auto-Labelling Apps With APIs


#1

Introduction

You might be familiar with one or more of the New Relic APIs. But are you using these APIs in a way that gets more done with less effort?
Let’s look at how we can nest some fairly simple API calls together to create a powerful solution.
We will look at a fairly common workflow people encounter while using New Relic and how we can streamline it with our APIs:
Please note we will not be covering the basics of API keys and the API explorer here, if you would like to learn about those then click here.

Labelling an Application Based on its Name

There are many reasons why labels are useful:

  • searchability
  • categorisation
  • alerting

Though it is probably easiest to apply a label when configuring your New Relic agent for the first time you may have either not added the labels or your label needs may have changed.

Let’s look at one specific scenario:

You’ve started with a naming convention of adding the environment to the application name for example “Prod - My Application” or “Staging - My Application” and now you have realised that using Environent:Production and Environment:Staging labels would be better in terms of searchability and targeting your app for alerts.

What do you do?

You could manually apply the labels, or update the application configurations but if you have a large number of apps or services this could be a very laborious process. Enter the API!

First of all, let’s think about what we want to achieve here and break it down into steps.

  1. Get the details or all our apps/services.
  2. Identify if the names contain a specific string, say “prod”.
  3. Create a label for example “Environment:Production” and apply it to all apps/services where the name contains “prod”
  4. Smile

Or for those of you who prefer a flow chart:

*please note that “New Relic Data” is a rather radical over-simplification of how things actually work - this is just for illustration.

Disclaimer: this is my first time writing a Node.JS script so I am sure there are more elegant ways to do this - however it is functional.

You will need to replace "YOUR_ADMIN_KEY_HERE" with your Admin API Key.

If you like this, please watch out for my next post on how to automate setting your Apdex T value for all applications.

We’d love to hear your thoughts on this post and some of the things you have done with New Relic APIs.

Remember, if you are interested in learning more about APIs please check out developer.newrelic.com

Happy automating!

var request = require("request");

var adminKey = "YOUR_ADMIN_KEY_HERE";

var headers = {
    "Content-Type": "json/application",
    "X-Api-Key": adminKey
};

var options = {
    url: "https://api.newrelic.com/v2/applications.json",
    method: 'GET',
    headers: headers
};

request(options, 
  function(error, response, body) {
    if (error) return onErr(error);
    if (!error && response.statusCode == 200) {
      var result = JSON.parse(body);
      console.log('Number of applications found:'+result["applications"].length);
      for (var i = 0; i < result["applications"].length; ++i) { 
        var application = result["applications"][i]
        var appName  = application["name"];
        var lowerCaseName = appName.toLowerCase();

        if (lowerCaseName.indexOf('prod')==-1) {
          console.log ('application name: '+application["name"]+' does not contain prod');
        }
         else{ 
          console.log('application name: '+application["name"]+' contains prod');
          addLabel(application);
        }
        
      }
    }
  });

function addLabel(application){

console.log ('application name: '+application["name"]+ ' application ID '+application["id"]);
  var request = require("request");

  var options = { method: 'PUT',
    url: 'https://api.newrelic.com/v2/labels.json',
    headers: 
    { 
      'X-Api-Key': adminKey,
      'Content-Type': 'application/json' },
    body: 
    { label: 
        { category: 'Environment',
          name: 'Production',
          links: { applications: [ application["id"] ] } } },
    json: true };

  request(options, function (error, response, body) {
    if (error) throw new Error(error);

    console.log(body);
  });
}

function onErr(err) {
    console.log(err);
    return 1;
}