Skip to content

Commit

Permalink
Fix not using scoped classes when required
Browse files Browse the repository at this point in the history
  • Loading branch information
stayallive committed Jul 3, 2024
1 parent 2df2d87 commit 897db31
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 13 deletions.
16 changes: 14 additions & 2 deletions src/tracing/class-wp-sentry-php-tracing.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php

use GuzzleHttp\Psr7\ServerRequest;
use Sentry\SentrySdk;
use Sentry\State\HubInterface;
use Sentry\Tracing\SpanContext;
Expand Down Expand Up @@ -82,7 +81,8 @@ private function start_transaction( HubInterface $sentry ): bool {

$requestStartTime = $_SERVER['REQUEST_TIME_FLOAT'] ?? microtime( true );

$request = ServerRequest::fromGlobals();
/** @var \GuzzleHttp\Psr7\ServerRequest $request */
$request = $this->resolve_request_from_globals();

$context = continueTrace(
$request->getHeaderLine( 'sentry-trace' ) ?: $request->getHeaderLine( 'traceparent' ),
Expand Down Expand Up @@ -278,4 +278,16 @@ private function set_transaction_name( string $transaction ): void {
public function get_transaction_name(): ?string {
return $this->transaction_name;
}

private function resolve_request_from_globals() {
if ( class_exists( WPSentry\ScopedVendor\GuzzleHttp\Psr7\ServerRequest::class ) ) {
return WPSentry\ScopedVendor\GuzzleHttp\Psr7\ServerRequest::fromGlobals();
}

if ( class_exists( GuzzleHttp\Psr7\ServerRequest::class ) ) {
return GuzzleHttp\Psr7\ServerRequest::fromGlobals();
}

throw new RuntimeException( 'Cannot find a PSR-7 implementation to create a request from globals.' );
}
}
44 changes: 33 additions & 11 deletions src/tracing/features/class-wp-sentry-tracing-feature-http.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

use GuzzleHttp\Psr7\Uri;
use Psr\Http\Message\UriInterface;
use Sentry\Breadcrumb;
use Sentry\SentrySdk;
use Sentry\Tracing\SpanContext;
Expand Down Expand Up @@ -121,16 +119,40 @@ private function handle_breadcrumb( array $data ): void {
) );
}

private function get_full_uri( string $url ): UriInterface {
return new Uri( $url );
/** @return \Psr\Http\Message\UriInterface */
private function get_full_uri( string $url ) {
if ( class_exists( WPSentry\ScopedVendor\GuzzleHttp\Psr7\Uri::class ) ) {
/** @noinspection PhpIncompatibleReturnTypeInspection */
return new WPSentry\ScopedVendor\GuzzleHttp\Psr7\Uri( $url );
}

if ( class_exists( GuzzleHttp\Psr7\Uri::class ) ) {
return new GuzzleHttp\Psr7\Uri( $url );
}

throw new RuntimeException( 'No compatible PSR-7 implementation found' );
}

private function get_partial_uri( UriInterface $uri ): string {
return (string) Uri::fromParts( [
'scheme' => $uri->getScheme(),
'host' => $uri->getHost(),
'port' => $uri->getPort(),
'path' => $uri->getPath(),
] );
/** @param \Psr\Http\Message\UriInterface $uri */
private function get_partial_uri( $uri ): string {
if ( class_exists( WPSentry\ScopedVendor\GuzzleHttp\Psr7\Uri::class ) ) {
return (string) WPSentry\ScopedVendor\GuzzleHttp\Psr7\Uri::fromParts( [
'scheme' => $uri->getScheme(),
'host' => $uri->getHost(),
'port' => $uri->getPort(),
'path' => $uri->getPath(),
] );
}

if ( class_exists( GuzzleHttp\Psr7\Uri::class ) ) {
return (string) GuzzleHttp\Psr7\Uri::fromParts( [
'scheme' => $uri->getScheme(),
'host' => $uri->getHost(),
'port' => $uri->getPort(),
'path' => $uri->getPath(),
] );
}

throw new RuntimeException( 'No compatible PSR-7 implementation found' );
}
}

0 comments on commit 897db31

Please sign in to comment.