Skip to content
This repository was archived by the owner on Dec 4, 2023. It is now read-only.

Commit 6f41bfa

Browse files
Update telemetry to log attachments (#1114)
* Update to log attachments * Add unit test for logging attachments
1 parent e43a7dd commit 6f41bfa

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

libraries/bot-builder/src/main/java/com/microsoft/bot/builder/TelemetryConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ private TelemetryConstants() {
1111

1212
}
1313

14+
public static final String ATTACHMENTSPROPERTY = "attachments";
1415
public static final String CHANNELIDPROPERTY = "channelId";
1516
public static final String CONVERSATIONIDPROPERTY = "conversationId";
1617
public static final String CONVERSATIONNAMEPROPERTY = "conversationName";

libraries/bot-builder/src/main/java/com/microsoft/bot/builder/TelemetryLoggerMiddleware.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ protected CompletableFuture<Void> onDeleteActivity(Activity activity) {
188188
* {@link BotTelemetryClient#trackEvent} method for the
189189
* BotMessageReceived event.
190190
*/
191+
@SuppressWarnings("PMD.EmptyCatchBlock")
191192
protected CompletableFuture<Map<String, String>> fillReceiveEventProperties(
192193
Activity activity,
193194
Map<String, String> additionalProperties
@@ -217,6 +218,14 @@ protected CompletableFuture<Map<String, String>> fillReceiveEventProperties(
217218
if (!StringUtils.isEmpty(activity.getSpeak())) {
218219
properties.put(TelemetryConstants.SPEAKPROPERTY, activity.getSpeak());
219220
}
221+
222+
if (activity.getAttachments() != null && activity.getAttachments().size() > 0) {
223+
try {
224+
properties.put(TelemetryConstants.ATTACHMENTSPROPERTY,
225+
Serialization.toString(activity.getAttachments()));
226+
} catch (JsonProcessingException e) {
227+
}
228+
}
220229
}
221230

222231
populateAdditionalChannelProperties(activity, properties);

libraries/bot-builder/src/test/java/com/microsoft/bot/builder/TelemetryMiddlewareTests.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.microsoft.bot.connector.Channels;
1010
import com.microsoft.bot.schema.Activity;
1111
import com.microsoft.bot.schema.ActivityTypes;
12+
import com.microsoft.bot.schema.Attachment;
1213
import com.microsoft.bot.schema.ChannelAccount;
1314
import com.microsoft.bot.schema.ResourceResponse;
1415
import com.microsoft.bot.schema.Serialization;
@@ -528,6 +529,45 @@ public void Telemetry_AdditionalProps() {
528529
);
529530
}
530531

532+
@Test
533+
public void Telemetry_LogAttachments() throws JsonProcessingException {
534+
BotTelemetryClient mockTelemetryClient = mock(BotTelemetryClient.class);
535+
TestAdapter adapter = new TestAdapter(Channels.MSTEAMS).use(
536+
new TelemetryLoggerMiddleware(mockTelemetryClient, true)
537+
);
538+
539+
TeamInfo teamInfo = new TeamInfo();
540+
teamInfo.setId("teamId");
541+
teamInfo.setName("teamName");
542+
543+
Activity activity = MessageFactory.text("test");
544+
ChannelAccount from = new ChannelAccount();
545+
from.setId("userId");
546+
from.setName("userName");
547+
from.setAadObjectId("aadId");
548+
activity.setFrom(from);
549+
Attachment attachment = new Attachment();
550+
attachment.setContent("Hello World");
551+
attachment.setContentType("test/attachment");
552+
attachment.setName("testname");
553+
activity.setAttachment(attachment);
554+
555+
new TestFlow(adapter).send(activity).startTest().join();
556+
557+
verify(mockTelemetryClient).trackEvent(
558+
eventNameCaptor.capture(),
559+
propertiesCaptor.capture()
560+
);
561+
List<String> eventNames = eventNameCaptor.getAllValues();
562+
List<Map<String, String>> properties = propertiesCaptor.getAllValues();
563+
564+
Assert.assertEquals(TelemetryLoggerConstants.BOTMSGRECEIVEEVENT, eventNames.get(0));
565+
String loggedAttachment = properties.get(0).get("attachments");
566+
String originalAttachment = Serialization.toString(activity.getAttachments());
567+
Assert.assertTrue(StringUtils.equals(loggedAttachment, originalAttachment));
568+
}
569+
570+
531571
@Test
532572
public void Telemetry_LogTeamsProperties() throws JsonProcessingException {
533573
BotTelemetryClient mockTelemetryClient = mock(BotTelemetryClient.class);

0 commit comments

Comments
 (0)