Skip to content

Commit

Permalink
Merge pull request #22 from Kanti/feature/mail-stopwatch
Browse files Browse the repository at this point in the history
  • Loading branch information
Kanti authored Aug 15, 2023
2 parents 11915a4 + a908276 commit 5a218c0
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
35 changes: 35 additions & 0 deletions Classes/EventListener/MailEventListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Kanti\ServerTiming\EventListener;

use Kanti\ServerTiming\Dto\StopWatch;
use Kanti\ServerTiming\Utility\TimingUtility;
use Symfony\Component\Mime\Email;
use TYPO3\CMS\Core\Mail\Event\AfterMailerSentMessageEvent;
use TYPO3\CMS\Core\Mail\Event\BeforeMailerSentMessageEvent;

final class MailEventListener
{
public ?StopWatch $stopWatch = null;

public function start(BeforeMailerSentMessageEvent $event): void
{
$info = '';
$message = $event->getMessage();
if ($message instanceof Email) {
$emails = implode(', ', array_map(static fn($address): string => $address->getAddress(), $message->getTo()));
$info = $message->getSubject() . ' -> ' . $emails;
}

$this->stopWatch?->stopIfNot();
$this->stopWatch = TimingUtility::stopWatch('mail', $info);
}

public function stop(AfterMailerSentMessageEvent $event): void
{
$this->stopWatch?->stopIfNot();
$this->stopWatch = null;
}
}
13 changes: 13 additions & 0 deletions Configuration/Services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,16 @@ services:
identifier: kanti/server-timing/console-terminate-event-listener
event: Symfony\Component\Console\Event\ConsoleTerminateEvent
method: stop

Kanti\ServerTiming\EventListener\MailEventListener:
tags:
-
name: event.listener
identifier: kanti/server-timing/mail-event-listener
event: TYPO3\CMS\Core\Mail\Event\BeforeMailerSentMessageEvent
method: start
-
name: event.listener
identifier: kanti/server-timing/mail-event-listener
event: TYPO3\CMS\Core\Mail\Event\AfterMailerSentMessageEvent
method: stop
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,23 @@ at the moment there is nothing to configure
- `php`: from start of php call to the register shutdown function
- `middleware`: will show how much time was spend in the **inward** and **outward** middleware directions
- `sql`: shows the sql query's
- `mail`: shows the mails that are send (only TYPO3 12)
- `extbase`: show all Extbase dispatches, (forwards are included in the original action call)
- `guzzle`: external API calls are measured if they use the official TYPO3 `RequestFactory` or the `GuzzleClientFactory`)

> if a measurement key has more than 4 entries, they will get combined into one total time with a count.
> And the 3 longest entries will be kept
## Sentry Profiling

if you have sentry enabled (different Extension eg. `pluswerk/sentry` or `networkteam/sentry-client`) than you can activate the profiling.
- `sentry_sample_rate`
- if empty ` ` it will keep the setting that was set from the sentry extension.
- if set to a number like `0.1` it will track 10% of all Requests in sentry.
- `sentry_cli_sample_rate`
- just like `sentry_sample_rate` but this setting is for the cli calls of the `typo3` binary
- `stop_watch_limit` is set for long-running processes, if you get memory problems you can lower this setting. (default: 100_000)

## Measure your own timings:

### `stopWatch` function (recommended)
Expand Down
15 changes: 15 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,18 @@ parameters:
message: "#^Parameter \\#1 \\$loggers of class Doctrine\\\\DBAL\\\\Logging\\\\LoggerChain constructor expects iterable\\<Doctrine\\\\DBAL\\\\Logging\\\\SQLLogger\\>, array\\<int, Doctrine\\\\DBAL\\\\Logging\\\\SQLLogger\\|TYPO3\\\\CMS\\\\Adminpanel\\\\Log\\\\DoctrineSqlLogger\\> given\\.$#"
count: 1
path: Classes/Middleware/AdminpanelSqlLoggingMiddleware.php

-
message: "#^Call to method getMessage\\(\\) on an unknown class TYPO3\\\\CMS\\\\Core\\\\Mail\\\\Event\\\\BeforeMailerSentMessageEvent\\.$#"
count: 1
path: Classes/EventListener/MailEventListener.php

-
message: "#^Parameter \\$event of method Kanti\\\\ServerTiming\\\\EventListener\\\\MailEventListener\\:\\:start\\(\\) has invalid type TYPO3\\\\CMS\\\\Core\\\\Mail\\\\Event\\\\BeforeMailerSentMessageEvent\\.$#"
count: 1
path: Classes/EventListener/MailEventListener.php

-
message: "#^Parameter \\$event of method Kanti\\\\ServerTiming\\\\EventListener\\\\MailEventListener\\:\\:stop\\(\\) has invalid type TYPO3\\\\CMS\\\\Core\\\\Mail\\\\Event\\\\AfterMailerSentMessageEvent\\.$#"
count: 1
path: Classes/EventListener/MailEventListener.php

0 comments on commit 5a218c0

Please sign in to comment.