I’m playing around with instrumentation using Sentry and I’m so discouraged by how many lines of code I have to add everywhere.
According to the Sentry docs, I have to add all these lines every time I want to measure something:
$sentryTransactionContext = (new TransactionContext('Something that needs measuring'));
$sentryTransactionContext->setOp('http.server');
$sentryTransaction = startTransaction($sentryTransactionContext);
SentrySdk::getCurrentHub()->setSpan($sentryTransaction);
$spanContext = (new SpanContext());
$spanContext->setOp('something.that.needs.measuring');
$span1 = $sentryTransaction->startChild($spanContext);
SentrySentrySdk::getCurrentHub()->setSpan($span1);
// Do something that needs to be measured...
$span1->finish();
SentrySdk::getCurrentHub()->setSpan($sentryTransaction);
$sentryTransaction->finish();
Is all that stuff really supposed to go in all my different Controller methods, or places where I need to measure how long a piece of code takes? It would be so much duplicate code.
Ideally, I would like to just do this:
public function create(HttpRequest $request)
{
sentry_measure_start('slow.task');
// Something slow that needs to be measured
sentry_measure_stop('slow.task');
}
Is that possible?
2
Answers
I found that Sentry's SDK has a less verbose static method that apparently isn't mentioned in the documentation. Here's how I use it. It's still a little cumbersome, but tolerable.
You could write a Service class that handles and simplifies the syntax for starting and stopping Sentry transactions and spans.
Create a
app/Services/SentryMeasureService.php
Create a
app/helpers.php
file to implement your helper functionsTo make the helper functions visible you need to modify your
composer.json
more specifically theautoload
key. Add afiles
arrayinside
autoload
.Once you added the file you need to dump the autloader
Now you should be able to use the functions the way you suggested: