Skip to content

Commit 673cf26

Browse files
committed
Add additional logs
1 parent 52e86db commit 673cf26

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

lambda-runtime/src/commonMain/kotlin/io/github/trueangle/knative/lambda/runtime/LambdaRuntime.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ object LambdaRuntime {
4646

4747
inline fun <reified I, reified O> run(crossinline initHandler: () -> LambdaHandler<I, O>) = runBlocking {
4848
val handler = try {
49+
Log.info("Initializing Kotlin Native Lambda Runtime")
50+
4951
initHandler()
5052
} catch (e: Exception) {
5153
Log.fatal(e)
@@ -54,11 +56,14 @@ object LambdaRuntime {
5456
exitProcess(1)
5557
}
5658

59+
val handlerName = handler::class.simpleName
5760
val inputTypeInfo = typeInfo<I>()
5861
val outputTypeInfo = typeInfo<O>()
5962

6063
while (true) {
6164
try {
65+
Log.info("Runtime is ready for a new event")
66+
6267
val (event, context) = client.retrieveNextEvent<I>(inputTypeInfo)
6368

6469
with(Log) {
@@ -68,13 +73,21 @@ object LambdaRuntime {
6873
trace(context)
6974
}
7075

76+
Log.info("$handlerName invocation started")
77+
7178
if (handler is LambdaStreamHandler<I, *>) {
7279
val response = streamingResponse { handler.handleRequest(event, it, context) }
80+
81+
Log.info("$handlerName started response streaming")
82+
7383
client.streamResponse(context, response)
7484
} else {
7585
handler as LambdaBufferedHandler<I, O>
7686
val response = bufferedResponse(context) { handler.handleRequest(event, context) }
7787

88+
Log.info("$handlerName invocation completed")
89+
Log.trace("$handlerName's buffered response: $response")
90+
7891
client.sendResponse(context, response, outputTypeInfo)
7992
}
8093
} catch (e: LambdaRuntimeException) {
@@ -107,6 +120,7 @@ internal inline fun streamingResponse(crossinline handler: suspend (ByteWriteCha
107120
handler(channel)
108121
} catch (e: Exception) {
109122
Log.warn(e)
123+
110124
channel.writeStringUtf8(e.toTrailer())
111125
}
112126
}

lambda-runtime/src/commonMain/kotlin/io/github/trueangle/knative/lambda/runtime/api/LambdaRuntimeClient.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,13 @@ import io.ktor.http.ContentType.Application.Json as ContentTypeJson
2929
@PublishedApi
3030
internal class LambdaClient(private val httpClient: HttpClient) {
3131
private val invokeUrl = "http://${LambdaEnvironment.RUNTIME_API}/2018-06-01/runtime"
32+
private val requestTimeout = 15.minutes.inWholeMilliseconds
3233

3334
suspend fun <T> retrieveNextEvent(bodyType: TypeInfo): Pair<T, Context> {
3435
val response = httpClient.get {
3536
url("${invokeUrl}/invocation/next")
3637

37-
timeout { requestTimeoutMillis = 15.minutes.inWholeMilliseconds }
38+
timeout { requestTimeoutMillis = requestTimeout }
3839
}
3940
val context = contextFromResponseHeaders(response)
4041
val body = try {
@@ -47,8 +48,6 @@ internal class LambdaClient(private val httpClient: HttpClient) {
4748
}
4849

4950
suspend fun <T> sendResponse(event: Context, body: T, bodyType: TypeInfo): HttpResponse {
50-
Log.trace("Response from handler: $body")
51-
5251
val response = httpClient.post {
5352
url("${invokeUrl}/invocation/${event.awsRequestId}/response")
5453
contentType(ContentTypeJson)
@@ -70,7 +69,7 @@ internal class LambdaClient(private val httpClient: HttpClient) {
7069
url("${invokeUrl}/invocation/${event.awsRequestId}/response")
7170

7271
timeout {
73-
requestTimeoutMillis = 15.minutes.inWholeMilliseconds
72+
requestTimeoutMillis = requestTimeout
7473
}
7574

7675
headers {

0 commit comments

Comments
 (0)