Skip to content

Commit 918b97e

Browse files
authored
Add debugging logs for lambda API (#2048)
1 parent d68582c commit 918b97e

File tree

3 files changed

+90
-18
lines changed

3 files changed

+90
-18
lines changed

src/api/Elastic.Documentation.Api.Infrastructure/Adapters/AskAi/LlmGatewayAskAiGateway.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,20 @@ public async Task<Stream> AskAi(AskAiRequest askAiRequest, Cancel ctx = default)
2525
request.Headers.Add("User-Agent", "elastic-docs-proxy/1.0");
2626
request.Headers.Add("Accept", "text/event-stream");
2727
request.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
28+
29+
// Use HttpCompletionOption.ResponseHeadersRead to get headers immediately
30+
// This allows us to start streaming as soon as headers are received
2831
var response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, ctx);
32+
33+
// Ensure the response is successful before streaming
34+
if (!response.IsSuccessStatusCode)
35+
{
36+
var errorContent = await response.Content.ReadAsStringAsync(ctx);
37+
throw new HttpRequestException($"LLM Gateway returned {response.StatusCode}: {errorContent}");
38+
}
39+
40+
// Return the response stream directly - this enables true streaming
41+
// The stream will be consumed as data arrives from the LLM Gateway
2942
return await response.Content.ReadAsStreamAsync(ctx);
3043
}
3144
}

src/api/Elastic.Documentation.Api.Infrastructure/ServicesExtension.cs

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,23 @@ private static void AddParameterProvider(IServiceCollection services, AppEnv app
7878
case AppEnv.Edge:
7979
{
8080
logger?.LogInformation("Configuring LambdaExtensionParameterProvider for environment {AppEnvironment}", appEnv);
81-
_ = services.AddHttpClient(LambdaExtensionParameterProvider.HttpClientName, client =>
81+
try
8282
{
83-
client.BaseAddress = new Uri("http://localhost:2773");
84-
client.DefaultRequestHeaders.Add("X-Aws-Parameters-Secrets-Token", Environment.GetEnvironmentVariable("AWS_SESSION_TOKEN"));
85-
});
86-
_ = services.AddSingleton<IParameterProvider, LambdaExtensionParameterProvider>();
83+
_ = services.AddHttpClient(LambdaExtensionParameterProvider.HttpClientName, client =>
84+
{
85+
client.BaseAddress = new Uri("http://localhost:2773");
86+
client.DefaultRequestHeaders.Add("X-Aws-Parameters-Secrets-Token", Environment.GetEnvironmentVariable("AWS_SESSION_TOKEN"));
87+
});
88+
logger?.LogInformation("Lambda extension HTTP client configured");
89+
90+
_ = services.AddSingleton<IParameterProvider, LambdaExtensionParameterProvider>();
91+
logger?.LogInformation("LambdaExtensionParameterProvider registered successfully");
92+
}
93+
catch (Exception ex)
94+
{
95+
logger?.LogError(ex, "Failed to configure LambdaExtensionParameterProvider for environment {AppEnvironment}", appEnv);
96+
throw;
97+
}
8798
break;
8899
}
89100
case AppEnv.Dev:
@@ -104,10 +115,26 @@ private static void AddAskAiUsecase(IServiceCollection services, AppEnv appEnv)
104115
{
105116
var logger = GetLogger(services);
106117
logger?.LogInformation("Configuring AskAi use case for environment {AppEnvironment}", appEnv);
107-
_ = services.AddSingleton<GcpIdTokenProvider>();
108-
_ = services.AddScoped<LlmGatewayOptions>();
109-
_ = services.AddScoped<AskAiUsecase>();
110-
_ = services.AddScoped<IAskAiGateway<Stream>, LlmGatewayAskAiGateway>();
118+
119+
try
120+
{
121+
_ = services.AddSingleton<GcpIdTokenProvider>();
122+
logger?.LogInformation("GcpIdTokenProvider registered successfully");
123+
124+
_ = services.AddScoped<LlmGatewayOptions>();
125+
logger?.LogInformation("LlmGatewayOptions registered successfully");
126+
127+
_ = services.AddScoped<AskAiUsecase>();
128+
logger?.LogInformation("AskAiUsecase registered successfully");
129+
130+
_ = services.AddScoped<IAskAiGateway<Stream>, LlmGatewayAskAiGateway>();
131+
logger?.LogInformation("LlmGatewayAskAiGateway registered successfully");
132+
}
133+
catch (Exception ex)
134+
{
135+
logger?.LogError(ex, "Failed to configure AskAi use case for environment {AppEnvironment}", appEnv);
136+
throw;
137+
}
111138
}
112139
private static void AddSearchUsecase(IServiceCollection services, AppEnv appEnv)
113140
{

src/api/Elastic.Documentation.Api.Lambda/Program.cs

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,52 @@
1010
using Elastic.Documentation.Api.Infrastructure;
1111
using Elastic.Documentation.ServiceDefaults;
1212

13-
var builder = WebApplication.CreateSlimBuilder(args);
13+
try
14+
{
15+
var process = System.Diagnostics.Process.GetCurrentProcess();
16+
Console.WriteLine($"Starting Lambda application... Memory: {process.WorkingSet64 / 1024 / 1024} MB");
1417

15-
builder.AddDocumentationServiceDefaults(ref args);
18+
var builder = WebApplication.CreateSlimBuilder(args);
19+
process.Refresh();
20+
Console.WriteLine($"WebApplication builder created. Memory: {process.WorkingSet64 / 1024 / 1024} MB");
1621

17-
builder.Services.AddAWSLambdaHosting(LambdaEventSource.RestApi, new SourceGeneratorLambdaJsonSerializer<LambdaJsonSerializerContext>());
18-
builder.Services.AddElasticDocsApiUsecases(Environment.GetEnvironmentVariable("ENVIRONMENT"));
19-
builder.WebHost.UseKestrelHttpsConfiguration();
22+
_ = builder.AddDocumentationServiceDefaults(ref args);
23+
process.Refresh();
24+
Console.WriteLine($"Documentation service defaults added. Memory: {process.WorkingSet64 / 1024 / 1024} MB");
2025

21-
var app = builder.Build();
26+
_ = builder.Services.AddAWSLambdaHosting(LambdaEventSource.RestApi, new SourceGeneratorLambdaJsonSerializer<LambdaJsonSerializerContext>());
27+
process.Refresh();
28+
Console.WriteLine($"AWS Lambda hosting configured. Memory: {process.WorkingSet64 / 1024 / 1024} MB");
2229

23-
var v1 = app.MapGroup("/docs/_api/v1");
24-
v1.MapElasticDocsApiEndpoints();
30+
var environment = Environment.GetEnvironmentVariable("ENVIRONMENT");
31+
Console.WriteLine($"Environment: {environment}");
2532

26-
app.Run();
33+
builder.Services.AddElasticDocsApiUsecases(environment);
34+
process.Refresh();
35+
Console.WriteLine($"Elastic docs API use cases added. Memory: {process.WorkingSet64 / 1024 / 1024} MB");
36+
37+
_ = builder.WebHost.UseKestrelHttpsConfiguration();
38+
process.Refresh();
39+
Console.WriteLine($"Kestrel HTTPS configuration applied. Memory: {process.WorkingSet64 / 1024 / 1024} MB");
40+
41+
var app = builder.Build();
42+
process.Refresh();
43+
Console.WriteLine($"Application built successfully. Memory: {process.WorkingSet64 / 1024 / 1024} MB");
44+
45+
var v1 = app.MapGroup("/docs/_api/v1");
46+
v1.MapElasticDocsApiEndpoints();
47+
Console.WriteLine("API endpoints mapped");
48+
49+
Console.WriteLine("Application startup completed successfully");
50+
app.Run();
51+
}
52+
catch (Exception ex)
53+
{
54+
Console.WriteLine($"FATAL ERROR during startup: {ex}");
55+
Console.WriteLine($"Exception type: {ex.GetType().Name}");
56+
Console.WriteLine($"Stack trace: {ex.StackTrace}");
57+
throw;
58+
}
2759

2860
[JsonSerializable(typeof(APIGatewayProxyRequest))]
2961
[JsonSerializable(typeof(APIGatewayProxyResponse))]

0 commit comments

Comments
 (0)