Skip to content

Commit

Permalink
Merge pull request #471 from Okhoshi/sampling-config
Browse files Browse the repository at this point in the history
Add configuration support for SamplingHandler
  • Loading branch information
stof committed Nov 6, 2023
2 parents d14cf79 + f004f27 commit 913cb79
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
9 changes: 7 additions & 2 deletions DependencyInjection/Configuration.php
Expand Up @@ -366,6 +366,10 @@
* - [split_long_messages]: bool, defaults to false, split messages longer than 4096 bytes into multiple messages
* - [delay_between_messages]: bool, defaults to false, adds a 1sec delay/sleep between sending split messages
*
* - sampling:
* - handler: the wrapped handler's name
* - factor: the sampling factor (e.g. 10 means every ~10th record is sampled)
*
* All handlers can also be marked with `nested: true` to make sure they are never added explicitly to the stack
*
* @author Jordi Boggiano <j.boggiano@seld.be>
Expand Down Expand Up @@ -591,6 +595,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->booleanNode('disable_notification')->defaultNull()->end() // telegram
->booleanNode('split_long_messages')->defaultFalse()->end() // telegram
->booleanNode('delay_between_messages')->defaultFalse()->end() // telegram
->integerNode('factor')->defaultValue(1)->min(1)->end() // sampling
->arrayNode('tags') // loggly
->beforeNormalization()
->ifString()
Expand Down Expand Up @@ -650,8 +655,8 @@ public function getConfigTreeBuilder(): TreeBuilder
->thenInvalid('Service handlers can not have a formatter configured in the bundle, you must reconfigure the service itself instead')
->end()
->validate()
->ifTrue(function ($v) { return ('fingers_crossed' === $v['type'] || 'buffer' === $v['type'] || 'filter' === $v['type']) && empty($v['handler']); })
->thenInvalid('The handler has to be specified to use a FingersCrossedHandler or BufferHandler or FilterHandler')
->ifTrue(function ($v) { return ('fingers_crossed' === $v['type'] || 'buffer' === $v['type'] || 'filter' === $v['type'] || 'sampling' === $v['type']) && empty($v['handler']); })
->thenInvalid('The handler has to be specified to use a FingersCrossedHandler or BufferHandler or FilterHandler or SamplingHandler')
->end()
->validate()
->ifTrue(function ($v) { return 'fingers_crossed' === $v['type'] && !empty($v['excluded_404s']) && !empty($v['activation_strategy']); })
Expand Down
10 changes: 10 additions & 0 deletions DependencyInjection/MonologExtension.php
Expand Up @@ -890,6 +890,15 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
$handler['bubble'],
]);
break;
case 'sampling':
$nestedHandlerId = $this->getHandlerId($handler['handler']);
$this->markNestedHandler($nestedHandlerId);

$definition->setArguments([
new Reference($nestedHandlerId),
$handler['factor'],
]);
break;

// Handlers using the constructor of AbstractHandler without adding their own arguments
case 'browser_console':
Expand Down Expand Up @@ -990,6 +999,7 @@ private function getHandlerClassByType($handlerType)
'redis' => 'Monolog\Handler\RedisHandler',
'predis' => 'Monolog\Handler\RedisHandler',
'insightops' => 'Monolog\Handler\InsightOpsHandler',
'sampling' => 'Monolog\Handler\SamplingHandler',
];

$v2HandlerTypesAdded = [
Expand Down

0 comments on commit 913cb79

Please sign in to comment.