首页 > 解决方案 > cURL 错误 7:无法连接到 logging.googleapis.com 端口 443:连接超时

问题描述

我们目前在尝试通过我们的 Laravel 6 应用程序登录到 Google Stackdriver 时遇到了一些连接问题。

我们使用google/cloud-logging自定义 Laravel 日志通道中的官方包进行日志记录,设置如下,这使我们能够使用 Laravel 的原生日志方法(Log::info('...')):

// Within the `channels` array in `logging.php`
'googlelog' => [
            'driver'               => 'custom',
            'via'                  => CreateStackdriverLogger::class,
            'logName'              => 'api',
            'loggingClientOptions' => [
                'keyFilePath' => resource_path('google-service-account-prod.json'),
            ],
            'level'                => env('LOG_LEVEL', 'info'),
            'username'             => 'Logger'
        ],
use Monolog\Logger;


class CreateStackdriverLogger {

    /**
     * Create a custom Monolog instance.
     *
     * @param array $config
     * @return Logger
     */
    public function __invoke(array $config) {
        $projectId = $config['logName'] ?? '';
        $loggingClientOptions = $config['loggingClientOptions'] ?? [];
        $loggerOptions = $config['loggerOptions'] ?? [];
        $entryOptionsWrapper = $config['entryOptionsWrapper'] ?? 'stackdriver';
        $lineFormat = $config['lineFormat'] ?? '%message%';
        $level = $config['level'] ?? Logger::DEBUG;
        $bubble = $config['bubble'] ?? true;

        $stackdriverHandler = new StackdriverLogger($projectId, $loggingClientOptions, $loggerOptions, $entryOptionsWrapper, $lineFormat, $level, $bubble);

        return new Logger('stackdriver', [$stackdriverHandler]);
    }
}

use Google\Cloud\Logging\LoggingClient;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\AbstractProcessingHandler;
use Monolog\Logger;

class StackdriverLogger extends AbstractProcessingHandler {

    /**
     * The Stackdriver logger
     *
     * @var \Google\Cloud\Logging\Logger
     */
    private $logger;

    /**
     * A context array key used to take log entry options from
     *
     * @var string
     */
    private $entryOptionsWrapper;

    /**
     * Log entry options (all but severity) as supported by Google\Cloud\Logging\Logger::entry
     *
     * @var array Entry options.
     */
    private $entryOptions = [
        'resource',
        'httpRequest',
        'labels',
        'operation',
        'insertId',
        'timestamp',
    ];

    /**
     * @param string  $logName              Name of your log
     * @param array   $loggingClientOptions Google\Cloud\Logging\LoggingClient valid options
     * @param array   $loggerOptions        Google\Cloud\Logging\LoggingClient::logger valid options
     * @param string  $entryOptionsWrapper  Array key used in the context array to take Google\Cloud\Logging\Entry options from
     * @param string  $lineFormat           Monolog\Formatter\LineFormatter format
     * @param int     $level                The minimum logging level at which this handler will be triggered
     * @param Boolean $bubble               Whether the messages that are handled can bubble up the stack or not
     */
    public function __construct($logName, $loggingClientOptions, $loggerOptions = [], $entryOptionsWrapper = 'stackdriver', $lineFormat = '%message%', $level = Logger::DEBUG,
                                $bubble = true) {
        parent::__construct($level, $bubble);

        $this->logger = (new LoggingClient($loggingClientOptions))->logger($logName, $loggerOptions);
        $this->formatter = new LineFormatter($lineFormat);
        $this->entryOptionsWrapper = $entryOptionsWrapper;
    }

    /**
     * Writes the record down to the log
     *
     * @param array $record
     * @return void
     */
    protected function write(array $record): void {
        $options = $this->getOptionsFromRecord($record);

        $data = [
            'message' => $record['formatted'],
            'data'    => $record['context']
        ];

        $entry = $this->logger->entry($data, $options);

        $this->logger->write($entry);
    }

    /**
     * Get the Google\Cloud\Logging\Entry options
     *
     * @param array $record by reference
     * @return array $options
     */
    private function getOptionsFromRecord(array &$record) {
        $options = [
            'severity' => $record['level_name']
        ];

        if (isset($record['context'][$this->entryOptionsWrapper])) {
            foreach ($this->entryOptions as $entryOption) {
                if ($record['context'][$this->entryOptionsWrapper][$entryOption] ?? false) {
                    $options[$entryOption] = $record['context'][$this->entryOptionsWrapper][$entryOption];
                }
            }
            unset($record['context'][$this->entryOptionsWrapper]);
        }

        return $options;
    }
}

因此,通过此设置进行的日志记录似乎在大多数情况下都有效,但有时我们会在不规则的时间间隔内收到以下不同的错误,所有这些错误都是在日志记录过程的上下文中:

cURL 错误 7:无法连接到 logging.googleapis.com 端口 443:连接超时(请参阅 http://curl.haxx.se/libcurl/c/libcurl-errors.html

cURL 错误 7:无法连接到 oauth2.googleapis.com 端口 443:连接超时(请参阅 http://curl.haxx.se/libcurl/c/libcurl-errors.html

cURL 错误 35:与 oauth2.googleapis.com:443 相关的未知 SSL 协议错误(请参阅 http://curl.haxx.se/libcurl/c/libcurl-errors.html

这是完整堆栈跟踪的示例:https ://sentry.io/share/issue/7a25c0a3575e4e2684ad5220cd89b86a/

我们已经检查过我们是否违反了 Google 的任何速率限制,但似乎并非如此。是否还有其他可能导致此类连接问题的原因?

标签: phplaravelloggingstackdriver

解决方案


推荐阅读