Provider Update: Custom Error Grouping For PHP

| 3 min. (447 words)

However, we understand that sometimes you may need to be able to manually group your errors according specific criteria.

For example, if you use Amazon Beanstalk, there could be many machines under one application but you may only find an error under a single machine. In this case, it becomes quite handy to be able to see exactly which machine is finding the error.

This is why Raygun has released custom grouping for PHP.

This means Raygun4PHP provider now has the ability to allow you to define your own grouping hash for PHP errors and exceptions before you send them.

(Raygun also provides custom error grouping for .NET exceptions, here’s how to start)

How Raygun groups errors

Raygun uses a group hash to determine which 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. In most situations, this is an effective way to manage the volume of errors arriving into your ‘Active‘ tab. The same error won’t keep sending notifications, and the error can be resolved quickly if it keeps occurring:

However, there is the chance that Raygun’s hasher doesn’t group your error how you desire.

Custom error grouping for PHP

Step #1  Execute the SetGroupingKey method on the RaygunClient and attach a callback. Which will then give you Error details that Raygun4PHP has created..

Step #2  Send back the key you want to use for grouping that error. If you don’t set a grouping key, we use our normal hashing routine.

Here’s an example:

<?php
$client = new \Raygun4php\RaygunClient($apiKey);

$client->SetGroupingKey(function($payload, $stackTrace) {

  // Group errors by hostname
  $hash = gethostname();

  // ClassName, File, and LineNumber which contained them
  foreach( $stackTrace as $trace ) {
    $hash .= $trace->ClassName . $trace->FileName . $trace->LineNumber;
  }

  return sha1( $hash );
});
?>

This will set the custom grouping key for any Exception to be a hash of the hostname, plus the class name, file name and line number of each item in the stack trace. Allowing you see the individual errors on each machine, just the Amazon Beanstalk example above.

The grouping key you produce should be less than 100 characters. Although we don’t actually restrict you here, but if it’s longer than that we will SHA1 hash your key and use the results of that. It’s quite unlikely that this will produce duplicates, but it’s worth mentioning.

Recommended Use

We recommend that a custom hasher is the last resort when Raygun’s error grouping is unsuitable for the task at hand.

**Need help? **

Do you have any questions about custom error groups? Contact a team member  or leave a comment below.