1 Commits

Author SHA1 Message Date
Khwezi Mngoma 3572902300 GRPC switch 2026-05-02 12:47:13 +02:00
+20 -15
View File
@@ -7,18 +7,29 @@ using System.Diagnostics.Metrics;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
// ONLY FOR TESTING: Allow untrusted certificates
var handler = new HttpClientHandler
{
ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true
};
builder.Services.AddHttpClient("OtlpClient").ConfigurePrimaryHttpMessageHandler(() => handler);
// 1. Define Resource Information // 1. Define Resource Information
var resourceBuilder = ResourceBuilder.CreateDefault() var resourceBuilder = ResourceBuilder.CreateDefault()
.AddService("SampleApi"); .AddService("SampleApi");
// 2. Configuration Variables // 2. Configuration Variables
// Using the API Key from your aspire-dashboard-auth secret
var oltpApiKey = "mc3G63K2j5ZOEsi0AjMojLTXm1KEZFctzIIjSwDiTGut8qGSkPuWwxGP1RbscJUo"; var oltpApiKey = "mc3G63K2j5ZOEsi0AjMojLTXm1KEZFctzIIjSwDiTGut8qGSkPuWwxGP1RbscJUo";
var oltpAddress = "http://aspire-dashboard-service.aspire.svc.cluster.local:18889";
// Pointing to your Traefik IngressRoute.
// The SDK will append /v1/traces, /v1/metrics, etc., to this base URL
var oltpAddress = "https://aspire.khongisa.co.za/otlp-grpc";
//var oltpAddress = "https://aspire.khongisa.co.za/otlp-http";
builder.Services.AddControllers(); builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer(); builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(); builder.Services.AddSwaggerGen();
builder.Services.AddHealthChecks();
// 3. Configure OpenTelemetry Logging // 3. Configure OpenTelemetry Logging
builder.Logging.AddOpenTelemetry(logging => builder.Logging.AddOpenTelemetry(logging =>
@@ -27,50 +38,44 @@ builder.Logging.AddOpenTelemetry(logging =>
logging.AddOtlpExporter(opt => logging.AddOtlpExporter(opt =>
{ {
opt.Endpoint = new Uri(oltpAddress); opt.Endpoint = new Uri(oltpAddress);
opt.Protocol = OtlpExportProtocol.Grpc; opt.Protocol = OtlpExportProtocol.Grpc; // Switched to gRPC
opt.Headers = $"x-otlp-api-key={oltpApiKey}"; // Note: Headers are only needed if you re-enable ApiKey auth mode
}); });
}); });
// 4. Configure Tracing and Metrics (Merged into one registration) // 4. Configure Tracing and Metrics
builder.Services.AddOpenTelemetry() builder.Services.AddOpenTelemetry()
.WithTracing(tracing => tracing .WithTracing(tracing => tracing
.SetResourceBuilder(resourceBuilder) .SetResourceBuilder(resourceBuilder)
.AddAspNetCoreInstrumentation() .AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation() .AddHttpClientInstrumentation()
.AddOtlpExporter(opt => .AddOtlpExporter(opt =>
{ {
opt.Endpoint = new Uri(oltpAddress); opt.Endpoint = new Uri(oltpAddress);
opt.Protocol = OtlpExportProtocol.Grpc; opt.Protocol = OtlpExportProtocol.Grpc;
opt.Headers = $"x-otlp-api-key={oltpApiKey}";
})) }))
.WithMetrics(metrics => metrics .WithMetrics(metrics => metrics
.SetResourceBuilder(resourceBuilder) .SetResourceBuilder(resourceBuilder)
.AddMeter("SampleApi") // Register your custom meter here .AddMeter("SampleApi")
.AddAspNetCoreInstrumentation() .AddAspNetCoreInstrumentation()
.AddRuntimeInstrumentation() .AddRuntimeInstrumentation()
.AddOtlpExporter(opt => .AddOtlpExporter(opt =>
{ {
opt.Endpoint = new Uri(oltpAddress); opt.Endpoint = new Uri(oltpAddress);
opt.Protocol = OtlpExportProtocol.Grpc; opt.Protocol = OtlpExportProtocol.Grpc;
opt.Headers = $"x-otlp-api-key={oltpApiKey}";
})); }));
var app = builder.Build(); var app = builder.Build();
// 5. Initialize your Meter for use in the app
var myMeter = new Meter("SampleApi", "1.0.0");
var transactionCounter = myMeter.CreateCounter<long>("CustomCounter");
if (app.Environment.IsDevelopment()) if (app.Environment.IsDevelopment())
{ {
app.UseSwagger(); app.UseSwagger();
app.UseSwaggerUI(); app.UseSwaggerUI();
} }
app.MapHealthChecks("/health");
app.UseRouting(); app.UseRouting();
app.UseAuthorization(); app.UseAuthorization();
app.MapControllers(); app.MapControllers();
app.Run();
app.Run(); app.Run();