1
Из того, что я понимаю из дефицитной документации, я должен добавить что -то подобное в запрос:
Код: Выделить всё
var httpClient = new HttpClient(new HttpClientXRayTracingHandler(new HttpClientHandler()));
httpClient.GetAsync(URL);
Это мой код в программе:
const string HealthCheckEndpoint = "api/health";
var builder = WebApplication.CreateBuilder(args);
AWSXRayRecorder.InitializeInstance(builder.Configuration);
builder.Configuration
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"ocelot.{builder.Environment.EnvironmentName}.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables();
builder.Services.AddSwaggerForOcelot(builder.Configuration);
builder.Services.AddOcelot(builder.Configuration)
.AddDelegatingHandler(true);
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo
{
Title = "Template Gateway API",
Version = "v1",
});
});
builder.Services.AddHealthChecks();
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
var app = builder.Build();
var options = new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedProto,
ForwardLimit = 1 // Only trust the first proxy (ALB)
};
app.UseForwardedHeaders(options);
app.UseRouting();
app.MapHealthChecks(HealthCheckEndpoint);
app.UseXRay("Template-Gateway");
app.UseAuthorization();
//this and use routing is for the health check endpoint bypass ocelot conf.
app.UseEndpoints(e =>
{
e.MapControllers();
});
if (app.Environment.IsDevelopment())
{
app.UseSwaggerForOcelotUI(options =>
{
options.PathToSwaggerGenerator = "/swagger/docs";
});
}
await app.UseOcelot();
app.Run();
< /code>
Это делегат Ocelot: < /p>
Код: Выделить всё
public class XRayTracingHandler : DelegatingHandler
{
private readonly ILogger _logger;
public XRayTracingHandler(ILogger logger)
: base(new HttpClientXRayTracingHandler(new HttpClientHandler()))
{
_logger = logger;
}
protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
// Log the request before sending
_logger.LogInformation("XRayTracingHandler - Sending request to {Url}", request.RequestUri);
// Check if the X-Amzn-Trace-Id header is set
if (request.Headers.Contains("X-Amzn-Trace-Id"))
{
string traceId = request.Headers.GetValues("X-Amzn-Trace-Id").FirstOrDefault() ?? "Not Found";
_logger.LogInformation("XRayTracingHandler - X-Amzn-Trace-Id: {TraceId}", traceId);
}
else
{
_logger.LogWarning("XRayTracingHandler - X-Amzn-Trace-Id header is missing!");
}
var response = await base.SendAsync(request, cancellationToken);
// Log the response status code
_logger.LogInformation("XRayTracingHandler - Response Status Code: {StatusCode}", response.StatusCode);
return response;
}
}
< /code>
Это глобальная конфигурация, которую я добавил в ocelot.json: < /p>
"GlobalConfiguration": {
"BaseUrl": "https://test.ats.mytalentaid.com",
"HttpHeadersToPass": [
"X-Amzn-Trace-Id",
"Authorization",
"Content-Type"
]
},
"DelegatingHandlers": [
"XRayTracingHandler"
]
< /code>
Любая идея, что я могу сделать неправильно или что мне не хватает?2025-03-07T16:16:57.663Z
info: Microsoft.Hosting.Lifetime[14]
2025-03-07T16:16:57.663Z
Now listening on: http://[::]:5000
2025-03-07T16:16:57.665Z
info: Microsoft.Hosting.Lifetime[0]
2025-03-07T16:16:57.665Z
Application started. Press Ctrl+C to shut down.
2025-03-07T16:16:57.666Z
info: Microsoft.Hosting.Lifetime[0]
2025-03-07T16:16:57.666Z
Hosting environment: Test
2025-03-07T16:16:57.666Z
info: Microsoft.Hosting.Lifetime[0]
2025-03-07T16:16:57.666Z
Content root path: /app
2025-03-07T16:21:03.785Z
info: Ocelot.RateLimiting.Middleware.RateLimitingMiddleware[0]
2025-03-07T16:21:03.785Z
requestId: 0HNATIE349S20:00000001, previousRequestId: No PreviousRequestId, message: 'EndpointRateLimiting is not enabled for /api/template'
2025-03-07T16:21:03.793Z
info: Ocelot.Authentication.Middleware.AuthenticationMiddleware[0]
2025-03-07T16:21:03.793Z
requestId: 0HNATIE349S20:00000001, previousRequestId: No PreviousRequestId, message: 'No authentication needed for path '/ats/template'.'
2025-03-07T16:21:03.797Z
info: Ocelot.Authorization.Middleware.AuthorizationMiddleware[0]
2025-03-07T16:21:03.797Z
requestId: 0HNATIE349S20:00000001, previousRequestId: No PreviousRequestId, message: '/api/template route does not require user to be authorized'
2025-03-07T16:21:03.876Z
info: ATS.Template.Gateway.Handlers.XRayTracingHandler[0]
2025-03-07T16:21:03.876Z
XRayTracingHandler - Sending request to http://mta-template-write-api.ats.local:5001/api/template
2025-03-07T16:21:03.876Z
warn: ATS.Template.Gateway.Handlers.XRayTracingHandler[0]
2025-03-07T16:21:03.876Z
XRayTracingHandler - X-Amzn-Trace-Id header is missing!
2025-03-07T16:21:04.116Z
info: ATS.Template.Gateway.Handlers.XRayTracingHandler[0]
2025-03-07T16:21:04.116Z
XRayTracingHandler - Response Status Code: OK
2025-03-07T16:21:04.118Z
info: Ocelot.Requester.Middleware.HttpRequesterMiddleware[0]
2025-03-07T16:21:04.118Z
requestId: 0HNATIE349S20:00000001, previousRequestId: No PreviousRequestId, message: '200 (OK) status code of request URI: http://mta-template-write-api.ats.local:5001/api/template.'
Подробнее здесь: https://stackoverflow.com/questions/794 ... ssue-net-8