Skip to content

Commit 8174b8a

Browse files
committed
feature #393 Add ability to specify custom Sentry hub service (HypeMC)
This PR was merged into the 3.x-dev branch. Discussion ---------- Add ability to specify custom Sentry hub service As mentioned in getsentry/sentry-docs#3137 (comment) , this will allow users to set a custom hub service, eg the one registered by the [Sentry bundle](https://github.com/getsentry/sentry-symfony). Commits ------- 4b6b423 Add ability to specify custom Sentry hub service
2 parents 5e0fdd3 + 4b6b423 commit 8174b8a

File tree

4 files changed

+86
-27
lines changed

4 files changed

+86
-27
lines changed

DependencyInjection/Configuration.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,9 @@
217217
* - [auto_log_stacks]: bool, defaults to false
218218
* - [environment]: string, default to null (no env specified)
219219
*
220+
* - sentry:
221+
* - hub_id: Sentry hub custom service id (optional)
222+
*
220223
* - newrelic:
221224
* - [level]: level name or int value, defaults to DEBUG
222225
* - [bubble]: bool, defaults to true
@@ -644,6 +647,7 @@ public function getConfigTreeBuilder()
644647
->scalarNode('connection_timeout')->end() // socket_handler, logentries, pushover, hipchat & slack
645648
->booleanNode('persistent')->end() // socket_handler
646649
->scalarNode('dsn')->end() // raven_handler, sentry_handler
650+
->scalarNode('hub_id')->defaultNull()->end() // sentry_handler
647651
->scalarNode('client_id')->defaultNull()->end() // raven_handler, sentry_handler
648652
->scalarNode('auto_log_stacks')->defaultFalse()->end() // raven_handler
649653
->scalarNode('release')->defaultNull()->end() // raven_handler, sentry_handler
@@ -884,9 +888,13 @@ public function getConfigTreeBuilder()
884888
->thenInvalid('The DSN has to be specified to use a RavenHandler')
885889
->end()
886890
->validate()
887-
->ifTrue(function ($v) { return 'sentry' === $v['type'] && !array_key_exists('dsn', $v) && null === $v['client_id']; })
891+
->ifTrue(function ($v) { return 'sentry' === $v['type'] && !array_key_exists('dsn', $v) && null === $v['hub_id'] && null === $v['client_id']; })
888892
->thenInvalid('The DSN has to be specified to use Sentry\'s handler')
889893
->end()
894+
->validate()
895+
->ifTrue(function ($v) { return 'sentry' === $v['type'] && null !== $v['hub_id'] && null !== $v['client_id']; })
896+
->thenInvalid('You can not use both a hub_id and a client_id in a Sentry handler')
897+
->end()
890898
->validate()
891899
->ifTrue(function ($v) { return 'hipchat' === $v['type'] && (empty($v['token']) || empty($v['room'])); })
892900
->thenInvalid('The token and room have to be specified to use a HipChatHandler')

DependencyInjection/MonologExtension.php

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -721,42 +721,46 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
721721
break;
722722

723723
case 'sentry':
724-
if (null !== $handler['client_id']) {
725-
$clientId = $handler['client_id'];
724+
if (null !== $handler['hub_id']) {
725+
$hub = new Reference($handler['hub_id']);
726726
} else {
727-
$options = new Definition(
728-
'Sentry\\Options',
729-
[['dsn' => $handler['dsn']]]
730-
);
727+
if (null !== $handler['client_id']) {
728+
$clientId = $handler['client_id'];
729+
} else {
730+
$options = new Definition(
731+
'Sentry\\Options',
732+
[['dsn' => $handler['dsn']]]
733+
);
731734

732-
if (!empty($handler['environment'])) {
733-
$options->addMethodCall('setEnvironment', [$handler['environment']]);
734-
}
735+
if (!empty($handler['environment'])) {
736+
$options->addMethodCall('setEnvironment', [$handler['environment']]);
737+
}
735738

736-
if (!empty($handler['release'])) {
737-
$options->addMethodCall('setRelease', [$handler['release']]);
738-
}
739+
if (!empty($handler['release'])) {
740+
$options->addMethodCall('setRelease', [$handler['release']]);
741+
}
739742

740-
$builder = new Definition('Sentry\\ClientBuilder', [$options]);
743+
$builder = new Definition('Sentry\\ClientBuilder', [$options]);
741744

742-
$client = new Definition('Sentry\\Client');
743-
$client->setFactory([$builder, 'getClient']);
745+
$client = new Definition('Sentry\\Client');
746+
$client->setFactory([$builder, 'getClient']);
744747

745-
$clientId = 'monolog.sentry.client.'.sha1($handler['dsn']);
746-
$container->setDefinition($clientId, $client);
748+
$clientId = 'monolog.sentry.client.'.sha1($handler['dsn']);
749+
$container->setDefinition($clientId, $client);
747750

748-
if (!$container->hasAlias('Sentry\\ClientInterface')) {
749-
$container->setAlias('Sentry\\ClientInterface', $clientId);
751+
if (!$container->hasAlias('Sentry\\ClientInterface')) {
752+
$container->setAlias('Sentry\\ClientInterface', $clientId);
753+
}
750754
}
751-
}
752755

753-
$hub = new Definition(
754-
'Sentry\\State\\Hub',
755-
[new Reference($clientId)]
756-
);
756+
$hub = new Definition(
757+
'Sentry\\State\\Hub',
758+
[new Reference($clientId)]
759+
);
757760

758-
// can't set the hub to the current hub, getting into a recursion otherwise...
759-
//$hub->addMethodCall('setCurrent', array($hub));
761+
// can't set the hub to the current hub, getting into a recursion otherwise...
762+
//$hub->addMethodCall('setCurrent', array($hub));
763+
}
760764

761765
$definition->setArguments([
762766
$hub,

Resources/config/schema/monolog-1.0.xsd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
<xsd:attribute name="connection-timeout" type="xsd:string" />
6868
<xsd:attribute name="persistent" type="xsd:boolean" />
6969
<xsd:attribute name="dsn" type="xsd:string" />
70+
<xsd:attribute name="hub-id" type="xsd:string" />
7071
<xsd:attribute name="client-id" type="xsd:string" />
7172
<xsd:attribute name="use-ssl" type="xsd:boolean" />
7273
<xsd:attribute name="formatter" type="xsd:string" />

Tests/DependencyInjection/MonologExtensionTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,52 @@ public function testSentryHandlerWhenAClientIsSpecified()
456456
$this->assertDICConstructorArguments($handler->getArguments()[0], [new Reference('sentry.client')]);
457457
}
458458

459+
public function testSentryHandlerWhenAHubIsSpecified()
460+
{
461+
$container = $this->getContainer(
462+
[
463+
[
464+
'handlers' => [
465+
'sentry' => [
466+
'type' => 'sentry',
467+
'hub_id' => 'sentry.hub',
468+
],
469+
],
470+
],
471+
],
472+
[
473+
'sentry.hub' => new Definition(\Sentry\State\HubInterface::class),
474+
]
475+
);
476+
477+
$logger = $container->getDefinition('monolog.logger');
478+
$this->assertDICDefinitionMethodCallAt(0, $logger, 'useMicrosecondTimestamps', ['%monolog.use_microseconds%']);
479+
$this->assertDICDefinitionMethodCallAt(1, $logger, 'pushHandler', [new Reference('monolog.handler.sentry')]);
480+
481+
$handler = $container->getDefinition('monolog.handler.sentry');
482+
$this->assertDICConstructorArguments($handler, [new Reference('sentry.hub'), \Monolog\Logger::DEBUG, true]);
483+
}
484+
485+
public function testSentryHandlerWhenAHubAndAClientAreSpecified()
486+
{
487+
$this->expectException(InvalidConfigurationException::class);
488+
$this->expectExceptionMessage('You can not use both a hub_id and a client_id in a Sentry handler');
489+
490+
$this->getContainer(
491+
[
492+
[
493+
'handlers' => [
494+
'sentry' => [
495+
'type' => 'sentry',
496+
'hub_id' => 'sentry.hub',
497+
'client_id' => 'sentry.client',
498+
],
499+
],
500+
],
501+
]
502+
);
503+
}
504+
459505
public function testLogglyHandler()
460506
{
461507
$token = '026308d8-2b63-4225-8fe9-e01294b6e472';

0 commit comments

Comments
 (0)