PHP SDK 2.0 Breadcrumb creation improvements

With API 1.10.0 and the Raven_Client, we have the ability to create a breadcrumb with an associative array providing the parameters, which makes reading the code very clear as to what’s happening.

It also gives us other options in terms of how the parameter array is built up. It could be assembled over the course of some logic making some decisions, or, given all at once in the call.

eg:

$sentry_client->breadcrumbs->record([
  'level' => 'debug',
  'category' => 'my category',
  'message' => 'my message',
]);

or

// logic that builds $breadcrumb[]
$breadcrumb = [];
...
// default
$breadcrumb['type'] = 'debug';

// is it really an error?
if({decide whether severity needs upgrading}) {
$breadcrumb['type'] = 'error';
}
...

$sentry_client->breadcrumbs->record($breadcrumb);

The benefits and flexibility of this approach include it not mattering which order the parameter name/value pairs are presented, and, flexibility in how the parameters are built.

For those that prefer readability over flexibility, if the array with values are all given in the call, then you have great readability.

By contrast: Currently, with API 2.0’s Sdk, we have available, the following:

use Sentry\Breadcrumb;

Sentry\addBreadcrumb(new Sentry\Breadcrumb(
  Breadcrumb::LEVEL_DEBUG,
  Breadcrumb::TYPE_ERROR,
  'my category',
  'my message'
));

There’s also the fluent way to arrive at the Breadcrumb object, but, leaving that aside:

Reading the above code sample, you’d have to ‘just know’ what order the parameters are required to be in. I understand that there’s some overhead and memory cost in constructing an associative array, but, the readability improvement is considered as outweighing the cost.

So: How about allowing an alternative approach to creating a breadcrumb being supported that allows us to continue the benefits of the old way?

eg:

Sentry\addBreadcrumbFromArray([
  'level' => 'debug',
  'type' => 'error',
  'category' => 'sample category',
  'message' => 'test message',
]);

or:

Sentry\addBreadcrumbFromArray([
  'level' => Sentry\Breadcrumb:LEVEL_DEBUG,
  'type' => Sentry\Breadcrumb:TYPE_ERROR,
  'category' => 'my category',
  'message' => 'test message',
]);

An even friendlier alternative would be to make the Sdk::addBreadcrumb function deal with variable arguments, so that both variants would be doable via the one function name:

so:

Sentry\addBreadcrumb([
'level' => 'debug',  
'type' => 'error',
'category' => 'my category',
'message' => 'my message'
]);

and the existing way would be supported too:

Sentry\addBreadcrumb(new Breadcrumb('debug','error','my category', 'my message'));

I’d be willing to make some code submissions in the github repo to achieve being able to use the array to build the Breadcrumb either with or without variable arguments, if that helps.

how about it?

regards,
peter

1 Like

This would be a big improvement to how breadcrumbs are added, and would ensure that version 2 of the PHP SDK doesn’t destroy benefits previously present in version 1.

I think that makes sense, I would prefer to just have setters on the breadcrumb object instead of passing in array. We would accept a PR for this!

@HazAT, just for some clarification:

You are saying you prefer to have setters on the breadcrumb object. Since Breadcrumb already has setters, am I correct in concluding you’re in favor of adding something to the Sdk to allow an associative array to be used to construct a breadcrumb?

This is the approach I’m taking with the pull request I created, which seems to be facing some resistance from other contributors.

This approach retains all of the Breadcrumb’s responsibility / ability to reject creation if/when inappropriate values are provided, and all that’s required is some simple marshaling of array element details to the constructor arguments with some defaults thrown in.