Skip to content

Commit 9da2871

Browse files
Move to Azure Monitor OpenTelemetry for telemetry (#571)
Replaced Microsoft.ApplicationInsights with Azure.Monitor.OpenTelemetry.AspNetCore for monitoring and telemetry. Updated package references in various project files. Removed obsolete ApplicationInsights configuration and related code. Simplified appsettings.json configuration. Updated logging to use Activity instead of RequestTelemetry.
1 parent ade7aa8 commit 9da2871

File tree

19 files changed

+47
-165
lines changed

19 files changed

+47
-165
lines changed

Directory.Packages.props

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
44
</PropertyGroup>
55
<ItemGroup>
6+
<PackageVersion Include="Azure.Monitor.OpenTelemetry.AspNetCore" Version="1.2.0" />
67
<PackageVersion Include="MaxMind.GeoIP2" Version="5.2.0" />
78
<PackageVersion Include="ExcelMapper" Version="5.2.598" />
89
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="9.0.1" />
@@ -23,7 +24,6 @@
2324
<PackageVersion Include="DotLiquid" Version="2.3.18" />
2425
<PackageVersion Include="FluentValidation.DependencyInjectionExtensions" Version="11.11.0" />
2526
<PackageVersion Include="GoogleAuthenticator" Version="3.2.0" />
26-
<PackageVersion Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.22.0" />
2727
<PackageVersion Include="Microsoft.Azure.AppConfiguration.AspNetCore" Version="8.0.0" />
2828
<PackageVersion Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.1" />
2929
<PackageVersion Include="Microsoft.Extensions.Caching.Memory" Version="9.0.1" />
@@ -52,7 +52,6 @@
5252
<PackageVersion Include="Azure.Extensions.AspNetCore.DataProtection.Keys" Version="1.3.0" />
5353
<PackageVersion Include="Azure.Identity" Version="1.13.2" />
5454
<PackageVersion Include="Flurl.Http" Version="4.0.2" />
55-
<PackageVersion Include="Microsoft.ApplicationInsights.Profiler.AspNetCore" Version="2.7.2" />
5655
<PackageVersion Include="Microsoft.AspNetCore.DataProtection.StackExchangeRedis" Version="9.0.1" />
5756
<PackageVersion Include="NetEscapades.AspNetCore.SecurityHeaders" Version="0.24.0" />
5857
<PackageVersion Include="Wangkanai.Detection" Version="8.14.0" />

src/Aspire/Aspire.ServiceDefaults/Aspire.ServiceDefaults.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
<ItemGroup>
1111
<FrameworkReference Include="Microsoft.AspNetCore.App" />
12+
<PackageReference Include="Azure.Monitor.OpenTelemetry.AspNetCore" />
1213

1314
<PackageReference Include="Microsoft.Extensions.Http.Resilience" />
1415
<PackageReference Include="Microsoft.Extensions.ServiceDiscovery" />

src/Aspire/Aspire.ServiceDefaults/Extensions.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
using Azure.Monitor.OpenTelemetry.AspNetCore;
12
using Microsoft.AspNetCore.Builder;
23
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
34
using Microsoft.Extensions.DependencyInjection;
45
using Microsoft.Extensions.Diagnostics.HealthChecks;
56
using Microsoft.Extensions.Logging;
67
using Microsoft.Extensions.ServiceDiscovery;
78
using OpenTelemetry;
9+
using OpenTelemetry.Logs;
810
using OpenTelemetry.Metrics;
11+
using OpenTelemetry.Resources;
912
using OpenTelemetry.Trace;
1013

1114
namespace Microsoft.Extensions.Hosting;
@@ -78,11 +81,19 @@ private static TBuilder AddOpenTelemetryExporters<TBuilder>(this TBuilder builde
7881
}
7982

8083
// Uncomment the following lines to enable the Azure Monitor exporter (requires the Azure.Monitor.OpenTelemetry.AspNetCore package)
81-
//if (!string.IsNullOrEmpty(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"]))
82-
//{
83-
// builder.Services.AddOpenTelemetry()
84-
// .UseAzureMonitor();
85-
//}
84+
if (!string.IsNullOrEmpty(builder.Configuration["ApplicationInsights:ConnectionString"]))
85+
{
86+
builder.Services.AddOpenTelemetry().UseAzureMonitor(o =>
87+
{
88+
o.ConnectionString = builder.Configuration["ApplicationInsights:ConnectionString"];
89+
}).ConfigureResource(resource =>
90+
{
91+
resource.AddAttributes(
92+
[
93+
new("service.name", "Grandnode")
94+
]);
95+
});
96+
}
8697

8798
return builder;
8899
}

src/Core/Grand.Infrastructure/Configuration/ApplicationInsightsConfig.cs

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/Core/Grand.Infrastructure/Grand.Infrastructure.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
<ItemGroup>
44
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" />
55
<PackageReference Include="GoogleAuthenticator" />
6-
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" />
76
<PackageReference Include="Microsoft.Azure.AppConfiguration.AspNetCore" />
87
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" />
98
<PackageReference Include="Microsoft.Extensions.Caching.Memory" />

src/Core/Grand.Infrastructure/Startup/ApplicationInsightsSubscriber.cs

Lines changed: 0 additions & 56 deletions
This file was deleted.

src/Core/Grand.Infrastructure/Startup/StartupApplication.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ private void RegisterDataLayer(IServiceCollection serviceCollection, IConfigurat
3434
var dbConfig = new DatabaseConfig();
3535
configuration.GetSection("Database").Bind(dbConfig);
3636

37-
var applicationInsights = new ApplicationInsightsConfig();
38-
configuration.GetSection("ApplicationInsights").Bind(applicationInsights);
39-
4037
var dataProviderSettings = DataSettingsManager.Instance.LoadSettings();
4138
if (string.IsNullOrEmpty(dataProviderSettings.ConnectionString))
4239
{
@@ -54,13 +51,6 @@ private void RegisterDataLayer(IServiceCollection serviceCollection, IConfigurat
5451
var mongoUrl = new MongoUrl(connectionString);
5552
var databaseName = mongoUrl.DatabaseName;
5653
var clientSettings = MongoClientSettings.FromConnectionString(connectionString);
57-
58-
if (applicationInsights.TrackDependencyMongoDb)
59-
clientSettings.ClusterConfigurator = builder =>
60-
{
61-
builder.Subscribe(new ApplicationInsightsSubscriber(serviceCollection));
62-
};
63-
6454
serviceCollection.AddScoped(_ => new MongoClient(clientSettings).GetDatabase(databaseName));
6555
}
6656
else

src/Core/Grand.Infrastructure/StartupBase.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,6 @@ private static void RegisterConfigurations(IServiceCollection services, IConfigu
227227
services.StartupConfig<DatabaseConfig>(configuration.GetSection("Database"));
228228
services.StartupConfig<AmazonConfig>(configuration.GetSection("Amazon"));
229229
services.StartupConfig<AzureConfig>(configuration.GetSection("Azure"));
230-
services.StartupConfig<ApplicationInsightsConfig>(configuration.GetSection("ApplicationInsights"));
231230
}
232231

233232
#endregion

src/Web/Grand.Web.Admin/App_Data/appsettings.json

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -179,15 +179,6 @@
179179
}
180180
},
181181
"ApplicationInsights": {
182-
"ConnectionString": "",
183-
"TrackDependencyMongoDb": false
184-
},
185-
//https://github.com/microsoft/ApplicationInsights-Profiler-AspNetCore/blob/main/Configurations.md
186-
"ServiceProfiler": {
187-
"IsDisabled": true,
188-
"Duration": "00:00:30",
189-
"InitialDelay": "00:00:03",
190-
"ProvideAnonymousTelemetry": true,
191-
"IsSkipCompatibilityTest": false
182+
"ConnectionString": ""
192183
}
193184
}

src/Web/Grand.Web.Admin/Program.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
var builder = WebApplication.CreateBuilder(args);
77

8+
//add configuration
9+
builder.Configuration.AddAppSettingsJsonFile(args);
10+
811
builder.AddServiceDefaults();
912

1013
builder.Host.UseDefaultServiceProvider((_, options) =>
@@ -13,9 +16,6 @@
1316
options.ValidateOnBuild = false;
1417
});
1518

16-
//add configuration
17-
builder.Configuration.AddAppSettingsJsonFile(args);
18-
1919
//add services
2020
StartupBase.ConfigureServices(builder.Services, builder.Configuration);
2121

@@ -28,4 +28,4 @@
2828
StartupBase.ConfigureRequestPipeline(app, builder.Environment);
2929

3030
//run app
31-
app.Run();
31+
await app.RunAsync();

0 commit comments

Comments
 (0)