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

fix: Register IBotTelemetryClient and add telemetry initializer middleware… #2916

Merged
merged 10 commits into from
May 8, 2020
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