mirror of
https://github.com/bitwarden/server
synced 2026-01-28 15:23:38 +00:00
198 lines
8.6 KiB
C#
198 lines
8.6 KiB
C#
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.Extensions.Logging;
|
|
using OpenTelemetry;
|
|
using OpenTelemetry.Metrics;
|
|
using OpenTelemetry.Trace;
|
|
|
|
namespace Bit.ServiceDefaults;
|
|
|
|
// Adds common .NET Aspire services: service discovery, resilience, health checks, and OpenTelemetry.
|
|
// This project should be referenced by each service project in your solution.
|
|
// To learn more about using this project, see https://aka.ms/dotnet/aspire/service-defaults
|
|
public static class Extensions
|
|
{
|
|
private const string HealthEndpointPath = "/health";
|
|
private const string AlivenessEndpointPath = "/alive";
|
|
|
|
public static TBuilder AddServiceDefaults<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
|
|
{
|
|
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.AddServiceDiscovery();
|
|
});
|
|
|
|
// Uncomment the following to restrict the allowed schemes for service discovery.
|
|
// builder.Services.Configure<ServiceDiscoveryOptions>(options =>
|
|
// {
|
|
// options.AllowedSchemes = ["https"];
|
|
// });
|
|
|
|
return builder;
|
|
}
|
|
|
|
public static TBuilder ConfigureOpenTelemetry<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
|
|
{
|
|
builder.Logging.AddOpenTelemetry(logging =>
|
|
{
|
|
logging.IncludeFormattedMessage = true;
|
|
logging.IncludeScopes = true;
|
|
});
|
|
|
|
builder.Services.AddOpenTelemetry()
|
|
.WithMetrics(metrics =>
|
|
{
|
|
metrics.AddAspNetCoreInstrumentation()
|
|
.AddHttpClientInstrumentation()
|
|
.AddRuntimeInstrumentation();
|
|
})
|
|
.WithTracing(tracing =>
|
|
{
|
|
tracing.AddSource(builder.Environment.ApplicationName)
|
|
.AddAspNetCoreInstrumentation(tracing =>
|
|
// Exclude health check requests from tracing
|
|
tracing.Filter = context =>
|
|
!context.Request.Path.StartsWithSegments(HealthEndpointPath)
|
|
&& !context.Request.Path.StartsWithSegments(AlivenessEndpointPath)
|
|
)
|
|
// Uncomment the following line to enable gRPC instrumentation (requires the OpenTelemetry.Instrumentation.GrpcNetClient package)
|
|
//.AddGrpcClientInstrumentation()
|
|
.AddHttpClientInstrumentation();
|
|
});
|
|
|
|
builder.AddOpenTelemetryExporters();
|
|
|
|
return builder;
|
|
}
|
|
|
|
private static TBuilder AddOpenTelemetryExporters<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
|
|
{
|
|
var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]);
|
|
|
|
if (useOtlpExporter)
|
|
{
|
|
builder.Services.AddOpenTelemetry().UseOtlpExporter();
|
|
}
|
|
|
|
// Uncomment the following lines to enable the Azure Monitor exporter (requires the Azure.Monitor.OpenTelemetry.AspNetCore package)
|
|
//if (!string.IsNullOrEmpty(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"]))
|
|
//{
|
|
// builder.Services.AddOpenTelemetry()
|
|
// .UseAzureMonitor();
|
|
//}
|
|
|
|
return builder;
|
|
}
|
|
|
|
public static TBuilder AddDefaultHealthChecks<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
|
|
{
|
|
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)
|
|
{
|
|
// Adding health checks endpoints to applications in non-development environments has security implications.
|
|
// See https://aka.ms/dotnet/aspire/healthchecks for details before enabling these endpoints in non-development environments.
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
// All health checks must pass for app to be considered ready to accept traffic after starting
|
|
app.MapHealthChecks(HealthEndpointPath);
|
|
|
|
// Only health checks tagged with the "live" tag must pass for app to be considered alive
|
|
app.MapHealthChecks(AlivenessEndpointPath, new HealthCheckOptions
|
|
{
|
|
Predicate = r => r.Tags.Contains("live")
|
|
});
|
|
}
|
|
|
|
return app;
|
|
}
|
|
|
|
// Overload for IHostBuilder
|
|
public static IHostBuilder AddServiceDefaults(this IHostBuilder hostBuilder)
|
|
{
|
|
hostBuilder.ConfigureServices((context, services) =>
|
|
{
|
|
if (context.HostingEnvironment.IsDevelopment())
|
|
{
|
|
services.AddHealthChecks()
|
|
.AddCheck("self", () => HealthCheckResult.Healthy(), new[] { "live" });
|
|
}
|
|
|
|
services.AddServiceDiscovery();
|
|
services.ConfigureHttpClientDefaults(http =>
|
|
{
|
|
http.AddStandardResilienceHandler();
|
|
http.AddServiceDiscovery();
|
|
});
|
|
services.AddOpenTelemetry()
|
|
.WithMetrics(metrics =>
|
|
{
|
|
metrics.AddAspNetCoreInstrumentation()
|
|
.AddHttpClientInstrumentation()
|
|
.AddRuntimeInstrumentation();
|
|
})
|
|
.WithTracing(tracing =>
|
|
{
|
|
tracing.AddSource(context.HostingEnvironment.ApplicationName)
|
|
.AddAspNetCoreInstrumentation(tracing =>
|
|
tracing.Filter = context =>
|
|
!context.Request.Path.StartsWithSegments(HealthEndpointPath)
|
|
&& !context.Request.Path.StartsWithSegments(AlivenessEndpointPath)
|
|
)
|
|
.AddHttpClientInstrumentation(httpClient =>
|
|
{
|
|
httpClient.EnrichWithHttpRequestMessage = (activity, message) =>
|
|
{
|
|
if (context.HostingEnvironment.IsDevelopment())
|
|
{
|
|
activity.SetTag("http.request_content_length", message.Content?.Headers.ContentLength);
|
|
activity.SetTag("http.request_method", message.Method.Method);
|
|
activity.SetTag("http.request_url", message.RequestUri?.ToString());
|
|
activity.SetTag("http.request_message_headers", message.Headers.ToString());
|
|
activity.SetTag("http.request_body", message.Content?.ReadAsStringAsync().Result);
|
|
}
|
|
};
|
|
httpClient.EnrichWithHttpResponseMessage = (activity, message) =>
|
|
{
|
|
if (context.HostingEnvironment.IsDevelopment())
|
|
{
|
|
activity.SetTag("http.response_content_length",
|
|
message.Content.Headers.ContentLength);
|
|
activity.SetTag("http.response_status_code", (int)message.StatusCode);
|
|
activity.SetTag("http.response_status_text", message.ReasonPhrase);
|
|
activity.SetTag("http.response_content_type",
|
|
message.Content.Headers.ContentType?.MediaType);
|
|
activity.SetTag("http.response_message_headers", message.Headers.ToString());
|
|
activity.SetTag("http.response_body", message.Content.ReadAsStringAsync().Result);
|
|
}
|
|
};
|
|
});
|
|
});
|
|
var useOtlpExporter = !string.IsNullOrWhiteSpace(context.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]);
|
|
if (useOtlpExporter)
|
|
{
|
|
services.AddOpenTelemetry().UseOtlpExporter();
|
|
}
|
|
});
|
|
return hostBuilder;
|
|
}
|
|
}
|