add telemetry
This commit is contained in:
@@ -7,10 +7,11 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.3" />
|
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.4" />
|
||||||
<PackageReference Include="MaybeError" Version="1.0.5" />
|
<PackageReference Include="MaybeError" Version="1.0.6" />
|
||||||
<PackageReference Include="MongoDB.Analyzer" Version="1.5.0" />
|
<PackageReference Include="MongoDB.Analyzer" Version="1.5.0" />
|
||||||
<PackageReference Include="MongoDB.Driver" Version="2.30.0" />
|
<PackageReference Include="MongoDB.Driver" Version="3.3.0" />
|
||||||
|
<PackageReference Include="MongoDB.Driver.Core.Extensions.DiagnosticSources" Version="2.0.0" />
|
||||||
<PackageReference Include="MongoDB.Driver.GridFS" Version="2.30.0" />
|
<PackageReference Include="MongoDB.Driver.GridFS" Version="2.30.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
using MongoDB.Driver;
|
using MongoDB.Driver;
|
||||||
|
using MongoDB.Driver.Core.Extensions.DiagnosticSources;
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
@@ -14,7 +15,9 @@ public static class Extensions
|
|||||||
{
|
{
|
||||||
public static IServiceCollection AddAoba(this IServiceCollection services)
|
public static IServiceCollection AddAoba(this IServiceCollection services)
|
||||||
{
|
{
|
||||||
var dbClient = new MongoClient("mongodb://NinoIna:27017");
|
var settings = MongoClientSettings.FromConnectionString("mongodb://NinoIna:27017");
|
||||||
|
settings.ClusterConfigurator = cb => cb.Subscribe(new DiagnosticsActivityEventSubscriber());
|
||||||
|
var dbClient = new MongoClient(settings);
|
||||||
var db = dbClient.GetDatabase("Aoba");
|
var db = dbClient.GetDatabase("Aoba");
|
||||||
|
|
||||||
services.AddSingleton(dbClient);
|
services.AddSingleton(dbClient);
|
||||||
|
|||||||
@@ -15,6 +15,11 @@
|
|||||||
<PackageReference Include="Microsoft.IdentityModel.JsonWebTokens" Version="8.8.0" />
|
<PackageReference Include="Microsoft.IdentityModel.JsonWebTokens" Version="8.8.0" />
|
||||||
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.2" />
|
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.2" />
|
||||||
<PackageReference Include="MimeTypesMap" Version="1.0.9" />
|
<PackageReference Include="MimeTypesMap" Version="1.0.9" />
|
||||||
|
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.9.0" />
|
||||||
|
<PackageReference Include="OpenTelemetry.Exporter.Prometheus.AspNetCore" Version="1.9.0-beta.2" />
|
||||||
|
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.9.0" />
|
||||||
|
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.9.0" />
|
||||||
|
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.9.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ using Microsoft.Extensions.Options;
|
|||||||
|
|
||||||
using System.Text.Encodings.Web;
|
using System.Text.Encodings.Web;
|
||||||
|
|
||||||
namespace AobaServer;
|
namespace AobaServer.Auth;
|
||||||
|
|
||||||
internal class AobaAuthenticationHandler(IOptionsMonitor<AuthenticationSchemeOptions> options, ILoggerFactory logger, UrlEncoder encoder) : AuthenticationHandler<AuthenticationSchemeOptions>(options, logger, encoder)
|
internal class AobaAuthenticationHandler(IOptionsMonitor<AuthenticationSchemeOptions> options, ILoggerFactory logger, UrlEncoder encoder) : AuthenticationHandler<AuthenticationSchemeOptions>(options, logger, encoder)
|
||||||
{
|
{
|
||||||
42
AobaServer/Auth/MetricsTokenValidator.cs
Normal file
42
AobaServer/Auth/MetricsTokenValidator.cs
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
using Microsoft.IdentityModel.Tokens;
|
||||||
|
|
||||||
|
using System.IdentityModel.Tokens.Jwt;
|
||||||
|
using System.Security.Claims;
|
||||||
|
|
||||||
|
namespace AobaServer.Auth;
|
||||||
|
|
||||||
|
public class MetricsTokenValidator(AuthInfo authInfo) : JwtSecurityTokenHandler
|
||||||
|
{
|
||||||
|
private readonly JwtSecurityTokenHandler _handler = new();
|
||||||
|
public override Task<TokenValidationResult> ValidateTokenAsync(string token, TokenValidationParameters validationParameters)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var principal = _handler.ValidateToken(token, new TokenValidationParameters
|
||||||
|
{
|
||||||
|
ValidateIssuerSigningKey = true,
|
||||||
|
IssuerSigningKey = new SymmetricSecurityKey(authInfo.SecureKey),
|
||||||
|
ValidateIssuer = true,
|
||||||
|
ValidIssuer = authInfo.Issuer,
|
||||||
|
ValidateAudience = true,
|
||||||
|
ValidAudience = "metrics",
|
||||||
|
ValidateLifetime = false,
|
||||||
|
ClockSkew = TimeSpan.FromMinutes(1)
|
||||||
|
}, out var validatedToken);
|
||||||
|
return Task.FromResult(new TokenValidationResult
|
||||||
|
{
|
||||||
|
IsValid = true,
|
||||||
|
SecurityToken = validatedToken,
|
||||||
|
ClaimsIdentity = new ClaimsIdentity(principal.Identity),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
return Task.FromResult(new TokenValidationResult
|
||||||
|
{
|
||||||
|
IsValid = false,
|
||||||
|
Exception = e
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
73
AobaServer/Middleware/OpenTelemetry.cs
Normal file
73
AobaServer/Middleware/OpenTelemetry.cs
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
#nullable enable
|
||||||
|
|
||||||
|
using AobaServer.Middleware;
|
||||||
|
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.AspNetCore.Routing;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
|
using OpenTelemetry;
|
||||||
|
using OpenTelemetry.Metrics;
|
||||||
|
using OpenTelemetry.Resources;
|
||||||
|
using OpenTelemetry.Trace;
|
||||||
|
|
||||||
|
|
||||||
|
namespace AobaServer.Middleware;
|
||||||
|
|
||||||
|
public static class OpenTelemetry
|
||||||
|
{
|
||||||
|
public static void AddObersability(this IServiceCollection services, IConfiguration configuration)
|
||||||
|
{
|
||||||
|
var otel = services.AddOpenTelemetry();
|
||||||
|
|
||||||
|
otel.ConfigureResource(res =>
|
||||||
|
{
|
||||||
|
res.AddService(serviceName: $"Breeze: {Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}");
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
// Add Metrics for ASP.NET Core and our custom metrics and export to Prometheus
|
||||||
|
otel.WithMetrics(metrics => metrics
|
||||||
|
// Metrics provider from OpenTelemetry
|
||||||
|
.AddAspNetCoreInstrumentation()
|
||||||
|
.AddCustomMetrics()
|
||||||
|
// Metrics provides by ASP.NET Core in .NET 8
|
||||||
|
.AddMeter("Microsoft.AspNetCore.Hosting")
|
||||||
|
.AddMeter("Microsoft.AspNetCore.Server.Kestrel")
|
||||||
|
// Metrics provided by System.Net libraries
|
||||||
|
.AddMeter("System.Net.Http")
|
||||||
|
.AddMeter("System.Net.NameResolution")
|
||||||
|
.AddMeter("MongoDB.Driver.Core.Extensions.DiagnosticSources")
|
||||||
|
.AddPrometheusExporter());
|
||||||
|
|
||||||
|
// Add Tracing for ASP.NET Core and our custom ActivitySource and export to Jaeger
|
||||||
|
var tracingOtlpEndpoint = configuration["OTLP_ENDPOINT_URL"];
|
||||||
|
otel.WithTracing(tracing =>
|
||||||
|
{
|
||||||
|
tracing.AddAspNetCoreInstrumentation();
|
||||||
|
tracing.AddHttpClientInstrumentation();
|
||||||
|
if (!string.IsNullOrWhiteSpace(tracingOtlpEndpoint))
|
||||||
|
{
|
||||||
|
tracing.AddOtlpExporter(otlpOptions =>
|
||||||
|
{
|
||||||
|
otlpOptions.Endpoint = new Uri(tracingOtlpEndpoint);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static MeterProviderBuilder AddCustomMetrics(this MeterProviderBuilder builder)
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
return builder;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IEndpointRouteBuilder MapObserability(this IEndpointRouteBuilder endpoints)
|
||||||
|
{
|
||||||
|
endpoints.MapPrometheusScrapingEndpoint().RequireAuthorization(p => p.RequireRole("metrics"));
|
||||||
|
return endpoints;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
using AobaCore;
|
using AobaCore;
|
||||||
|
|
||||||
using AobaServer;
|
using AobaServer;
|
||||||
|
using AobaServer.Auth;
|
||||||
|
using AobaServer.Middleware;
|
||||||
using AobaServer.Models;
|
using AobaServer.Models;
|
||||||
|
|
||||||
using Microsoft.AspNetCore.Authentication;
|
using Microsoft.AspNetCore.Authentication;
|
||||||
@@ -13,6 +15,7 @@ var builder = WebApplication.CreateBuilder(args);
|
|||||||
// Add services to the container.
|
// Add services to the container.
|
||||||
builder.Services.AddControllers(opt => opt.ModelBinderProviders.Add(new BsonIdModelBinderProvider()));
|
builder.Services.AddControllers(opt => opt.ModelBinderProviders.Add(new BsonIdModelBinderProvider()));
|
||||||
|
|
||||||
|
builder.Services.AddObersability(builder.Configuration);
|
||||||
|
|
||||||
var authInfo = AuthInfo.LoadOrCreate("Auth.json", "aobaV2", "aoba");
|
var authInfo = AuthInfo.LoadOrCreate("Auth.json", "aobaV2", "aoba");
|
||||||
builder.Services.AddSingleton(authInfo);
|
builder.Services.AddSingleton(authInfo);
|
||||||
@@ -37,6 +40,7 @@ builder.Services.AddAuthentication(options =>
|
|||||||
}).AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options => //Bearer auth
|
}).AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options => //Bearer auth
|
||||||
{
|
{
|
||||||
options.TokenValidationParameters = validationParams;
|
options.TokenValidationParameters = validationParams;
|
||||||
|
options.TokenHandlers.Add(new MetricsTokenValidator(authInfo));
|
||||||
options.Events = new JwtBearerEvents
|
options.Events = new JwtBearerEvents
|
||||||
{
|
{
|
||||||
OnMessageReceived = ctx => //Retreive token from cookie if not found in headers
|
OnMessageReceived = ctx => //Retreive token from cookie if not found in headers
|
||||||
@@ -87,6 +91,7 @@ app.UseAuthorization();
|
|||||||
|
|
||||||
|
|
||||||
app.MapControllers();
|
app.MapControllers();
|
||||||
|
app.MapObserability();
|
||||||
|
|
||||||
|
|
||||||
app.Run();
|
app.Run();
|
||||||
|
|||||||
Reference in New Issue
Block a user