Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"php": "^7.2||^8.0",
"guzzlehttp/psr7": "^2.1.1",
"jean85/pretty-package-versions": "^1.5||^2.0",
"sentry/sentry": "^4.22.0",
"sentry/sentry": "^4.23.0",
"symfony/cache-contracts": "^1.1||^2.4||^3.0",
"symfony/config": "^4.4.20||^5.0.11||^6.0||^7.0||^8.0",
"symfony/console": "^4.4.20||^5.0.11||^6.0||^7.0||^8.0",
Expand Down
1 change: 1 addition & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->info('The sampling factor to apply to profiles. A value of 0 will deny sending any profiles, and a value of 1 will send all profiles. Profiles are sampled in relation to traces_sample_rate')
->end()
->booleanNode('enable_logs')->end()
->variableNode('log_flush_threshold')->defaultNull()->end()
->booleanNode('enable_metrics')->defaultTrue()->end()
->booleanNode('attach_stacktrace')->end()
->booleanNode('attach_metric_code_locations')->end()
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/schema/sentry-1.0.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<xsd:attribute name="sample-rate" type="xsd:float" />
<xsd:attribute name="enable-tracing" type="xsd:boolean" />
<xsd:attribute name="enable-logs" type="xsd:boolean" />
<xsd:attribute name="log-flush-threshold" type="xsd:integer" />
<xsd:attribute name="traces-sample-rate" type="xsd:float" />
<xsd:attribute name="profiles-sample-rate" type="xsd:float" />
<xsd:attribute name="traces-sampler" type="xsd:string" />
Expand Down
17 changes: 17 additions & 0 deletions tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function testProcessConfigurationWithDefaultConfiguration(): void
'options' => [
'integrations' => [],
'prefixes' => array_merge(['%kernel.project_dir%'], array_filter(explode(\PATH_SEPARATOR, get_include_path() ?: ''))),
'log_flush_threshold' => null,
'enable_metrics' => true,
'environment' => '%kernel.environment%',
'release' => '%env(default::SENTRY_RELEASE)%',
Expand Down Expand Up @@ -275,6 +276,22 @@ public function testOrgIdOption(): void
$this->assertSame(1, $config['options']['org_id']);
}

public function testLogFlushThresholdOption(): void
{
/** @var array{options: array{log_flush_threshold: int}} $config */
$config = $this->processConfiguration(['options' => ['log_flush_threshold' => 2]]);

$this->assertSame(2, $config['options']['log_flush_threshold']);
}

public function testLogFlushThresholdOptionCanBeNull(): void
{
/** @var array{options: array{log_flush_threshold: null}} $config */
$config = $this->processConfiguration(['options' => ['log_flush_threshold' => null]]);

$this->assertNull($config['options']['log_flush_threshold']);
}

/**
* @dataProvider strictTraceContinuationOptionDataProvider
*/
Expand Down
1 change: 1 addition & 0 deletions tests/DependencyInjection/Fixtures/php/full.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
'traces_sampler' => 'App\\Sentry\\Tracing\\TracesSampler',
'profiles_sample_rate' => 1,
'enable_logs' => true,
'log_flush_threshold' => 2,
'attach_stacktrace' => true,
'attach_metric_code_locations' => true,
'context_lines' => 0,
Expand Down
1 change: 1 addition & 0 deletions tests/DependencyInjection/Fixtures/xml/full.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
sample-rate="1"
enable-tracing="true"
enable-logs="true"
log-flush-threshold="2"
traces-sample-rate="1"
traces-sampler="App\Sentry\Tracing\TracesSampler"
profiles-sample-rate="1"
Expand Down
1 change: 1 addition & 0 deletions tests/DependencyInjection/Fixtures/yml/full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ sentry:
traces_sampler: App\Sentry\Tracing\TracesSampler
profiles_sample_rate: 1
enable_logs: true
log_flush_threshold: 2
attach_stacktrace: true
attach_metric_code_locations: true
context_lines: 0
Expand Down
1 change: 1 addition & 0 deletions tests/DependencyInjection/SentryExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ public function testClientIsCreatedFromOptions(): void
'traces_sampler' => new Reference('App\\Sentry\\Tracing\\TracesSampler'),
'profiles_sample_rate' => 1,
'enable_logs' => true,
'log_flush_threshold' => 2,
'enable_metrics' => true,
'attach_stacktrace' => true,
'attach_metric_code_locations' => true,
Expand Down
3 changes: 3 additions & 0 deletions tests/End2End/App/config/log_flush_threshold.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
sentry:
options:
log_flush_threshold: 2
3 changes: 3 additions & 0 deletions tests/End2End/App/config/log_flush_threshold_null.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
sentry:
options:
log_flush_threshold: ~
102 changes: 102 additions & 0 deletions tests/End2End/LoggingFlushThresholdEnd2EndTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

declare(strict_types=1);

namespace Sentry\SentryBundle\Tests\End2End;

use Sentry\Event;
use Sentry\Logs\Log;
use Sentry\SentryBundle\Tests\End2End\App\KernelWithExtraConfig;
use Symfony\Bundle\FrameworkBundle\Client;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpKernel\KernelInterface;

if (!class_exists(KernelBrowser::class) && class_exists(Client::class)) {
class_alias(Client::class, KernelBrowser::class);
}

/**
* @runTestsInSeparateProcesses
*/
final class LoggingFlushThresholdEnd2EndTest extends WebTestCase
{
/**
* @param array{extra_config_files?: list<string>} $options
*/
protected static function createKernel(array $options = []): KernelInterface
{
return new KernelWithExtraConfig(array_merge([
__DIR__ . '/App/logging.yml',
], $options['extra_config_files'] ?? []));
}

protected function setUp(): void
{
parent::setUp();

StubTransport::$events = [];
}

public function testLoggerFlushesLogsWhenThresholdIsReached(): void
{
$client = static::createClient([
'extra_config_files' => [__DIR__ . '/App/config/log_flush_threshold.yml'],
]);

$client->request('GET', '/just-logging');

$this->assertSame(200, $client->getResponse()->getStatusCode());

$logs = $this->getFilteredLogBodies();

$this->assertCount(2, $logs);
$this->assertSame(['Emergency Log', 'Critical Log'], $logs[0]);
$this->assertSame(['Error Log', 'Warn Log'], $logs[1]);
}

public function testLoggerDoesNotFlushLogsWhenThresholdIsNull(): void
{
$client = static::createClient([
'extra_config_files' => [__DIR__ . '/App/config/log_flush_threshold_null.yml'],
]);

$client->request('GET', '/just-logging');

$this->assertSame(200, $client->getResponse()->getStatusCode());

$logs = $this->getFilteredLogBodies();

$this->assertCount(1, $logs);
$this->assertSame(['Emergency Log', 'Critical Log', 'Error Log', 'Warn Log'], $logs[0]);
}

/**
* Removes framework logs so that the tests can focus on our expected logs.
*
* @param Log[] $logs
*
* @return Log[]
*/
private function filterFrameworkLogs(array $logs): array
{
return array_values(array_filter($logs, static function (Log $log): bool {
return 'Matched route "{route}".' !== $log->getBody()
&& 'Notified event "{event}" to listener "{listener}".' !== $log->getBody();
}));
}

/**
* @return array<int, string[]>
*/
private function getFilteredLogBodies(): array
{
return array_values(array_filter(array_map(function (Event $event): array {
return array_map(static function (Log $log): string {
return $log->getBody();
}, $this->filterFrameworkLogs($event->getLogs()));
}, StubTransport::$events), static function (array $logs): bool {
return \count($logs) > 0;
}));
}
}
Loading