Skip to content

Commit

Permalink
fix: Register IBotTelemetryClient and add telemetry initializer middl…
Browse files Browse the repository at this point in the history
…eware… (#2916)

* Register IBotTelemetryClient and add telemetry initializer middleware to the adapter pipeline

* Update functions telemetry config and amend webapp implementaton

* Remove yarn.lock

* Pass instrumentation key to AddBotApplicationInsights method

* Explicitly pass the instrumentation key in functions runtime

* Fix issues introduced in merge

Co-authored-by: Ben Brown <benbro@microsoft.com>
Co-authored-by: Chris Whitten <christopher.whitten@microsoft.com>
  • Loading branch information
3 people authored May 8, 2020
1 parent 9d221cf commit 819ede5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
8 changes: 6 additions & 2 deletions runtime/dotnet/azurefunctions/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
using System.IO;
using System.Reflection;
using System.Text;
using Microsoft.Bot.Builder.ApplicationInsights;

[assembly: FunctionsStartup(typeof(Microsoft.BotFramework.Composer.Functions.Startup))]

Expand Down Expand Up @@ -86,9 +87,10 @@ public override void Configure(IFunctionsHostBuilder builder)
services.AddSingleton<ChannelServiceHandler, SkillHandler>();

// Register telemetry client, initializers and middleware
services.AddApplicationInsightsTelemetry();
services.AddApplicationInsightsTelemetry(settings.ApplicationInsights.InstrumentationKey);
services.AddSingleton<ITelemetryInitializer, OperationCorrelationTelemetryInitializer>();
services.AddSingleton<ITelemetryInitializer, TelemetryBotIdInitializer>();
services.AddSingleton<IBotTelemetryClient, BotTelemetryClient>();
services.AddSingleton<TelemetryLoggerMiddleware>(sp =>
{
var telemetryClient = sp.GetService<IBotTelemetryClient>();
Expand Down Expand Up @@ -130,12 +132,14 @@ public override void Configure(IFunctionsHostBuilder builder)
IStorage storage = s.GetService<IStorage>();
UserState userState = s.GetService<UserState>();
ConversationState conversationState = s.GetService<ConversationState>();
TelemetryInitializerMiddleware telemetryInitializerMiddleware = s.GetService<TelemetryInitializerMiddleware>();

var adapter = new BotFrameworkHttpAdapter(new ConfigurationCredentialProvider(rootConfiguration));

adapter
.UseStorage(storage)
.UseBotState(userState, conversationState);
.UseBotState(userState, conversationState)
.Use(telemetryInitializerMiddleware);

// Configure Middlewares
ConfigureTranscriptLoggerMiddleware(adapter, settings);
Expand Down
13 changes: 8 additions & 5 deletions runtime/dotnet/azurewebapp/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.AI.Luis;
using Microsoft.Bot.Builder.AI.QnA;
using Microsoft.Bot.Builder.ApplicationInsights;
using Microsoft.Bot.Builder.Azure;
using Microsoft.Bot.Builder.BotFramework;
using Microsoft.Bot.Builder.Dialogs;
Expand Down Expand Up @@ -82,14 +83,15 @@ public IStorage ConfigureStorage(BotSettings settings)
return storage;
}

public BotFrameworkHttpAdapter GetBotAdapter(IStorage storage, BotSettings settings, UserState userState, ConversationState conversationState, IServiceProvider s)
public BotFrameworkHttpAdapter GetBotAdapter(IStorage storage, BotSettings settings, UserState userState, ConversationState conversationState, IServiceProvider s, TelemetryInitializerMiddleware telemetryInitializerMiddleware)
{
var adapter = new BotFrameworkHttpAdapter(new ConfigurationCredentialProvider(this.Configuration));

adapter
.UseStorage(storage)
.UseBotState(userState, conversationState)
.Use(new RegisterClassMiddleware<IConfiguration>(Configuration));
.Use(new RegisterClassMiddleware<IConfiguration>(Configuration))
.Use(telemetryInitializerMiddleware);

// Configure Middlewares
ConfigureTranscriptLoggerMiddleware(adapter, settings);
Expand Down Expand Up @@ -137,9 +139,10 @@ public void ConfigureServices(IServiceCollection services)
services.AddSingleton<ChannelServiceHandler, SkillHandler>();

// Register telemetry client, initializers and middleware
services.AddApplicationInsightsTelemetry();
services.AddApplicationInsightsTelemetry(settings.ApplicationInsights.InstrumentationKey);
services.AddSingleton<ITelemetryInitializer, OperationCorrelationTelemetryInitializer>();
services.AddSingleton<ITelemetryInitializer, TelemetryBotIdInitializer>();
services.AddSingleton<IBotTelemetryClient, BotTelemetryClient>();
services.AddSingleton<TelemetryLoggerMiddleware>(sp =>
{
var telemetryClient = sp.GetService<IBotTelemetryClient>();
Expand All @@ -152,7 +155,7 @@ public void ConfigureServices(IServiceCollection services)
return new TelemetryInitializerMiddleware(httpContextAccessor, telemetryLoggerMiddleware, settings.Telemetry.LogActivities);
});

IStorage storage = ConfigureStorage(settings);
var storage = ConfigureStorage(settings);

services.AddSingleton(storage);
var userState = new UserState(storage);
Expand All @@ -169,7 +172,7 @@ public void ConfigureServices(IServiceCollection services)

services.AddSingleton(resourceExplorer);

services.AddSingleton<IBotFrameworkHttpAdapter, BotFrameworkHttpAdapter>((s) => GetBotAdapter(storage, settings, userState, conversationState, s));
services.AddSingleton<IBotFrameworkHttpAdapter, BotFrameworkHttpAdapter>((s) => GetBotAdapter(storage, settings, userState, conversationState, s, s.GetService<TelemetryInitializerMiddleware>()));

services.AddSingleton<IBot>(s =>
new ComposerBot(
Expand Down

0 comments on commit 819ede5

Please sign in to comment.