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

Add configuration support for SamplingHandler #471

Merged
merged 1 commit into from
Nov 6, 2023
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
9 changes: 7 additions & 2 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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