从IBM云函数调用PHP SDK时未向Sentry发送错误

从IBM云函数调用PHP SDK时未向Sentry发送错误,php,ibm-cloud,serverless-framework,sentry,openwhisk,Php,Ibm Cloud,Serverless Framework,Sentry,Openwhisk,我使用无服务器框架将PHP代码部署为IBM云函数 以下是action PHP文件中的代码: 函数main$args:array{ 岗哨\init['dsn'=>'Sentry_dsn']; 试一试{ 抛出新的\异常“某些错误” }catch\Throwable$异常{ Sentry\captureException$exception; } } 这是serverless.yml文件: service: cloudfunc provider: name: openwhisk runtim

我使用无服务器框架将PHP代码部署为IBM云函数

以下是action PHP文件中的代码:

函数main$args:array{ 岗哨\init['dsn'=>'Sentry_dsn']; 试一试{ 抛出新的\异常“某些错误” }catch\Throwable$异常{ Sentry\captureException$exception; } } 这是serverless.yml文件:

service: cloudfunc

provider:
  name: openwhisk
  runtime: php

package:
  individually: true
  exclude:
    - "**"
  include:
    - "vendor/**"

functions:
    test-sentry:
    handler: actions/test-sentry.main
    annotations:
        raw-http: true
    events:
        - http:
            path: /test-sentry
            method: post
            resp: http
    package:
        include:
        - actions/test-sentry.php

plugins:
  - serverless-openwhisk
当我从本地环境nginx/PHP Docker容器测试操作处理程序时,错误被发送到Sentry

但是,当我尝试从IBMCloud调用操作时,Sentry控制台中没有显示任何内容

编辑:

经过一段时间的调查,我发现问题的根源与向Sentry发送http请求的异步性质有关。我有其他库,它们可以与Loggly、RabbitMQ、MySQL建立http/TCP连接,并且它们都可以正常工作:

vendor/sentry/sentry/src/Transport/HttpTransport.php
在发送实际http请求的发送方法中:

公共函数sendEvent$事件:?字符串 { $request=$this->requestFactory->createRequest "岗位",, sprintf'/api/%d/store/',$this->config->getProjectId, ['Content-Type'=>'application/json'], JSON::encode$event ; $PROMITE=$this->httpClient->sendAsyncRequest$request; //这里的承诺状态是悬而未决的 //这里的这一行被记录在所调用操作的stdout中 var_dump$promise->getState; //此函数是在行中定义的,因此它不会显示为类型提示 $cleanupPromiseCallback=函数$responseOrException使用$promise{ //这里的承诺实现了 //这里的这一行永远不会记录在所调用操作的标准输出中 //就像这里从来没有发生过死刑一样 var_dump$promise->getState; $index=array_search$promise,$this->pendingRequests,true; 如果为false!==$index{ 取消设置$this->pendingRequests[$index]; } 返回$responseOrException; }; $PROMITE->然后是$cleanupPromiseCallback,$cleanupPromiseCallback; $this->pendingRequests[]=$promise; 返回$event->getId; }
函数运行时在平台请求之间暂停。这意味着任何后台进程如果在函数返回时未完成,都将被阻止

异步HTTP请求似乎在运行时暂停之前没有机会完成


您需要找到某种方法阻止函数返回,直到该请求完成。如果Sentry SDK有一些回调处理程序或其他机制在消息发送时得到通知,您可以使用它吗

函数运行时在平台请求之间暂停。这意味着任何后台进程如果在函数返回时未完成,都将被阻止

异步HTTP请求似乎在运行时暂停之前没有机会完成


您需要找到某种方法阻止函数返回,直到该请求完成。如果Sentry SDK有一些回调处理程序或其他机制在消息发送时得到通知,您可以使用它吗

异步注册的请求在HttpTransport实例的析构函数中发送,或者在注册关闭函数时PHP关闭时发送。在OpenWhisk中,在Docker容器被杀死之前,我们从未关闭,因为我们在一个永无止境的循环中运行

因此,要使其工作,我们需要调用Hub的$client的$transport属性的析构函数。不幸的是,这是私有的,因此最简单的方法是使用反射使其可见,然后调用它:

$client=Sentry\State\Hub::getCurrent->getClient; $property=newreflectionobject$client->getProperty'transport'; $property->setAccessibletrue; $transport=$property->getValue$client; $transport->uu destruct; 这将使$transport属性可见,以便我们可以检索它并调用其析构函数,析构函数将调用cleanupPendingRequests,然后将请求发送到sentry.io

因此,主要问题如下:

函数main$args:array{ 岗哨\init['dsn'=>'Sentry_dsn']; 试一试{ 抛出新的\异常“某些错误” }catch\Throwable$异常{ Sentry\captureException$exception; } $client=Sentry\State\Hub::getCurrent->getClient; $property=newreflectionobject$client->getProperty'transport'; $property->setAccessibletrue; $transport=$property->getValue$client; $transport->uu destruct; 返回[ '正文'=>['结果'=>'确定'] ]; }
顺便说一句,我想知道Sentry SDK是否与Swoole配合使用?

异步注册的请求在HttpTransport实例的析构函数中发送,或者在PHP关闭时发送 已注册作为关闭函数的own。在OpenWhisk中,在Docker容器被杀死之前,我们从未关闭,因为我们在一个永无止境的循环中运行

因此,要使其工作,我们需要调用Hub的$client的$transport属性的析构函数。不幸的是,这是私有的,因此最简单的方法是使用反射使其可见,然后调用它:

$client=Sentry\State\Hub::getCurrent->getClient; $property=newreflectionobject$client->getProperty'transport'; $property->setAccessibletrue; $transport=$property->getValue$client; $transport->uu destruct; 这将使$transport属性可见,以便我们可以检索它并调用其析构函数,析构函数将调用cleanupPendingRequests,然后将请求发送到sentry.io

因此,主要问题如下:

函数main$args:array{ 岗哨\init['dsn'=>'Sentry_dsn']; 试一试{ 抛出新的\异常“某些错误” }catch\Throwable$异常{ Sentry\captureException$exception; } $client=Sentry\State\Hub::getCurrent->getClient; $property=newreflectionobject$client->getProperty'transport'; $property->setAccessibletrue; $transport=$property->getValue$client; $transport->uu destruct; 返回[ '正文'=>['结果'=>'确定'] ]; }
顺便问一下,我想知道这个Sentry SDK是否与Swoole配合使用?

Sentry\u DSN是如何设置的?代码使用的是实际的还是如何绑定的?它是硬编码的。添加操作创建和调用的详细信息。如何设置SENTRY_DSN?代码使用的是实际的还是如何绑定的?它是硬编码的。添加如何创建操作以及如何调用它的详细信息。我已经返回到Sentry PHP SDK的1.0版,该版本同步发送请求。谢谢。我已经恢复到Sentry PHP SDK的1.0版,它同步发送请求。谢谢。我们将很快在SDK中添加对此的支持。将有flush/close ref:@HazA,太棒了!现在它工作得很好,只需调用flush客户端的方法。我们将很快在SDK中添加对此的支持。将有flush/close ref:@HazA,这太好了!现在它工作得很好,只需调用flush客户端的方法。