Skip to content

Commit

Permalink
Fix AppInsights dev time provisioning (dotnet#2053)
Browse files Browse the repository at this point in the history
  • Loading branch information
BrennanConroy authored and radical committed Feb 6, 2024
1 parent e3083bd commit 8a59d1c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<PackageVersion Include="Azure.Storage.Queues" Version="12.17.1" />
<PackageVersion Include="Microsoft.Azure.Cosmos" Version="3.37.1" />
<PackageVersion Include="Microsoft.Extensions.Azure" Version="1.7.1" />
<PackageVersion Include="Azure.Monitor.OpenTelemetry.AspNetCore" Version="1.1.0" />
<!-- Azure Management SDK for .NET dependencies -->
<PackageVersion Include="Azure.ResourceManager.CosmosDB" Version="1.4.0-beta.5" />
<PackageVersion Include="Azure.ResourceManager.KeyVault" Version="1.2.0" />
Expand All @@ -24,6 +25,7 @@
<PackageVersion Include="Azure.ResourceManager.AppConfiguration" Version="1.1.0" />
<PackageVersion Include="Azure.ResourceManager.Sql" Version="1.2.0" />
<PackageVersion Include="Azure.ResourceManager.ApplicationInsights" Version="1.0.0-beta.4" />
<PackageVersion Include="Azure.ResourceManager.OperationalInsights" Version="1.2.0" />
<!-- ASP.NET Core dependencies -->
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="$(MicrosoftAspNetCoreOpenApiPackageVersion)" />
<PackageVersion Include="Microsoft.AspNetCore.OutputCaching.StackExchangeRedis" Version="$(MicrosoftAspNetCoreOutputCachingStackExchangeRedisPackageVersion)" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<PackageReference Include="Azure.ResourceManager.Storage" />
<PackageReference Include="Azure.ResourceManager.Sql" />
<PackageReference Include="Azure.ResourceManager.ApplicationInsights" />
<PackageReference Include="Azure.ResourceManager.OperationalInsights" />
<Compile Include="$(SharedDir)ValueStopwatch\*.cs" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@

using System.Diagnostics;
using Aspire.Hosting.ApplicationModel;
using Azure.ResourceManager.ApplicationInsights;
using Azure;
using Azure.ResourceManager.ApplicationInsights;
using Azure.ResourceManager.OperationalInsights;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

Expand Down Expand Up @@ -38,6 +39,17 @@ public override async Task GetOrCreateResourceAsync(AzureApplicationInsightsReso

if (applicationInsightsResource is null)
{
var logAnalytics = new OperationalInsightsWorkspaceData(context.Location);
logAnalytics.Tags.Add(AzureProvisioner.AspireResourceNameTag, resource.Name);

var workspaceName = Guid.NewGuid().ToString().Replace("-", string.Empty)[0..20];

logger.LogInformation("Creating Log Analytics Workspace {workspaceName} in {location}...", workspaceName, context.Location);
var sw = Stopwatch.StartNew();
var workspaceOp = await context.ResourceGroup.GetOperationalInsightsWorkspaces().CreateOrUpdateAsync(WaitUntil.Completed, workspaceName, logAnalytics, cancellationToken).ConfigureAwait(false);
sw.Stop();
logger.LogInformation("Log Analytics Workspace {workspaceName} created in {elapsed}", workspaceOp.Value.Data.Name, TimeSpan.FromSeconds(10));

var applicationInsightsName = Guid.NewGuid().ToString().Replace("-", string.Empty)[0..20];

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

var applicationInsightsCreateOrUpdateContent = new ApplicationInsightsComponentData(context.Location, "web")
{
WorkspaceResourceId = ""// context.LogAnalyticsWorkspace.Id
WorkspaceResourceId = workspaceOp.Value.Id
};
applicationInsightsCreateOrUpdateContent.Tags.Add(AzureProvisioner.AspireResourceNameTag, resource.Name);

var sw = Stopwatch.StartNew();
sw.Restart();
var operation = await context.ResourceGroup.GetApplicationInsightsComponents().CreateOrUpdateAsync(WaitUntil.Completed, applicationInsightsName, applicationInsightsCreateOrUpdateContent, cancellationToken).ConfigureAwait(false);
applicationInsightsResource = operation.Value;
sw.Stop();
Expand Down

0 comments on commit 8a59d1c

Please sign in to comment.