Skip to content
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

#310 : Add sentry 2.0 type #313

Merged
merged 1 commit into from Aug 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,7 @@
## 3.5.0 (xxxx-xx-xx)

* Add `sentry` type to use sentry 2.0 client
lyrixx marked this conversation as resolved.
Show resolved Hide resolved

## 3.4.0 (2019-06-20)

* Deprecate "excluded_404s" option
Expand Down
15 changes: 10 additions & 5 deletions DependencyInjection/Configuration.php
Expand Up @@ -194,13 +194,14 @@
* - [timeout]: float
* - [connection_timeout]: float
*
* - raven:
* - raven / sentry:
* - dsn: connection string
* - client_id: Raven client custom service id (optional)
* - [release]: release number of the application that will be attached to logs, defaults to null
* - [level]: level name or int value, defaults to DEBUG
* - [bubble]: bool, defaults to true
* - [auto_log_stacks]: bool, defaults to false
* - [environment]: string, default to null (no env specified)
*
* - newrelic:
* - [level]: level name or int value, defaults to DEBUG
Expand Down Expand Up @@ -620,11 +621,11 @@ public function getConfigTreeBuilder()
->scalarNode('store')->defaultNull()->end() // deduplication
->scalarNode('connection_timeout')->end() // socket_handler, logentries, pushover, hipchat & slack
->booleanNode('persistent')->end() // socket_handler
->scalarNode('dsn')->end() // raven_handler
->scalarNode('client_id')->defaultNull()->end() // raven_handler
->scalarNode('dsn')->end() // raven_handler, sentry_handler
->scalarNode('client_id')->defaultNull()->end() // raven_handler, sentry_handler
->scalarNode('auto_log_stacks')->defaultFalse()->end() // raven_handler
->scalarNode('release')->defaultNull()->end() // raven_handler
->scalarNode('environment')->defaultNull()->end() // raven_handler
->scalarNode('release')->defaultNull()->end() // raven_handler, sentry_handler
->scalarNode('environment')->defaultNull()->end() // raven_handler, sentry_handler
->scalarNode('message_type')->defaultValue(0)->end() // error_log
->arrayNode('tags') // loggly
->beforeNormalization()
Expand Down Expand Up @@ -837,6 +838,10 @@ public function getConfigTreeBuilder()
->ifTrue(function ($v) { return 'raven' === $v['type'] && !array_key_exists('dsn', $v) && null === $v['client_id']; })
->thenInvalid('The DSN has to be specified to use a RavenHandler')
->end()
->validate()
->ifTrue(function ($v) { return 'sentry' === $v['type'] && !array_key_exists('dsn', $v) && null === $v['client_id']; })
->thenInvalid('The DSN has to be specified to use Sentry\'s handler')
->end()
->validate()
->ifTrue(function ($v) { return 'hipchat' === $v['type'] && (empty($v['token']) || empty($v['room'])); })
->thenInvalid('The token and room have to be specified to use a HipChatHandler')
Expand Down
46 changes: 46 additions & 0 deletions DependencyInjection/MonologExtension.php
Expand Up @@ -668,6 +668,51 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
));
break;

case 'sentry':
if (null !== $handler['client_id']) {
$clientId = $handler['client_id'];
} else {
$options = new Definition(
'Sentry\\Options',
array(array('dsn' => $handler['dsn']))
);

if (!empty($handler['environment'])) {
$options->addMethodCall('setEnvironment', [$handler['environment']]);
}

if (!empty($handler['release'])) {
$options->addMethodCall('setRelease', [$handler['release']]);
}

$builder = new Definition('Sentry\\ClientBuilder', array($options));

$client = new Definition('Sentry\\Client');
$client->setFactory(array($builder, 'getClient'));

$clientId = 'monolog.sentry.client.'.sha1($handler['dsn']);
$container->setDefinition($clientId, $client);

if (!$container->hasAlias('Sentry\\ClientInterface')) {
$container->setAlias('Sentry\\ClientInterface', $clientId);
}
}

$hub = new Definition(
'Sentry\\State\\Hub',
array(new Reference($clientId))
);

// can't set the hub to the current hub, getting into a recursion otherwise...
lyrixx marked this conversation as resolved.
Show resolved Hide resolved
//$hub->addMethodCall('setCurrent', array($hub));

$definition->setArguments(array(
$hub,
$handler['level'],
$handler['bubble'],
));
break;

case 'raven':
if (null !== $handler['client_id']) {
$clientId = $handler['client_id'];
Expand Down Expand Up @@ -854,6 +899,7 @@ private function getHandlerClassByType($handlerType)
'socket' => 'Monolog\Handler\SocketHandler',
'pushover' => 'Monolog\Handler\PushoverHandler',
'raven' => 'Monolog\Handler\RavenHandler',
'sentry' => 'Sentry\Monolog\Handler',
lyrixx marked this conversation as resolved.
Show resolved Hide resolved
'newrelic' => 'Monolog\Handler\NewRelicHandler',
'hipchat' => 'Monolog\Handler\HipChatHandler',
'slack' => 'Monolog\Handler\SlackHandler',
Expand Down
82 changes: 82 additions & 0 deletions Tests/DependencyInjection/MonologExtensionTest.php
Expand Up @@ -342,6 +342,88 @@ public function testRavenHandlerWhenAClientIsSpecified()
$this->assertDICConstructorArguments($handler, array(new Reference('raven.client'), 100, true));
}

public function testSentryHandlerWhenConfigurationIsWrong()
{
try {
$this->getContainer(array(array('handlers' => array('sentry' => array('type' => 'sentry')))));
$this->fail();
} catch (InvalidConfigurationException $e) {
$this->assertContains('DSN', $e->getMessage());
}
}

public function testSentryHandlerWhenADSNIsSpecified()
{
$dsn = 'http://43f6017361224d098402974103bfc53d:a6a0538fc2934ba2bed32e08741b2cd3@marca.python.live.cheggnet.com:9000/1';

$container = $this->getContainer(array(array('handlers' => array('sentry' => array(
'type' => 'sentry', 'dsn' => $dsn)
))));
$this->assertTrue($container->hasDefinition('monolog.logger'));
$this->assertTrue($container->hasDefinition('monolog.handler.sentry'));

$logger = $container->getDefinition('monolog.logger');
$this->assertDICDefinitionMethodCallAt(0, $logger, 'useMicrosecondTimestamps', array('%monolog.use_microseconds%'));
$this->assertDICDefinitionMethodCallAt(1, $logger, 'pushHandler', array(new Reference('monolog.handler.sentry')));

$handler = $container->getDefinition('monolog.handler.sentry');
$this->assertDICDefinitionClass($handler, 'Sentry\Monolog\Handler');
}

public function testSentryHandlerWhenADSNAndAClientAreSpecified()
{
$container = $this->getContainer(
array(
array(
'handlers' => array(
'sentry' => array(
'type' => 'sentry',
'dsn' => 'foobar',
'client_id' => 'sentry.client',
),
),
),
),
array(
'sentry.client' => new Definition('Sentry\Client'),
)
);

$logger = $container->getDefinition('monolog.logger');
$this->assertDICDefinitionMethodCallAt(0, $logger, 'useMicrosecondTimestamps', array('%monolog.use_microseconds%'));
$this->assertDICDefinitionMethodCallAt(1, $logger, 'pushHandler', array(new Reference('monolog.handler.sentry')));

$handler = $container->getDefinition('monolog.handler.sentry');
$this->assertDICConstructorArguments($handler->getArguments()[0], array(new Reference('sentry.client')));
}

public function testSentryHandlerWhenAClientIsSpecified()
{
$container = $this->getContainer(
array(
array(
'handlers' => array(
'sentry' => array(
'type' =>
'sentry',
'client_id' => 'sentry.client',
),
),
),
),
array(
'sentry.client' => new Definition('Sentry\Client'),
)
);

$logger = $container->getDefinition('monolog.logger');
$this->assertDICDefinitionMethodCallAt(0, $logger, 'useMicrosecondTimestamps', array('%monolog.use_microseconds%'));
$this->assertDICDefinitionMethodCallAt(1, $logger, 'pushHandler', array(new Reference('monolog.handler.sentry')));

$handler = $container->getDefinition('monolog.handler.sentry');
$this->assertDICConstructorArguments($handler->getArguments()[0], array(new Reference('sentry.client')));
}

public function testLogglyHandler()
{
$token = '026308d8-2b63-4225-8fe9-e01294b6e472';
Expand Down