php Sentry.io 路线上的JavaScript隧道?

mzsu5hc0  于 2023-02-03  发布在  PHP
关注(0)|答案(1)|浏览(179)

问题:

由于JavaScript代码加载的问题,我尝试将sentry与tunnel选项集成在一起。如果用户启用了广告拦截器,这将防止拦截。https://docs.sentry.io/platforms/javascript/troubleshooting/#using-the-tunnel-option

现在,他们在文档中提供了此隧道的示例代码:

<?php
// Change $host appropriately if you run your own Sentry instance.
$host = "sentry.io";
// Set $known_project_ids to an array with your Sentry project IDs which you
// want to accept through this proxy.
$known_project_ids = array(  );

$envelope = stream_get_contents(STDIN);
$pieces = explode("\n", $envelope, 2);
$header = json_decode($pieces[0], true);
if (isset($header["dsn"])) {
    $dsn = parse_url($header["dsn"]);
    $project_id = intval(trim($dsn["path"], "/"));
    if (in_array($project_id, $known_project_ids)) {
      $options = array(
        'http' => array(
            'header'  => "Content-type: application/x-sentry-envelope\r\n",
            'method'  => 'POST',
            'content' => $envelope
        )
      );
      echo file_get_contents(
          "https://$host/api/$project_id/envelope/",
          false,
          stream_context_create($options));
    }
}

app.php(我的项目的布局文件)中,我这样调用JavaScript Sentry:

<script src="{{ asset('/assets/js/app.js') }}" crossorigin="anonymous"></script>

我的问题:

我不明白的是如何将它作为一个路由集成到web.php中,所以每次发生JavaScript错误时都会调用它。

ekqde3dh

ekqde3dh1#

您应该能够像这样定义路由:

Route::post('/sentry-tunnel', function (Request $request) {
    // Change $host appropriately if you run your own Sentry instance.
    $host = "sentry.io";

    // Set $known_project_ids to an array with your Sentry project IDs which you
    // want to accept through this proxy.
    $known_project_ids = [];

    $envelope = $request->getContent();
    $pieces = explode("\n", $envelope, 2);
    $header = json_decode($pieces[0], true);

    if (isset($header['dsn'])) {
        $dsn = parse_url($header['dsn']);
        $project_id = intval(trim($dsn['path'], '/'));

        if (in_array($project_id, $known_project_ids)) {
            return Http::withBody($envelope, "application/x-sentry-envelope")
                ->post("https://$host/api/$project_id/envelope/");
        }
    }
});

然后从JavaScript调用URL '/sentry-tunnel'。如果需要,不要忘记添加您的项目ID和凭据。

相关问题