|
| 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