Skip to content

Commit

Permalink
Merge pull request #93 from affordablemobiles/iamacarpet/exception-ha…
Browse files Browse the repository at this point in the history
…ndler-outside-gcp

Fallback to the default exception handler outside of serverless
  • Loading branch information
iamacarpet authored Nov 8, 2024
2 parents 67f9ab6 + fb18eb1 commit fbb8681
Show file tree
Hide file tree
Showing 11 changed files with 23 additions and 16 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@
},
"config": {
"allow-plugins": {
"php-http/discovery": true
"php-http/discovery": true,
"tbachert/spi": true
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ protected function isGroupMember(string $identifier)
return (new CloudIdentity($client))->groups_memberships->checkTransitiveMembership(
'groups/'.$this->group,
[
'query' => sprintf("member_key_id == '%s'", $identifier),
'query' => \sprintf("member_key_id == '%s'", $identifier),
],
)->getHasMembership();
} catch (\Throwable $ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public static function factory(array $audienceMap = []): self

$audienceSource = static function (UriInterface $request_uri) use ($audienceMap) {
if (self::isCloudFunction($request_uri)) {
return sprintf(
return \sprintf(
'%s://%s%s',
$request_uri->getScheme(),
$request_uri->getHost(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function handle(): int
$filePath = $file->getPathname();
$fileRelativePath = FileViewFinder::getRelativePath(base_path(), $filePath);

if (!preg_match('/(.*)\\.blade\\.php$/', $filePath)) {
if (!preg_match('/(.*)\.blade\.php$/', $filePath)) {
$this->info("Blade Compiler: \tSkipping view (".($g + 1).'/'.\count($files).') '.$fileRelativePath);

continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ class Handler extends LaravelHandler
*/
protected function reportThrowable(\Throwable $e): void
{
if (!is_g_serverless()) {
parent::reportThrowable($e);

return;
}

$this->reportedExceptionMap[$e] = true;

if (Reflector::isCallable($reportCallable = [$e, 'report'])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static function init(?PsrLogger $psrLogger = null): void

public static function exceptionHandler(\Throwable $ex, int $status_code = 500, array $context = [], string $level = LogLevel::ERROR): void
{
$message = sprintf('PHP Notice: %s', (string) $ex);
$message = \sprintf('PHP Notice: %s', (string) $ex);
if (self::$psrLogger) {
$logContext = [
'context' => array_merge($context, [
Expand Down Expand Up @@ -86,7 +86,7 @@ public static function errorHandler(int $level, string $message, string $file, i
if (!($level & error_reporting())) {
return true;
}
$message = sprintf(
$message = \sprintf(
'%s: %s in %s on line %d',
self::getErrorPrefix($level),
$message,
Expand Down Expand Up @@ -139,7 +139,7 @@ public static function shutdownHandler(): void
case E_PARSE:
case E_COMPILE_ERROR:
case E_CORE_ERROR:
$message = sprintf(
$message = \sprintf(
'%s: %s in %s on line %d',
self::getErrorPrefix($err['type']),
$err['message'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function createHash($payload, Key $key)

$service = new \Google_Service_IAMCredentials($client);

$keyID = sprintf('projects/-/serviceAccounts/%s', $key->contents());
$keyID = \sprintf('projects/-/serviceAccounts/%s', $key->contents());

$requestBody = new \Google_Service_IAMCredentials_SignBlobRequest();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function read(string $id): false|string
}
} catch (Exception $e) {
trigger_error(
sprintf('Datastore lookup failed: %s', $e->getMessage()),
\sprintf('Datastore lookup failed: %s', $e->getMessage()),
E_USER_WARNING
);
}
Expand All @@ -88,7 +88,7 @@ public function write(string $id, string $data): bool
(new ExponentialBackoff(6, [DatastoreFactory::class, 'shouldRetry']))->execute([$this->datastore, 'upsert'], [$entity]);
} catch (Exception $e) {
trigger_error(
sprintf('Datastore upsert failed: %s', $e->getMessage()),
\sprintf('Datastore upsert failed: %s', $e->getMessage()),
E_USER_WARNING
);

Expand All @@ -106,7 +106,7 @@ public function destroy(string $id): bool
(new ExponentialBackoff(6, [DatastoreFactory::class, 'shouldRetry']))->execute([$this->datastore, 'delete'], [$key]);
} catch (Exception $e) {
trigger_error(
sprintf('Datastore delete failed: %s', $e->getMessage()),
\sprintf('Datastore delete failed: %s', $e->getMessage()),
E_USER_WARNING
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static function register(CachedInstrumentation $instrumentation): void
Gateway::class,
'execute',
pre: static function (Gateway $client, array $params, string $class, string $function, ?string $filename, ?int $lineno) use ($instrumentation): void {
SimpleSpan::pre($instrumentation, sprintf('GDS/execute/%s', $params[0] ?? 'unknown'), []);
SimpleSpan::pre($instrumentation, \sprintf('GDS/execute/%s', $params[0] ?? 'unknown'), []);
},
post: static function (Gateway $client, array $params, mixed $returnValue, ?\Throwable $exception): void {
SimpleSpan::post();
Expand Down Expand Up @@ -67,7 +67,7 @@ public static function register(CachedInstrumentation $instrumentation): void
GatewayREST::class,
'executePostRequest',
pre: static function (GatewayREST $client, array $params, string $class, string $function, ?string $filename, ?int $lineno) use ($instrumentation): void {
SimpleSpan::pre($instrumentation, sprintf('GDS/execute/%s', $params[0] ?? 'unknown'), []);
SimpleSpan::pre($instrumentation, \sprintf('GDS/execute/%s', $params[0] ?? 'unknown'), []);
},
post: static function (GatewayREST $client, array $params, mixed $returnValue, ?\Throwable $exception): void {
SimpleSpan::post();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static function register(CachedInstrumentation $instrumentation): void
foreach (array_filter(explode('|', env('TRACE_GUZZLE_HTTP_REQUEST_HEADERS', ''))) as $header) {
if ($request->hasHeader($header)) {
$spanBuilder->setAttribute(
sprintf('http.request.header.%s', strtolower($header)),
\sprintf('http.request.header.%s', strtolower($header)),
$request->getHeader($header)
);
}
Expand Down Expand Up @@ -124,7 +124,7 @@ public static function register(CachedInstrumentation $instrumentation): void
foreach (array_filter(explode('|', env('TRACE_GUZZLE_HTTP_RESPONSE_HEADERS', ''))) as $header) {
if ($response->hasHeader($header)) {
// @psalm-suppress ArgumentTypeCoercion
$span->setAttribute(sprintf('http.response.header.%s', strtolower($header)), $response->getHeader($header));
$span->setAttribute(\sprintf('http.response.header.%s', strtolower($header)), $response->getHeader($header));
}
}
if ($response->getStatusCode() >= 400 && $response->getStatusCode() < 600) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static function register(CachedInstrumentation $instrumentation): void

/** @psalm-suppress ArgumentTypeCoercion */
$builder = $instrumentation->tracer()
->spanBuilder(sprintf('%s', $request?->method() ?? 'unknown'))
->spanBuilder(\sprintf('%s', $request?->method() ?? 'unknown'))
->setSpanKind(SpanKind::KIND_SERVER)
->setAttribute(TraceAttributes::CODE_FUNCTION, $function)
->setAttribute(TraceAttributes::CODE_NAMESPACE, $class)
Expand Down

0 comments on commit fbb8681

Please sign in to comment.