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

Commit 85e9731

Browse files
authored
[Samples] Add 21.corebot-app-insights sample (#1138)
* Add pom file * Add documentation * Add deploymentTemplates folder * Add cognitiveModels folder * Add webapp folder * Add card and application.properties * Add empty ApplicationTest * Add dialogs * Add AdapterWithErrorHandler * Add bots * Add models * Add startup file * Add package-info file * Bump applicationinsights-core dependency to 2.6.3 * Set includeInstanceData to true * Validate activities properties before adding into a Map * Refactor of the initialization of an ApplicationInsightsBotTelemetryClient to consume instrumentationKey * Populate ApplicationTest class * Fix formats
1 parent 3041eca commit 85e9731

30 files changed

Lines changed: 3252 additions & 33 deletions

File tree

libraries/bot-ai-luis-v3/src/main/java/com/microsoft/bot/ai/luis/LuisRecognizerOptionsV3.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public class LuisRecognizerOptionsV3 extends LuisRecognizerOptions {
7777
/**
7878
* Value indicating whether or not instance data should be included in response.
7979
*/
80-
private boolean includeInstanceData = false;
80+
private boolean includeInstanceData = true;
8181

8282
/**
8383
* Value indicating whether queries should be logged in LUIS. If queries should

libraries/bot-applicationinsights/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
<dependency>
5858
<groupId>com.microsoft.azure</groupId>
5959
<artifactId>applicationinsights-core</artifactId>
60-
<version>2.4.1</version>
60+
<version>2.6.3</version>
6161
</dependency>
6262

6363
<dependency>

libraries/bot-applicationinsights/src/main/java/com/microsoft/bot/applicationinsights/ApplicationInsightsBotTelemetryClient.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package com.microsoft.bot.applicationinsights;
55

66
import com.microsoft.applicationinsights.TelemetryClient;
7+
import com.microsoft.applicationinsights.TelemetryConfiguration;
78
import com.microsoft.applicationinsights.telemetry.EventTelemetry;
89
import com.microsoft.applicationinsights.telemetry.ExceptionTelemetry;
910
import com.microsoft.applicationinsights.telemetry.PageViewTelemetry;
@@ -12,6 +13,7 @@
1213
import com.microsoft.applicationinsights.telemetry.TraceTelemetry;
1314
import com.microsoft.bot.builder.BotTelemetryClient;
1415
import com.microsoft.bot.builder.Severity;
16+
import org.apache.commons.lang3.StringUtils;
1517

1618
import java.time.Duration;
1719
import java.time.OffsetDateTime;
@@ -26,17 +28,30 @@
2628
public class ApplicationInsightsBotTelemetryClient implements BotTelemetryClient {
2729

2830
private final TelemetryClient telemetryClient;
31+
private final TelemetryConfiguration telemetryConfiguration;
32+
33+
/**
34+
* Provides access to the Application Insights configuration that is running here.
35+
* Allows developers to adjust the options.
36+
* @return Application insights configuration.
37+
*/
38+
public TelemetryConfiguration getTelemetryConfiguration() {
39+
return telemetryConfiguration;
40+
}
2941

3042
/**
3143
* Initializes a new instance of the {@link BotTelemetryClient}.
3244
*
33-
* @param withTelemetryClient The telemetry client to forward bot events to.
45+
* @param instrumentationKey The instrumentation key provided to create
46+
* the {@link ApplicationInsightsBotTelemetryClient}.
3447
*/
35-
public ApplicationInsightsBotTelemetryClient(TelemetryClient withTelemetryClient) {
36-
if (withTelemetryClient == null) {
37-
throw new IllegalArgumentException("withTelemetry should be provided");
48+
public ApplicationInsightsBotTelemetryClient(String instrumentationKey) {
49+
if (StringUtils.isBlank(instrumentationKey)) {
50+
throw new IllegalArgumentException("instrumentationKey should be provided");
3851
}
39-
this.telemetryClient = withTelemetryClient;
52+
this.telemetryConfiguration = TelemetryConfiguration.getActive();
53+
telemetryConfiguration.setInstrumentationKey(instrumentationKey);
54+
this.telemetryClient = new TelemetryClient(telemetryConfiguration);
4055
}
4156

4257
/**

libraries/bot-applicationinsights/src/test/java/com/microsoft/bot/applicationinsights/BotTelemetryClientTests.java

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
package com.microsoft.bot.applicationinsights;
55

6-
import com.microsoft.applicationinsights.TelemetryClient;
7-
import com.microsoft.applicationinsights.TelemetryConfiguration;
86
import com.microsoft.applicationinsights.channel.TelemetryChannel;
97
import com.microsoft.applicationinsights.telemetry.EventTelemetry;
108
import com.microsoft.applicationinsights.telemetry.RemoteDependencyTelemetry;
@@ -26,19 +24,14 @@
2624

2725
public class BotTelemetryClientTests {
2826

29-
private BotTelemetryClient botTelemetryClient;
27+
private ApplicationInsightsBotTelemetryClient botTelemetryClient;
3028
private TelemetryChannel mockTelemetryChannel;
3129

3230
@Before
3331
public void initialize() {
32+
botTelemetryClient = new ApplicationInsightsBotTelemetryClient("fakeKey");
3433
mockTelemetryChannel = Mockito.mock(TelemetryChannel.class);
35-
36-
TelemetryConfiguration telemetryConfiguration = new TelemetryConfiguration();
37-
telemetryConfiguration.setInstrumentationKey("UNITTEST-INSTRUMENTATION-KEY");
38-
telemetryConfiguration.setChannel(mockTelemetryChannel);
39-
TelemetryClient telemetryClient = new TelemetryClient(telemetryConfiguration);
40-
41-
botTelemetryClient = new ApplicationInsightsBotTelemetryClient(telemetryClient);
34+
botTelemetryClient.getTelemetryConfiguration().setChannel(mockTelemetryChannel);
4235
}
4336

4437
@Test
@@ -49,16 +42,20 @@ public void nullTelemetryClientThrows() {
4942
}
5043

5144
@Test
52-
public void nonNullTelemetryClientSucceeds() {
53-
TelemetryClient telemetryClient = new TelemetryClient();
45+
public void emptyTelemetryClientThrows() {
46+
Assert.assertThrows(IllegalArgumentException.class, () -> {
47+
new ApplicationInsightsBotTelemetryClient("");
48+
});
49+
}
5450

55-
BotTelemetryClient botTelemetryClient = new ApplicationInsightsBotTelemetryClient(telemetryClient);
51+
@Test
52+
public void nonNullTelemetryClientSucceeds() {
53+
BotTelemetryClient botTelemetryClient = new ApplicationInsightsBotTelemetryClient("fakeKey");
5654
}
5755

5856
@Test
5957
public void overrideTest() {
60-
TelemetryClient telemetryClient = new TelemetryClient();
61-
MyBotTelemetryClient botTelemetryClient = new MyBotTelemetryClient(telemetryClient);
58+
MyBotTelemetryClient botTelemetryClient = new MyBotTelemetryClient("fakeKey");
6259
}
6360

6461
@Test
@@ -180,4 +177,13 @@ public void trackPageViewTest() {
180177
Assert.assertEquals(0, Double.compare(0.6, pageViewTelemetry.getMetrics().get("metric")));
181178
}).send(Mockito.any(PageViewTelemetry.class));
182179
}
180+
181+
@Test
182+
public void flushTest() {
183+
botTelemetryClient.flush();
184+
185+
Mockito.verify(mockTelemetryChannel, invocations -> {
186+
Assert.assertEquals(1, invocations.getAllInvocations().size());
187+
}).send(Mockito.any());
188+
}
183189
}

libraries/bot-applicationinsights/src/test/java/com/microsoft/bot/applicationinsights/MyBotTelemetryClient.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@
33

44
package com.microsoft.bot.applicationinsights;
55

6-
import com.microsoft.applicationinsights.TelemetryClient;
76
import com.microsoft.bot.builder.Severity;
87

98
import java.time.Duration;
109
import java.time.OffsetDateTime;
1110
import java.util.Map;
1211

1312
public class MyBotTelemetryClient extends ApplicationInsightsBotTelemetryClient {
14-
public MyBotTelemetryClient(TelemetryClient telemetryClient) {
15-
super(telemetryClient);
13+
public MyBotTelemetryClient(String instrumentationKey) {
14+
super(instrumentationKey);
1615
}
1716

1817
@Override

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -195,14 +195,17 @@ protected CompletableFuture<Map<String, String>> fillReceiveEventProperties(
195195
) {
196196

197197
Map<String, String> properties = new HashMap<String, String>();
198-
properties.put(TelemetryConstants.FROMIDPROPERTY, activity.getFrom().getId());
199-
properties.put(
200-
TelemetryConstants.CONVERSATIONNAMEPROPERTY,
201-
activity.getConversation().getName()
202-
);
203-
properties.put(TelemetryConstants.LOCALEPROPERTY, activity.getLocale());
204-
properties.put(TelemetryConstants.RECIPIENTIDPROPERTY, activity.getRecipient().getId());
205-
properties.put(TelemetryConstants.RECIPIENTNAMEPROPERTY, activity.getRecipient().getName());
198+
String fromId = activity.getFrom().getId() != null ? activity.getFrom().getId() : "";
199+
properties.put(TelemetryConstants.FROMIDPROPERTY, fromId);
200+
String conversationName =
201+
activity.getConversation().getName() != null ? activity.getConversation().getName() : "";
202+
properties.put(TelemetryConstants.CONVERSATIONNAMEPROPERTY, conversationName);
203+
String activityLocale = activity.getLocale() != null ? activity.getLocale() : "";
204+
properties.put(TelemetryConstants.LOCALEPROPERTY, activityLocale);
205+
String recipientId = activity.getRecipient().getId() != null ? activity.getRecipient().getId() : "";
206+
properties.put(TelemetryConstants.RECIPIENTIDPROPERTY, recipientId);
207+
String recipientName = activity.getRecipient().getName() != null ? activity.getRecipient().getName() : "";
208+
properties.put(TelemetryConstants.RECIPIENTNAMEPROPERTY, recipientName);
206209

207210
// Use the LogPersonalInformation flag to toggle logging PII data, text and user
208211
// name are common examples
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) Microsoft Corporation. All rights reserved.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE

0 commit comments

Comments
 (0)