Skip to content

Commit

Permalink
Merge pull request #1340 from scireum/feature/ymo/SIRI-917-mails
Browse files Browse the repository at this point in the history
SIRI-917 add missing metric value for mails
  • Loading branch information
ymo-sci authored Dec 13, 2023
2 parents c8495b3 + 93e564d commit 9339bba
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
15 changes: 14 additions & 1 deletion src/main/java/sirius/web/mails/Mails.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ public class Mails implements MetricProvider {
@Override
public void gather(MetricsCollector collector) {
collector.metric("mails_out", "mails-out", "Mails Sent", mailsOut.getCount(), null);
collector.metric("mails_duration", "mails-duration", "Send Mail Duration", mailsOut.getAndClear(), Metric.UNIT_MS);
collector.metric("mails_duration",
"mails-duration",
"Send Mail Duration",
mailsOut.getAndClear(),
Metric.UNIT_MS);
}

/**
Expand Down Expand Up @@ -121,4 +125,13 @@ public void failForInvalidEmail(@Nullable String address, @Nullable String name)
.handle();
}
}

/**
* Updated the mail metrics to count sent mails and their duration.
*
* @param time the time it took to send the mail
*/
public void collectMailSendMetric(long time) {
mailsOut.addValue(time);
}
}
11 changes: 9 additions & 2 deletions src/main/java/sirius/web/mails/SendMailTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import sirius.kernel.async.Tasks;
import sirius.kernel.commons.Explain;
import sirius.kernel.commons.Strings;
import sirius.kernel.commons.Watch;
import sirius.kernel.di.std.ConfigValue;
import sirius.kernel.di.std.Part;
import sirius.kernel.di.std.Parts;
Expand Down Expand Up @@ -94,6 +95,9 @@ class SendMailTask implements Runnable {
@Part
private static Tasks tasks;

@Part
private static Mails mails;

@ConfigValue("mail.smtp.dkim.keyFile")
private static String dkimKeyFile;

Expand Down Expand Up @@ -185,6 +189,7 @@ private void logToConsole() {
}

private void sendMail() {
Watch mailSendTime = Watch.start();
try {
Mails.LOG.FINE("Sending eMail: " + mail.subject + " to: " + mail.receiverEmail);
Session session = getMailSession(config);
Expand All @@ -201,7 +206,7 @@ private void sendMail() {
mail.remainingAttempts.get());

// Re-schedule mail in async task...
mail.sendMailAsync();
mail.sendMailAsync();

return;
}
Expand All @@ -223,6 +228,7 @@ private void sendMail() {
// We either successfully completed (or finally failed) sending the task - log mail and cleanup scheduler...
tasks.forgetSynchronizer(mail.internalMessageId);
logSentMail();
mails.collectMailSendMetric(mailSendTime.elapsedMillis());
}

private void sendMailViaTransport(Session session, Transport transport) {
Expand Down Expand Up @@ -362,7 +368,8 @@ private void determineTechnicalSender() {

private Session getMailSession(SMTPConfiguration config) {
Properties props = new Properties();
props.setProperty(MAIL_SMTP_PORT, Strings.isEmpty(config.getMailPort()) ? DEFAULT_SMTP_PORT : config.getMailPort());
props.setProperty(MAIL_SMTP_PORT,
Strings.isEmpty(config.getMailPort()) ? DEFAULT_SMTP_PORT : config.getMailPort());
props.setProperty(MAIL_SMTP_HOST, config.getMailHost());
if (Strings.isFilled(config.getMailSender())) {
props.setProperty(MAIL_FROM, config.getMailSender());
Expand Down

0 comments on commit 9339bba

Please sign in to comment.