我正在使用Serverless框架将我的PHP代码部署为IBM Cloud Function。
以下是来自操作PHP文件的代码:
function main($args): array {
Sentry\init(['dsn' => 'SENTRY_DSN' ]);
try {
throw new \Exception('Some error')
} catch (\Throwable $exception) {
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。
但是,当我尝试从IBM Cloud调用该操作时,Sentry控制台中什么也没有显示。
编辑:
在尝试调查问题的根源后,我发现这与向Sentry发送http请求的异步特性有关(我还有其他库可以与Loggly、RabbitMQ、MySQL建立HTTP/TCP连接,它们都能按预期工作):
vendor/sentry/sentry/src/Transport/HttpTransport.php在实际发送http请求的send方法中:
public function send(Event $event): ?string
{
$request = $this->requestFactory->createRequest(
'POST',
sprintf('/api/%d/store/', $this->config->getProjectId()),
['Content-Type' => 'application/json'],
JSON::encode($event)
);
$promise = $this->httpClient->sendAsyncRequest($request);
//The promise state here is "pending"
//This line here is being logged in the stdout of the invoked action
var_dump($promise->getState());
// This function is defined in-line so it doesn't show up for type-hinting
$cleanupPromiseCallback = function ($responseOrException) use ($promise) {
//The promise state here is "fulfilled"
//This line here is never logged in the stdout of the invoked action
//Like the execution never happens here
var_dump($promise->getState());
$index = array_search($promise, $this->pendingRequests, true);
if (false !== $index) {
unset($this->pendingRequests[$index]);
}
return $responseOrException;
};
$promise->then($cleanupPromiseCallback, $cleanupPromiseCallback);
$this->pendingRequests[] = $promise;
return $event->getId();
}发布于 2019-04-23 23:24:23
异步注册的请求在HttpTransport实例的析构函数中发送,或者在注册关闭函数时发送。在OpenWhisk中,我们永远不会关机,因为我们在一个永无止境的循环中运行,直到Docker容器被杀死。
因此,要实现这一点,我们需要调用Hub的$client的$transport属性的析构函数。不幸的是,这是私有的,所以最简单的方法是使用反射使其可见,然后调用它:
$client = Sentry\State\Hub::getCurrent()->getClient();
$property = (new ReflectionObject($client))->getProperty('transport');
$property->setAccessible(true);
$transport = $property->getValue($client);
$transport->__destruct();这将使$transport属性可见,这样我们就可以检索它并调用它的析构函数,该析构函数将调用cleanupPendingRequests(),然后将请求发送到sentry.io。
因此,main()如下所示:
function main($args): array {
Sentry\init(['dsn' => 'SENTRY_DSN' ]);
try {
throw new \Exception('Some error')
} catch (\Throwable $exception) {
Sentry\captureException($exception);
}
$client = Sentry\State\Hub::getCurrent()->getClient();
$property = (new ReflectionObject($client))->getProperty('transport');
$property->setAccessible(true);
$transport = $property->getValue($client);
$transport->__destruct();
return [
'body' => ['result' => 'ok']
];
} 顺便说一句,我想知道这个Sentry SDK是否可以与Swoole一起工作?
发布于 2019-04-23 22:05:21
函数运行时由平台在请求之间“暂停”。这意味着任何后台进程如果在函数返回时没有完成,都将被阻塞。
看起来异步HTTP请求在运行时暂停之前没有机会完成。
您需要找到一些方法来阻止从函数返回,直到该请求完成。如果Sentry SDK有一些回调处理程序或其他机制在消息发送时得到通知,您可以使用它吗?
https://stackoverflow.com/questions/55810868
复制相似问题