|
15 | 15 | */
|
16 | 16 | package io.micrometer.jakarta9.instrument.mail;
|
17 | 17 |
|
| 18 | +import io.micrometer.common.docs.KeyName; |
| 19 | +import io.micrometer.common.lang.Nullable; |
18 | 20 | import io.micrometer.jakarta9.instrument.mail.MailObservationDocumentation.LowCardinalityKeyNames;
|
19 | 21 |
|
20 | 22 | import java.util.Arrays;
|
| 23 | +import java.util.Locale; |
21 | 24 | import java.util.stream.Collectors;
|
22 | 25 |
|
23 | 26 | import io.micrometer.common.KeyValue;
|
|
28 | 31 |
|
29 | 32 | class MailKeyValues {
|
30 | 33 |
|
31 |
| - private static final KeyValue SMTP_MESSAGE_FROM_UNKNOWN = KeyValue |
32 |
| - .of(MailObservationDocumentation.HighCardinalityKeyNames.SMTP_MESSAGE_FROM, "unknown"); |
| 34 | + /** |
| 35 | + * The value is when value can't be determined. |
| 36 | + */ |
| 37 | + public static final String UNKNOWN = "unknown"; |
33 | 38 |
|
34 |
| - private static final KeyValue SMTP_MESSAGE_TO_UNKNOWN = KeyValue |
35 |
| - .of(MailObservationDocumentation.HighCardinalityKeyNames.SMTP_MESSAGE_TO, "unknown"); |
| 39 | + /** |
| 40 | + * The value is when the value is not set or empty. |
| 41 | + */ |
| 42 | + public static final String EMPTY = ""; |
36 | 43 |
|
37 |
| - private static final KeyValue SMTP_MESSAGE_SUBJECT_UNKNOWN = KeyValue |
38 |
| - .of(MailObservationDocumentation.HighCardinalityKeyNames.SMTP_MESSAGE_SUBJECT, "unknown"); |
| 44 | + private MailKeyValues() { |
| 45 | + } |
39 | 46 |
|
40 |
| - private static final KeyValue SMTP_MESSAGE_ID_UNKNOWN = KeyValue |
41 |
| - .of(MailObservationDocumentation.HighCardinalityKeyNames.SMTP_MESSAGE_ID, "unknown"); |
| 47 | + static KeyValue smtpMessageFrom(Message message) { |
| 48 | + return safeExtractValue(MailObservationDocumentation.HighCardinalityKeyNames.SMTP_MESSAGE_FROM, |
| 49 | + () -> addressesToValue(message.getFrom())); |
| 50 | + } |
42 | 51 |
|
43 |
| - private static final KeyValue SERVER_ADDRESS_UNKNOWN = KeyValue.of(LowCardinalityKeyNames.SERVER_ADDRESS, |
44 |
| - "unknown"); |
| 52 | + static KeyValue smtpMessageRecipients(Message message, RecipientType recipientType) { |
| 53 | + MailObservationDocumentation.HighCardinalityKeyNames key = MailObservationDocumentation.HighCardinalityKeyNames |
| 54 | + .valueOf("SMTP_MESSAGE_" + recipientType.toString().toUpperCase(Locale.ROOT)); |
| 55 | + return safeExtractValue(key, () -> addressesToValue(message.getRecipients(recipientType))); |
| 56 | + } |
45 | 57 |
|
46 |
| - private static final KeyValue SERVER_PORT_UNKNOWN = KeyValue.of(LowCardinalityKeyNames.SERVER_PORT, "unknown"); |
| 58 | + static KeyValue smtpMessageSubject(Message message) { |
47 | 59 |
|
48 |
| - private MailKeyValues() { |
| 60 | + return safeExtractValue(MailObservationDocumentation.HighCardinalityKeyNames.SMTP_MESSAGE_SUBJECT, |
| 61 | + message::getSubject); |
49 | 62 | }
|
50 | 63 |
|
51 |
| - static KeyValue smtpMessageFrom(Message message) { |
52 |
| - try { |
53 |
| - if (message.getFrom() == null || message.getFrom().length == 0) { |
54 |
| - return SMTP_MESSAGE_FROM_UNKNOWN; |
| 64 | + static KeyValue smtpMessageId(Message message) { |
| 65 | + return safeExtractValue(MailObservationDocumentation.HighCardinalityKeyNames.SMTP_MESSAGE_ID, () -> { |
| 66 | + String[] header = message.getHeader("Message-ID"); |
| 67 | + if (header == null || header.length == 0) { |
| 68 | + return EMPTY; |
55 | 69 | }
|
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 |
| - } |
| 70 | + return String.join(", ", header); |
| 71 | + }); |
64 | 72 | }
|
65 | 73 |
|
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; |
| 74 | + static KeyValue serverAddress(MailSendObservationContext context) { |
| 75 | + String host = context.getHost(); |
| 76 | + if (host == null || host.isEmpty()) { |
| 77 | + host = UNKNOWN; |
79 | 78 | }
|
| 79 | + return LowCardinalityKeyNames.SERVER_ADDRESS.withValue(host); |
80 | 80 | }
|
81 | 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()); |
| 82 | + static KeyValue serverPort(MailSendObservationContext context) { |
| 83 | + String port = UNKNOWN; |
| 84 | + if (context.getPort() > 0) { |
| 85 | + port = String.valueOf(context.getPort()); |
89 | 86 | }
|
90 |
| - catch (MessagingException exc) { |
91 |
| - return SMTP_MESSAGE_SUBJECT_UNKNOWN; |
| 87 | + return LowCardinalityKeyNames.SERVER_PORT.withValue(port); |
| 88 | + } |
| 89 | + |
| 90 | + static KeyValue networkProtocolName(MailSendObservationContext context) { |
| 91 | + String protocol = context.getProtocol(); |
| 92 | + if (protocol == null || protocol.isEmpty()) { |
| 93 | + protocol = UNKNOWN; |
92 | 94 | }
|
| 95 | + return KeyValue.of(LowCardinalityKeyNames.NETWORK_PROTOCOL_NAME, protocol); |
93 | 96 | }
|
94 | 97 |
|
95 |
| - static KeyValue smtpMessageId(Message message) { |
| 98 | + private static KeyValue safeExtractValue(KeyName key, ValueExtractor extractor) { |
| 99 | + String value; |
96 | 100 | try {
|
97 |
| - if (message.getHeader("Message-ID") == null) { |
98 |
| - return SMTP_MESSAGE_ID_UNKNOWN; |
| 101 | + value = extractor.extract(); |
| 102 | + if (value == null || value.isEmpty()) { |
| 103 | + value = EMPTY; |
99 | 104 | }
|
100 |
| - return KeyValue.of(MailObservationDocumentation.HighCardinalityKeyNames.SMTP_MESSAGE_ID, |
101 |
| - message.getHeader("Message-ID")[0]); |
102 | 105 | }
|
103 | 106 | catch (MessagingException exc) {
|
104 |
| - return SMTP_MESSAGE_ID_UNKNOWN; |
| 107 | + value = UNKNOWN; |
105 | 108 | }
|
| 109 | + return key.withValue(value); |
106 | 110 | }
|
107 | 111 |
|
108 |
| - static KeyValue serverAddress(MailSendObservationContext context) { |
109 |
| - if (context.getHost() == null) { |
110 |
| - return SERVER_ADDRESS_UNKNOWN; |
| 112 | + private static String addressesToValue(@Nullable Address[] addresses) { |
| 113 | + String value; |
| 114 | + if (addresses == null || addresses.length == 0) { |
| 115 | + value = EMPTY; |
111 | 116 | }
|
112 |
| - return KeyValue.of(LowCardinalityKeyNames.SERVER_ADDRESS, context.getHost()); |
113 |
| - } |
114 |
| - |
115 |
| - static KeyValue serverPort(MailSendObservationContext context) { |
116 |
| - if (context.getPort() <= 0) { |
117 |
| - return SERVER_PORT_UNKNOWN; |
| 117 | + else { |
| 118 | + value = Arrays.stream(addresses).map(Address::toString).collect(Collectors.joining(", ")); |
118 | 119 | }
|
119 |
| - return KeyValue.of(LowCardinalityKeyNames.SERVER_PORT, String.valueOf(context.getPort())); |
| 120 | + return value; |
120 | 121 | }
|
121 | 122 |
|
122 |
| - static KeyValue networkProtocolName(MailSendObservationContext context) { |
123 |
| - return KeyValue.of(LowCardinalityKeyNames.NETWORK_PROTOCOL_NAME, context.getProtocol()); |
| 123 | + private interface ValueExtractor { |
| 124 | + |
| 125 | + @Nullable |
| 126 | + String extract() throws MessagingException; |
| 127 | + |
124 | 128 | }
|
125 | 129 |
|
126 | 130 | }
|
0 commit comments