Skip to content

Commit 2ff5801

Browse files
committed
Signed-off-by: Johnny Lim <[email protected]>
1 parent 7d54dd1 commit 2ff5801

File tree

5 files changed

+102
-124
lines changed

5 files changed

+102
-124
lines changed

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

Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,36 @@ public class DefaultMailSendObservationConvention implements MailSendObservation
4040

4141
private static final String UNKNOWN = "unknown";
4242

43+
private static final KeyValue NETWORK_PROTOCOL_NAME_UNKNOWN = NETWORK_PROTOCOL_NAME.withValue(UNKNOWN);
44+
45+
private static final KeyValue SERVER_PORT_UNKNOWN = SERVER_PORT.withValue(UNKNOWN);
46+
47+
private static final KeyValue SERVER_ADDRESS_UNKNOWN = SERVER_ADDRESS.withValue(UNKNOWN);
48+
49+
private static final KeyValue SMTP_MESSAGE_SUBJECT_UNKNOWN = SMTP_MESSAGE_SUBJECT.withValue(UNKNOWN);
50+
51+
private static final KeyValue SMTP_MESSAGE_FROM_UNKNOWN = SMTP_MESSAGE_FROM.withValue(UNKNOWN);
52+
53+
private static final KeyValue SMTP_MESSAGE_ID_UNKNOWN = SMTP_MESSAGE_ID.withValue(UNKNOWN);
54+
55+
private static final Map<RecipientType, KeyName> RECIPIENT_TYPE_KEY_NAME_MAP;
56+
static {
57+
Map<RecipientType, KeyName> map = new IdentityHashMap<>();
58+
map.put(RecipientType.TO, SMTP_MESSAGE_TO);
59+
map.put(RecipientType.CC, SMTP_MESSAGE_CC);
60+
map.put(RecipientType.BCC, SMTP_MESSAGE_BCC);
61+
RECIPIENT_TYPE_KEY_NAME_MAP = map;
62+
}
63+
64+
private static final Map<RecipientType, KeyValue> RECIPIENT_TYPE_UNKNOWN_MAP;
65+
static {
66+
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));
70+
RECIPIENT_TYPE_UNKNOWN_MAP = map;
71+
}
72+
4373
@Override
4474
public String getName() {
4575
return "mail.send";
@@ -58,68 +88,62 @@ public KeyValues getLowCardinalityKeyValues(MailSendObservationContext context)
5888
@Override
5989
public KeyValues getHighCardinalityKeyValues(MailSendObservationContext context) {
6090
Message message = context.getCarrier();
91+
if (message == null) {
92+
return KeyValues.empty();
93+
}
94+
6195
List<KeyValue> values = new ArrayList<>();
6296
smtpMessageSubject(message).ifPresent(values::add);
6397
smtpMessageFrom(message).ifPresent(values::add);
6498
smtpMessageRecipients(message, RecipientType.TO).ifPresent(values::add);
6599
smtpMessageRecipients(message, RecipientType.CC).ifPresent(values::add);
66100
smtpMessageRecipients(message, RecipientType.BCC).ifPresent(values::add);
67-
68101
return KeyValues.of(values);
69102
}
70103

71104
private KeyValue serverAddress(MailSendObservationContext context) {
72105
String host = context.getHost();
73106
if (host == null || host.isEmpty()) {
74-
return SERVER_ADDRESS.withValue(UNKNOWN);
107+
return SERVER_ADDRESS_UNKNOWN;
75108
}
76109
return SERVER_ADDRESS.withValue(host);
77110
}
78111

79112
private KeyValue serverPort(MailSendObservationContext context) {
80113
int port = context.getPort();
81114
if (port <= 0) {
82-
return SERVER_PORT.withValue(UNKNOWN);
115+
return SERVER_PORT_UNKNOWN;
83116
}
84117
return SERVER_PORT.withValue(String.valueOf(port));
85118
}
86119

87120
private KeyValue networkProtocolName(MailSendObservationContext context) {
88121
String protocol = context.getProtocol();
89122
if (protocol == null || protocol.isEmpty()) {
90-
return NETWORK_PROTOCOL_NAME.withValue(UNKNOWN);
123+
return NETWORK_PROTOCOL_NAME_UNKNOWN;
91124
}
92125
return NETWORK_PROTOCOL_NAME.withValue(protocol);
93126
}
94127

95-
private Optional<KeyValue> smtpMessageSubject(@Nullable Message message) {
96-
if (message == null) {
97-
return Optional.empty();
98-
}
99-
return safeExtractValue(SMTP_MESSAGE_SUBJECT, () -> Optional.ofNullable(message.getSubject()));
128+
private Optional<KeyValue> smtpMessageSubject(Message message) {
129+
return safeExtractValue(SMTP_MESSAGE_SUBJECT, () -> Optional.ofNullable(message.getSubject()),
130+
SMTP_MESSAGE_SUBJECT_UNKNOWN);
100131
}
101132

102-
private Optional<KeyValue> smtpMessageFrom(@Nullable Message message) {
103-
if (message == null) {
104-
return Optional.empty();
105-
}
106-
return safeExtractValue(SMTP_MESSAGE_FROM, () -> addressesToValue(message.getFrom()));
133+
private Optional<KeyValue> smtpMessageFrom(Message message) {
134+
return safeExtractValue(SMTP_MESSAGE_FROM, () -> addressesToValue(message.getFrom()),
135+
SMTP_MESSAGE_FROM_UNKNOWN);
107136
}
108137

109-
private Optional<KeyValue> smtpMessageRecipients(@Nullable Message message, RecipientType recipientType) {
110-
if (message == null) {
111-
return Optional.empty();
112-
}
113-
MailObservationDocumentation.HighCardinalityKeyNames key = MailObservationDocumentation.HighCardinalityKeyNames
114-
.valueOf("SMTP_MESSAGE_" + recipientType.toString().toUpperCase(Locale.ROOT));
115-
return safeExtractValue(key, () -> addressesToValue(message.getRecipients(recipientType)));
138+
private Optional<KeyValue> smtpMessageRecipients(Message message, RecipientType recipientType) {
139+
KeyName keyName = Objects.requireNonNull(RECIPIENT_TYPE_KEY_NAME_MAP.get(recipientType));
140+
KeyValue unknownValue = Objects.requireNonNull(RECIPIENT_TYPE_UNKNOWN_MAP.get(recipientType));
141+
return safeExtractValue(keyName, () -> addressesToValue(message.getRecipients(recipientType)), unknownValue);
116142
}
117143

118-
Optional<KeyValue> smtpMessageId(@Nullable Message message) {
119-
if (message == null) {
120-
return Optional.empty();
121-
}
122-
return safeExtractValue(SMTP_MESSAGE_ID, () -> extractHeaderValue(message, "Message-ID"));
144+
Optional<KeyValue> smtpMessageId(Message message) {
145+
return safeExtractValue(SMTP_MESSAGE_ID, () -> extractHeaderValue(message, "Message-ID"),
146+
SMTP_MESSAGE_ID_UNKNOWN);
123147
}
124148

125149
private Optional<String> extractHeaderValue(Message message, String headerName) throws MessagingException {
@@ -130,12 +154,12 @@ private Optional<String> extractHeaderValue(Message message, String headerName)
130154
return Optional.of(String.join(", ", header));
131155
}
132156

133-
private Optional<KeyValue> safeExtractValue(KeyName key, ValueExtractor extractor) {
157+
private Optional<KeyValue> safeExtractValue(KeyName key, ValueExtractor extractor, KeyValue unknownValue) {
134158
try {
135159
return extractor.extract().map(key::withValue);
136160
}
137161
catch (MessagingException ex) {
138-
return Optional.of(key.withValue(UNKNOWN));
162+
return Optional.of(unknownValue);
139163
}
140164
}
141165

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
import org.jspecify.annotations.Nullable;
2323

2424
/**
25-
* Wraps a {@link Transport} so that it is instrumented with a Micrometer
26-
* {@link Observation}.
25+
* Wraps a {@link Transport} so that it is instrumented with an {@link Observation}.
2726
*
2827
* @since 1.16.0
2928
* @author famaridon
@@ -32,18 +31,18 @@ public class InstrumentedTransport extends Transport {
3231

3332
private static final DefaultMailSendObservationConvention DEFAULT_CONVENTION = new DefaultMailSendObservationConvention();
3433

34+
private final Transport delegate;
35+
3536
private final ObservationRegistry observationRegistry;
3637

37-
private final Transport delegate;
38+
private final @Nullable ObservationConvention<MailSendObservationContext> customConvention;
3839

3940
private final @Nullable String protocol;
4041

4142
private @Nullable String host;
4243

4344
private int port;
4445

45-
private final @Nullable ObservationConvention<MailSendObservationContext> customConvention;
46-
4746
/**
4847
* Create an instrumented transport using the
4948
* {@link DefaultMailSendObservationConvention default} {@link ObservationConvention}.
@@ -66,10 +65,10 @@ public InstrumentedTransport(Session session, Transport delegate, ObservationReg
6665
public InstrumentedTransport(Session session, Transport delegate, ObservationRegistry observationRegistry,
6766
@Nullable ObservationConvention<MailSendObservationContext> customConvention) {
6867
super(session, delegate.getURLName());
69-
this.protocol = this.url.getProtocol();
7068
this.delegate = delegate;
7169
this.observationRegistry = observationRegistry;
7270
this.customConvention = customConvention;
71+
this.protocol = this.url.getProtocol();
7372
}
7473

7574
@Override

micrometer-jakarta9/src/main/java/io/micrometer/jakarta9/instrument/mail/package-info.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
/**
1818
* Observation instrumentation for Jakarta Mail.
19-
*
20-
* @since 1.16.0
2119
*/
2220
@NullMarked
2321
package io.micrometer.jakarta9.instrument.mail;

0 commit comments

Comments
 (0)