自定义插桩
了解如何捕获应用程序中任何操作的性能数据。
要捕获针对您组织需求定制的事务和跨度,您必须首先 设置追踪。
要为代码的某些部分添加 instrumentation,您可以创建事务来捕获它们。
以下示例为包含昂贵操作(例如 expensive_operation
)的范围创建一个事务,并将结果发送到 Sentry:
Copied
// Setup context for the full transaction
$transactionContext = \Sentry\Tracing\TransactionContext::make()
->setName('Example Transaction')
->setOp('http.server');
// Start the transaction
$transaction = \Sentry\startTransaction($transactionContext);
// Set the current transaction as the current span so we can retrieve it later
\Sentry\SentrySdk::getCurrentHub()->setSpan($transaction);
// Setup the context for the expensive operation span
$spanContext = \Sentry\Tracing\SpanContext::make()
->setOp('expensive_operation');
// Start the span
$span1 = $transaction->startChild($spanContext);
// Set the current span to the span we just started
\Sentry\SentrySdk::getCurrentHub()->setSpan($span1);
// Calling expensive_operation()
expensive_operation();
// Finish the span
$span1->finish();
// 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();
下一个示例包含上一节代码片段中调用的假设 expensive_operation
函数的实现。但这次我们使用 trace
函数来创建一个新的 span 并将其设置为当前 span,从而减少了大量样板代码。
您可以选择 op
和 description
的值。
Copied
function expensive_operation(): void
{
$spanContext = \Sentry\Tracing\SpanContext::make()
->setOp('some_operation')
->setDescription('This is a description');
\Sentry\trace(function () {
// Do the expensive operation...
}, $spanContext);
}
您也可以手动执行此操作,如果您希望对 span 的创建有更多控制。
Copied
function expensive_operation(): void
{
$parent = \Sentry\SentrySdk::getCurrentHub()->getSpan();
$span = null;
// Check if we have a parent span (this is the case if we started a transaction earlier)
if ($parent !== null) {
$context = \Sentry\Tracing\SpanContext::make()
->setOp('some_operation')
->setDescription('This is a description');
$span = $parent->startChild($context);
// Set the current span to the span we just started
\Sentry\SentrySdk::getCurrentHub()->setSpan($span);
}
try {
// Do the expensive operation...
} finally {
// We only have a span if we started a span earlier
if ($span !== null) {
$span->finish();
// Restore the current span back to the parent span
\Sentry\SentrySdk::getCurrentHub()->setSpan($parent);
}
}
}
此代码检查是否存在父 span,如果存在,则创建一个新的 span 并将其设置为当前 span。然后我们调用昂贵的操作。最后,我们完成该 span 并将当前 span 恢复为父 span。
在需要将 spans 附加到正在进行的事务时,可以使用当前 hub 上的 getTransaction()
方法。如果设置了事务,则此属性将返回一个 \Sentry\Tracing\Transaction
;否则,返回 null
。
Copied
$transaction = \Sentry\SentrySdk::getCurrentHub()->getTransaction();
if ($transaction !== null) {
$context = \Sentry\Tracing\SpanContext::make();
->setOp('some_operation');
$span = $transaction->startChild($context);
// ...
$span->finish();
}
您也可以在启动事务后设置当前事务:
Copied
$transactionContext = \Sentry\Tracing\TransactionContext::make()
->setName('Example Transaction');
->setOp('http.server');
$transaction = \Sentry\startTransaction($transactionContext);
// A transaction is a span so we set it using `setSpan`
\Sentry\SentrySdk::getCurrentHub()->setSpan($transaction);
在需要将 spans 附加到正在进行的 span 时,可以使用当前 hub 上的 getSpan()
方法。如果设置了 span,则此属性将返回一个 \Sentry\Tracing\Span
;否则,返回 null
。您也可以在启动 span 后设置当前 span,并在 span 结束后将其恢复为父 span。
Copied
$parentSpan = \Sentry\SentrySdk::getCurrentHub()->getSpan();
if ($parentSpan !== null) {
$context = \Sentry\Tracing\SpanContext::make()
->setOp('some_operation');
$span = $parentSpan->startChild($context);
// Set the span we just started as the current span
\Sentry\SentrySdk::getCurrentHub()->setSpan($span);
// ...
$span->finish();
// Restore the span back to the parent span
\Sentry\SentrySdk::getCurrentHub()->setSpan($parentSpan);
}
您可以与跨度和事务一起捕获数据属性。您可以在启动跨度或事务时指定数据属性:
Copied
// Create a transaction and assign data attributes...
$transactionContext = \Sentry\Tracing\TransactionContext::make()
->setName('Example Transaction')
->setOp('http.server')
->setData([
'data_attribute_1' => 42,
'data_attribute_2' => true,
]);
$transaction = \Sentry\startTransaction($transactionContext);
// ... or create a span and assign data attributes
$spanContext = \Sentry\Tracing\SpanContext::make()
->setOp('http.client')
->setData([
'data_attribute_1' => 42,
'data_attribute_2' => true,
]);
$transaction->startChild($context);
或者您可以将数据属性添加到现有的跨度或事务中:
Copied
$transaction = \Sentry\SentrySdk::getCurrentHub()->getTransaction();
if ($transaction !== null) {
$transaction->setData([
'data_attribute_1' => 42,
'data_attribute_2' => true,
]);
}
$span = \Sentry\SentrySdk::getCurrentHub()->getSpan();
if ($span !== null) {
$span->setData([
'data_attribute_1' => 42,
'data_attribute_2' => true,
]);
}