Skip to content

Commit b82cafb

Browse files
authored
Support newsgroups KeyValue for Jakarta Mail instrumentation (#6558)
See #6554 (comment) Signed-off-by: Johnny Lim <[email protected]>
1 parent af78f20 commit b82cafb

File tree

4 files changed

+38
-15
lines changed

4 files changed

+38
-15
lines changed

micrometer-jakarta9/src/main/java/io/micrometer/jakarta9/instrument/mail/DefaultMailSendObservationConvention.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import jakarta.mail.Message;
2323
import jakarta.mail.Message.RecipientType;
2424
import jakarta.mail.MessagingException;
25+
import jakarta.mail.internet.MimeMessage;
2526
import org.jspecify.annotations.Nullable;
2627

2728
import java.util.*;
@@ -58,15 +59,19 @@ public class DefaultMailSendObservationConvention implements MailSendObservation
5859
map.put(RecipientType.TO, SMTP_MESSAGE_TO);
5960
map.put(RecipientType.CC, SMTP_MESSAGE_CC);
6061
map.put(RecipientType.BCC, SMTP_MESSAGE_BCC);
62+
map.put(MimeMessage.RecipientType.NEWSGROUPS, SMTP_MESSAGE_NEWSGROUPS);
6163
RECIPIENT_TYPE_KEY_NAME_MAP = map;
6264
}
6365

66+
// VisibleForTesting
67+
static final Set<RecipientType> RECIPIENT_TYPES = RECIPIENT_TYPE_KEY_NAME_MAP.keySet();
68+
6469
private static final Map<RecipientType, KeyValue> RECIPIENT_TYPE_UNKNOWN_MAP;
6570
static {
6671
Map<RecipientType, KeyValue> map = new IdentityHashMap<>();
67-
map.put(RecipientType.TO, SMTP_MESSAGE_TO.withValue(UNKNOWN));
68-
map.put(RecipientType.CC, SMTP_MESSAGE_CC.withValue(UNKNOWN));
69-
map.put(RecipientType.BCC, SMTP_MESSAGE_BCC.withValue(UNKNOWN));
72+
for (Map.Entry<RecipientType, KeyName> entry : RECIPIENT_TYPE_KEY_NAME_MAP.entrySet()) {
73+
map.put(entry.getKey(), entry.getValue().withValue(UNKNOWN));
74+
}
7075
RECIPIENT_TYPE_UNKNOWN_MAP = map;
7176
}
7277

@@ -95,9 +100,9 @@ public KeyValues getHighCardinalityKeyValues(MailSendObservationContext context)
95100
List<KeyValue> values = new ArrayList<>();
96101
smtpMessageSubject(message).ifPresent(values::add);
97102
smtpMessageFrom(message).ifPresent(values::add);
98-
smtpMessageRecipients(message, RecipientType.TO).ifPresent(values::add);
99-
smtpMessageRecipients(message, RecipientType.CC).ifPresent(values::add);
100-
smtpMessageRecipients(message, RecipientType.BCC).ifPresent(values::add);
103+
for (RecipientType recipientType : RECIPIENT_TYPES) {
104+
smtpMessageRecipients(message, recipientType).ifPresent(values::add);
105+
}
101106
return KeyValues.of(values);
102107
}
103108

micrometer-jakarta9/src/main/java/io/micrometer/jakarta9/instrument/mail/MailObservationDocumentation.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,15 @@ public String asString() {
120120
return "smtp.message.bcc";
121121
}
122122
},
123+
/**
124+
* Newsgroup (Usenet news) recipient(s) of the mail.
125+
*/
126+
SMTP_MESSAGE_NEWSGROUPS {
127+
@Override
128+
public String asString() {
129+
return "smtp.message.newsgroups";
130+
}
131+
},
123132
/**
124133
* Subject line of the mail.
125134
*/

micrometer-jakarta9/src/test/java/io/micrometer/jakarta9/instrument/mail/DefaultMailSendObservationConventionTests.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616
package io.micrometer.jakarta9.instrument.mail;
1717

1818
import io.micrometer.common.KeyValue;
19+
import jakarta.mail.Address;
1920
import jakarta.mail.Message;
2021
import jakarta.mail.MessagingException;
2122
import jakarta.mail.Session;
2223
import jakarta.mail.internet.InternetAddress;
2324
import jakarta.mail.internet.MimeMessage;
25+
import jakarta.mail.internet.NewsAddress;
2426
import org.junit.jupiter.api.Test;
2527
import org.junit.jupiter.params.ParameterizedTest;
2628
import org.junit.jupiter.params.provider.Arguments;
@@ -176,19 +178,27 @@ void recipientsShouldBeMissingWhenEmpty(Message.RecipientType recipientType) thr
176178
@MethodSource("recipientTypes")
177179
void recipientsShouldBeThereWhenSet(Message.RecipientType recipientType) throws MessagingException {
178180
Message message = new MimeMessage((Session) null);
179-
message.addRecipient(recipientType, new InternetAddress("[email protected]"));
180-
assertThat(getHighCardinalityKeyValues(message))
181-
.containsExactly(KeyValue.of(getKey(recipientType), "[email protected]"));
181+
String newsgroup = "news.announce";
182+
String mail = "[email protected]";
183+
Address address = recipientType == MimeMessage.RecipientType.NEWSGROUPS ? new NewsAddress(newsgroup)
184+
: new InternetAddress(mail);
185+
message.addRecipient(recipientType, address);
186+
assertThat(getHighCardinalityKeyValues(message)).containsExactly(KeyValue.of(getKey(recipientType),
187+
recipientType == MimeMessage.RecipientType.NEWSGROUPS ? newsgroup : mail));
182188
}
183189

184190
@ParameterizedTest
185191
@MethodSource("recipientTypes")
186192
void multipleRecipientsShouldBeThereWhenMultipleSet(Message.RecipientType recipientType) throws MessagingException {
187193
Message message = new MimeMessage((Session) null);
188-
message.addRecipients(recipientType, new InternetAddress[] { new InternetAddress("[email protected]"),
189-
new InternetAddress("[email protected]") });
194+
NewsAddress[] newsAddresses = { new NewsAddress("news.announce"), new NewsAddress("news.misc") };
195+
InternetAddress[] internetAddresses = { new InternetAddress("[email protected]"),
196+
new InternetAddress("[email protected]") };
197+
message.addRecipients(recipientType,
198+
recipientType == MimeMessage.RecipientType.NEWSGROUPS ? newsAddresses : internetAddresses);
190199
assertThat(getHighCardinalityKeyValues(message))
191-
.containsExactly(KeyValue.of(getKey(recipientType), "[email protected], [email protected]"));
200+
.containsExactly(KeyValue.of(getKey(recipientType), recipientType == MimeMessage.RecipientType.NEWSGROUPS
201+
? "news.announce, news.misc" : "[email protected], [email protected]"));
192202
}
193203

194204
@Test
@@ -245,8 +255,7 @@ private List<KeyValue> getLowCardinalityKeyValues(MailSendObservationContext con
245255
}
246256

247257
static Stream<Arguments> recipientTypes() {
248-
return Stream.of(Message.RecipientType.TO, Message.RecipientType.CC, Message.RecipientType.BCC)
249-
.map(Arguments::of);
258+
return DefaultMailSendObservationConvention.RECIPIENT_TYPES.stream().map(Arguments::of);
250259
}
251260

252261
}

micrometer-jakarta9/src/test/java/io/micrometer/jakarta9/instrument/mail/MailObservationDocumentationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ void testMailSendObservation() {
4343
// Verify high cardinality key names
4444
KeyName[] highCardinalityKeyNames = mailSend.getHighCardinalityKeyNames();
4545
assertThat(highCardinalityKeyNames).containsExactly(SMTP_MESSAGE_FROM, SMTP_MESSAGE_TO, SMTP_MESSAGE_CC,
46-
SMTP_MESSAGE_BCC, SMTP_MESSAGE_SUBJECT, SMTP_MESSAGE_ID);
46+
SMTP_MESSAGE_BCC, SMTP_MESSAGE_NEWSGROUPS, SMTP_MESSAGE_SUBJECT, SMTP_MESSAGE_ID);
4747
}
4848

4949
}

0 commit comments

Comments
 (0)