Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/08b59d71-d7b8-4931-806f-105fd005c30e.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"id": "08b59d71-d7b8-4931-806f-105fd005c30e",
"type": "feature",
"description": "Emit additional tracing logs from the IMDS client"
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import aws.smithy.kotlin.runtime.io.Closeable
import aws.smithy.kotlin.runtime.io.closeIfCloseable
import aws.smithy.kotlin.runtime.io.middleware.Phase
import aws.smithy.kotlin.runtime.operation.ExecutionContext
import aws.smithy.kotlin.runtime.telemetry.logging.logger
import aws.smithy.kotlin.runtime.time.Clock
import aws.smithy.kotlin.runtime.util.PlatformProvider
import kotlin.coroutines.coroutineContext
Expand Down Expand Up @@ -111,11 +112,13 @@ public class ImdsClient private constructor(builder: Builder) : InstanceMetadata
* ```
*/
public override suspend fun get(path: String): String {
val logger = coroutineContext.logger<ImdsClient>()
val op = SdkHttpOperation.build<Unit, String> {
serializeWith = HttpSerializer.Unit
deserializeWith = object : HttpDeserializer.NonStreaming<String> {
override fun deserialize(context: ExecutionContext, call: HttpCall, payload: ByteArray?): String {
val response = call.response
logger.trace { call.toTraceString() }
if (response.status.isSuccess()) {
return payload!!.decodeToString()
} else {
Expand Down Expand Up @@ -240,3 +243,17 @@ public class EC2MetadataError(public val status: HttpStatusCode, message: String
@Deprecated("This property is now deprecated and should be fetched from status.value. This declaration will be removed in version 1.6.x.")
public val statusCode: Int = status.value
}

/**
* Formats an executed [HttpCall] into a form suitable for TRACE logging. Example output:
*
* `HTTP GET http://169.254.169.254/latest/meta-data/iam/security-credentials/ --> 200 OK`
*/
internal fun HttpCall.toTraceString() = listOf(
"HTTP",
request.method,
request.url,
"-->",
response.status.value,
response.status.description,
).joinToString(" ")
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
package aws.sdk.kotlin.runtime.config.imds

import aws.smithy.kotlin.runtime.http.*
import aws.smithy.kotlin.runtime.http.complete
import aws.smithy.kotlin.runtime.http.operation.ModifyRequestMiddleware
import aws.smithy.kotlin.runtime.http.operation.SdkHttpOperation
import aws.smithy.kotlin.runtime.http.operation.SdkHttpRequest
import aws.smithy.kotlin.runtime.http.operation.setResolvedEndpoint
import aws.smithy.kotlin.runtime.http.request.HttpRequestBuilder
import aws.smithy.kotlin.runtime.telemetry.logging.trace
import aws.smithy.kotlin.runtime.telemetry.logging.logger
import aws.smithy.kotlin.runtime.time.Clock
import aws.smithy.kotlin.runtime.util.CachedValue
import aws.smithy.kotlin.runtime.util.ExpiringValue
Expand Down Expand Up @@ -49,7 +48,8 @@ internal class TokenMiddleware(
}

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

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

val call = httpClient.call(tokenReq)
logger.trace { call.toTraceString() }
return try {
when (call.response.status) {
HttpStatusCode.OK -> {
Expand Down
Loading