Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Application Insights integration : ASP.Net Core review #1168

Merged
merged 5 commits into from
Nov 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.DataContracts;


namespace Microsoft.Bot.Builder.ApplicationInsights.Tests
{
[TestClass]
public class BotTelemetryClientTests
{
[TestMethod]
public void Construct()
{
var telemetryClient = new TelemetryClient();
var client = new BotTelemetryClient(telemetryClient);
Assert.IsNotNull(client);
}

[TestMethod]
public void TrackEventTest()
{
var telemetryClient = new TelemetryClient();
var client = new BotTelemetryClient(telemetryClient);
var eventTelemetry = new EventTelemetry();
client.TrackEvent(eventTelemetry);
}

[TestMethod]
public void TrackDependencyTest()
{
var telemetryClient = new TelemetryClient();
var client = new BotTelemetryClient(telemetryClient);
var telemetry = new DependencyTelemetry("my dependency", "my target", "foo", "data");
client.TrackDependency(telemetry);
}

[TestMethod]
public void TrackExceptionTest()
{
var telemetryClient = new TelemetryClient();
var client = new BotTelemetryClient(telemetryClient);
var telemetry = new ExceptionTelemetry();
client.TrackException(telemetry);
}

[TestMethod]
public void TrackTraceTest()
{
var telemetryClient = new TelemetryClient();
var client = new BotTelemetryClient(telemetryClient);
var telemetry = new TraceTelemetry();
client.TrackTrace(telemetry);
}


}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="2.1.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
<PackageReference Include="Moq" Version="4.10.0" />
<PackageReference Include="MSTest.TestAdapter" Version="1.3.2" />
<PackageReference Include="MSTest.TestFramework" Version="1.3.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\libraries\Microsoft.Bot.Builder.ApplicationInsights\Microsoft.Bot.Builder.ApplicationInsights.csproj" />
<ProjectReference Include="..\libraries\Microsoft.Bot.Builder.Dialogs\Microsoft.Bot.Builder.Dialogs.csproj" />
<ProjectReference Include="..\libraries\Microsoft.Bot.Builder\Microsoft.Bot.Builder.csproj" />
<ProjectReference Include="..\libraries\Microsoft.Bot.Schema\Microsoft.Bot.Schema.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Threading.Tasks;
using Microsoft.Bot.Builder.Adapters;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

namespace Microsoft.Bot.Builder.ApplicationInsights.Tests
{
[TestClass]
public class TelemetryWaterfallTests
{

[TestMethod]
public async Task Waterfall()
{
var convoState = new ConversationState(new MemoryStorage());

var adapter = new TestAdapter()
.Use(new AutoSaveStateMiddleware(convoState));

var telemetryClient = new Mock<IBotTelemetryClient>();
var dialogState = convoState.CreateProperty<DialogState>("dialogState");
var dialogs = new DialogSet(dialogState);
dialogs.Add(new TelemetryWaterfallDialog("test", telemetryClient.Object, new WaterfallStep[]
{
async (step, cancellationToken) => { await step.Context.SendActivityAsync("step1"); return Dialog.EndOfTurn; },
async (step, cancellationToken) => { await step.Context.SendActivityAsync("step2"); return Dialog.EndOfTurn; },
async (step, cancellationToken) => { await step.Context.SendActivityAsync("step3"); return Dialog.EndOfTurn; },
}));

await new TestFlow(adapter, async (turnContext, cancellationToken) =>
{
var dc = await dialogs.CreateContextAsync(turnContext, cancellationToken);
await dc.ContinueDialogAsync(cancellationToken);
if (!turnContext.Responded)
{
await dc.BeginDialogAsync("test", null, cancellationToken);
}
})
.Send("hello")
.AssertReply("step1")
.Send("hello")
.AssertReply("step2")
.Send("hello")
.AssertReply("step3")
.StartTestAsync();
telemetryClient.Verify(m => m.TrackWaterfallStep(It.IsAny<WaterfallStepContext>(), It.IsAny<String>()), Times.Exactly(3));
Console.WriteLine("Complete");
}

[TestMethod]
public async Task WaterfallWithCallback()
{
var convoState = new ConversationState(new MemoryStorage());

var adapter = new TestAdapter()
.Use(new AutoSaveStateMiddleware(convoState));

var dialogState = convoState.CreateProperty<DialogState>("dialogState");
var dialogs = new DialogSet(dialogState);
var telemetryClient = new Mock<IBotTelemetryClient>(); ;
var waterfallDialog = new TelemetryWaterfallDialog("test", telemetryClient.Object, new WaterfallStep[]
{
async (step, cancellationToken) => { await step.Context.SendActivityAsync("step1"); return Dialog.EndOfTurn; },
async (step, cancellationToken) => { await step.Context.SendActivityAsync("step2"); return Dialog.EndOfTurn; },
async (step, cancellationToken) => { await step.Context.SendActivityAsync("step3"); return Dialog.EndOfTurn; },
});

dialogs.Add(waterfallDialog);

await new TestFlow(adapter, async (turnContext, cancellationToken) =>
{
var dc = await dialogs.CreateContextAsync(turnContext, cancellationToken);
await dc.ContinueDialogAsync(cancellationToken);
if (!turnContext.Responded)
{
await dc.BeginDialogAsync("test", null, cancellationToken);
}
})
.Send("hello")
.AssertReply("step1")
.Send("hello")
.AssertReply("step2")
.Send("hello")
.AssertReply("step3")
.StartTestAsync();
telemetryClient.Verify(m => m.TrackWaterfallStep(It.IsAny<WaterfallStepContext>(), It.IsAny<String>()), Times.Exactly(3));
}

[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public async Task WaterfallWithNullTelemetryClient()
{
var waterfall = new TelemetryWaterfallDialog("test", null);
}


[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public async Task WaterfallWithStepsNull()
{
var telemetryClient = new Mock<IBotTelemetryClient>();
var waterfall = new TelemetryWaterfallDialog("test", telemetryClient.Object);
waterfall.AddStep(null);
}

}
}
44 changes: 44 additions & 0 deletions Microsoft.Bot.Builder.sln
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.Bot.Configuration
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.Bot.Builder.TestBot.WebApi", "tests\Microsoft.Bot.Builder.TestBot.WebApi\Microsoft.Bot.Builder.TestBot.WebApi.csproj", "{298009D3-971D-4375-A2F2-04CFB168CED9}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Bot.Builder.ApplicationInsights.Core", "libraries\integration\Microsoft.Bot.Builder.ApplicationInsights.Core\Microsoft.Bot.Builder.ApplicationInsights.Core.csproj", "{8FC920C6-E895-4A17-AB2F-452FAAA36CC8}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Bot.Builder.ApplicationInsights", "libraries\Microsoft.Bot.Builder.ApplicationInsights\Microsoft.Bot.Builder.ApplicationInsights.csproj", "{B609DB2C-5C1F-46D1-A0FA-A0FF9216899A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Bot.ApplicationInsights.Core.Tests", "tests\integration\Microsoft.Bot.ApplicationInsights.Core.Tests\Microsoft.Bot.ApplicationInsights.Core.Tests.csproj", "{3F4A0DD8-4D47-4B9C-939A-3146E68C84F7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.Bot.Builder.ApplicationInsights.Tests", "Microsoft.Bot.Builder.ApplicationInsights.Tests\Microsoft.Bot.Builder.ApplicationInsights.Tests.csproj", "{FC304BC8-D719-4E97-8C6C-36BDC342F069}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug - NuGet Packages|Any CPU = Debug - NuGet Packages|Any CPU
Expand Down Expand Up @@ -263,6 +271,38 @@ Global
{298009D3-971D-4375-A2F2-04CFB168CED9}.Documentation|Any CPU.Build.0 = Debug|Any CPU
{298009D3-971D-4375-A2F2-04CFB168CED9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{298009D3-971D-4375-A2F2-04CFB168CED9}.Release|Any CPU.Build.0 = Release|Any CPU
{8FC920C6-E895-4A17-AB2F-452FAAA36CC8}.Debug - NuGet Packages|Any CPU.ActiveCfg = Debug|Any CPU
{8FC920C6-E895-4A17-AB2F-452FAAA36CC8}.Debug - NuGet Packages|Any CPU.Build.0 = Debug|Any CPU
{8FC920C6-E895-4A17-AB2F-452FAAA36CC8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8FC920C6-E895-4A17-AB2F-452FAAA36CC8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8FC920C6-E895-4A17-AB2F-452FAAA36CC8}.Documentation|Any CPU.ActiveCfg = Debug|Any CPU
{8FC920C6-E895-4A17-AB2F-452FAAA36CC8}.Documentation|Any CPU.Build.0 = Debug|Any CPU
{8FC920C6-E895-4A17-AB2F-452FAAA36CC8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8FC920C6-E895-4A17-AB2F-452FAAA36CC8}.Release|Any CPU.Build.0 = Release|Any CPU
{B609DB2C-5C1F-46D1-A0FA-A0FF9216899A}.Debug - NuGet Packages|Any CPU.ActiveCfg = Debug|Any CPU
{B609DB2C-5C1F-46D1-A0FA-A0FF9216899A}.Debug - NuGet Packages|Any CPU.Build.0 = Debug|Any CPU
{B609DB2C-5C1F-46D1-A0FA-A0FF9216899A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B609DB2C-5C1F-46D1-A0FA-A0FF9216899A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B609DB2C-5C1F-46D1-A0FA-A0FF9216899A}.Documentation|Any CPU.ActiveCfg = Debug|Any CPU
{B609DB2C-5C1F-46D1-A0FA-A0FF9216899A}.Documentation|Any CPU.Build.0 = Debug|Any CPU
{B609DB2C-5C1F-46D1-A0FA-A0FF9216899A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B609DB2C-5C1F-46D1-A0FA-A0FF9216899A}.Release|Any CPU.Build.0 = Release|Any CPU
{3F4A0DD8-4D47-4B9C-939A-3146E68C84F7}.Debug - NuGet Packages|Any CPU.ActiveCfg = Debug|Any CPU
{3F4A0DD8-4D47-4B9C-939A-3146E68C84F7}.Debug - NuGet Packages|Any CPU.Build.0 = Debug|Any CPU
{3F4A0DD8-4D47-4B9C-939A-3146E68C84F7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3F4A0DD8-4D47-4B9C-939A-3146E68C84F7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3F4A0DD8-4D47-4B9C-939A-3146E68C84F7}.Documentation|Any CPU.ActiveCfg = Debug|Any CPU
{3F4A0DD8-4D47-4B9C-939A-3146E68C84F7}.Documentation|Any CPU.Build.0 = Debug|Any CPU
{3F4A0DD8-4D47-4B9C-939A-3146E68C84F7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3F4A0DD8-4D47-4B9C-939A-3146E68C84F7}.Release|Any CPU.Build.0 = Release|Any CPU
{FC304BC8-D719-4E97-8C6C-36BDC342F069}.Debug - NuGet Packages|Any CPU.ActiveCfg = Debug|Any CPU
{FC304BC8-D719-4E97-8C6C-36BDC342F069}.Debug - NuGet Packages|Any CPU.Build.0 = Debug|Any CPU
{FC304BC8-D719-4E97-8C6C-36BDC342F069}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FC304BC8-D719-4E97-8C6C-36BDC342F069}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FC304BC8-D719-4E97-8C6C-36BDC342F069}.Documentation|Any CPU.ActiveCfg = Debug|Any CPU
{FC304BC8-D719-4E97-8C6C-36BDC342F069}.Documentation|Any CPU.Build.0 = Debug|Any CPU
{FC304BC8-D719-4E97-8C6C-36BDC342F069}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FC304BC8-D719-4E97-8C6C-36BDC342F069}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -296,6 +336,10 @@ Global
{0B8ABFDB-F9CF-4EC6-988E-9C32D9E01C26} = {4269F3C3-6B42-419B-B64A-3E6DC0F1574A}
{1B7920E6-5262-4054-B72D-3A8DBBA057D2} = {AD743B78-D61F-4FBF-B620-FA83CE599A50}
{298009D3-971D-4375-A2F2-04CFB168CED9} = {AD743B78-D61F-4FBF-B620-FA83CE599A50}
{8FC920C6-E895-4A17-AB2F-452FAAA36CC8} = {276EBE79-A13A-46BD-A566-B01DC0477A9B}
{B609DB2C-5C1F-46D1-A0FA-A0FF9216899A} = {4269F3C3-6B42-419B-B64A-3E6DC0F1574A}
{3F4A0DD8-4D47-4B9C-939A-3146E68C84F7} = {0A0E26B0-7A46-4F1A-8BFE-9A763FDF6CF8}
{FC304BC8-D719-4E97-8C6C-36BDC342F069} = {AD743B78-D61F-4FBF-B620-FA83CE599A50}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {7173C9F3-A7F9-496E-9078-9156E35D6E16}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.Bot.Builder.Dialogs;

namespace Microsoft.Bot.Builder.ApplicationInsights
{
public class BotTelemetryClient : IBotTelemetryClient
{
private readonly TelemetryClient _telemetryClient;

public BotTelemetryClient(TelemetryClient telemetryClient)
{
_telemetryClient = telemetryClient;
}
/// <summary>
/// Send information about external dependency call in the application. Create a
// separate Microsoft.ApplicationInsights.DataContracts.DependencyTelemetry instance
// for each call to Microsoft.ApplicationInsights.TelemetryClient.TrackDependency(Microsoft.ApplicationInsights.DataContracts.DependencyTelemetry)
/// </summary>
/// <param name="telemetry">A </param>
public void TrackDependency(DependencyTelemetry telemetry)
{
_telemetryClient.TrackDependency(telemetry);
}


/// <summary>
/// Send an Microsoft.ApplicationInsights.DataContracts.EventTelemetry for display
// in Diagnostic Search and in the Analytics Portal.
/// </summary>
/// <param name="telemetry">An event log item.</param>
public void TrackEvent(EventTelemetry telemetry)
{
_telemetryClient.TrackEvent(telemetry);
}

/// <summary>
/// Send an Microsoft.ApplicationInsights.DataContracts.ExceptionTelemetry for display
// in Diagnostic Search. Create a separate Microsoft.ApplicationInsights.DataContracts.ExceptionTelemetry
// instance for each call to Microsoft.ApplicationInsights.TelemetryClient.TrackException(Microsoft.ApplicationInsights.DataContracts.ExceptionTelemetry)
/// </summary>
/// <param name="telemetry"></param>
public void TrackException(ExceptionTelemetry telemetry)
{
_telemetryClient.TrackException(telemetry);
}

/// <summary>
/// Send a trace message for display in Diagnostic Search. Create a separate Microsoft.ApplicationInsights.DataContracts.TraceTelemetry
// instance for each call to Microsoft.ApplicationInsights.TelemetryClient.TrackTrace(Microsoft.ApplicationInsights.DataContracts.TraceTelemetry).
//
/// </summary>
/// <param name="telemetry">Message with optional properties.</param>
public void TrackTrace(TraceTelemetry telemetry)
{
_telemetryClient.TrackTrace(telemetry);
}

/// <summary>
/// Tracks single Waterfall step in Application Insights.
/// </summary>
/// <param name="waterfallStepContext">The WaterfallStepContext for this waterfall dialog.</param>
/// <param name="stepFriendlyName">The friendly name that is logged with the waterfall dialog id.</param>
public void TrackWaterfallStep(WaterfallStepContext waterfallStepContext, string stepFriendlyName)
{
_telemetryClient.TrackWaterfallStep(waterfallStepContext, stepFriendlyName);
}

/// <summary>
/// Flushes the in-memory buffer and any metrics being pre-aggregated.
/// </summary>
public void Flush()
{
_telemetryClient.Flush();
}

}
}
Loading