-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Not all data seems to be flowing but now this uses otel.
- Loading branch information
Showing
9 changed files
with
226 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
src/BlazorPong.Web/Server/WebApplicationBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
using Microsoft.AspNetCore.Diagnostics.HealthChecks; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
using OpenTelemetry.Logs; | ||
using OpenTelemetry.Metrics; | ||
using OpenTelemetry.Resources; | ||
using OpenTelemetry.Trace; | ||
|
||
namespace BlazorPong.Web.Server; | ||
|
||
public static class WebApplicationBuilderExtensions | ||
{ | ||
public static IHostApplicationBuilder AddServiceDefaults(this IHostApplicationBuilder builder) | ||
{ | ||
builder.ConfigureOpenTelemetry(); | ||
|
||
builder.AddDefaultHealthChecks(); | ||
|
||
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(); | ||
}); | ||
|
||
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()); | ||
} | ||
|
||
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) | ||
{ | ||
// 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"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.