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

Commit

Permalink
[Samples] Add 21.corebot-app-insights sample (#1138)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Batta32 authored Apr 7, 2021
1 parent 3041eca commit 85e9731
Show file tree
Hide file tree
Showing 30 changed files with 3,252 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public class LuisRecognizerOptionsV3 extends LuisRecognizerOptions {
/**
* Value indicating whether or not instance data should be included in response.
*/
private boolean includeInstanceData = false;
private boolean includeInstanceData = true;

/**
* Value indicating whether queries should be logged in LUIS. If queries should
Expand Down
2 changes: 1 addition & 1 deletion libraries/bot-applicationinsights/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>applicationinsights-core</artifactId>
<version>2.4.1</version>
<version>2.6.3</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package com.microsoft.bot.applicationinsights;

import com.microsoft.applicationinsights.TelemetryClient;
import com.microsoft.applicationinsights.TelemetryConfiguration;
import com.microsoft.applicationinsights.telemetry.EventTelemetry;
import com.microsoft.applicationinsights.telemetry.ExceptionTelemetry;
import com.microsoft.applicationinsights.telemetry.PageViewTelemetry;
Expand All @@ -12,6 +13,7 @@
import com.microsoft.applicationinsights.telemetry.TraceTelemetry;
import com.microsoft.bot.builder.BotTelemetryClient;
import com.microsoft.bot.builder.Severity;
import org.apache.commons.lang3.StringUtils;

import java.time.Duration;
import java.time.OffsetDateTime;
Expand All @@ -26,17 +28,30 @@
public class ApplicationInsightsBotTelemetryClient implements BotTelemetryClient {

private final TelemetryClient telemetryClient;
private final TelemetryConfiguration telemetryConfiguration;

/**
* Provides access to the Application Insights configuration that is running here.
* Allows developers to adjust the options.
* @return Application insights configuration.
*/
public TelemetryConfiguration getTelemetryConfiguration() {
return telemetryConfiguration;
}

/**
* Initializes a new instance of the {@link BotTelemetryClient}.
*
* @param withTelemetryClient The telemetry client to forward bot events to.
* @param instrumentationKey The instrumentation key provided to create
* the {@link ApplicationInsightsBotTelemetryClient}.
*/
public ApplicationInsightsBotTelemetryClient(TelemetryClient withTelemetryClient) {
if (withTelemetryClient == null) {
throw new IllegalArgumentException("withTelemetry should be provided");
public ApplicationInsightsBotTelemetryClient(String instrumentationKey) {
if (StringUtils.isBlank(instrumentationKey)) {
throw new IllegalArgumentException("instrumentationKey should be provided");
}
this.telemetryClient = withTelemetryClient;
this.telemetryConfiguration = TelemetryConfiguration.getActive();
telemetryConfiguration.setInstrumentationKey(instrumentationKey);
this.telemetryClient = new TelemetryClient(telemetryConfiguration);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

package com.microsoft.bot.applicationinsights;

import com.microsoft.applicationinsights.TelemetryClient;
import com.microsoft.applicationinsights.TelemetryConfiguration;
import com.microsoft.applicationinsights.channel.TelemetryChannel;
import com.microsoft.applicationinsights.telemetry.EventTelemetry;
import com.microsoft.applicationinsights.telemetry.RemoteDependencyTelemetry;
Expand All @@ -26,19 +24,14 @@

public class BotTelemetryClientTests {

private BotTelemetryClient botTelemetryClient;
private ApplicationInsightsBotTelemetryClient botTelemetryClient;
private TelemetryChannel mockTelemetryChannel;

@Before
public void initialize() {
botTelemetryClient = new ApplicationInsightsBotTelemetryClient("fakeKey");
mockTelemetryChannel = Mockito.mock(TelemetryChannel.class);

TelemetryConfiguration telemetryConfiguration = new TelemetryConfiguration();
telemetryConfiguration.setInstrumentationKey("UNITTEST-INSTRUMENTATION-KEY");
telemetryConfiguration.setChannel(mockTelemetryChannel);
TelemetryClient telemetryClient = new TelemetryClient(telemetryConfiguration);

botTelemetryClient = new ApplicationInsightsBotTelemetryClient(telemetryClient);
botTelemetryClient.getTelemetryConfiguration().setChannel(mockTelemetryChannel);
}

@Test
Expand All @@ -49,16 +42,20 @@ public void nullTelemetryClientThrows() {
}

@Test
public void nonNullTelemetryClientSucceeds() {
TelemetryClient telemetryClient = new TelemetryClient();
public void emptyTelemetryClientThrows() {
Assert.assertThrows(IllegalArgumentException.class, () -> {
new ApplicationInsightsBotTelemetryClient("");
});
}

BotTelemetryClient botTelemetryClient = new ApplicationInsightsBotTelemetryClient(telemetryClient);
@Test
public void nonNullTelemetryClientSucceeds() {
BotTelemetryClient botTelemetryClient = new ApplicationInsightsBotTelemetryClient("fakeKey");
}

@Test
public void overrideTest() {
TelemetryClient telemetryClient = new TelemetryClient();
MyBotTelemetryClient botTelemetryClient = new MyBotTelemetryClient(telemetryClient);
MyBotTelemetryClient botTelemetryClient = new MyBotTelemetryClient("fakeKey");
}

@Test
Expand Down Expand Up @@ -180,4 +177,13 @@ public void trackPageViewTest() {
Assert.assertEquals(0, Double.compare(0.6, pageViewTelemetry.getMetrics().get("metric")));
}).send(Mockito.any(PageViewTelemetry.class));
}

@Test
public void flushTest() {
botTelemetryClient.flush();

Mockito.verify(mockTelemetryChannel, invocations -> {
Assert.assertEquals(1, invocations.getAllInvocations().size());
}).send(Mockito.any());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@

package com.microsoft.bot.applicationinsights;

import com.microsoft.applicationinsights.TelemetryClient;
import com.microsoft.bot.builder.Severity;

import java.time.Duration;
import java.time.OffsetDateTime;
import java.util.Map;

public class MyBotTelemetryClient extends ApplicationInsightsBotTelemetryClient {
public MyBotTelemetryClient(TelemetryClient telemetryClient) {
super(telemetryClient);
public MyBotTelemetryClient(String instrumentationKey) {
super(instrumentationKey);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,17 @@ protected CompletableFuture<Map<String, String>> fillReceiveEventProperties(
) {

Map<String, String> properties = new HashMap<String, String>();
properties.put(TelemetryConstants.FROMIDPROPERTY, activity.getFrom().getId());
properties.put(
TelemetryConstants.CONVERSATIONNAMEPROPERTY,
activity.getConversation().getName()
);
properties.put(TelemetryConstants.LOCALEPROPERTY, activity.getLocale());
properties.put(TelemetryConstants.RECIPIENTIDPROPERTY, activity.getRecipient().getId());
properties.put(TelemetryConstants.RECIPIENTNAMEPROPERTY, activity.getRecipient().getName());
String fromId = activity.getFrom().getId() != null ? activity.getFrom().getId() : "";
properties.put(TelemetryConstants.FROMIDPROPERTY, fromId);
String conversationName =
activity.getConversation().getName() != null ? activity.getConversation().getName() : "";
properties.put(TelemetryConstants.CONVERSATIONNAMEPROPERTY, conversationName);
String activityLocale = activity.getLocale() != null ? activity.getLocale() : "";
properties.put(TelemetryConstants.LOCALEPROPERTY, activityLocale);
String recipientId = activity.getRecipient().getId() != null ? activity.getRecipient().getId() : "";
properties.put(TelemetryConstants.RECIPIENTIDPROPERTY, recipientId);
String recipientName = activity.getRecipient().getName() != null ? activity.getRecipient().getName() : "";
properties.put(TelemetryConstants.RECIPIENTNAMEPROPERTY, recipientName);

// Use the LogPersonalInformation flag to toggle logging PII data, text and user
// name are common examples
Expand Down
21 changes: 21 additions & 0 deletions samples/21.corebot-app-insights/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) Microsoft Corporation. All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE
Loading

0 comments on commit 85e9731

Please sign in to comment.