Skip to content

Commit def6913

Browse files
committed
feat: emit additional tracing logs from ImdsClient
1 parent c9fc7e2 commit def6913

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"id": "08b59d71-d7b8-4931-806f-105fd005c30e",
3+
"type": "feature",
4+
"description": "Emit additional tracing logs from the IMDS client"
5+
}

aws-runtime/aws-config/common/src/aws/sdk/kotlin/runtime/config/imds/ImdsClient.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import aws.smithy.kotlin.runtime.io.Closeable
2424
import aws.smithy.kotlin.runtime.io.closeIfCloseable
2525
import aws.smithy.kotlin.runtime.io.middleware.Phase
2626
import aws.smithy.kotlin.runtime.operation.ExecutionContext
27+
import aws.smithy.kotlin.runtime.telemetry.logging.logger
2728
import aws.smithy.kotlin.runtime.time.Clock
2829
import aws.smithy.kotlin.runtime.util.PlatformProvider
2930
import kotlin.coroutines.coroutineContext
@@ -111,11 +112,13 @@ public class ImdsClient private constructor(builder: Builder) : InstanceMetadata
111112
* ```
112113
*/
113114
public override suspend fun get(path: String): String {
115+
val logger = coroutineContext.logger<ImdsClient>()
114116
val op = SdkHttpOperation.build<Unit, String> {
115117
serializeWith = HttpSerializer.Unit
116118
deserializeWith = object : HttpDeserializer.NonStreaming<String> {
117119
override fun deserialize(context: ExecutionContext, call: HttpCall, payload: ByteArray?): String {
118120
val response = call.response
121+
logger.trace { call.toTraceString() }
119122
if (response.status.isSuccess()) {
120123
return payload!!.decodeToString()
121124
} else {
@@ -240,3 +243,17 @@ public class EC2MetadataError(public val status: HttpStatusCode, message: String
240243
@Deprecated("This property is now deprecated and should be fetched from status.value. This declaration will be removed in version 1.6.x.")
241244
public val statusCode: Int = status.value
242245
}
246+
247+
/**
248+
* Formats an executed [HttpCall] into a form suitable for TRACE logging. Example output:
249+
*
250+
* `HTTP GET http://169.254.169.254/latest/meta-data/iam/security-credentials/ --> 200 OK`
251+
*/
252+
internal fun HttpCall.toTraceString() = listOf(
253+
"HTTP",
254+
request.method,
255+
request.url,
256+
"-->",
257+
response.status.value,
258+
response.status.description,
259+
).joinToString(" ")

aws-runtime/aws-config/common/src/aws/sdk/kotlin/runtime/config/imds/TokenMiddleware.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@
66
package aws.sdk.kotlin.runtime.config.imds
77

88
import aws.smithy.kotlin.runtime.http.*
9-
import aws.smithy.kotlin.runtime.http.complete
109
import aws.smithy.kotlin.runtime.http.operation.ModifyRequestMiddleware
1110
import aws.smithy.kotlin.runtime.http.operation.SdkHttpOperation
1211
import aws.smithy.kotlin.runtime.http.operation.SdkHttpRequest
1312
import aws.smithy.kotlin.runtime.http.operation.setResolvedEndpoint
1413
import aws.smithy.kotlin.runtime.http.request.HttpRequestBuilder
15-
import aws.smithy.kotlin.runtime.telemetry.logging.trace
14+
import aws.smithy.kotlin.runtime.telemetry.logging.logger
1615
import aws.smithy.kotlin.runtime.time.Clock
1716
import aws.smithy.kotlin.runtime.util.CachedValue
1817
import aws.smithy.kotlin.runtime.util.ExpiringValue
@@ -49,7 +48,8 @@ internal class TokenMiddleware(
4948
}
5049

5150
private suspend fun getToken(clock: Clock, req: SdkHttpRequest): Token {
52-
coroutineContext.trace<TokenMiddleware> { "refreshing IMDS token" }
51+
val logger = coroutineContext.logger<TokenMiddleware>()
52+
logger.trace { "refreshing IMDS token" }
5353

5454
val tokenReq = HttpRequestBuilder().apply {
5555
method = HttpMethod.PUT
@@ -63,6 +63,7 @@ internal class TokenMiddleware(
6363
setResolvedEndpoint(SdkHttpRequest(tokenReq), endpoint)
6464

6565
val call = httpClient.call(tokenReq)
66+
logger.trace { call.toTraceString() }
6667
return try {
6768
when (call.response.status) {
6869
HttpStatusCode.OK -> {

0 commit comments

Comments
 (0)