Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

Add Azure Application Insights for MongoDB

supporta20e05acad9b
diff --git a/src/Core/Grand.Infrastructure/Grand.Infrastructure.csproj b/src/Core/Grand.Infrastructure/Grand.Infrastructure.csproj
index 6c0c7bd2c..e05647dcc 100644
--- a/src/Core/Grand.Infrastructure/Grand.Infrastructure.csproj
+++ b/src/Core/Grand.Infrastructure/Grand.Infrastructure.csproj
@@ -4,6 +4,7 @@
<PackageReference Include="GoogleAuthenticator" Version="2.4.0" />
<PackageReference Include="MassTransit" Version="8.0.0" />
<PackageReference Include="MassTransit.RabbitMQ" Version="8.0.0" />
+ <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.20.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.3" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="6.0.1" />
diff --git a/src/Core/Grand.Infrastructure/Startup/ApplicationInsightsSubscriber.cs b/src/Core/Grand.Infrastructure/Startup/ApplicationInsightsSubscriber.cs
new file mode 100644
index 000000000..207ee7700
--- /dev/null
+++ b/src/Core/Grand.Infrastructure/Startup/ApplicationInsightsSubscriber.cs
@@ -0,0 +1,66 @@
+using Microsoft.ApplicationInsights;
+using Microsoft.ApplicationInsights.DataContracts;
+using Microsoft.Extensions.DependencyInjection;
+using MongoDB.Driver.Core.Events;
+using System.Collections.Concurrent;
+using System.Reflection;
+
+namespace Grand.Infrastructure.Startup
+{
+ public class ApplicationInsightsSubscriber : IEventSubscriber
+ {
+ private readonly ReflectionEventSubscriber _subscriber;
+
+ private TelemetryClient _telementryClient;
+ protected TelemetryClient TelementryClient {
+ get {
+ if (_telementryClient == null)
+ {
+ _telementryClient = _services.BuildServiceProvider().GetRequiredService<TelemetryClient>();
+ }
+ return _telementryClient;
+ }
+ }
+ private readonly IServiceCollection _services;
+
+ private readonly ConcurrentDictionary<int, Operation> dictionary = new ();
+
+ record Operation(string Name, string Command);
+
+ public ApplicationInsightsSubscriber(IServiceCollection services)
+ {
+ _subscriber = new ReflectionEventSubscriber(this,
+ bindingFlags: BindingFlags.Instance | BindingFlags.NonPublic);
+
+ _services = services;
+ }
+
+ public bool TryGetEventHandler<TEvent>(out Action<TEvent> handler)
+ {
+ return _subscriber.TryGetEventHandler(out handler);
+ }
+
+ private void Handle(CommandStartedEvent cmdStart)
+ {
+ dictionary.TryAdd(cmdStart.RequestId, new Operation(cmdStart.CommandName, cmdStart.Command.ToString()));
+ }
+
+ private void Handle(CommandSucceededEvent cmdSuccess)
+ {
+ if (dictionary.TryGetValue(cmdSuccess.RequestId, out var operation))
+ {
+ var telemetry = new DependencyTelemetry {
+ Data = operation.Command,
+ Name = operation.Name,
+ Type = "MongoDB",
+ Timestamp = DateTime.UtcNow.Subtract(cmdSuccess.Duration),
+ Duration = cmdSuccess.Duration,
+ Success = true,
+ };
+ TelementryClient?.TrackDependency(telemetry);
+
+ dictionary.TryRemove(cmdSuccess.RequestId, out var _);
+ }
+ }
+ }
+}
diff --git a/src/Core/Grand.Infrastructure/Startup/StartupApplication.cs b/src/Core/Grand.Infrastructure/Startup/StartupApplication.cs
index edb02fe6c..102acc97b 100644
--- a/src/Core/Grand.Infrastructure/Startup/StartupApplication.cs
+++ b/src/Core/Grand.Infrastructure/Startup/StartupApplication.cs
@@ -8,6 +8,7 @@ using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using MongoDB.Driver;
+using MongoDB.Driver.Core.Events;
namespace Grand.Infrastructure.Startup
{
@@ -25,19 +26,23 @@ namespace Grand.Infrastructure.Startup
public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
{
- var config = new DatabaseConfig();
- configuration.GetSection("Database").Bind(config);
- RegisterDataLayer(services, config);
+ RegisterDataLayer(services, configuration);
}
- private void RegisterDataLayer(IServiceCollection serviceCollection, DatabaseConfig dbConfig)
+ private void RegisterDataLayer(IServiceCollection serviceCollection, IConfiguration configuration)
{
+ var dbConfig = new DatabaseConfig();
+ configuration.GetSection("Database").Bind(dbConfig);
+
+ var applicationInsights = new ApplicationInsightsConfig();
+ configuration.GetSection("ApplicationInsights").Bind(applicationInsights);
+
var dataProviderSettings = DataSettingsManager.LoadSettings();
if (string.IsNullOrEmpty(dataProviderSettings.ConnectionString))
{
serviceCollection.AddTransient(c => dataProviderSettings);
- if(dbConfig.UseLiteDb)
+ if (dbConfig.UseLiteDb)
serviceCollection.AddSingleton(c => new LiteDatabase(dbConfig.LiteDbConnectionString));
}
if (dataProviderSettings != null && dataProviderSettings.IsValid())
@@ -49,11 +54,16 @@ namespace Grand.Infrastructure.Startup
var databaseName = mongourl.DatabaseName;
var clientSettings = MongoClientSettings.FromConnectionString(connectionString);
clientSettings.LinqProvider = MongoDB.Driver.Linq.LinqProvider.V3;
+
+ if (applicationInsights.Enabled)
+ clientSettings.ClusterConfigurator = builder => { builder.Subscribe(new ApplicationInsightsSubscriber(serviceCollection)); };
+
serviceCollection.AddScoped(c => new MongoClient(clientSettings).GetDatabase(databaseName));
+
}
else
{
- if(dbConfig.Singleton)
+ if (dbConfig.Singleton)
serviceCollection.AddSingleton(c => new LiteDatabase(dataProviderSettings.ConnectionString) { UtcDate = true });
else
serviceCollection.AddScoped(c => new LiteDatabase(dataProviderSettings.ConnectionString) { UtcDate = true });
@@ -81,5 +91,20 @@ namespace Grand.Infrastructure.Startup
}
}
+ private void CmdStartHandlerForFindCommand(CommandStartedEvent cmdStart)
+ {
+ if (cmdStart.CommandName == "find")
+ {
+ //WriteToConsole(cmdStart.Command, "request");
+ }
+ }
+
+ private void CmdSuccessHandlerForFindCommand(CommandSucceededEvent cmdSuccess)
+ {
+ if (cmdSuccess.CommandName == "find")
+ {
+ //WriteToConsole(cmdSuccess.Reply, "response");
+ }
+ }
}
}
diff --git a/src/Web/Grand.Web.Common/Grand.Web.Common.csproj b/src/Web/Grand.Web.Common/Grand.Web.Common.csproj
index 4b7c93234..d3f78fbe0 100644
--- a/src/Web/Grand.Web.Common/Grand.Web.Common.csproj
+++ b/src/Web/Grand.Web.Common/Grand.Web.Common.csproj
@@ -2,7 +2,6 @@
<Import Project="..\..\Build\Grand.Common.props" />
<ItemGroup>
<PackageReference Include="Flurl.Http" Version="3.2.2" />
- <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.20.0" />
<PackageReference Include="Microsoft.AspNetCore.DataProtection.StackExchangeRedis" Version="6.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="6.0.3" />
<PackageReference Include="AspNetCore.HealthChecks.MongoDb" Version="6.0.2" />