Introducing custom error group hashing

| 3 min. (465 words)

Error grouping can be tricky – everyone has slightly different ideas of what can and what should be grouped together. Add in some inconsistencies between languages and it gets even harder – we are looking at you here, Javascript!

Here at Raygun, we think our hashers are pretty sweet, but there are always edge cases that we can’t deal with – enter custom group hashing.

What is a group hash?

It’s the key that tells Raygun which error group the error should go into. Generally the payload hits the Raygun servers, which runs a hashing algorithm over it and sorts it into the correct group. This works 99.99% of the time – your errors show up exactly where you expect them and everybody is happy.

Of course there is always the chance that Ragyun’s hasher doesn’t group your error how you desire.

What are my options?

We have two options to get around this problem:

  1. Use message based hashing, but this has limitations the second you have two messages that are the same but have very different causes.
  2. Now you can also provide your own hashing key!

What does providing your own hashing key involve?

Not that much actually. A word of warning though: generating a useful hash key can be quite a bit harder than it first appears – but with a decent amount of care, you should be able to build something that matches your needs perfectly if one of the built-in ones don’t do the trick.

But back to the point, you simply pass a “GroupingKey” property through on the payload and Raygun uses that instead of it’s own built- in hashing methods. Your errors are then fired straight through to the group that you’ve chosen!

At present the Javascript and NodeJS providers offer full support for the custom hashing key with the other providers not too far behind. Let’s have a quick look at the NodeJS provider and see what we can do!

When setting up the provider, we implement the groupingKey function like so:

var raygunClient = new raygun.Client().init({
    apiKey: 'YOUR_KEY',
    groupingKey: function(message, exception, customData, request, tags) {
        return "CUSTOMKEY";
    }
});

Obviously that’s not very useful as all of your errors will be grouped together. So let’s try something a bit more useful:

var raygunClient = new raygun.Client().init({
    apiKey: 'YOUR_KEY',
    groupingKey: function(message, exception, customData, request, tags) {
        var key = exception + message.details.error.stackTrace.length + message.details.error.className;
        console.log(key.replace(/\s/g, ''));
        return key;
    }
});

Boom! Errors are now grouped using their message, stack trace length and error class name. Helpful? Probably not – but it makes a good demo, right‽

As I said before, custom error groups should be a last resort for when the built-in hashers don’t quite suit you – but as always, the more choices you have the better!