Getting Started with Triggers, Part II: Trigger Breakdown

triggers

#1

In part 1, we discussed triggers at a very high level. Now, we’ll dive in a little deeper. For now, I’m feeling like walking through some of the triggers already here on the forums. Here’s a super short trigger with real powerful results:

/* ----------------------------------- */
Event: HTTP_REQUEST
Trigger: mr_leaf
/* ----------------------------------- */
if(HTTP.headers['X-TeaLeaf-Page-Render']) {
    var render_time = HTTP.headers['X-TeaLeaf-Page-Render'];
    render_time = parseInt(render_time);
    var uri = HTTP.referer;
    //debug("time: uri: " +render_time + uri );

    Network.metricAddSampleset("page_load_time", render_time);
    Network.metricAddDetailSampleset("page_load_time_detail", uri, render_time);
}

Eight lines of code, and you’ve got easy integration with browser render times from a TeaLeaf plugin. From here you can tie in DB response times, etc. etc.

Let’s have a look at the code, line by line:

 if(HTTP.headers['X-TeaLeaf-Page-Render']) {

This is an “if” test in javascript. The syntax is like:

if( something ) { // do something }

Here, we’re testing for the existence of the X-TeaLeaf-Page-Render header. Notice that this header is sitting inside the HTTP.headers property; this property is nothing more than a simple list of headers and their values. If this header exists, we move on. If it doesn’t, we don’t do anything. Let’s continue…

var render_time = HTTP.headers['X-TeaLeaf-Page-Render'];

This is how you set a variable in javascript. We’re setting a variable names “render_time” with this statement. The value of this variable is a number, e.g. 200, which represents the render time in milliseconds. This part is real important: notice that the HTTP.headers list contains the values, as I mentioned above. So by calling

HTTP.headers['X-TeaLeaf-Page-Render'];

We’re actually getting the value associated with that header returned to us and stored in that variable.
Moving on…

render_time = parseInt(render_time);

This line converts the value into an integer, which is what the ExtraHop datastore expects (as opposed to a string). That’s it - a simple conversion into something the system expects.

var uri = HTTP.referer;

This line sets a variable called ‘uri’, and the value is the string returned from the HTTP.referer property. The TeaLeaf client-side plugin uses this header as a way to store the actual URI of the page that it’s reporting on.

//debug("time: uri: " +render_time + uri );

This is a call to the ‘debug’ command, which will print out the results in the triggers widget. Real handy during trigger development! This line is commented out with the double forward slashes “//”.

Believe it or not, we’ve now got all the data we need to render some useful information. Time to store it for graphs, trending, alerts, and historical look back.

 Network.metricAddSampleset("page_load_time", render_time);
 Network.metricAddDetailSampleset("page_load_time_detail", uri, render_time);
}

These lines add data into the ExtraHop data store. That way we can graph, alert and so on. Here, we’re adding it to the Network class, as opposed to an individual device. This allows me to report on an entire group of servers as opposed to individual ones. The type of metric is a Sampleset, which gives us the sample size, deviation, and mean of the data as it is stored into the data store.

The second line is storing a DetailSampleset, which shows per-object timings. An example of this is the URI detail screen you see when looking at one of the web servers. You see each object and their processing time with its associated mean and deviation.

The very last line closes the if block. That’s it, we’re done - a tiny chunk of code with incredibly useful results!

In part 3, we’ll start developing some triggers.