Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Perfomance Transactions #63

Open
arkhamvm opened this issue Dec 10, 2022 · 1 comment
Open

Perfomance Transactions #63

arkhamvm opened this issue Dec 10, 2022 · 1 comment

Comments

@arkhamvm
Copy link

arkhamvm commented Dec 10, 2022

Hello.
Can you provide example for Perfomance Transactions, please?
I had tried this, but it doesn't work:

'class'   => \notamedia\sentry\SentryTarget::class,
'dsn'     => SENTRY_DSN,
'levels'  => ['error', 'warning'],
// Write the context information (the default is true):
'context' => true,
// Additional options for `Sentry\init`:
'clientOptions' => [
	'traces_sample_rate' => (YII_DEBUG ? 1 : 0.1),
],
$transactionContext = new \Sentry\Tracing\TransactionContext();
$transactionContext->setName('Request Transaction');
$transactionContext->setOp('request');

// Start the transaction
$transaction = \Sentry\startTransaction($transactionContext);
'on beforeRequest' => function() use ($transaction) {
	// Set the current transaction as the current span so we can retrieve it later
	\Sentry\SentrySdk::getCurrentHub()->setSpan($transaction);
},
'on afterRequest' => function() use ($transaction) {
	// Set the current span back to the transaction since we just finished the previous span
	\Sentry\SentrySdk::getCurrentHub()->setSpan($transaction);

	// Finish the transaction, this submits the transaction and it's span to Sentry
	$transaction->finish();
}
@white43
Copy link

white43 commented Sep 12, 2024

If somebody finds this issue, here is the version that worked for me. Please note, that in my case sentry/sdk was 4.0, while this library currently relies on version 3.0. So, there might me some incompatibilities that require changes.

'on beforeRequest' => function() {
    $request = Yii::$app->getRequest();

    $context = new TransactionContext(sprintf('%s %s', $request->getMethod(), $request->getPathInfo()));
    $context->setOp('http.server');
    $context->setData([
        'http.request.method' => $request->getMethod(),
    ]);

    $transaction = SentrySdk::getCurrentHub()->startTransaction($context);
    SentrySdk::getCurrentHub()->setSpan($transaction);
},
'on afterRequest' => function() {
    $transaction = SentrySdk::getCurrentHub()->getSpan();

    if ($transaction !== null) {
        $http_code = Yii::$app->getResponse()->getStatusCode();
        $status = SpanStatus::createFromHttpStatusCode($http_code);

        $transaction->setStatus($status);
        $transaction->setHttpStatus($http_code);
        $transaction->finish();
    }
},

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants