Skip to content

Commit a0e3473

Browse files
Fix AppInsights dev time provisioning (#2053)
1 parent 12d0a2d commit a0e3473

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

Directory.Packages.props

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<PackageVersion Include="Azure.Storage.Queues" Version="12.17.1" />
1515
<PackageVersion Include="Microsoft.Azure.Cosmos" Version="3.37.1" />
1616
<PackageVersion Include="Microsoft.Extensions.Azure" Version="1.7.1" />
17+
<PackageVersion Include="Azure.Monitor.OpenTelemetry.AspNetCore" Version="1.1.0" />
1718
<!-- Azure Management SDK for .NET dependencies -->
1819
<PackageVersion Include="Azure.ResourceManager.CosmosDB" Version="1.4.0-beta.5" />
1920
<PackageVersion Include="Azure.ResourceManager.KeyVault" Version="1.2.0" />
@@ -24,6 +25,7 @@
2425
<PackageVersion Include="Azure.ResourceManager.AppConfiguration" Version="1.1.0" />
2526
<PackageVersion Include="Azure.ResourceManager.Sql" Version="1.2.0" />
2627
<PackageVersion Include="Azure.ResourceManager.ApplicationInsights" Version="1.0.0-beta.4" />
28+
<PackageVersion Include="Azure.ResourceManager.OperationalInsights" Version="1.2.0" />
2729
<!-- ASP.NET Core dependencies -->
2830
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="$(MicrosoftAspNetCoreOpenApiPackageVersion)" />
2931
<PackageVersion Include="Microsoft.AspNetCore.OutputCaching.StackExchangeRedis" Version="$(MicrosoftAspNetCoreOutputCachingStackExchangeRedisPackageVersion)" />

src/Aspire.Hosting.Azure.Provisioning/Aspire.Hosting.Azure.Provisioning.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<PackageReference Include="Azure.ResourceManager.Storage" />
1919
<PackageReference Include="Azure.ResourceManager.Sql" />
2020
<PackageReference Include="Azure.ResourceManager.ApplicationInsights" />
21+
<PackageReference Include="Azure.ResourceManager.OperationalInsights" />
2122
<Compile Include="$(SharedDir)ValueStopwatch\*.cs" />
2223
</ItemGroup>
2324

src/Aspire.Hosting.Azure.Provisioning/Provisioners/AzureApplicationInsightsProvisioner.cs

+15-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33

44
using System.Diagnostics;
55
using Aspire.Hosting.ApplicationModel;
6-
using Azure.ResourceManager.ApplicationInsights;
76
using Azure;
7+
using Azure.ResourceManager.ApplicationInsights;
8+
using Azure.ResourceManager.OperationalInsights;
89
using Microsoft.Extensions.Configuration;
910
using Microsoft.Extensions.Logging;
1011

@@ -38,6 +39,17 @@ public override async Task GetOrCreateResourceAsync(AzureApplicationInsightsReso
3839

3940
if (applicationInsightsResource is null)
4041
{
42+
var logAnalytics = new OperationalInsightsWorkspaceData(context.Location);
43+
logAnalytics.Tags.Add(AzureProvisioner.AspireResourceNameTag, resource.Name);
44+
45+
var workspaceName = Guid.NewGuid().ToString().Replace("-", string.Empty)[0..20];
46+
47+
logger.LogInformation("Creating Log Analytics Workspace {workspaceName} in {location}...", workspaceName, context.Location);
48+
var sw = Stopwatch.StartNew();
49+
var workspaceOp = await context.ResourceGroup.GetOperationalInsightsWorkspaces().CreateOrUpdateAsync(WaitUntil.Completed, workspaceName, logAnalytics, cancellationToken).ConfigureAwait(false);
50+
sw.Stop();
51+
logger.LogInformation("Log Analytics Workspace {workspaceName} created in {elapsed}", workspaceOp.Value.Data.Name, TimeSpan.FromSeconds(10));
52+
4153
var applicationInsightsName = Guid.NewGuid().ToString().Replace("-", string.Empty)[0..20];
4254

4355
// We could model application insights as a child resource of a log analytics workspace, but instead,
@@ -47,11 +59,11 @@ public override async Task GetOrCreateResourceAsync(AzureApplicationInsightsReso
4759

4860
var applicationInsightsCreateOrUpdateContent = new ApplicationInsightsComponentData(context.Location, "web")
4961
{
50-
WorkspaceResourceId = ""// context.LogAnalyticsWorkspace.Id
62+
WorkspaceResourceId = workspaceOp.Value.Id
5163
};
5264
applicationInsightsCreateOrUpdateContent.Tags.Add(AzureProvisioner.AspireResourceNameTag, resource.Name);
5365

54-
var sw = Stopwatch.StartNew();
66+
sw.Restart();
5567
var operation = await context.ResourceGroup.GetApplicationInsightsComponents().CreateOrUpdateAsync(WaitUntil.Completed, applicationInsightsName, applicationInsightsCreateOrUpdateContent, cancellationToken).ConfigureAwait(false);
5668
applicationInsightsResource = operation.Value;
5769
sw.Stop();

0 commit comments

Comments
 (0)