Skip to content

Commit e83d6a2

Browse files
committed
add servicedefaults package
1 parent 9e2315e commit e83d6a2

File tree

3 files changed

+144
-0
lines changed

3 files changed

+144
-0
lines changed

dotnet/AutoGen.sln

+7
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HelloAgents.Web", "samples\
127127
EndProject
128128
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Hello", "samples\Hello\Hello.csproj", "{6C9135E6-9D15-4D86-B3F4-9666DB87060A}"
129129
EndProject
130+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.Autogen.ServiceDefaults", "src\Microsoft.Autogen.ServiceDefaults\Microsoft.Autogen.ServiceDefaults.csproj", "{F70C6FD7-9615-4EDD-8D55-5460FCC5A46D}"
131+
EndProject
130132
Global
131133
GlobalSection(SolutionConfigurationPlatforms) = preSolution
132134
Debug|Any CPU = Debug|Any CPU
@@ -337,6 +339,10 @@ Global
337339
{6C9135E6-9D15-4D86-B3F4-9666DB87060A}.Debug|Any CPU.Build.0 = Debug|Any CPU
338340
{6C9135E6-9D15-4D86-B3F4-9666DB87060A}.Release|Any CPU.ActiveCfg = Release|Any CPU
339341
{6C9135E6-9D15-4D86-B3F4-9666DB87060A}.Release|Any CPU.Build.0 = Release|Any CPU
342+
{F70C6FD7-9615-4EDD-8D55-5460FCC5A46D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
343+
{F70C6FD7-9615-4EDD-8D55-5460FCC5A46D}.Debug|Any CPU.Build.0 = Debug|Any CPU
344+
{F70C6FD7-9615-4EDD-8D55-5460FCC5A46D}.Release|Any CPU.ActiveCfg = Release|Any CPU
345+
{F70C6FD7-9615-4EDD-8D55-5460FCC5A46D}.Release|Any CPU.Build.0 = Release|Any CPU
340346
EndGlobalSection
341347
GlobalSection(SolutionProperties) = preSolution
342348
HideSolutionNode = FALSE
@@ -397,6 +403,7 @@ Global
397403
{6B88F4B3-26AB-4034-B0AC-5BA6EEDEB8E5} = {F7AC0FF1-8500-49C6-8CB3-97C6D52C8BEF}
398404
{8B56BE22-5CF4-44BB-AFA5-732FEA2AFF0B} = {F7AC0FF1-8500-49C6-8CB3-97C6D52C8BEF}
399405
{6C9135E6-9D15-4D86-B3F4-9666DB87060A} = {FBFEAD1F-29EB-4D99-A672-0CD8473E10B9}
406+
{F70C6FD7-9615-4EDD-8D55-5460FCC5A46D} = {18BF8DD7-0585-48BF-8F97-AD333080CE06}
400407
EndGlobalSection
401408
GlobalSection(ExtensibilityGlobals) = postSolution
402409
SolutionGuid = {93384647-528D-46C8-922C-8DB36A382F0B}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
using Microsoft.AspNetCore.Builder;
2+
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using Microsoft.Extensions.Diagnostics.HealthChecks;
5+
using Microsoft.Extensions.Logging;
6+
using OpenTelemetry;
7+
using OpenTelemetry.Metrics;
8+
using OpenTelemetry.Trace;
9+
10+
namespace Microsoft.Extensions.Hosting;
11+
12+
// Adds common .NET Aspire services: service discovery, resilience, health checks, and OpenTelemetry.
13+
// This project should be referenced by each service project in your solution.
14+
// To learn more about using this project, see https://aka.ms/dotnet/aspire/service-defaults
15+
public static class Extensions
16+
{
17+
public static IHostApplicationBuilder AddServiceDefaults(this IHostApplicationBuilder builder)
18+
{
19+
builder.ConfigureOpenTelemetry();
20+
21+
builder.AddDefaultHealthChecks();
22+
23+
builder.Services.AddServiceDiscovery();
24+
25+
builder.Services.ConfigureHttpClientDefaults(http =>
26+
{
27+
// Turn on resilience by default
28+
http.AddStandardResilienceHandler();
29+
30+
// Turn on service discovery by default
31+
http.AddServiceDiscovery();
32+
});
33+
34+
// Uncomment the following to restrict the allowed schemes for service discovery.
35+
// builder.Services.Configure<ServiceDiscoveryOptions>(options =>
36+
// {
37+
// options.AllowedSchemes = ["https"];
38+
// });
39+
40+
return builder;
41+
}
42+
43+
public static IHostApplicationBuilder ConfigureOpenTelemetry(this IHostApplicationBuilder builder)
44+
{
45+
builder.Logging.AddOpenTelemetry(logging =>
46+
{
47+
logging.IncludeFormattedMessage = true;
48+
logging.IncludeScopes = true;
49+
});
50+
51+
builder.Services.AddOpenTelemetry()
52+
.WithMetrics(metrics =>
53+
{
54+
metrics.AddAspNetCoreInstrumentation()
55+
.AddHttpClientInstrumentation()
56+
.AddRuntimeInstrumentation()
57+
.AddMeter("Microsoft.Orleans");
58+
})
59+
.WithTracing(tracing =>
60+
{
61+
tracing.AddAspNetCoreInstrumentation()
62+
//.AddGrpcClientInstrumentation()
63+
.AddHttpClientInstrumentation()
64+
.AddSource("Microsoft.Orleans.Application")
65+
.AddSource("AutoGen.Agent");
66+
});
67+
68+
builder.AddOpenTelemetryExporters();
69+
70+
return builder;
71+
}
72+
73+
private static IHostApplicationBuilder AddOpenTelemetryExporters(this IHostApplicationBuilder builder)
74+
{
75+
var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]);
76+
77+
if (useOtlpExporter)
78+
{
79+
builder.Services.AddOpenTelemetry().UseOtlpExporter();
80+
}
81+
82+
// Uncomment the following lines to enable the Azure Monitor exporter (requires the Azure.Monitor.OpenTelemetry.AspNetCore package)
83+
//if (!string.IsNullOrEmpty(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"]))
84+
//{
85+
// builder.Services.AddOpenTelemetry()
86+
// .UseAzureMonitor();
87+
//}
88+
89+
return builder;
90+
}
91+
92+
public static IHostApplicationBuilder AddDefaultHealthChecks(this IHostApplicationBuilder builder)
93+
{
94+
builder.Services.AddHealthChecks()
95+
// Add a default liveness check to ensure app is responsive
96+
.AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]);
97+
98+
return builder;
99+
}
100+
101+
public static WebApplication MapDefaultEndpoints(this WebApplication app)
102+
{
103+
// Adding health checks endpoints to applications in non-development environments has security implications.
104+
// See https://aka.ms/dotnet/aspire/healthchecks for details before enabling these endpoints in non-development environments.
105+
if (app.Environment.IsDevelopment())
106+
{
107+
// All health checks must pass for app to be considered ready to accept traffic after starting
108+
app.MapHealthChecks("/health");
109+
110+
// Only health checks tagged with the "live" tag must pass for app to be considered alive
111+
app.MapHealthChecks("/alive", new HealthCheckOptions
112+
{
113+
Predicate = r => r.Tags.Contains("live")
114+
});
115+
}
116+
117+
return app;
118+
}
119+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net8.0</TargetFramework>
4+
<ImplicitUsings>enable</ImplicitUsings>
5+
<Nullable>enable</Nullable>
6+
<IsAspireSharedProject>true</IsAspireSharedProject>
7+
</PropertyGroup>
8+
<ItemGroup>
9+
<FrameworkReference Include="Microsoft.AspNetCore.App" />
10+
<PackageReference Include="Microsoft.Extensions.Http.Resilience" />
11+
<PackageReference Include="Microsoft.Extensions.ServiceDiscovery" />
12+
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" />
13+
<PackageReference Include="OpenTelemetry.Extensions.Hosting" />
14+
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" />
15+
<PackageReference Include="OpenTelemetry.Instrumentation.Http" />
16+
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" />
17+
</ItemGroup>
18+
</Project>

0 commit comments

Comments
 (0)