Skip to content

Commit

Permalink
Add activityTypeProperty to the TelemetryLoggerMiddleware (#4160)
Browse files Browse the repository at this point in the history
Co-authored-by: Eric Dahlvang <erdahlva@microsoft.com>
  • Loading branch information
sw-joelmut and EricDahlvang authored Mar 16, 2022
1 parent cde0370 commit 7db3aeb
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 5 deletions.
60 changes: 60 additions & 0 deletions libraries/botbuilder-core/src/telemetryConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,78 @@
* Defines names of common properties for use with a [BotTelemetryClient](xref:botbuilder-core.BotTelemetryClient) object.
*/
export class TelemetryConstants {
/**
* The telemetry property value for channel id.
*/
public static readonly channelIdProperty: string = 'channelId';

/**
* The telemetry property value for conversation id.
*/
public static readonly conversationIdProperty: string = 'conversationId';

/**
* The telemetry property value for conversation name.
*/
public static readonly conversationNameProperty: string = 'conversationName';

/**
* The telemetry property value for dialog id.
*/
public static readonly dialogIdProperty: string = 'dialogId';

/**
* The telemetry property value for from id.
*/
public static readonly fromIdProperty: string = 'fromId';

/**
* The telemetry property value for from name.
*/
public static readonly fromNameProperty: string = 'fromName';

/**
* The telemetry property value for locale.
*/
public static readonly localeProperty: string = 'locale';

/**
* The telemetry property value for recipient id.
*/
public static readonly recipientIdProperty: string = 'recipientId';

/**
* The telemetry property value for recipient name.
*/
public static readonly recipientNameProperty: string = 'recipientName';

/**
* The telemetry property value for reply activity id.
*/
public static readonly replyActivityIdProperty: string = 'replyActivityId';

/**
* The telemetry property value for text.
*/
public static readonly textProperty: string = 'text';

/**
* The telemetry property value for speak.
*/
public static readonly speakProperty: string = 'speak';

/**
* The telemetry property value for user id.
*/
public static readonly userIdProperty: string = 'userId';

/**
* The telemetry property value for attachments.
*/
public static readonly attachmentsProperty: string = 'attachments';

/**
* The telemetry property value for activity type.
*/
public static readonly activityTypeProperty: string = 'type';
}
4 changes: 4 additions & 0 deletions libraries/botbuilder-core/src/telemetryLoggerMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ export class TelemetryLoggerMiddleware implements Middleware {
properties[TelemetryConstants.localeProperty] = activity.locale ?? '';
properties[TelemetryConstants.recipientIdProperty] = activity.recipient?.id ?? '';
properties[TelemetryConstants.recipientNameProperty] = activity.recipient?.name ?? '';
properties[TelemetryConstants.activityTypeProperty] = activity.type ?? '';

// Use the LogPersonalInformation flag to toggle logging PII data, text and user name are common examples
if (this.logPersonalInformation) {
Expand Down Expand Up @@ -269,6 +270,7 @@ export class TelemetryLoggerMiddleware implements Middleware {
properties[TelemetryConstants.conversationIdProperty] = activity.conversation?.id ?? '';
properties[TelemetryConstants.conversationNameProperty] = activity.conversation?.name ?? '';
properties[TelemetryConstants.localeProperty] = activity.locale ?? '';
properties[TelemetryConstants.activityTypeProperty] = activity.type ?? '';

// Use the LogPersonalInformation flag to toggle logging PII data, text and user name are common examples
if (this.logPersonalInformation) {
Expand Down Expand Up @@ -321,6 +323,7 @@ export class TelemetryLoggerMiddleware implements Middleware {
properties[TelemetryConstants.conversationIdProperty] = activity.conversation?.id ?? '';
properties[TelemetryConstants.conversationNameProperty] = activity.conversation?.name ?? '';
properties[TelemetryConstants.localeProperty] = activity.locale ?? '';
properties[TelemetryConstants.activityTypeProperty] = activity.type ?? '';

// Use the LogPersonalInformation flag to toggle logging PII data, text is a common example
if (this.logPersonalInformation) {
Expand Down Expand Up @@ -357,6 +360,7 @@ export class TelemetryLoggerMiddleware implements Middleware {
properties[TelemetryConstants.recipientIdProperty] = activity.recipient?.id ?? '';
properties[TelemetryConstants.conversationIdProperty] = activity.conversation?.id ?? '';
properties[TelemetryConstants.conversationNameProperty] = activity.conversation?.name ?? '';
properties[TelemetryConstants.activityTypeProperty] = activity.type ?? '';

// Additional Properties can override "stock" properties.
if (telemetryProperties) {
Expand Down
14 changes: 9 additions & 5 deletions libraries/botbuilder-core/tests/telemetryMiddleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ describe(`TelemetryMiddleware`, () => {
expectExactEvent(name, {
conversationName: sinon.match.string,
recipientId: sinon.match.string,
type: sinon.match.string,
...properties,
});
};
Expand Down Expand Up @@ -96,19 +97,21 @@ describe(`TelemetryMiddleware`, () => {
fromName: sinon.match.string,
recipientName: sinon.match.string,
text: 'foo',
type: ActivityTypes.Message
});

expectSendEvent({ conversationId: sinon.match.string });
expectSendEvent({ conversationId: sinon.match.string, type: ActivityTypes.Typing });
expectSendEvent({ conversationId: sinon.match.string, text: 'echo:foo' });

expectReceiveEvent({
conversationId: sinon.match.string,
fromName: sinon.match.string,
recipientName: sinon.match.string,
text: 'bar',
type: ActivityTypes.Message
});

expectSendEvent({ conversationId: sinon.match.string });
expectSendEvent({ conversationId: sinon.match.string, type: ActivityTypes.Typing });
expectSendEvent({ conversationId: sinon.match.string, text: 'echo:bar' });

const adapter = makeAdapter({
Expand Down Expand Up @@ -167,13 +170,14 @@ describe(`TelemetryMiddleware`, () => {
it(`telemetry should log update activities`, async () => {
const { client, expectReceiveEvent, expectSendEvent, expectTrackEvent } = makeTelemetryClient();

expectReceiveEvent({ text: 'foo' });
expectReceiveEvent({ text: 'foo', type: ActivityTypes.Message });
expectSendEvent();

expectReceiveEvent({ text: 'update' });
expectTrackEvent(TelemetryLoggerMiddleware.botMsgUpdateEvent, {
conversationId: sinon.match.string,
text: 'new response',
type: ActivityTypes.Message
});

const adapter = makeAdapter({
Expand All @@ -196,11 +200,11 @@ describe(`TelemetryMiddleware`, () => {
it(`telemetry should log delete activities`, async () => {
const { client, expectReceiveEvent, expectSendEvent, expectTrackEvent } = makeTelemetryClient();

expectReceiveEvent({ text: 'foo' });
expectReceiveEvent({ text: 'foo', type: ActivityTypes.Message });
expectSendEvent({ text: 'response' });

expectReceiveEvent({ text: 'delete' });
expectTrackEvent(TelemetryLoggerMiddleware.botMsgDeleteEvent);
expectTrackEvent(TelemetryLoggerMiddleware.botMsgDeleteEvent, { type: ActivityTypes.MessageDelete });

let response;
const adapter = makeAdapter({
Expand Down

0 comments on commit 7db3aeb

Please sign in to comment.