Skip to content

Commit dbdef93

Browse files
committed
Add Jakarta Mail instrumentation for observability
close gh-5985 Signed-off-by: famaridon <[email protected]>
1 parent c70fd43 commit dbdef93

17 files changed

+895
-0
lines changed

Diff for: dependencies.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def VERSIONS = [
3434
libs.restAssured,
3535
libs.retrofit2,
3636
libs.jakarta.jmsApi,
37+
libs.jakarta.mailApi,
3738
libs.jakarta.servletApi,
3839
libs.javax.cacheApi,
3940
libs.javax.inject,

Diff for: gradle/libs.versions.toml

+1
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ jacksonDatabind = { module = "com.fasterxml.jackson.core:jackson-databind", vers
134134
jakarta-annotationApi = { module = "jakarta.annotation:jakarta.annotation-api", version = "3.0.0" }
135135
jakarta-injectApi = { module = "jakarta.inject:jakarta.inject-api", version = "2.0.1" }
136136
jakarta-jmsApi = { module = "jakarta.jms:jakarta.jms-api", version = "3.0.0" }
137+
jakarta-mailApi = { module = "jakarta.mail:jakarta.mail-api", version = "2.1.3" }
137138
jakarta-servletApi = { module = "jakarta.servlet:jakarta.servlet-api", version = "5.0.0" }
138139
javalin = { module = "io.javalin:javalin", version = "5.6.5" }
139140
javax-cacheApi = { module = "javax.cache:cache-api", version.ref = "javax-cache" }

Diff for: micrometer-jakarta9/build.gradle

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ jar {
66
bnd '''\
77
Import-Package: \
88
jakarta.jms.*;resolution:=dynamic;version="${@}",\
9+
jakarta.mail.*;resolution:=dynamic;version="${@}",\
910
io.micrometer.observation.*;resolution:=dynamic;version="${@}",\
1011
*
1112
'''.stripIndent()
@@ -18,6 +19,7 @@ dependencies {
1819
api project(":micrometer-observation")
1920

2021
optionalApi 'jakarta.jms:jakarta.jms-api'
22+
optionalApi 'jakarta.mail:jakarta.mail-api'
2123

2224
testImplementation(libs.archunitJunit5) {
2325
// avoid transitively pulling in slf4j 2
@@ -26,4 +28,6 @@ dependencies {
2628
testImplementation libs.slf4jApi
2729
testImplementation libs.mockitoCore5
2830
testImplementation 'org.assertj:assertj-core'
31+
testImplementation 'org.assertj:assertj-core'
32+
testImplementation 'org.eclipse.angus:jakarta.mail:2.0.3'
2933
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2023 VMware, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.micrometer.jakarta9.instrument.mail;
17+
18+
import io.micrometer.common.KeyValues;
19+
import jakarta.mail.Message;
20+
21+
/**
22+
* Default implementation for {@link MailSendObservationConvention}.
23+
*/
24+
public class DefaultMailSendObservationConvention implements MailSendObservationConvention {
25+
26+
@Override
27+
public String getName() {
28+
return "mail.send";
29+
}
30+
31+
@Override
32+
public String getContextualName(MailSendObservationContext context) {
33+
return "mail send";
34+
}
35+
36+
@Override
37+
public KeyValues getLowCardinalityKeyValues(MailSendObservationContext context) {
38+
return KeyValues.of(MailKeyValues.serverAddress(context), MailKeyValues.serverPort(context),
39+
MailKeyValues.networkProtocolName(context));
40+
}
41+
42+
@Override
43+
public KeyValues getHighCardinalityKeyValues(MailSendObservationContext context) {
44+
Message message = context.getCarrier();
45+
return KeyValues.of(MailKeyValues.smtpMessageFrom(message), MailKeyValues.smtpMessageTo(message),
46+
MailKeyValues.smtpMessageSubject(message));
47+
}
48+
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
* Copyright 2023 VMware, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.micrometer.jakarta9.instrument.mail;
17+
18+
import io.micrometer.jakarta9.instrument.mail.MailObservationDocumentation.LowCardinalityKeyNames;
19+
20+
import java.util.Arrays;
21+
import java.util.stream.Collectors;
22+
23+
import io.micrometer.common.KeyValue;
24+
import jakarta.mail.Address;
25+
import jakarta.mail.Message;
26+
import jakarta.mail.MessagingException;
27+
import jakarta.mail.Message.RecipientType;
28+
29+
public class MailKeyValues {
30+
31+
private static final KeyValue SMTP_MESSAGE_FROM_UNKNOWN = KeyValue
32+
.of(MailObservationDocumentation.HighCardinalityKeyNames.SMTP_MESSAGE_FROM, "unknown");
33+
34+
private static final KeyValue SMTP_MESSAGE_TO_UNKNOWN = KeyValue
35+
.of(MailObservationDocumentation.HighCardinalityKeyNames.SMTP_MESSAGE_TO, "unknown");
36+
37+
private static final KeyValue SMTP_MESSAGE_SUBJECT_UNKNOWN = KeyValue
38+
.of(MailObservationDocumentation.HighCardinalityKeyNames.SMTP_MESSAGE_SUBJECT, "unknown");
39+
40+
private static final KeyValue SMTP_MESSAGE_ID_UNKNOWN = KeyValue
41+
.of(MailObservationDocumentation.HighCardinalityKeyNames.SMTP_MESSAGE_ID, "unknown");
42+
43+
private static final KeyValue SERVER_ADDRESS_UNKNOWN = KeyValue.of(LowCardinalityKeyNames.SERVER_ADDRESS,
44+
"unknown");
45+
46+
private static final KeyValue SERVER_PORT_UNKNOWN = KeyValue.of(LowCardinalityKeyNames.SERVER_PORT, "unknown");
47+
48+
private MailKeyValues() {
49+
}
50+
51+
static KeyValue smtpMessageFrom(Message message) {
52+
try {
53+
if (message.getFrom() == null || message.getFrom().length == 0) {
54+
return SMTP_MESSAGE_FROM_UNKNOWN;
55+
}
56+
String fromString = Arrays.stream(message.getFrom())
57+
.map(Address::toString)
58+
.collect(Collectors.joining(", "));
59+
return KeyValue.of(MailObservationDocumentation.HighCardinalityKeyNames.SMTP_MESSAGE_FROM, fromString);
60+
}
61+
catch (MessagingException exc) {
62+
return SMTP_MESSAGE_FROM_UNKNOWN;
63+
}
64+
}
65+
66+
static KeyValue smtpMessageTo(Message message) {
67+
try {
68+
Address[] recipients = message.getRecipients(RecipientType.TO);
69+
if (recipients == null || recipients.length == 0) {
70+
return SMTP_MESSAGE_TO_UNKNOWN;
71+
}
72+
String recipientsString = Arrays.stream(recipients)
73+
.map(Address::toString)
74+
.collect(Collectors.joining(", "));
75+
return KeyValue.of(MailObservationDocumentation.HighCardinalityKeyNames.SMTP_MESSAGE_TO, recipientsString);
76+
}
77+
catch (MessagingException exc) {
78+
return SMTP_MESSAGE_TO_UNKNOWN;
79+
}
80+
}
81+
82+
static KeyValue smtpMessageSubject(Message message) {
83+
try {
84+
if (message.getSubject() == null) {
85+
return SMTP_MESSAGE_SUBJECT_UNKNOWN;
86+
}
87+
return KeyValue.of(MailObservationDocumentation.HighCardinalityKeyNames.SMTP_MESSAGE_SUBJECT,
88+
message.getSubject());
89+
}
90+
catch (MessagingException exc) {
91+
return SMTP_MESSAGE_SUBJECT_UNKNOWN;
92+
}
93+
}
94+
95+
static KeyValue smtpMessageId(Message message) {
96+
try {
97+
if (message.getHeader("Message-ID") == null) {
98+
return SMTP_MESSAGE_ID_UNKNOWN;
99+
}
100+
return KeyValue.of(MailObservationDocumentation.HighCardinalityKeyNames.SMTP_MESSAGE_ID,
101+
message.getHeader("Message-ID")[0]);
102+
}
103+
catch (MessagingException exc) {
104+
return SMTP_MESSAGE_ID_UNKNOWN;
105+
}
106+
}
107+
108+
static KeyValue serverAddress(MailSendObservationContext context) {
109+
return KeyValue.of(LowCardinalityKeyNames.SERVER_ADDRESS,
110+
context.getHost() == null ? "unknown" : context.getHost());
111+
}
112+
113+
static KeyValue serverPort(MailSendObservationContext context) {
114+
return KeyValue.of(LowCardinalityKeyNames.SERVER_PORT,
115+
context.getPort() > 0 ? String.valueOf(context.getPort()) : "unknown");
116+
}
117+
118+
static KeyValue networkProtocolName(MailSendObservationContext context) {
119+
return KeyValue.of(LowCardinalityKeyNames.NETWORK_PROTOCOL_NAME, context.getProtocol());
120+
}
121+
122+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright 2023 VMware, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.micrometer.jakarta9.instrument.mail;
17+
18+
import io.micrometer.common.docs.KeyName;
19+
import io.micrometer.observation.Observation;
20+
import io.micrometer.observation.ObservationConvention;
21+
import io.micrometer.observation.docs.ObservationDocumentation;
22+
23+
/**
24+
* Documented {@link io.micrometer.common.KeyValue KeyValues} for the observations on
25+
* {@link jakarta.mail.Transport send} of mail messages.
26+
*/
27+
public enum MailObservationDocumentation implements ObservationDocumentation {
28+
29+
/**
30+
* Observation for mail send operations. Measures the time spent sending a mail
31+
* message.
32+
*/
33+
MAIL_SEND {
34+
@Override
35+
public Class<? extends ObservationConvention<? extends Observation.Context>> getDefaultConvention() {
36+
return DefaultMailSendObservationConvention.class;
37+
}
38+
39+
@Override
40+
public KeyName[] getLowCardinalityKeyNames() {
41+
return LowCardinalityKeyNames.values();
42+
}
43+
44+
@Override
45+
public KeyName[] getHighCardinalityKeyNames() {
46+
return HighCardinalityKeyNames.values();
47+
}
48+
};
49+
50+
public enum LowCardinalityKeyNames implements KeyName {
51+
52+
SERVER_ADDRESS {
53+
@Override
54+
public String asString() {
55+
return "server.address";
56+
}
57+
},
58+
SERVER_PORT {
59+
@Override
60+
public String asString() {
61+
return "server.port";
62+
}
63+
},
64+
NETWORK_PROTOCOL_NAME {
65+
@Override
66+
public String asString() {
67+
return "network.protocol.name";
68+
}
69+
}
70+
71+
}
72+
73+
public enum HighCardinalityKeyNames implements KeyName {
74+
75+
SMTP_MESSAGE_FROM {
76+
@Override
77+
public String asString() {
78+
return "smtp.message.from";
79+
}
80+
},
81+
SMTP_MESSAGE_TO {
82+
@Override
83+
public String asString() {
84+
return "smtp.message.to";
85+
}
86+
},
87+
SMTP_MESSAGE_SUBJECT {
88+
@Override
89+
public String asString() {
90+
return "smtp.message.subject";
91+
}
92+
},
93+
SMTP_MESSAGE_ID {
94+
@Override
95+
public String asString() {
96+
return "smtp.message.id";
97+
}
98+
}
99+
100+
}
101+
102+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2023 VMware, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.micrometer.jakarta9.instrument.mail;
17+
18+
import io.micrometer.common.lang.Nullable;
19+
import io.micrometer.common.util.internal.logging.WarnThenDebugLogger;
20+
import io.micrometer.observation.transport.SenderContext;
21+
import jakarta.mail.Message;
22+
import jakarta.mail.MessagingException;
23+
24+
/**
25+
* Context that holds information for observation metadata collection during the
26+
* {@link MailObservationDocumentation#MAIL_SEND sending of mail messages}.
27+
* <p>
28+
* This propagates the tracing information with the message sent by
29+
* {@link Message#setHeader(String, String) setting a message header}.
30+
*/
31+
public class MailSendObservationContext extends SenderContext<Message> {
32+
33+
private static final WarnThenDebugLogger logger = new WarnThenDebugLogger(MailSendObservationContext.class);
34+
35+
private final String protocol;
36+
37+
@Nullable
38+
private final String host;
39+
40+
private final int port;
41+
42+
public MailSendObservationContext(Message msg, String protocol, @Nullable String host, int port) {
43+
super((message, key, value) -> {
44+
if (message != null) {
45+
try {
46+
message.setHeader(key, value);
47+
}
48+
catch (MessagingException exc) {
49+
logger.log("Failed to set message header.", exc);
50+
}
51+
}
52+
});
53+
setCarrier(msg);
54+
this.protocol = protocol;
55+
this.host = host;
56+
this.port = port;
57+
}
58+
59+
public String getProtocol() {
60+
return protocol;
61+
}
62+
63+
@Nullable
64+
public String getHost() {
65+
return host;
66+
}
67+
68+
public int getPort() {
69+
return port;
70+
}
71+
72+
}

0 commit comments

Comments
 (0)