Skip to content
This repository was archived by the owner on Jun 30, 2022. It is now read-only.

Commit d444d8a

Browse files
authored
QnATelemetry improvements (#467)
* add knowledgebase id into telemetry * add qna and luis application/knowledgebase ids into telemetry * update enterprise template sample with the latest telemetry change
1 parent dbe8d63 commit d444d8a

File tree

12 files changed

+52
-21
lines changed

12 files changed

+52
-21
lines changed

solutions/Virtual-Assistant/src/csharp/microsoft.bot.solutions/Middleware/Telemetry/LuisTelemetryConstants.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Microsoft.Bot.Solutions
88
/// </summary>
99
public static class LuisTelemetryConstants
1010
{
11-
public const string ApplicationId = "applicationId";
11+
public const string ApplicationIdProperty = "applicationId";
1212
public const string IntentPrefix = "luisIntent"; // Application Insights Custom Event name (with Intent)
1313
public const string IntentProperty = "intent";
1414
public const string IntentScoreProperty = "intentScore";

solutions/Virtual-Assistant/src/csharp/microsoft.bot.solutions/Middleware/Telemetry/QnATelemetryConstants.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace Microsoft.Bot.Solutions
88
/// </summary>
99
public static class QnATelemetryConstants
1010
{
11+
public const string KnowledgeBaseIdProperty = "knowledgeBaseId";
1112
public const string ActivityIdProperty = "activityId";
1213
public const string AnswerProperty = "answer";
1314
public const string ArticleFoundProperty = "articleFound";
@@ -18,4 +19,4 @@ public static class QnATelemetryConstants
1819
public const string ScoreProperty = "score";
1920
public const string UsernameProperty = "username";
2021
}
21-
}
22+
}

solutions/Virtual-Assistant/src/csharp/microsoft.bot.solutions/Middleware/Telemetry/TelemetryLuisRecognizer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public TelemetryLuisRecognizer(LuisApplication application, LuisPredictionOption
125125
// Add the intent score and conversation id properties
126126
var telemetryProperties = new Dictionary<string, string>()
127127
{
128-
{ LuisTelemetryConstants.ApplicationId, _luisApplication.ApplicationId },
128+
{ LuisTelemetryConstants.ApplicationIdProperty, _luisApplication.ApplicationId },
129129
{ LuisTelemetryConstants.IntentProperty, topLuisIntent.intent },
130130
{ LuisTelemetryConstants.IntentScoreProperty, intentScore },
131131
};

solutions/Virtual-Assistant/src/csharp/microsoft.bot.solutions/Middleware/Telemetry/TelemetryQnAMaker.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Microsoft.ApplicationInsights;
88
using Microsoft.Bot.Builder;
99
using Microsoft.Bot.Builder.AI.QnA;
10+
using Newtonsoft.Json;
1011

1112
namespace Microsoft.Bot.Solutions
1213
{
@@ -19,7 +20,9 @@ namespace Microsoft.Bot.Solutions
1920
/// </summary>
2021
public class TelemetryQnAMaker : QnAMaker, ITelemetryQnAMaker
2122
{
22-
public static readonly string QnaMsgEvent = "QnaMessage";
23+
public const string QnaMsgEvent = "QnaMessage";
24+
25+
private QnAMakerEndpoint _endpoint;
2326

2427
/// <summary>
2528
/// Initializes a new instance of the <see cref="TelemetryQnAMaker"/> class.
@@ -35,13 +38,15 @@ public TelemetryQnAMaker(QnAMakerEndpoint endpoint, QnAMakerOptions options = nu
3538
{
3639
LogUserName = logUserName;
3740
LogOriginalMessage = logOriginalMessage;
41+
42+
_endpoint = endpoint;
3843
}
3944

4045
public bool LogUserName { get; }
4146

4247
public bool LogOriginalMessage { get; }
4348

44-
public new async Task<QueryResult[]> GetAnswersAsync(ITurnContext context)
49+
public async Task<QueryResult[]> GetAnswersAsync(ITurnContext context)
4550
{
4651
// Call Qna Maker
4752
var queryResults = await base.GetAnswersAsync(context);
@@ -52,6 +57,7 @@ public TelemetryQnAMaker(QnAMakerEndpoint endpoint, QnAMakerOptions options = nu
5257
var telemetryProperties = new Dictionary<string, string>();
5358
var telemetryMetrics = new Dictionary<string, double>();
5459

60+
telemetryProperties.Add(QnATelemetryConstants.KnowledgeBaseIdProperty, _endpoint.KnowledgeBaseId);
5561
// Make it so we can correlate our reports with Activity or Conversation
5662
telemetryProperties.Add(QnATelemetryConstants.ActivityIdProperty, context.Activity.Id);
5763
var conversationId = context.Activity.Conversation.Id;
@@ -78,15 +84,15 @@ public TelemetryQnAMaker(QnAMakerEndpoint endpoint, QnAMakerOptions options = nu
7884
if (queryResults.Length > 0)
7985
{
8086
var queryResult = queryResults[0];
81-
telemetryProperties.Add(QnATelemetryConstants.QuestionProperty, string.Join(",", queryResult.Questions));
87+
telemetryProperties.Add(QnATelemetryConstants.QuestionProperty, JsonConvert.SerializeObject(queryResult.Questions));
8288
telemetryProperties.Add(QnATelemetryConstants.AnswerProperty, queryResult.Answer);
8389
telemetryMetrics.Add(QnATelemetryConstants.ScoreProperty, queryResult.Score);
8490
telemetryProperties.Add(QnATelemetryConstants.ArticleFoundProperty, "true");
8591
}
8692
else
8793
{
8894
telemetryProperties.Add(QnATelemetryConstants.QuestionProperty, "No Qna Question matched");
89-
telemetryProperties.Add(QnATelemetryConstants.AnswerProperty, "No Qna Question matched");
95+
telemetryProperties.Add(QnATelemetryConstants.AnswerProperty, "No Qna Answer matched");
9096
telemetryProperties.Add(QnATelemetryConstants.ArticleFoundProperty, "true");
9197
}
9298

templates/Enterprise-Template/src/csharp/EnterpriseBotSample/Middleware/Telemetry/LuisTelemetryConstants.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace EnterpriseBotSample.Middleware.Telemetry
88
/// </summary>
99
public static class LuisTelemetryConstants
1010
{
11+
public const string ApplicationIdProperty = "applicationId";
1112
public const string IntentPrefix = "LuisIntent"; // Application Insights Custom Event name (with Intent)
1213
public const string IntentProperty = "Intent";
1314
public const string IntentScoreProperty = "IntentScore";
@@ -17,4 +18,4 @@ public static class LuisTelemetryConstants
1718
public const string SentimentLabelProperty = "SentimentLabel";
1819
public const string SentimentScoreProperty = "SentimentScore";
1920
}
20-
}
21+
}

templates/Enterprise-Template/src/csharp/EnterpriseBotSample/Middleware/Telemetry/QnATelemetryConstants.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace EnterpriseBotSample.Middleware.Telemetry
88
/// </summary>
99
public static class QnATelemetryConstants
1010
{
11+
public const string KnowledgeBaseIdProperty = "knowledgeBaseId";
1112
public const string ActivityIdProperty = "ActivityId";
1213
public const string UsernameProperty = "Username";
1314
public const string ConversationIdProperty = "ConversationId";
@@ -17,4 +18,4 @@ public static class QnATelemetryConstants
1718
public const string ScoreProperty = "Score";
1819
public const string ArticleFoundProperty = "QnAItemFound";
1920
}
20-
}
21+
}

templates/Enterprise-Template/src/csharp/EnterpriseBotSample/Middleware/Telemetry/TelemetryLuisRecognizer.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ namespace EnterpriseBotSample.Middleware.Telemetry
2323
/// </summary>
2424
public class TelemetryLuisRecognizer : LuisRecognizer
2525
{
26+
private LuisApplication _luisApplication;
27+
2628
/// <summary>
2729
/// Initializes a new instance of the <see cref="TelemetryLuisRecognizer"/> class.
2830
/// </summary>
@@ -34,6 +36,8 @@ public class TelemetryLuisRecognizer : LuisRecognizer
3436
public TelemetryLuisRecognizer(LuisApplication application, LuisPredictionOptions predictionOptions = null, bool includeApiResults = false, bool logOriginalMessage = false, bool logUserName = false)
3537
: base(application, predictionOptions, includeApiResults)
3638
{
39+
_luisApplication = application;
40+
3741
LogOriginalMessage = logOriginalMessage;
3842
LogUsername = logUserName;
3943
}
@@ -85,6 +89,7 @@ public TelemetryLuisRecognizer(LuisApplication application, LuisPredictionOption
8589
// Add the intent score and conversation id properties
8690
var telemetryProperties = new Dictionary<string, string>()
8791
{
92+
{ LuisTelemetryConstants.ApplicationIdProperty, _luisApplication.ApplicationId },
8893
{ LuisTelemetryConstants.ActivityIdProperty, context.Activity.Id },
8994
{ LuisTelemetryConstants.IntentProperty, topLuisIntent.intent },
9095
{ LuisTelemetryConstants.IntentScoreProperty, intentScore },
@@ -131,4 +136,4 @@ public TelemetryLuisRecognizer(LuisApplication application, LuisPredictionOption
131136
return recognizerResult;
132137
}
133138
}
134-
}
139+
}

templates/Enterprise-Template/src/csharp/EnterpriseBotSample/Middleware/Telemetry/TelemetryQnAMaker.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Microsoft.ApplicationInsights;
88
using Microsoft.Bot.Builder;
99
using Microsoft.Bot.Builder.AI.QnA;
10+
using Newtonsoft.Json;
1011

1112
namespace EnterpriseBotSample.Middleware.Telemetry
1213
{
@@ -19,7 +20,9 @@ namespace EnterpriseBotSample.Middleware.Telemetry
1920
/// </summary>
2021
public class TelemetryQnAMaker : QnAMaker
2122
{
22-
public static readonly string QnaMsgEvent = "QnaMessage";
23+
public const string QnaMsgEvent = "QnaMessage";
24+
25+
private QnAMakerEndpoint _endpoint;
2326

2427
/// <summary>
2528
/// Initializes a new instance of the <see cref="TelemetryQnAMaker"/> class.
@@ -35,6 +38,8 @@ public TelemetryQnAMaker(QnAMakerEndpoint endpoint, QnAMakerOptions options = nu
3538
{
3639
LogUserName = logUserName;
3740
LogOriginalMessage = logOriginalMessage;
41+
42+
_endpoint = endpoint;
3843
}
3944

4045
public bool LogUserName { get; }
@@ -52,6 +57,7 @@ public TelemetryQnAMaker(QnAMakerEndpoint endpoint, QnAMakerOptions options = nu
5257
var telemetryProperties = new Dictionary<string, string>();
5358
var telemetryMetrics = new Dictionary<string, double>();
5459

60+
telemetryProperties.Add(QnATelemetryConstants.KnowledgeBaseIdProperty, _endpoint.KnowledgeBaseId);
5561
// Make it so we can correlate our reports with Activity or Conversation
5662
telemetryProperties.Add(QnATelemetryConstants.ActivityIdProperty, context.Activity.Id);
5763
var conversationId = context.Activity.Conversation.Id;
@@ -78,14 +84,14 @@ public TelemetryQnAMaker(QnAMakerEndpoint endpoint, QnAMakerOptions options = nu
7884
if (queryResults.Length > 0)
7985
{
8086
var queryResult = queryResults[0];
81-
telemetryProperties.Add(QnATelemetryConstants.QuestionProperty, string.Join(",", queryResult.Questions));
87+
telemetryProperties.Add(QnATelemetryConstants.QuestionProperty, JsonConvert.SerializeObject(queryResult.Questions));
8288
telemetryProperties.Add(QnATelemetryConstants.AnswerProperty, queryResult.Answer);
8389
telemetryMetrics.Add(QnATelemetryConstants.ScoreProperty, queryResult.Score);
8490
}
8591
else
8692
{
8793
telemetryProperties.Add(QnATelemetryConstants.QuestionProperty, "No Qna Question matched");
88-
telemetryProperties.Add(QnATelemetryConstants.AnswerProperty, "No Qna Question matched");
94+
telemetryProperties.Add(QnATelemetryConstants.AnswerProperty, "No Qna Answer matched");
8995
}
9096

9197
// Track the event

templates/Enterprise-Template/src/csharp/EnterpriseBotTemplate/Bot Framework/Middleware/Telemetry/LuisTelemetryConstants.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace $safeprojectname$.Middleware.Telemetry
88
/// </summary>
99
public static class LuisTelemetryConstants
1010
{
11+
public const string ApplicationId = "applicationId";
1112
public const string IntentPrefix = "luisIntent"; // Application Insights Custom Event name (with Intent)
1213
public const string IntentProperty = "intent";
1314
public const string IntentScoreProperty = "intentScore";
@@ -18,4 +19,4 @@ public static class LuisTelemetryConstants
1819
public const string SentimentScoreProperty = "sentimentScore";
1920
public const string DialogId = "dialogId";
2021
}
21-
}
22+
}

templates/Enterprise-Template/src/csharp/EnterpriseBotTemplate/Bot Framework/Middleware/Telemetry/QnATelemetryConstants.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace $safeprojectname$.Middleware.Telemetry
88
/// </summary>
99
public static class QnATelemetryConstants
1010
{
11+
public const string KnowledgeBaseIdProperty = "knowledgeBaseId";
1112
public const string ActivityIdProperty = "activityId";
1213
public const string AnswerProperty = "answer";
1314
public const string ArticleFoundProperty = "articleFound";
@@ -18,4 +19,4 @@ public static class QnATelemetryConstants
1819
public const string ScoreProperty = "score";
1920
public const string UsernameProperty = "username";
2021
}
21-
}
22+
}

0 commit comments

Comments
 (0)