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

Add AWS CloudFormation Provisioning and SDK Configuration #1905

Merged
merged 53 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
b79ba3f
Add CloudFormation provisioner
normj Nov 29, 2023
61ea393
Combine work to AWS.Hosting.AWS project
normj Dec 14, 2023
c4724b9
Add ability to configure the CloudFormation service client used for p…
normj Dec 16, 2023
bc5cbac
Add methods to configure service clients
normj Dec 16, 2023
1b3af50
Fix AWS Blazor sample app to enable InteractiveServer
normj Dec 17, 2023
839a68a
Rework SDK configuration as an IAWSSDKConfigResource
normj Dec 18, 2023
e1fc1cb
Merge branch 'main' into normj/aws-provisioning
normj Jan 26, 2024
708c32e
Rework AWS sample application to display the configuration applied fr…
normj Jan 26, 2024
3a9159b
Add the AWS .NET SDK OTel provider
normj Jan 27, 2024
56c12f7
Add more comments about what is going on in AppHost
normj Jan 27, 2024
a21c441
Add missing AddAWSService call for SNS
normj Jan 27, 2024
ff67ca4
Fail CloudFormation provisioning of stack is already in an "in progre…
normj Jan 27, 2024
88bbe5b
Minor cleanup
normj Jan 27, 2024
162b3bf
Fix whitespace
normj Jan 27, 2024
7a1c8f9
Move AWS sample to playground
normj Jan 27, 2024
73b6761
Address some of the PR comments.
normj Jan 29, 2024
53494e5
Address PR comments
normj Jan 31, 2024
12468ae
Add support for importing existing AWS resources using the AddAWSClou…
normj Feb 2, 2024
b8a5e7e
Merge branch 'main' into normj/aws-provisioning
normj Feb 2, 2024
ee17fa2
Fix after latest merge from main
normj Feb 2, 2024
411d32a
Add CloudFormation to manifest
normj Feb 2, 2024
46e4917
Add ability to set input parameters to CloudFormation template
normj Feb 3, 2024
b28f102
Lifecycle skips work if publishing manifest
normj Feb 3, 2024
dd93d1f
Add "aws." prefix to the AWS resources in the manifest
normj Feb 3, 2024
f9093ab
Rename WithParameter to AddParameter
normj Feb 3, 2024
f67fb5f
Merge branch 'main' into normj/aws-provisioning
normj Feb 24, 2024
c9f4744
Fixes after merging preview 4
normj Feb 24, 2024
b72ea36
Merge branch 'main' into normj/aws-provisioning
normj Mar 3, 2024
21b0f89
Publish logs and state to dashboard while doing CLoudFormation provis…
normj Mar 5, 2024
0b39eea
Block projects from starting till CloudFormation is done.
normj Mar 5, 2024
5198bcc
Fix issue with resource state being set for the initial state.
normj Mar 5, 2024
40a590d
Switch to use update state instead of replace state pattern.
normj Mar 5, 2024
46ec9d1
Ran VS code cleanup to clean up the formatting
normj Mar 6, 2024
106f5a1
Add logging message when project is waiting on CloudFormation stack.
normj Mar 6, 2024
a316cf7
Set source property for resource dashboard
normj Mar 6, 2024
8f242c4
Add ability to bind a single output variable to a project.
normj Mar 6, 2024
9c1756b
Fix source not being set when using AddAWSCloudFormationStack for bin…
normj Mar 6, 2024
4cc515a
Delete orphaned files
normj Mar 6, 2024
2ecd5ba
Merge branch 'main' into normj/aws-provisioning
normj Mar 6, 2024
b8dc8d3
Clean up adding manifest callbacks and rename AddParameter to WithPar…
normj Mar 6, 2024
e4cd5bb
Address some CR comments
normj Mar 7, 2024
8e33322
Replace the CloudFormation reference resource with an Annotation
normj Mar 7, 2024
dae3f4a
Add SDK configuration
normj Mar 7, 2024
b3470f1
Merge branch 'main' into normj/aws-provisioning
normj Mar 8, 2024
2ef50f1
Refactor the logic for making the provisioning calls to CloudFormation.
normj Mar 9, 2024
79f6f6b
Update project description and package tags
normj Mar 9, 2024
dc4cf58
Add unit tests
normj Mar 9, 2024
e2947f8
Test cleanup
normj Mar 9, 2024
ba86f69
Update the AWS dependencies in the Directory.Packages.props
normj Mar 13, 2024
39907b5
Remove unnecessary Prometheus dependency
eerhardt Mar 13, 2024
3492fcd
Merge remote-tracking branch 'upstream/main' into normj/aws-provisioning
eerhardt Mar 13, 2024
9ac29a0
Fix build for latest code.
eerhardt Mar 13, 2024
eaea240
Fix AWS tests
normj Mar 14, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 37 additions & 6 deletions Aspire.sln
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,20 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestProject.IntegrationServ
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tools", "Tools", "{2136E31D-2CBB-41BB-8618-716FF8E46E9E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfigurationSchemaGenerator", "src\Tools\ConfigurationSchemaGenerator\ConfigurationSchemaGenerator.csproj", "{39FA2A64-012F-4EB9-A14F-E8AC54C975F6}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "AWS", "AWS", "{EF91843F-C4AB-47F8-909B-C494EABB2BA2}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AWS.AppHost", "samples\AWS\AWS.AppHost\AWS.AppHost.csproj", "{5FB59EA4-5D5D-4942-BF00-F034F57A146A}"
normj marked this conversation as resolved.
Show resolved Hide resolved
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AWS.ServiceDefaults", "samples\AWS\AWS.ServiceDefaults\AWS.ServiceDefaults.csproj", "{2E54D01B-87AD-460E-A773-75C6DF50F30C}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Frontend", "samples\AWS\Frontend\Frontend.csproj", "{A1662C12-DB8B-4C68-94EA-101860072BB1}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Aspire.Hosting.AWS", "src\Aspire.Hosting.AWS\Aspire.Hosting.AWS.csproj", "{3112D63C-A9E3-44F8-92A5-8F3052627312}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Aspire.MongoDB.Driver", "src\Components\Aspire.MongoDB.Driver\Aspire.MongoDB.Driver.csproj", "{20A5A907-A135-4735-B4BF-E13514F360E3}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfigurationSchemaGenerator", "src\Tools\ConfigurationSchemaGenerator\ConfigurationSchemaGenerator.csproj", "{39FA2A64-012F-4EB9-A14F-E8AC54C975F6}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Aspire.MongoDB.Driver.Tests", "tests\Aspire.MongoDB.Driver.Tests\Aspire.MongoDB.Driver.Tests.csproj", "{E592E447-BA3C-44FA-86C1-EBEDC864A644}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfigurationSchemaGenerator.Tests", "tests\ConfigurationSchemaGenerator.Tests\ConfigurationSchemaGenerator.Tests.csproj", "{00FEA181-84C9-42A7-AC81-29A9F176A1A0}"
Expand Down Expand Up @@ -471,14 +481,30 @@ Global
{DCF2D47A-921A-4900-B5B2-CF97B3531CE8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DCF2D47A-921A-4900-B5B2-CF97B3531CE8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DCF2D47A-921A-4900-B5B2-CF97B3531CE8}.Release|Any CPU.Build.0 = Release|Any CPU
{39FA2A64-012F-4EB9-A14F-E8AC54C975F6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{39FA2A64-012F-4EB9-A14F-E8AC54C975F6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{39FA2A64-012F-4EB9-A14F-E8AC54C975F6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{39FA2A64-012F-4EB9-A14F-E8AC54C975F6}.Release|Any CPU.Build.0 = Release|Any CPU
{5FB59EA4-5D5D-4942-BF00-F034F57A146A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5FB59EA4-5D5D-4942-BF00-F034F57A146A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5FB59EA4-5D5D-4942-BF00-F034F57A146A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5FB59EA4-5D5D-4942-BF00-F034F57A146A}.Release|Any CPU.Build.0 = Release|Any CPU
{2E54D01B-87AD-460E-A773-75C6DF50F30C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2E54D01B-87AD-460E-A773-75C6DF50F30C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2E54D01B-87AD-460E-A773-75C6DF50F30C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2E54D01B-87AD-460E-A773-75C6DF50F30C}.Release|Any CPU.Build.0 = Release|Any CPU
{A1662C12-DB8B-4C68-94EA-101860072BB1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A1662C12-DB8B-4C68-94EA-101860072BB1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A1662C12-DB8B-4C68-94EA-101860072BB1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A1662C12-DB8B-4C68-94EA-101860072BB1}.Release|Any CPU.Build.0 = Release|Any CPU
{3112D63C-A9E3-44F8-92A5-8F3052627312}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3112D63C-A9E3-44F8-92A5-8F3052627312}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3112D63C-A9E3-44F8-92A5-8F3052627312}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3112D63C-A9E3-44F8-92A5-8F3052627312}.Release|Any CPU.Build.0 = Release|Any CPU
{20A5A907-A135-4735-B4BF-E13514F360E3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{20A5A907-A135-4735-B4BF-E13514F360E3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{20A5A907-A135-4735-B4BF-E13514F360E3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{20A5A907-A135-4735-B4BF-E13514F360E3}.Release|Any CPU.Build.0 = Release|Any CPU
{39FA2A64-012F-4EB9-A14F-E8AC54C975F6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{39FA2A64-012F-4EB9-A14F-E8AC54C975F6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{39FA2A64-012F-4EB9-A14F-E8AC54C975F6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{39FA2A64-012F-4EB9-A14F-E8AC54C975F6}.Release|Any CPU.Build.0 = Release|Any CPU
{E592E447-BA3C-44FA-86C1-EBEDC864A644}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E592E447-BA3C-44FA-86C1-EBEDC864A644}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E592E447-BA3C-44FA-86C1-EBEDC864A644}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -614,8 +640,13 @@ Global
{CA283D7F-EB95-4353-B196-C409965D2B42} = {27381127-6C45-4B4C-8F18-41FF48DFE4B2}
{C8079F06-304F-49B1-A0C1-45AA3782A923} = {4981B3A5-4AFD-4191-BF7D-8692D9783D60}
{DCF2D47A-921A-4900-B5B2-CF97B3531CE8} = {975F6F41-B455-451D-A312-098DE4A167B6}
{39FA2A64-012F-4EB9-A14F-E8AC54C975F6} = {2136E31D-2CBB-41BB-8618-716FF8E46E9E}
{EF91843F-C4AB-47F8-909B-C494EABB2BA2} = {D173887B-AF42-4576-B9C1-96B9E9B3D9C0}
{5FB59EA4-5D5D-4942-BF00-F034F57A146A} = {EF91843F-C4AB-47F8-909B-C494EABB2BA2}
{2E54D01B-87AD-460E-A773-75C6DF50F30C} = {EF91843F-C4AB-47F8-909B-C494EABB2BA2}
{A1662C12-DB8B-4C68-94EA-101860072BB1} = {EF91843F-C4AB-47F8-909B-C494EABB2BA2}
{3112D63C-A9E3-44F8-92A5-8F3052627312} = {B80354C7-BE58-43F6-8928-9F3A74AB7F47}
{20A5A907-A135-4735-B4BF-E13514F360E3} = {27381127-6C45-4B4C-8F18-41FF48DFE4B2}
{39FA2A64-012F-4EB9-A14F-E8AC54C975F6} = {2136E31D-2CBB-41BB-8618-716FF8E46E9E}
{E592E447-BA3C-44FA-86C1-EBEDC864A644} = {4981B3A5-4AFD-4191-BF7D-8692D9783D60}
{00FEA181-84C9-42A7-AC81-29A9F176A1A0} = {4981B3A5-4AFD-4191-BF7D-8692D9783D60}
{1828FA88-6BFB-437E-A163-A98AE2B948F1} = {27381127-6C45-4B4C-8F18-41FF48DFE4B2}
Expand Down
9 changes: 9 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
<!-- AWS SDK for .NET dependencies -->
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great to get these packages added to the Aspire NuGet feeds. Some of these are newer versions of packages that are already in there. Currently I'm testing with hacking the NuGet.config to allow the public NuGet feed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these the versions you need?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently I'm testing with hacking the NuGet.config to allow the public NuGet feed.

Would it be possible to use the "hack" while the PR was in draft, and adding more dependencies? (and possibly updating new versions that are released). Then once the PR is ready for review, we can revert the nuget.config change, and mirror the necessary packages to the trusted feed.

<PackageVersion Include="AWSSDK.CloudFormation" Version="3.7.302.28" />
<PackageVersion Include="AWSSDK.SQS" Version="3.7.300.41" />
<PackageVersion Include="AWSSDK.SimpleNotificationService" Version="3.7.300.41" />
<PackageVersion Include="AWSSDK.Core" Version="3.7.302" />
<PackageVersion Include="AWSSDK.Extensions.NETCore.Setup" Version="3.7.300" />
<PackageVersion Include="AWS.Messaging" Version="0.1.0-beta" />
<PackageVersion Include="OpenTelemetry.Instrumentation.AWS" Version="1.1.0-beta.3" />
<PackageVersion Include="OpenTelemetry.Extensions.AWS" Version="1.3.0-beta.1" />
<!-- Azure SDK for .NET dependencies -->
<PackageVersion Include="Azure.AI.OpenAI" Version="1.0.0-beta.12" />
<PackageVersion Include="Azure.Data.Tables" Version="12.8.2" />
Expand Down
23 changes: 23 additions & 0 deletions samples/AWS/AWS.AppHost/AWS.AppHost.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsAspireHost>true</IsAspireHost>
</PropertyGroup>

<ItemGroup>
<Compile Include="..\..\..\src\Aspire.Hosting\Utils\KnownResourceNames.cs" Link="KnownResourceNames.cs" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\Aspire.Dashboard\Aspire.Dashboard.csproj" />
<ProjectReference Include="..\..\..\src\Aspire.Hosting.AWS\Aspire.Hosting.AWS.csproj" IsAspireProjectResource="false" />
<ProjectReference Include="..\..\..\src\Aspire.Hosting\Aspire.Hosting.csproj" IsAspireProjectResource="false" />

<ProjectReference Include="..\Frontend\Frontend.csproj" />
</ItemGroup>

</Project>
8 changes: 8 additions & 0 deletions samples/AWS/AWS.AppHost/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project>

<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))" />

<!-- NOTE: This line is only required because we are using P2P references, not NuGet. It will not exist in real apps. -->
<Import Project="../../../src/Aspire.Hosting/build/Aspire.Hosting.props" />

</Project>
9 changes: 9 additions & 0 deletions samples/AWS/AWS.AppHost/Directory.Build.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project>

<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.targets', '$(MSBuildThisFileDirectory)../'))" />

<!-- NOTE: These lines are only required because we are using P2P references, not NuGet. They will not exist in real apps. -->
<Import Project="..\..\..\src\Aspire.Hosting\build\Aspire.Hosting.targets" />
<Import Project="..\..\..\src\Aspire.Hosting.Sdk\SDK\Sdk.targets" />

</Project>
24 changes: 24 additions & 0 deletions samples/AWS/AWS.AppHost/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Amazon;

var builder = DistributedApplication.CreateBuilder(args);

// Setup a configuration for the AWS .NET SDK.
var awsConfig = builder.AddAWSSDKConfig("frontend-aws-config")
.WithProfile("default")
.WithRegion(RegionEndpoint.USEast2);

// Provision application level resources like SQS queues and SNS topics defined in the CloudFormation template file app-resources.template.
var awsResources = builder.AddAWSCloudFormationProvisioning("AspireSampleDevResources", "app-resources.template")
// Add the SDK configuration so the AppHost knows what account/region to provision the resources.
.WithAWSSDKReference(awsConfig);

builder.AddProject<Projects.Frontend>("frontend")
// Reference the CloudFormation resource to project. The output parameters will added to the IConfiguration of the project.
.WithAWSCloudFormationReference(awsResources)
// Assign the SDK config to the project. The service clients created in the project relying on environment config
// will pick up these configuration.
.WithAWSSDKReference(awsConfig);

builder.Build().Run();
16 changes: 16 additions & 0 deletions samples/AWS/AWS.AppHost/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:15069",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"DOTNET_ENVIRONMENT": "Development",
"DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:16216"
}
}
}
}
26 changes: 26 additions & 0 deletions samples/AWS/AWS.AppHost/app-resources.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"AWSTemplateFormatVersion" : "2010-09-09",
"Resources" : {
"ChatMessagesQueue" : {
"Type" : "AWS::SQS::Queue",
"Properties" : {
}
},
"ChatTopic" : {
"Type" : "AWS::SNS::Topic",
"Properties" : {
"Subscription" : [
{"Protocol" : "sqs", "Endpoint" : {"Fn::GetAtt" : [ "ChatMessagesQueue", "Arn"]}}
]
}
}
},
"Outputs" : {
"ChatMessagesQueueUrl" : {
"Value" : { "Ref" : "ChatMessagesQueue" }
},
"ChatTopicArn" : {
"Value" : { "Ref" : "ChatTopic" }
}
}
}
9 changes: 9 additions & 0 deletions samples/AWS/AWS.AppHost/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning",
"Aspire.Hosting.Dcp": "Warning"
}
}
}
29 changes: 29 additions & 0 deletions samples/AWS/AWS.ServiceDefaults/AWS.ServiceDefaults.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />

<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" />
<PackageReference Include="OpenTelemetry.Exporter.Prometheus.AspNetCore" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" />
<PackageReference Include="OpenTelemetry.Instrumentation.GrpcNetClient" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" />

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

<PackageReference Include="OpenTelemetry.Instrumentation.AWS" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\Microsoft.Extensions.ServiceDiscovery.Dns\Microsoft.Extensions.ServiceDiscovery.Dns.csproj" />
<ProjectReference Include="..\..\..\src\Microsoft.Extensions.ServiceDiscovery\Microsoft.Extensions.ServiceDiscovery.csproj" />
</ItemGroup>

</Project>
125 changes: 125 additions & 0 deletions samples/AWS/AWS.ServiceDefaults/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Logging;
using OpenTelemetry.Logs;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;

namespace Microsoft.Extensions.Hosting;

public static class Extensions
{
public static IHostApplicationBuilder AddServiceDefaults(this IHostApplicationBuilder builder)
{
builder.ConfigureOpenTelemetry();

builder.AddDefaultHealthChecks();

builder.Services.AddServiceDiscovery();

builder.Services.ConfigureHttpClientDefaults(http =>
{
// Turn on resilience by default
http.AddStandardResilienceHandler();

// Turn on service discovery by default
http.UseServiceDiscovery();
});

return builder;
}

public static IHostApplicationBuilder ConfigureOpenTelemetry(this IHostApplicationBuilder builder)
{
builder.Logging.AddOpenTelemetry(logging =>
{
logging.IncludeFormattedMessage = true;
logging.IncludeScopes = true;
});

builder.Services.AddOpenTelemetry()
.WithMetrics(metrics =>
{
metrics.AddRuntimeInstrumentation()
.AddBuiltInMeters();
})
.WithTracing(tracing =>
{
if (builder.Environment.IsDevelopment())
{
// We want to view all traces in development
tracing.SetSampler(new AlwaysOnSampler());
}

tracing.AddAspNetCoreInstrumentation()
.AddGrpcClientInstrumentation()
.AddHttpClientInstrumentation()

// Add instrumentation for the AWS .NET SDK.
.AddAWSInstrumentation();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding the AWS .NET SDK Otel provider. Wonder how a component could possible add it without access to the TracerProviderBuilder.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding the AWS .NET SDK Otel provider. Wonder how a component could possible add it without access to the TracerProviderBuilder.

Multiple calls to AddOpenTelemetry().WithTracing(t => ...) compose. This is what our aspire components do.

});

builder.AddOpenTelemetryExporters();

return builder;
}

private static IHostApplicationBuilder AddOpenTelemetryExporters(this IHostApplicationBuilder builder)
{
var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]);

if (useOtlpExporter)
{
builder.Services.Configure<OpenTelemetryLoggerOptions>(logging => logging.AddOtlpExporter());
builder.Services.ConfigureOpenTelemetryMeterProvider(metrics => metrics.AddOtlpExporter());
builder.Services.ConfigureOpenTelemetryTracerProvider(tracing => tracing.AddOtlpExporter());
}

// Uncomment the following lines to enable the Prometheus exporter (requires the OpenTelemetry.Exporter.Prometheus.AspNetCore package)
// builder.Services.AddOpenTelemetry()
// .WithMetrics(metrics => metrics.AddPrometheusExporter());

// Uncomment the following lines to enable the Azure Monitor exporter (requires the Azure.Monitor.OpenTelemetry.Exporter package)
// builder.Services.AddOpenTelemetry()
// .UseAzureMonitor();

return builder;
}

public static IHostApplicationBuilder AddDefaultHealthChecks(this IHostApplicationBuilder builder)
{
builder.Services.AddHealthChecks()
// Add a default liveness check to ensure app is responsive
.AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]);

return builder;
}

public static WebApplication MapDefaultEndpoints(this WebApplication app)
{
// Uncomment the following line to enable the Prometheus endpoint (requires the OpenTelemetry.Exporter.Prometheus.AspNetCore package)
// app.MapPrometheusScrapingEndpoint();

// All health checks must pass for app to be considered ready to accept traffic after starting
app.MapHealthChecks("/health");

// Only health checks tagged with the "live" tag must pass for app to be considered alive
app.MapHealthChecks("/alive", new HealthCheckOptions
{
Predicate = r => r.Tags.Contains("live")
});

return app;
}

private static MeterProviderBuilder AddBuiltInMeters(this MeterProviderBuilder meterProviderBuilder) =>
meterProviderBuilder.AddMeter(
"Microsoft.AspNetCore.Hosting",
"Microsoft.AspNetCore.Server.Kestrel",
"System.Net.Http");
}
20 changes: 20 additions & 0 deletions samples/AWS/Frontend/Components/App.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<base href="/" />
<link rel="stylesheet" href="bootstrap/bootstrap.min.css" />
<link rel="stylesheet" href="app.css" />
<link rel="stylesheet" href="Frontend.styles.css" />
<link rel="icon" type="image/png" href="favicon.png" />
<HeadOutlet />
</head>

<body>
<Routes @rendermode="@InteractiveServer" />
<script src="_framework/blazor.web.js"></script>
</body>

</html>
Loading
Loading