-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update the PHP transport classes section #1276
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -324,10 +324,21 @@ the events, but report them as sent. | |
use Sentry\ClientBuilder; | ||
use Sentry\Transport\NullTransport; | ||
use Sentry\State\Hub; | ||
use Sentry\Transport\TransportFactoryInterface; | ||
|
||
$transport = new NullTransport(); | ||
$builder = ClientBuilder::create(['dsn' => '___PUBLIC_DSN___']); | ||
$builder->setTransport($transport); | ||
$options = ['dsn' => '___PUBLIC_DSN___']; | ||
|
||
$transportFactory = new class implements TransportFactoryInterface { | ||
|
||
public function create(\Sentry\Options $options): \Sentry\Transport\TransportInterface | ||
{ | ||
return new NullTransport(); | ||
} | ||
}; | ||
|
||
|
||
$builder = ClientBuilder::create($options); | ||
$builder->setTransportFactory($transportFactory); | ||
|
||
Hub::getCurrent()->bindClient($builder->getClient()); | ||
``` | ||
|
@@ -345,16 +356,50 @@ The best adapter available is automatically selected when creating a client inst | |
through the client builder, but you can override it using the appropriate methods. | ||
|
||
```php | ||
use Http\Discovery\HttpAsyncClientDiscovery; | ||
use Http\Discovery\MessageFactoryDiscovery; | ||
use Http\Discovery\StreamFactoryDiscovery; | ||
use Http\Discovery\UriFactoryDiscovery; | ||
use Http\Message\ResponseFactory; | ||
use Http\Message\UriFactory; | ||
use Sentry\ClientBuilder; | ||
use Sentry\HttpClient\HttpClientFactory; | ||
use Sentry\HttpClient\HttpClientFactoryInterface; | ||
use Sentry\Transport\HttpTransport; | ||
use Sentry\State\Hub; | ||
use Sentry\Transport\TransportFactoryInterface; | ||
|
||
$options = ['dsn' => '___PUBLIC_DSN___']; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would just inline this variable since we won't reuse it besides in the |
||
|
||
$transport = new HttpTransport($options, HttpAsyncClientDiscovery::find(), MessageFactoryDiscovery::find()); | ||
$httpClientFactory = new HttpClientFactory( | ||
UriFactoryDiscovery::find(), | ||
MessageFactoryDiscovery::find(), | ||
StreamFactoryDiscovery::find(), | ||
null, | ||
'sentry.php', | ||
'2.3' | ||
); | ||
|
||
$transportFactory = new class($httpClientFactory) implements TransportFactoryInterface { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of creating a custom factory, the |
||
/** | ||
* @var HttpClientFactoryInterface | ||
*/ | ||
private $clientFactory; | ||
|
||
public function __construct(HttpClientFactoryInterface $clientFactory) | ||
{ | ||
$this->clientFactory = $clientFactory; | ||
} | ||
|
||
public function create(\Sentry\Options $options): \Sentry\Transport\TransportInterface | ||
{ | ||
return new HttpTransport($options, $this->clientFactory->create($options), MessageFactoryDiscovery::find()); | ||
} | ||
}; | ||
|
||
|
||
$builder = ClientBuilder::create($options); | ||
$builder->setTransport($transport); | ||
$builder->setTransportFactory($transportFactory); | ||
|
||
Hub::getCurrent()->bindClient($builder->getClient()); | ||
``` | ||
|
@@ -382,12 +427,19 @@ use Sentry\State\Hub; | |
|
||
$options = ['dsn' => '___PUBLIC_DSN___']; | ||
|
||
$spool = new MemorySpool(); | ||
$transport = new SpoolTransport($spool); | ||
|
||
$httpTransport = new HttpTransport($options, HttpAsyncClientDiscovery::find(), MessageFactoryDiscovery::find()); | ||
|
||
$transportFactory = new class implements TransportFactoryInterface { | ||
public function create(\Sentry\Options $options): \Sentry\Transport\TransportInterface | ||
{ | ||
$spool = new MemorySpool(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the spool is the class that users should interact with to flush the events, it should be instantiated outside of the factory |
||
return new SpoolTransport($spool); | ||
} | ||
}; | ||
|
||
$builder = ClientBuilder::create($options); | ||
$builder->setTransport($transport); | ||
$builder->setTransportFactory($transportFactory); | ||
|
||
Hub::getCurrent()->bindClient($builder->getClient()); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would just inline this variable since we won't reuse it besides in the
ClientBuilder::create
method