You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Elastic APM Agent PHP:事务未结束,请求终止所有运行中事务

Laravel Elastic APM 事务冲突问题解决

问题原因

Elastic APM PHP Agent 默认会为 Laravel 的 HTTP 请求自动创建事务,当你手动调用beginCurrentTransaction时,当前请求的自动事务还未结束,因此触发冲突提示。

解决方案

1. 先结束当前自动事务再创建手动事务

如果需要保留自动追踪同时手动创建事务,先获取并结束当前活跃事务:

use Elastic\Apm\ElasticApm;

Route::get('/test', function () {
    // 获取当前事务并结束
    $currentTx = ElasticApm::getCurrentTransaction();
    if ($currentTx !== null && !$currentTx->isEnded()) {
        $currentTx->end();
    }

    // 创建新的手动事务
    $transaction = ElasticApm::beginCurrentTransaction(
        'HomeController@index',
        'request'
    );

    $transaction->end();
    return view('welcome');
});

2. 禁用目标路由的自动事务追踪

如果该路由不需要自动事务,直接排除APM自动追踪中间件:

Route::get('/test', function () {
    $transaction = ElasticApm::beginCurrentTransaction(
        'HomeController@index',
        'request'
    );

    $transaction->end();
    return view('welcome');
})->withoutMiddleware(['elastic-apm']);

注:确保elastic-apm是你配置中注册的APM中间件名称

3. 批量终止所有活跃事务

如果需要一次性结束所有未完成的事务,可通过Agent的事务管理器操作:

use Elastic\Apm\ElasticApm;

$agent = ElasticApm::getAgent();
$activeTxList = $agent->getTransactionManager()->getActiveTransactions();
foreach ($activeTxList as $tx) {
    if (!$tx->isEnded()) {
        $tx->end();
    }
}

内容的提问来源于stack exchange,提问作者tomrlh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 04:12:48