@@ -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
0 commit comments