Skip to content

Commit 748de21

Browse files
famaridonshakuzen
authored andcommitted
Add Jakarta Mail instrumentation for observability
close gh-5985 Signed-off-by: famaridon <[email protected]>
1 parent aa4f922 commit 748de21

15 files changed

+875
-0
lines changed

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,12 +19,15 @@ dependencies {
1819
api project(":micrometer-observation")
1920

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

2224
testImplementation(libs.archunitJunit5) {
2325
// avoid transitively pulling in slf4j 2
2426
exclude group: "org.slf4j", module: "slf4j-api"
2527
}
2628
testImplementation libs.slf4jApi
2729
testImplementation libs.mockitoCore5
30+
testImplementation project(':micrometer-observation-test')
2831
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,97 @@
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.InternalLogger;
20+
import io.micrometer.common.util.internal.logging.InternalLoggerFactory;
21+
import io.micrometer.observation.Observation;
22+
import io.micrometer.observation.ObservationConvention;
23+
import io.micrometer.observation.ObservationRegistry;
24+
import jakarta.mail.*;
25+
26+
public class InstrumentedTransport extends Transport {
27+
28+
private static final InternalLogger LOGGER = InternalLoggerFactory.getInstance(InstrumentedTransport.class);
29+
30+
private static final ObservationConvention<MailSendObservationContext> DEFAULT_CONVENTION = new DefaultMailSendObservationConvention();
31+
32+
private ObservationRegistry observationRegistry;
33+
34+
private final Transport delegate;
35+
36+
private final String protocol;
37+
38+
@Nullable
39+
private String host;
40+
41+
@Nullable
42+
private final ObservationConvention<MailSendObservationContext> customConvention;
43+
44+
private int port;
45+
46+
public InstrumentedTransport(Session session, Transport delegate, ObservationRegistry observationRegistry)
47+
throws NoSuchProviderException {
48+
this(session, delegate, observationRegistry, null);
49+
}
50+
51+
public InstrumentedTransport(Session session, Transport delegate, ObservationRegistry observationRegistry,
52+
@Nullable ObservationConvention<MailSendObservationContext> customConvention)
53+
throws NoSuchProviderException {
54+
super(session, delegate.getURLName());
55+
this.protocol = this.url.getProtocol();
56+
this.delegate = delegate;
57+
this.observationRegistry = observationRegistry;
58+
this.customConvention = customConvention;
59+
}
60+
61+
@Override
62+
public void connect(String host, int port, String user, String password) throws MessagingException {
63+
this.delegate.connect(host, port, user, password);
64+
this.host = host;
65+
this.port = port;
66+
}
67+
68+
@Override
69+
public void sendMessage(Message msg, Address[] addresses) throws MessagingException {
70+
71+
Observation observation = MailObservationDocumentation.MAIL_SEND.observation(this.customConvention,
72+
DEFAULT_CONVENTION, () -> new MailSendObservationContext(msg, this.protocol, this.host, this.port),
73+
observationRegistry);
74+
75+
observation.start();
76+
try (Observation.Scope scope = observation.openScope()) {
77+
// the Message-Id is set by the SMTP after sending the message
78+
this.delegate.sendMessage(msg, addresses);
79+
observation.highCardinalityKeyValue(MailKeyValues.smtpMessageId(msg));
80+
}
81+
catch (MessagingException error) {
82+
observation.error(error);
83+
throw error;
84+
}
85+
finally {
86+
observation.stop();
87+
}
88+
}
89+
90+
@Override
91+
public synchronized void close() throws MessagingException {
92+
this.delegate.close();
93+
this.host = null;
94+
this.port = 0;
95+
}
96+
97+
}
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+
}

0 commit comments

Comments
 (0)