Files
webapitest/SampleApi/Program.cs
T
Khwezi Mngoma 3572902300 GRPC switch
2026-05-02 12:47:13 +02:00

81 lines
2.4 KiB
C#

using OpenTelemetry.Exporter;
using OpenTelemetry.Logs;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using System.Diagnostics.Metrics;
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
var resourceBuilder = ResourceBuilder.CreateDefault()
.AddService("SampleApi");
// 2. Configuration Variables
// Using the API Key from your aspire-dashboard-auth secret
var oltpApiKey = "mc3G63K2j5ZOEsi0AjMojLTXm1KEZFctzIIjSwDiTGut8qGSkPuWwxGP1RbscJUo";
// 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.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// 3. Configure OpenTelemetry Logging
builder.Logging.AddOpenTelemetry(logging =>
{
logging.SetResourceBuilder(resourceBuilder);
logging.AddOtlpExporter(opt =>
{
opt.Endpoint = new Uri(oltpAddress);
opt.Protocol = OtlpExportProtocol.Grpc; // Switched to gRPC
// Note: Headers are only needed if you re-enable ApiKey auth mode
});
});
// 4. Configure Tracing and Metrics
builder.Services.AddOpenTelemetry()
.WithTracing(tracing => tracing
.SetResourceBuilder(resourceBuilder)
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddOtlpExporter(opt =>
{
opt.Endpoint = new Uri(oltpAddress);
opt.Protocol = OtlpExportProtocol.Grpc;
}))
.WithMetrics(metrics => metrics
.SetResourceBuilder(resourceBuilder)
.AddMeter("SampleApi")
.AddAspNetCoreInstrumentation()
.AddRuntimeInstrumentation()
.AddOtlpExporter(opt =>
{
opt.Endpoint = new Uri(oltpAddress);
opt.Protocol = OtlpExportProtocol.Grpc;
}));
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseRouting();
app.UseAuthorization();
app.MapControllers();
app.Run();
app.Run();