Watch Users with New Relic and .NET

Microsoft

We're all programmers, right? We want to constantly improve our software and make the right choices about what to build. Since we're all good engineers, however, sometimes it's time to cut features. How will that impact the users? Do we even know if the users use a specific feature? One way is to remove the feature and see who complains. But there's a better way.

Telemetry

Telemetry lets us watch what the users are doing without standing over their shoulder. Ideally it lets them use the software naturally, but lets devs see how they're interacting with the system. In my case, I needed to see if users were clicking on a specific button in my web app so that I could justify cutting that button from future versions.

There are lots of ways to gather telemetry on users. I already had an investment in New Relic and didn't want to bring up any more services for this rather temporary data collection. So here's how I used ASP.NET Web API and New Relic to determine future features. Fair warning: the New Relic features mentioned in this article cost real dollars to use.

Any Service Will Do

Let's start with the service. It's simple. Just a REST URI in my ASP.NET Web API and is accessed via the URI /people/buttonclicks. The code looks like this:

[ActionName("ButtonClicks")]  
[HttpPost]
public HttpResponseMessage SaveButtonClicks(ButtonCounterDto countClicks, int id)  
{
     if (countClicks == null)
     {
          throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, WebResources.ERROR_NoCashTenderedClickData));
     }

     if (countClicks.NumberOfClicks < 0)
     {
          throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, WebResources.ERROR_NegativeButtonClickData));
      }

      NewRelic.Api.Agent.NewRelic.RecordMetric("Custom/ButtonClicks", countClicks.NumberOfClicks);
      return new HttpResponseMessage(HttpStatusCode.NoContent);
}

Right away, notice the New Relic code on line 15. This is the .NET New Relic Agent API they provide and you can view the API documentation at https://newrelic.com/docs/dotnet/AgentApi. The DLL itself can be extracted from the Agent install directory at \Program Files\New Relic .NET Agent\NewRelic.Api.Agent.dll . You need to include the reference for your code to compile, but if the New Relic Agent isn't installed on the production machine, this call won't do anything. Your event log won't fill up with complaints. But if the agent is installed, well, then there's magic.

The super important part is at the end of line 15. The name of the custom metric must begin with 'Custom/'. Without this, the metric doesn't show up correctly in the New Relic dashboard editor and the data starts to look funky. In my case, the metric I'm updating is a simple count. I'm not going to go into sending data to the service. It's a REST endpoint, so get to it how you want with whatever tech you want.

New Relic Custom Dashboard

To view the data for your custom metric, you need to create a custom dashboard. Like I said earlier, this is a feature that comes with the Pro subscription. If you're using Windows Azure, this is a $50 per month add on and is purchased from the Windows Azure Store.

The name of the custom metric must begin with 'Custom/'

Start by clicking the Dashboards tab and click on "Create new custom dashboard". This process has a number of steps that I'll leave you to figure out on your own. The important part is setting up your data source. In the dashboard editor click "Add chart or table". In my example, I'm creating a chart. The chart editor looks like this:

new-relic-chart-create 1

If you've got more than one application like I do, you may have to expand the "Advanced options" section and choose the specific application your metric is on for this to work. I've chosen "My Application" and that loads in the available metrics for that application (#1 above).

In step #2 above, pick the custom metric to display. One of the predefined drop-down items is "Custom". Select that and the available custom metrics will show. Choose your metric (I chose the ButtonClicks metric). You should see a green checkmark when the metric is chosen. Also, the Value drop-down box should light up (#3 above). I wanted the count of clicks per time slice, so I chose "Count". Other options include Total value, Average value, Average response time, Call count, Min response time, and Max response time. Pick what's right for the metric you're collecting.

After saving the chart, lets give it a look.

new-relic-custom-chart-results

I've centered the chart around the time period I was interested in. I hovered over a specific data point and discovered that in the 6 minute period between 14:36 and 14:42, users clicked the button 79 times.  The data show that over a 4 hour period or so, there were 950 button presses.

Sometimes You Lose

Unfortunately for me, the telemetry data suggest that my users really are using the feature, reducing the odds that I can cut the feature from the next version. At least I know and information is power. Then again, the designer in me still thinks the feature is ugly.

[endcall]Image credit: nerissa's ring (CC BY 2.0)[/endcall]