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
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import jakarta.mail.Message;
import jakarta.mail.Message.RecipientType;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import org.jspecify.annotations.Nullable;

import java.util.*;
Expand Down Expand Up @@ -58,15 +59,19 @@ public class DefaultMailSendObservationConvention implements MailSendObservation
map.put(RecipientType.TO, SMTP_MESSAGE_TO);
map.put(RecipientType.CC, SMTP_MESSAGE_CC);
map.put(RecipientType.BCC, SMTP_MESSAGE_BCC);
map.put(MimeMessage.RecipientType.NEWSGROUPS, SMTP_MESSAGE_NEWSGROUPS);
RECIPIENT_TYPE_KEY_NAME_MAP = map;
}

// VisibleForTesting
static final Set<RecipientType> RECIPIENT_TYPES = RECIPIENT_TYPE_KEY_NAME_MAP.keySet();

private static final Map<RecipientType, KeyValue> RECIPIENT_TYPE_UNKNOWN_MAP;
static {
Map<RecipientType, KeyValue> map = new IdentityHashMap<>();
map.put(RecipientType.TO, SMTP_MESSAGE_TO.withValue(UNKNOWN));
map.put(RecipientType.CC, SMTP_MESSAGE_CC.withValue(UNKNOWN));
map.put(RecipientType.BCC, SMTP_MESSAGE_BCC.withValue(UNKNOWN));
for (Map.Entry<RecipientType, KeyName> entry : RECIPIENT_TYPE_KEY_NAME_MAP.entrySet()) {
map.put(entry.getKey(), entry.getValue().withValue(UNKNOWN));
}
RECIPIENT_TYPE_UNKNOWN_MAP = map;
}

Expand Down Expand Up @@ -95,9 +100,9 @@ public KeyValues getHighCardinalityKeyValues(MailSendObservationContext context)
List<KeyValue> values = new ArrayList<>();
smtpMessageSubject(message).ifPresent(values::add);
smtpMessageFrom(message).ifPresent(values::add);
smtpMessageRecipients(message, RecipientType.TO).ifPresent(values::add);
smtpMessageRecipients(message, RecipientType.CC).ifPresent(values::add);
smtpMessageRecipients(message, RecipientType.BCC).ifPresent(values::add);
for (RecipientType recipientType : RECIPIENT_TYPES) {
smtpMessageRecipients(message, recipientType).ifPresent(values::add);
}
return KeyValues.of(values);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,15 @@ public String asString() {
return "smtp.message.bcc";
}
},
/**
* Newsgroup (Usenet news) recipient(s) of the mail.
*/
SMTP_MESSAGE_NEWSGROUPS {
@Override
public String asString() {
return "smtp.message.newsgroups";
}
},
/**
* Subject line of the mail.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
package io.micrometer.jakarta9.instrument.mail;

import io.micrometer.common.KeyValue;
import jakarta.mail.Address;
import jakarta.mail.Message;
import jakarta.mail.MessagingException;
import jakarta.mail.Session;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;
import jakarta.mail.internet.NewsAddress;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
Expand Down Expand Up @@ -176,19 +178,27 @@ void recipientsShouldBeMissingWhenEmpty(Message.RecipientType recipientType) thr
@MethodSource("recipientTypes")
void recipientsShouldBeThereWhenSet(Message.RecipientType recipientType) throws MessagingException {
Message message = new MimeMessage((Session) null);
message.addRecipient(recipientType, new InternetAddress("[email protected]"));
assertThat(getHighCardinalityKeyValues(message))
.containsExactly(KeyValue.of(getKey(recipientType), "[email protected]"));
String newsgroup = "news.announce";
String mail = "[email protected]";
Address address = recipientType == MimeMessage.RecipientType.NEWSGROUPS ? new NewsAddress(newsgroup)
: new InternetAddress(mail);
message.addRecipient(recipientType, address);
assertThat(getHighCardinalityKeyValues(message)).containsExactly(KeyValue.of(getKey(recipientType),
recipientType == MimeMessage.RecipientType.NEWSGROUPS ? newsgroup : mail));
}

@ParameterizedTest
@MethodSource("recipientTypes")
void multipleRecipientsShouldBeThereWhenMultipleSet(Message.RecipientType recipientType) throws MessagingException {
Message message = new MimeMessage((Session) null);
message.addRecipients(recipientType, new InternetAddress[] { new InternetAddress("[email protected]"),
new InternetAddress("[email protected]") });
NewsAddress[] newsAddresses = { new NewsAddress("news.announce"), new NewsAddress("news.misc") };
InternetAddress[] internetAddresses = { new InternetAddress("[email protected]"),
new InternetAddress("[email protected]") };
message.addRecipients(recipientType,
recipientType == MimeMessage.RecipientType.NEWSGROUPS ? newsAddresses : internetAddresses);
assertThat(getHighCardinalityKeyValues(message))
.containsExactly(KeyValue.of(getKey(recipientType), "[email protected], [email protected]"));
.containsExactly(KeyValue.of(getKey(recipientType), recipientType == MimeMessage.RecipientType.NEWSGROUPS
? "news.announce, news.misc" : "[email protected], [email protected]"));
}

@Test
Expand Down Expand Up @@ -245,8 +255,7 @@ private List<KeyValue> getLowCardinalityKeyValues(MailSendObservationContext con
}

static Stream<Arguments> recipientTypes() {
return Stream.of(Message.RecipientType.TO, Message.RecipientType.CC, Message.RecipientType.BCC)
.map(Arguments::of);
return DefaultMailSendObservationConvention.RECIPIENT_TYPES.stream().map(Arguments::of);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void testMailSendObservation() {
// Verify high cardinality key names
KeyName[] highCardinalityKeyNames = mailSend.getHighCardinalityKeyNames();
assertThat(highCardinalityKeyNames).containsExactly(SMTP_MESSAGE_FROM, SMTP_MESSAGE_TO, SMTP_MESSAGE_CC,
SMTP_MESSAGE_BCC, SMTP_MESSAGE_SUBJECT, SMTP_MESSAGE_ID);
SMTP_MESSAGE_BCC, SMTP_MESSAGE_NEWSGROUPS, SMTP_MESSAGE_SUBJECT, SMTP_MESSAGE_ID);
}

}