From f32de91f5bffe5a48b4af4718062c40182a69105 Mon Sep 17 00:00:00 2001 From: 0marperez Date: Mon, 14 Oct 2024 18:03:21 -0400 Subject: [PATCH 01/18] misc: reset attributes between provider chain attempts --- .../kotlin/codegen/core/RuntimeTypes.kt | 1 + .../runtime/identity/IdentityProviderChain.kt | 7 +++++- runtime/runtime-core/api/runtime-core.api | 2 ++ .../businessmetrics/BusinessMetricsUtils.kt | 3 +-- .../kotlin/runtime/collections/Attributes.kt | 24 +++++++++++++++++++ 5 files changed, 34 insertions(+), 3 deletions(-) diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/core/RuntimeTypes.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/core/RuntimeTypes.kt index d37e47737a..91bdfc5ab2 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/core/RuntimeTypes.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/core/RuntimeTypes.kt @@ -330,6 +330,7 @@ object RuntimeTypes { val CredentialsProvider = symbol("CredentialsProvider") val CredentialsProviderConfig = symbol("CredentialsProviderConfig") val SigV4aClientConfig = symbol("SigV4aClientConfig") + val simpleClassName = symbol("simpleClassName") } } diff --git a/runtime/auth/identity-api/common/src/aws/smithy/kotlin/runtime/identity/IdentityProviderChain.kt b/runtime/auth/identity-api/common/src/aws/smithy/kotlin/runtime/identity/IdentityProviderChain.kt index fd9246090b..c004da2c71 100644 --- a/runtime/auth/identity-api/common/src/aws/smithy/kotlin/runtime/identity/IdentityProviderChain.kt +++ b/runtime/auth/identity-api/common/src/aws/smithy/kotlin/runtime/identity/IdentityProviderChain.kt @@ -6,7 +6,7 @@ package aws.smithy.kotlin.runtime.identity import aws.smithy.kotlin.runtime.InternalApi -import aws.smithy.kotlin.runtime.collections.Attributes +import aws.smithy.kotlin.runtime.collections.* import aws.smithy.kotlin.runtime.io.Closeable import aws.smithy.kotlin.runtime.telemetry.logging.logger import aws.smithy.kotlin.runtime.telemetry.trace.withSpan @@ -37,11 +37,16 @@ public abstract class IdentityProviderChain

( val chainException = lazy { IdentityProviderException("No identity could be resolved from the chain: $chain") } for (provider in providers) { logger.trace { "Attempting to resolve identity from $provider" } + + val attributesAreMutable = attributes is MutableAttributes + val attributesSnapshot = if (attributesAreMutable) attributes.copy() else null + try { @Suppress("UNCHECKED_CAST") return@withSpan provider.resolve(attributes) as I } catch (ex: Exception) { logger.debug { "unable to resolve identity from $provider: ${ex.message}" } + if (attributesAreMutable) (attributes as MutableAttributes).resetTo(attributesSnapshot!!) chainException.value.addSuppressed(ex) } } diff --git a/runtime/runtime-core/api/runtime-core.api b/runtime/runtime-core/api/runtime-core.api index d1ba553de5..131a30a79a 100644 --- a/runtime/runtime-core/api/runtime-core.api +++ b/runtime/runtime-core/api/runtime-core.api @@ -133,6 +133,7 @@ public final class aws/smithy/kotlin/runtime/collections/AttributesBuilder { public final class aws/smithy/kotlin/runtime/collections/AttributesKt { public static final fun attributesOf (Lkotlin/jvm/functions/Function1;)Laws/smithy/kotlin/runtime/collections/Attributes; + public static final fun copy (Laws/smithy/kotlin/runtime/collections/Attributes;)Laws/smithy/kotlin/runtime/collections/Attributes; public static final fun emptyAttributes ()Laws/smithy/kotlin/runtime/collections/Attributes; public static final fun get (Laws/smithy/kotlin/runtime/collections/Attributes;Laws/smithy/kotlin/runtime/collections/AttributeKey;)Ljava/lang/Object; public static final fun isNotEmpty (Laws/smithy/kotlin/runtime/collections/Attributes;)Z @@ -141,6 +142,7 @@ public final class aws/smithy/kotlin/runtime/collections/AttributesKt { public static final fun mutableAttributesOf (Lkotlin/jvm/functions/Function1;)Laws/smithy/kotlin/runtime/collections/MutableAttributes; public static final fun putIfAbsent (Laws/smithy/kotlin/runtime/collections/MutableAttributes;Laws/smithy/kotlin/runtime/collections/AttributeKey;Ljava/lang/Object;)V public static final fun putIfAbsentNotNull (Laws/smithy/kotlin/runtime/collections/MutableAttributes;Laws/smithy/kotlin/runtime/collections/AttributeKey;Ljava/lang/Object;)V + public static final fun resetTo (Laws/smithy/kotlin/runtime/collections/MutableAttributes;Laws/smithy/kotlin/runtime/collections/Attributes;)V public static final fun setIfValueNotNull (Laws/smithy/kotlin/runtime/collections/MutableAttributes;Laws/smithy/kotlin/runtime/collections/AttributeKey;Ljava/lang/Object;)V public static final fun take (Laws/smithy/kotlin/runtime/collections/MutableAttributes;Laws/smithy/kotlin/runtime/collections/AttributeKey;)Ljava/lang/Object; public static final fun takeOrNull (Laws/smithy/kotlin/runtime/collections/MutableAttributes;Laws/smithy/kotlin/runtime/collections/AttributeKey;)Ljava/lang/Object; diff --git a/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtils.kt b/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtils.kt index 637c906dff..928418a626 100644 --- a/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtils.kt +++ b/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtils.kt @@ -5,8 +5,7 @@ package aws.smithy.kotlin.runtime.businessmetrics import aws.smithy.kotlin.runtime.InternalApi -import aws.smithy.kotlin.runtime.collections.AttributeKey -import aws.smithy.kotlin.runtime.collections.get +import aws.smithy.kotlin.runtime.collections.* import aws.smithy.kotlin.runtime.operation.ExecutionContext /** diff --git a/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/collections/Attributes.kt b/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/collections/Attributes.kt index 1fffcc82a2..8f7fc51282 100644 --- a/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/collections/Attributes.kt +++ b/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/collections/Attributes.kt @@ -235,3 +235,27 @@ public inline fun attributesOf(block: AttributesBuilder.() -> Unit): Attributes * Returns a new [MutableAttributes] instance with elements from this set of attributes. */ public fun Attributes.toMutableAttributes(): MutableAttributes = AttributesImpl(this) + +/** + * Creates a copy of this [Attributes] instance. + * + * This function generates a new [Attributes] instance containing all the key-value pairs + * from the current set of attributes. The new instance is independent of the original, + * meaning changes to the copied attributes will not affect the original set. + */ +public fun Attributes.copy(): Attributes { + val copy = mutableAttributes() + (this.keys as Set>).forEach { key -> + copy[key] = this[key] + } + return copy +} + +/** + * Resets this [MutableAttributes] instance to the state of the specified [Attributes]. + */ +public fun MutableAttributes.resetTo(attributes: Attributes) { + val obsoleteKeys = this.keys.subtract(attributes.keys) + obsoleteKeys.forEach { key -> this.remove(key as AttributeKey) } + this.merge(attributes) +} From 980abe61d6615908a477ac079f419c0fb26d7e4a Mon Sep 17 00:00:00 2001 From: 0marperez Date: Mon, 14 Oct 2024 18:42:44 -0400 Subject: [PATCH 02/18] misc: add function to emit business metrics with attributes as receiver --- runtime/runtime-core/api/runtime-core.api | 1 + .../kotlin/runtime/businessmetrics/BusinessMetricsUtils.kt | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/runtime/runtime-core/api/runtime-core.api b/runtime/runtime-core/api/runtime-core.api index 131a30a79a..dd84a33965 100644 --- a/runtime/runtime-core/api/runtime-core.api +++ b/runtime/runtime-core/api/runtime-core.api @@ -83,6 +83,7 @@ public abstract interface class aws/smithy/kotlin/runtime/businessmetrics/Busine public final class aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtilsKt { public static final fun containsBusinessMetric (Laws/smithy/kotlin/runtime/operation/ExecutionContext;Laws/smithy/kotlin/runtime/businessmetrics/BusinessMetric;)Z + public static final fun emitBusinessMetric (Laws/smithy/kotlin/runtime/collections/Attributes;Laws/smithy/kotlin/runtime/businessmetrics/BusinessMetric;)V public static final fun emitBusinessMetric (Laws/smithy/kotlin/runtime/operation/ExecutionContext;Laws/smithy/kotlin/runtime/businessmetrics/BusinessMetric;)V public static final fun getAccountIdBasedEndpointAccountId ()Laws/smithy/kotlin/runtime/collections/AttributeKey; public static final fun getBusinessMetrics ()Laws/smithy/kotlin/runtime/collections/AttributeKey; diff --git a/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtils.kt b/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtils.kt index 928418a626..3685ac2270 100644 --- a/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtils.kt +++ b/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtils.kt @@ -77,3 +77,10 @@ public enum class SmithyBusinessMetric(public override val identifier: String) : ACCOUNT_ID_BASED_ENDPOINT("O"), SIGV4A_SIGNING("S"), } + +/** + * Emits a business metric if the current [Attributes] instance is of type [ExecutionContext]. + */ +public fun Attributes.emitBusinessMetric(metric: BusinessMetric) { + if (this is ExecutionContext) this.emitBusinessMetric(metric) +} From 169c96a35f2bfc8ef91d484e2a1a27a864d5e53d Mon Sep 17 00:00:00 2001 From: 0marperez Date: Tue, 15 Oct 2024 17:19:56 -0400 Subject: [PATCH 03/18] Bug fixes & refactoring of new business metrics utils --- .../runtime/identity/IdentityProviderChain.kt | 7 +++--- runtime/runtime-core/api/runtime-core.api | 3 +-- .../businessmetrics/BusinessMetricsUtils.kt | 14 +++++++++++ .../kotlin/runtime/collections/Attributes.kt | 24 ------------------- 4 files changed, 19 insertions(+), 29 deletions(-) diff --git a/runtime/auth/identity-api/common/src/aws/smithy/kotlin/runtime/identity/IdentityProviderChain.kt b/runtime/auth/identity-api/common/src/aws/smithy/kotlin/runtime/identity/IdentityProviderChain.kt index c004da2c71..42d5a2d008 100644 --- a/runtime/auth/identity-api/common/src/aws/smithy/kotlin/runtime/identity/IdentityProviderChain.kt +++ b/runtime/auth/identity-api/common/src/aws/smithy/kotlin/runtime/identity/IdentityProviderChain.kt @@ -6,6 +6,8 @@ package aws.smithy.kotlin.runtime.identity import aws.smithy.kotlin.runtime.InternalApi +import aws.smithy.kotlin.runtime.businessmetrics.BusinessMetrics +import aws.smithy.kotlin.runtime.businessmetrics.copyBusinessMetrics import aws.smithy.kotlin.runtime.collections.* import aws.smithy.kotlin.runtime.io.Closeable import aws.smithy.kotlin.runtime.telemetry.logging.logger @@ -38,15 +40,14 @@ public abstract class IdentityProviderChain

( for (provider in providers) { logger.trace { "Attempting to resolve identity from $provider" } - val attributesAreMutable = attributes is MutableAttributes - val attributesSnapshot = if (attributesAreMutable) attributes.copy() else null + val businessMetricsSnapshot = attributes.copyBusinessMetrics() try { @Suppress("UNCHECKED_CAST") return@withSpan provider.resolve(attributes) as I } catch (ex: Exception) { logger.debug { "unable to resolve identity from $provider: ${ex.message}" } - if (attributesAreMutable) (attributes as MutableAttributes).resetTo(attributesSnapshot!!) + if (attributes is MutableAttributes) attributes[BusinessMetrics] = businessMetricsSnapshot chainException.value.addSuppressed(ex) } } diff --git a/runtime/runtime-core/api/runtime-core.api b/runtime/runtime-core/api/runtime-core.api index dd84a33965..6c85f9c536 100644 --- a/runtime/runtime-core/api/runtime-core.api +++ b/runtime/runtime-core/api/runtime-core.api @@ -83,6 +83,7 @@ public abstract interface class aws/smithy/kotlin/runtime/businessmetrics/Busine public final class aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtilsKt { public static final fun containsBusinessMetric (Laws/smithy/kotlin/runtime/operation/ExecutionContext;Laws/smithy/kotlin/runtime/businessmetrics/BusinessMetric;)Z + public static final fun copyBusinessMetrics (Laws/smithy/kotlin/runtime/collections/Attributes;)Ljava/util/Set; public static final fun emitBusinessMetric (Laws/smithy/kotlin/runtime/collections/Attributes;Laws/smithy/kotlin/runtime/businessmetrics/BusinessMetric;)V public static final fun emitBusinessMetric (Laws/smithy/kotlin/runtime/operation/ExecutionContext;Laws/smithy/kotlin/runtime/businessmetrics/BusinessMetric;)V public static final fun getAccountIdBasedEndpointAccountId ()Laws/smithy/kotlin/runtime/collections/AttributeKey; @@ -134,7 +135,6 @@ public final class aws/smithy/kotlin/runtime/collections/AttributesBuilder { public final class aws/smithy/kotlin/runtime/collections/AttributesKt { public static final fun attributesOf (Lkotlin/jvm/functions/Function1;)Laws/smithy/kotlin/runtime/collections/Attributes; - public static final fun copy (Laws/smithy/kotlin/runtime/collections/Attributes;)Laws/smithy/kotlin/runtime/collections/Attributes; public static final fun emptyAttributes ()Laws/smithy/kotlin/runtime/collections/Attributes; public static final fun get (Laws/smithy/kotlin/runtime/collections/Attributes;Laws/smithy/kotlin/runtime/collections/AttributeKey;)Ljava/lang/Object; public static final fun isNotEmpty (Laws/smithy/kotlin/runtime/collections/Attributes;)Z @@ -143,7 +143,6 @@ public final class aws/smithy/kotlin/runtime/collections/AttributesKt { public static final fun mutableAttributesOf (Lkotlin/jvm/functions/Function1;)Laws/smithy/kotlin/runtime/collections/MutableAttributes; public static final fun putIfAbsent (Laws/smithy/kotlin/runtime/collections/MutableAttributes;Laws/smithy/kotlin/runtime/collections/AttributeKey;Ljava/lang/Object;)V public static final fun putIfAbsentNotNull (Laws/smithy/kotlin/runtime/collections/MutableAttributes;Laws/smithy/kotlin/runtime/collections/AttributeKey;Ljava/lang/Object;)V - public static final fun resetTo (Laws/smithy/kotlin/runtime/collections/MutableAttributes;Laws/smithy/kotlin/runtime/collections/Attributes;)V public static final fun setIfValueNotNull (Laws/smithy/kotlin/runtime/collections/MutableAttributes;Laws/smithy/kotlin/runtime/collections/AttributeKey;Ljava/lang/Object;)V public static final fun take (Laws/smithy/kotlin/runtime/collections/MutableAttributes;Laws/smithy/kotlin/runtime/collections/AttributeKey;)Ljava/lang/Object; public static final fun takeOrNull (Laws/smithy/kotlin/runtime/collections/MutableAttributes;Laws/smithy/kotlin/runtime/collections/AttributeKey;)Ljava/lang/Object; diff --git a/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtils.kt b/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtils.kt index 3685ac2270..cc2c6633fe 100644 --- a/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtils.kt +++ b/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtils.kt @@ -84,3 +84,17 @@ public enum class SmithyBusinessMetric(public override val identifier: String) : public fun Attributes.emitBusinessMetric(metric: BusinessMetric) { if (this is ExecutionContext) this.emitBusinessMetric(metric) } + +/** + * Creates a copy of an [Attributes] business metrics. + * Returns an empty set if no business metrics are available. + */ +public fun Attributes.copyBusinessMetrics(): MutableSet { + val copy = mutableSetOf() + if (!this.contains(BusinessMetrics)) return copy + + this[BusinessMetrics].forEach { metric -> + copy.add(metric) + } + return copy +} diff --git a/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/collections/Attributes.kt b/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/collections/Attributes.kt index 8f7fc51282..1fffcc82a2 100644 --- a/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/collections/Attributes.kt +++ b/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/collections/Attributes.kt @@ -235,27 +235,3 @@ public inline fun attributesOf(block: AttributesBuilder.() -> Unit): Attributes * Returns a new [MutableAttributes] instance with elements from this set of attributes. */ public fun Attributes.toMutableAttributes(): MutableAttributes = AttributesImpl(this) - -/** - * Creates a copy of this [Attributes] instance. - * - * This function generates a new [Attributes] instance containing all the key-value pairs - * from the current set of attributes. The new instance is independent of the original, - * meaning changes to the copied attributes will not affect the original set. - */ -public fun Attributes.copy(): Attributes { - val copy = mutableAttributes() - (this.keys as Set>).forEach { key -> - copy[key] = this[key] - } - return copy -} - -/** - * Resets this [MutableAttributes] instance to the state of the specified [Attributes]. - */ -public fun MutableAttributes.resetTo(attributes: Attributes) { - val obsoleteKeys = this.keys.subtract(attributes.keys) - obsoleteKeys.forEach { key -> this.remove(key as AttributeKey) } - this.merge(attributes) -} From eb4999d1cfe42175b26f95d6a00e3e13c04b1b53 Mon Sep 17 00:00:00 2001 From: 0marperez Date: Thu, 17 Oct 2024 13:48:30 -0400 Subject: [PATCH 04/18] Remove simpleName runtime type --- .../software/amazon/smithy/kotlin/codegen/core/RuntimeTypes.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/core/RuntimeTypes.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/core/RuntimeTypes.kt index 91bdfc5ab2..d37e47737a 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/core/RuntimeTypes.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/core/RuntimeTypes.kt @@ -330,7 +330,6 @@ object RuntimeTypes { val CredentialsProvider = symbol("CredentialsProvider") val CredentialsProviderConfig = symbol("CredentialsProviderConfig") val SigV4aClientConfig = symbol("SigV4aClientConfig") - val simpleClassName = symbol("simpleClassName") } } From 287e0500b9b50d4e01b7f5c3552a42a0aed7acb7 Mon Sep 17 00:00:00 2001 From: 0marperez Date: Thu, 17 Oct 2024 15:06:01 -0400 Subject: [PATCH 05/18] Downstream attributes utils --- runtime/runtime-core/api/runtime-core.api | 2 ++ .../runtime/businessmetrics/BusinessMetricsUtils.kt | 7 +++++++ .../smithy/kotlin/runtime/collections/Attributes.kt | 12 ++++++++++++ 3 files changed, 21 insertions(+) diff --git a/runtime/runtime-core/api/runtime-core.api b/runtime/runtime-core/api/runtime-core.api index 6c85f9c536..cfbddef910 100644 --- a/runtime/runtime-core/api/runtime-core.api +++ b/runtime/runtime-core/api/runtime-core.api @@ -89,6 +89,7 @@ public final class aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtil public static final fun getAccountIdBasedEndpointAccountId ()Laws/smithy/kotlin/runtime/collections/AttributeKey; public static final fun getBusinessMetrics ()Laws/smithy/kotlin/runtime/collections/AttributeKey; public static final fun getServiceEndpointOverride ()Laws/smithy/kotlin/runtime/collections/AttributeKey; + public static final fun mergeBusinessMetrics (Laws/smithy/kotlin/runtime/collections/MutableAttributes;Laws/smithy/kotlin/runtime/collections/Attributes;)V public static final fun removeBusinessMetric (Laws/smithy/kotlin/runtime/operation/ExecutionContext;Laws/smithy/kotlin/runtime/businessmetrics/BusinessMetric;)V } @@ -139,6 +140,7 @@ public final class aws/smithy/kotlin/runtime/collections/AttributesKt { public static final fun get (Laws/smithy/kotlin/runtime/collections/Attributes;Laws/smithy/kotlin/runtime/collections/AttributeKey;)Ljava/lang/Object; public static final fun isNotEmpty (Laws/smithy/kotlin/runtime/collections/Attributes;)Z public static final fun merge (Laws/smithy/kotlin/runtime/collections/MutableAttributes;Laws/smithy/kotlin/runtime/collections/Attributes;)V + public static final fun mergeExcept (Laws/smithy/kotlin/runtime/collections/MutableAttributes;Laws/smithy/kotlin/runtime/collections/Attributes;Ljava/util/Set;)V public static final fun mutableAttributes ()Laws/smithy/kotlin/runtime/collections/MutableAttributes; public static final fun mutableAttributesOf (Lkotlin/jvm/functions/Function1;)Laws/smithy/kotlin/runtime/collections/MutableAttributes; public static final fun putIfAbsent (Laws/smithy/kotlin/runtime/collections/MutableAttributes;Laws/smithy/kotlin/runtime/collections/AttributeKey;Ljava/lang/Object;)V diff --git a/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtils.kt b/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtils.kt index cc2c6633fe..4ae611c8fa 100644 --- a/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtils.kt +++ b/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtils.kt @@ -98,3 +98,10 @@ public fun Attributes.copyBusinessMetrics(): MutableSet { } return copy } + +/** + * Merges another [Attributes] business metrics into this instance of [MutableAttributes]. + */ +public fun MutableAttributes.mergeBusinessMetrics(other: Attributes) { + this[BusinessMetrics] = this[BusinessMetrics].union(other[BusinessMetrics]).toMutableSet() +} diff --git a/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/collections/Attributes.kt b/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/collections/Attributes.kt index 1fffcc82a2..2404ad6697 100644 --- a/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/collections/Attributes.kt +++ b/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/collections/Attributes.kt @@ -115,6 +115,18 @@ public fun MutableAttributes.merge(other: Attributes) { } } +/** + * Merge another attributes instance into this set of attributes favoring [other] except for any keys that are + * specified in the [exceptions] set + */ +public fun MutableAttributes.mergeExcept(other: Attributes, exceptions: Set>) { + other.keys.forEach { + if (it in exceptions) return@forEach + @Suppress("UNCHECKED_CAST") + set(it as AttributeKey, other[it]) + } +} + private class AttributesImpl constructor(seed: Attributes) : MutableAttributes { private val map: MutableMap, Any> = mutableMapOf() constructor() : this(emptyAttributes()) From cb76cd536984a635815e7f31ee85e063bfb552eb Mon Sep 17 00:00:00 2001 From: 0marperez Date: Thu, 24 Oct 2024 10:47:22 -0400 Subject: [PATCH 06/18] Got rid of unnecessary code in identity provider chain for credentials business metrics --- .../runtime/identity/IdentityProviderChain.kt | 6 ---- runtime/runtime-core/api/runtime-core.api | 3 -- .../businessmetrics/BusinessMetricsUtils.kt | 28 ++++--------------- .../kotlin/runtime/collections/Attributes.kt | 12 -------- 4 files changed, 5 insertions(+), 44 deletions(-) diff --git a/runtime/auth/identity-api/common/src/aws/smithy/kotlin/runtime/identity/IdentityProviderChain.kt b/runtime/auth/identity-api/common/src/aws/smithy/kotlin/runtime/identity/IdentityProviderChain.kt index 42d5a2d008..d84b81f047 100644 --- a/runtime/auth/identity-api/common/src/aws/smithy/kotlin/runtime/identity/IdentityProviderChain.kt +++ b/runtime/auth/identity-api/common/src/aws/smithy/kotlin/runtime/identity/IdentityProviderChain.kt @@ -6,8 +6,6 @@ package aws.smithy.kotlin.runtime.identity import aws.smithy.kotlin.runtime.InternalApi -import aws.smithy.kotlin.runtime.businessmetrics.BusinessMetrics -import aws.smithy.kotlin.runtime.businessmetrics.copyBusinessMetrics import aws.smithy.kotlin.runtime.collections.* import aws.smithy.kotlin.runtime.io.Closeable import aws.smithy.kotlin.runtime.telemetry.logging.logger @@ -39,15 +37,11 @@ public abstract class IdentityProviderChain

( val chainException = lazy { IdentityProviderException("No identity could be resolved from the chain: $chain") } for (provider in providers) { logger.trace { "Attempting to resolve identity from $provider" } - - val businessMetricsSnapshot = attributes.copyBusinessMetrics() - try { @Suppress("UNCHECKED_CAST") return@withSpan provider.resolve(attributes) as I } catch (ex: Exception) { logger.debug { "unable to resolve identity from $provider: ${ex.message}" } - if (attributes is MutableAttributes) attributes[BusinessMetrics] = businessMetricsSnapshot chainException.value.addSuppressed(ex) } } diff --git a/runtime/runtime-core/api/runtime-core.api b/runtime/runtime-core/api/runtime-core.api index cfbddef910..7a46b10d3b 100644 --- a/runtime/runtime-core/api/runtime-core.api +++ b/runtime/runtime-core/api/runtime-core.api @@ -83,13 +83,11 @@ public abstract interface class aws/smithy/kotlin/runtime/businessmetrics/Busine public final class aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtilsKt { public static final fun containsBusinessMetric (Laws/smithy/kotlin/runtime/operation/ExecutionContext;Laws/smithy/kotlin/runtime/businessmetrics/BusinessMetric;)Z - public static final fun copyBusinessMetrics (Laws/smithy/kotlin/runtime/collections/Attributes;)Ljava/util/Set; public static final fun emitBusinessMetric (Laws/smithy/kotlin/runtime/collections/Attributes;Laws/smithy/kotlin/runtime/businessmetrics/BusinessMetric;)V public static final fun emitBusinessMetric (Laws/smithy/kotlin/runtime/operation/ExecutionContext;Laws/smithy/kotlin/runtime/businessmetrics/BusinessMetric;)V public static final fun getAccountIdBasedEndpointAccountId ()Laws/smithy/kotlin/runtime/collections/AttributeKey; public static final fun getBusinessMetrics ()Laws/smithy/kotlin/runtime/collections/AttributeKey; public static final fun getServiceEndpointOverride ()Laws/smithy/kotlin/runtime/collections/AttributeKey; - public static final fun mergeBusinessMetrics (Laws/smithy/kotlin/runtime/collections/MutableAttributes;Laws/smithy/kotlin/runtime/collections/Attributes;)V public static final fun removeBusinessMetric (Laws/smithy/kotlin/runtime/operation/ExecutionContext;Laws/smithy/kotlin/runtime/businessmetrics/BusinessMetric;)V } @@ -140,7 +138,6 @@ public final class aws/smithy/kotlin/runtime/collections/AttributesKt { public static final fun get (Laws/smithy/kotlin/runtime/collections/Attributes;Laws/smithy/kotlin/runtime/collections/AttributeKey;)Ljava/lang/Object; public static final fun isNotEmpty (Laws/smithy/kotlin/runtime/collections/Attributes;)Z public static final fun merge (Laws/smithy/kotlin/runtime/collections/MutableAttributes;Laws/smithy/kotlin/runtime/collections/Attributes;)V - public static final fun mergeExcept (Laws/smithy/kotlin/runtime/collections/MutableAttributes;Laws/smithy/kotlin/runtime/collections/Attributes;Ljava/util/Set;)V public static final fun mutableAttributes ()Laws/smithy/kotlin/runtime/collections/MutableAttributes; public static final fun mutableAttributesOf (Lkotlin/jvm/functions/Function1;)Laws/smithy/kotlin/runtime/collections/MutableAttributes; public static final fun putIfAbsent (Laws/smithy/kotlin/runtime/collections/MutableAttributes;Laws/smithy/kotlin/runtime/collections/AttributeKey;Ljava/lang/Object;)V diff --git a/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtils.kt b/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtils.kt index 4ae611c8fa..d66d0cba49 100644 --- a/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtils.kt +++ b/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtils.kt @@ -81,27 +81,9 @@ public enum class SmithyBusinessMetric(public override val identifier: String) : /** * Emits a business metric if the current [Attributes] instance is of type [ExecutionContext]. */ -public fun Attributes.emitBusinessMetric(metric: BusinessMetric) { - if (this is ExecutionContext) this.emitBusinessMetric(metric) -} - -/** - * Creates a copy of an [Attributes] business metrics. - * Returns an empty set if no business metrics are available. - */ -public fun Attributes.copyBusinessMetrics(): MutableSet { - val copy = mutableSetOf() - if (!this.contains(BusinessMetrics)) return copy - - this[BusinessMetrics].forEach { metric -> - copy.add(metric) +@InternalApi +public fun Attributes.emitBusinessMetric(metric: BusinessMetric): Unit = + when (this) { + is ExecutionContext -> this.emitBusinessMetric(metric) + else -> throw UnsupportedOperationException("Business metric emission is supported only for attributes of type 'execution context'") } - return copy -} - -/** - * Merges another [Attributes] business metrics into this instance of [MutableAttributes]. - */ -public fun MutableAttributes.mergeBusinessMetrics(other: Attributes) { - this[BusinessMetrics] = this[BusinessMetrics].union(other[BusinessMetrics]).toMutableSet() -} diff --git a/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/collections/Attributes.kt b/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/collections/Attributes.kt index 2404ad6697..1fffcc82a2 100644 --- a/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/collections/Attributes.kt +++ b/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/collections/Attributes.kt @@ -115,18 +115,6 @@ public fun MutableAttributes.merge(other: Attributes) { } } -/** - * Merge another attributes instance into this set of attributes favoring [other] except for any keys that are - * specified in the [exceptions] set - */ -public fun MutableAttributes.mergeExcept(other: Attributes, exceptions: Set>) { - other.keys.forEach { - if (it in exceptions) return@forEach - @Suppress("UNCHECKED_CAST") - set(it as AttributeKey, other[it]) - } -} - private class AttributesImpl constructor(seed: Attributes) : MutableAttributes { private val map: MutableMap, Any> = mutableMapOf() constructor() : this(emptyAttributes()) From 69a9e1bb422b24f6fc2a0d45a320c84592a36494 Mon Sep 17 00:00:00 2001 From: 0marperez Date: Thu, 24 Oct 2024 10:53:41 -0400 Subject: [PATCH 07/18] Fix type bug throwing unnecessary exception --- .../runtime/businessmetrics/BusinessMetricsUtils.kt | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtils.kt b/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtils.kt index d66d0cba49..a7b7243984 100644 --- a/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtils.kt +++ b/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtils.kt @@ -82,8 +82,6 @@ public enum class SmithyBusinessMetric(public override val identifier: String) : * Emits a business metric if the current [Attributes] instance is of type [ExecutionContext]. */ @InternalApi -public fun Attributes.emitBusinessMetric(metric: BusinessMetric): Unit = - when (this) { - is ExecutionContext -> this.emitBusinessMetric(metric) - else -> throw UnsupportedOperationException("Business metric emission is supported only for attributes of type 'execution context'") - } +public fun Attributes.emitBusinessMetric(metric: BusinessMetric) { + if (this is ExecutionContext) this.emitBusinessMetric(metric) +} From eb32e8f41589b17f1d671d67c7dcc63059dfe30f Mon Sep 17 00:00:00 2001 From: 0marperez Date: Mon, 28 Oct 2024 16:16:58 -0400 Subject: [PATCH 08/18] Remove * import --- .../aws/smithy/kotlin/runtime/identity/IdentityProviderChain.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/auth/identity-api/common/src/aws/smithy/kotlin/runtime/identity/IdentityProviderChain.kt b/runtime/auth/identity-api/common/src/aws/smithy/kotlin/runtime/identity/IdentityProviderChain.kt index d84b81f047..fd9246090b 100644 --- a/runtime/auth/identity-api/common/src/aws/smithy/kotlin/runtime/identity/IdentityProviderChain.kt +++ b/runtime/auth/identity-api/common/src/aws/smithy/kotlin/runtime/identity/IdentityProviderChain.kt @@ -6,7 +6,7 @@ package aws.smithy.kotlin.runtime.identity import aws.smithy.kotlin.runtime.InternalApi -import aws.smithy.kotlin.runtime.collections.* +import aws.smithy.kotlin.runtime.collections.Attributes import aws.smithy.kotlin.runtime.io.Closeable import aws.smithy.kotlin.runtime.telemetry.logging.logger import aws.smithy.kotlin.runtime.telemetry.trace.withSpan From 6c022f0f7385d0b96698d15a1c60a7ba5c2d9372 Mon Sep 17 00:00:00 2001 From: 0marperez Date: Thu, 31 Oct 2024 16:46:27 -0400 Subject: [PATCH 09/18] Move credentials business metrics to credentials attributes --- .../http/operation/SdkOperationExecution.kt | 20 ++++++++ runtime/runtime-core/api/runtime-core.api | 13 +++-- .../businessmetrics/BusinessMetricsUtils.kt | 49 +++++++++++-------- 3 files changed, 57 insertions(+), 25 deletions(-) diff --git a/runtime/protocol/http-client/common/src/aws/smithy/kotlin/runtime/http/operation/SdkOperationExecution.kt b/runtime/protocol/http-client/common/src/aws/smithy/kotlin/runtime/http/operation/SdkOperationExecution.kt index eb31dac605..bc6f19212e 100644 --- a/runtime/protocol/http-client/common/src/aws/smithy/kotlin/runtime/http/operation/SdkOperationExecution.kt +++ b/runtime/protocol/http-client/common/src/aws/smithy/kotlin/runtime/http/operation/SdkOperationExecution.kt @@ -6,6 +6,7 @@ package aws.smithy.kotlin.runtime.http.operation import aws.smithy.kotlin.runtime.InternalApi +import aws.smithy.kotlin.runtime.businessmetrics.BusinessMetrics import aws.smithy.kotlin.runtime.businessmetrics.SmithyBusinessMetric import aws.smithy.kotlin.runtime.businessmetrics.emitBusinessMetric import aws.smithy.kotlin.runtime.client.LogMode @@ -13,6 +14,7 @@ import aws.smithy.kotlin.runtime.client.endpoints.authOptions import aws.smithy.kotlin.runtime.client.logMode import aws.smithy.kotlin.runtime.collections.attributesOf import aws.smithy.kotlin.runtime.collections.emptyAttributes +import aws.smithy.kotlin.runtime.collections.get import aws.smithy.kotlin.runtime.collections.merge import aws.smithy.kotlin.runtime.http.* import aws.smithy.kotlin.runtime.http.auth.SignHttpRequest @@ -23,9 +25,11 @@ import aws.smithy.kotlin.runtime.http.request.dumpRequest import aws.smithy.kotlin.runtime.http.request.immutableView import aws.smithy.kotlin.runtime.http.request.toBuilder import aws.smithy.kotlin.runtime.http.response.dumpResponse +import aws.smithy.kotlin.runtime.identity.Identity import aws.smithy.kotlin.runtime.io.Handler import aws.smithy.kotlin.runtime.io.middleware.Middleware import aws.smithy.kotlin.runtime.io.middleware.Phase +import aws.smithy.kotlin.runtime.operation.ExecutionContext import aws.smithy.kotlin.runtime.retries.RetryStrategy import aws.smithy.kotlin.runtime.retries.StandardRetryStrategy import aws.smithy.kotlin.runtime.retries.policy.RetryPolicy @@ -285,6 +289,9 @@ internal class AuthHandler( identityProvider.resolve(request.context) } + // emit identity business metrics + emitIdentityBusinessMetrics(identity, request.context) + val resolveEndpointReq = ResolveEndpointRequest(request.context, request.subject.immutableView(), identity) if (endpointResolver != null) { @@ -408,3 +415,16 @@ private class InterceptorTransmitMiddleware( return call } } + +/** + * Emits an [Identity]'s attributes [BusinessMetrics] into an [ExecutionContext] + */ +private fun emitIdentityBusinessMetrics(identity: Identity, context: ExecutionContext) { // TODO: Add some E2E tests for this ! + val identityAttributes = identity.attributes + + if (identityAttributes.contains(BusinessMetrics)) { + identityAttributes[BusinessMetrics].forEach { metric -> + context.emitBusinessMetric(metric) + } + } +} diff --git a/runtime/runtime-core/api/runtime-core.api b/runtime/runtime-core/api/runtime-core.api index 7a46b10d3b..b606fa07f4 100644 --- a/runtime/runtime-core/api/runtime-core.api +++ b/runtime/runtime-core/api/runtime-core.api @@ -82,13 +82,14 @@ public abstract interface class aws/smithy/kotlin/runtime/businessmetrics/Busine } public final class aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtilsKt { - public static final fun containsBusinessMetric (Laws/smithy/kotlin/runtime/operation/ExecutionContext;Laws/smithy/kotlin/runtime/businessmetrics/BusinessMetric;)Z - public static final fun emitBusinessMetric (Laws/smithy/kotlin/runtime/collections/Attributes;Laws/smithy/kotlin/runtime/businessmetrics/BusinessMetric;)V - public static final fun emitBusinessMetric (Laws/smithy/kotlin/runtime/operation/ExecutionContext;Laws/smithy/kotlin/runtime/businessmetrics/BusinessMetric;)V + public static final fun containsBusinessMetric (Laws/smithy/kotlin/runtime/collections/Attributes;Laws/smithy/kotlin/runtime/businessmetrics/BusinessMetric;)Z + public static final fun emitBusinessMetric (Laws/smithy/kotlin/runtime/collections/MutableAttributes;Laws/smithy/kotlin/runtime/businessmetrics/BusinessMetric;)V + public static final fun emitBusinessMetric (Laws/smithy/kotlin/runtime/collections/MutableAttributes;Ljava/lang/String;)V + public static final fun emitBusinessMetrics (Laws/smithy/kotlin/runtime/collections/MutableAttributes;Ljava/util/Set;)V public static final fun getAccountIdBasedEndpointAccountId ()Laws/smithy/kotlin/runtime/collections/AttributeKey; public static final fun getBusinessMetrics ()Laws/smithy/kotlin/runtime/collections/AttributeKey; public static final fun getServiceEndpointOverride ()Laws/smithy/kotlin/runtime/collections/AttributeKey; - public static final fun removeBusinessMetric (Laws/smithy/kotlin/runtime/operation/ExecutionContext;Laws/smithy/kotlin/runtime/businessmetrics/BusinessMetric;)V + public static final fun removeBusinessMetric (Laws/smithy/kotlin/runtime/collections/MutableAttributes;Laws/smithy/kotlin/runtime/businessmetrics/BusinessMetric;)V } public final class aws/smithy/kotlin/runtime/businessmetrics/SmithyBusinessMetric : java/lang/Enum, aws/smithy/kotlin/runtime/businessmetrics/BusinessMetric { @@ -2049,6 +2050,10 @@ public final class aws/smithy/kotlin/runtime/smoketests/SmokeTestsFunctionsJVMKt public static final fun exitProcess (I)Ljava/lang/Void; } +public final class aws/smithy/kotlin/runtime/smoketests/SmokeTestsFunctionsKt { + public static final fun printExceptionStackTrace (Ljava/lang/Exception;)V +} + public final class aws/smithy/kotlin/runtime/text/Scanner { public fun (Ljava/lang/String;)V public final fun getText ()Ljava/lang/String; diff --git a/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtils.kt b/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtils.kt index a7b7243984..4c323373bf 100644 --- a/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtils.kt +++ b/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtils.kt @@ -6,7 +6,6 @@ package aws.smithy.kotlin.runtime.businessmetrics import aws.smithy.kotlin.runtime.InternalApi import aws.smithy.kotlin.runtime.collections.* -import aws.smithy.kotlin.runtime.operation.ExecutionContext /** * Keeps track of all business metrics along an operations execution @@ -27,33 +26,49 @@ public val AccountIdBasedEndpointAccountId: AttributeKey = AttributeKey( public val ServiceEndpointOverride: AttributeKey = AttributeKey("aws.smithy.kotlin#ServiceEndpointOverride") /** - * Emit a business metric to the execution context attributes + * Emits a business metric into [MutableAttributes] + * @param identifier The identifier of the [BusinessMetric] to be emitted. */ @InternalApi -public fun ExecutionContext.emitBusinessMetric(metric: BusinessMetric) { - if (this.attributes.contains(BusinessMetrics)) { - this.attributes[BusinessMetrics].add(metric.identifier) +public fun MutableAttributes.emitBusinessMetric(identifier: String) { + if (this.contains(BusinessMetrics)) { + this[BusinessMetrics].add(identifier) } else { - this.attributes[BusinessMetrics] = mutableSetOf(metric.identifier) + this[BusinessMetrics] = mutableSetOf(identifier) } } /** - * Removes a business metric from the execution context attributes + * Emits a business metric into [MutableAttributes] + * @param metric The [BusinessMetric] to be emitted. */ @InternalApi -public fun ExecutionContext.removeBusinessMetric(metric: BusinessMetric) { - if (this.attributes.contains(BusinessMetrics)) { - this.attributes[BusinessMetrics].remove(metric.identifier) +public fun MutableAttributes.emitBusinessMetric(metric: BusinessMetric): Unit = this.emitBusinessMetric(metric.identifier) + +/** + * Emits business metrics into [MutableAttributes] + * @param metrics The [BusinessMetric]s to be emitted. + */ +@InternalApi +public fun MutableAttributes.emitBusinessMetrics(metrics: Set): Unit = + metrics.forEach { emitBusinessMetric(it) } + +/** + * Removes a business metric from the [MutableAttributes] + */ +@InternalApi +public fun MutableAttributes.removeBusinessMetric(metric: BusinessMetric) { + if (this.contains(BusinessMetrics)) { + this[BusinessMetrics].remove(metric.identifier) } } /** - * Checks if a business metric exists in the execution context attributes + * Checks if a business metric exists in the [Attributes] */ @InternalApi -public fun ExecutionContext.containsBusinessMetric(metric: BusinessMetric): Boolean = - (this.attributes.contains(BusinessMetrics)) && this.attributes[BusinessMetrics].contains(metric.identifier) +public fun Attributes.containsBusinessMetric(metric: BusinessMetric): Boolean = + (this.contains(BusinessMetrics)) && this[BusinessMetrics].contains(metric.identifier) /** * Valid business metrics @@ -77,11 +92,3 @@ public enum class SmithyBusinessMetric(public override val identifier: String) : ACCOUNT_ID_BASED_ENDPOINT("O"), SIGV4A_SIGNING("S"), } - -/** - * Emits a business metric if the current [Attributes] instance is of type [ExecutionContext]. - */ -@InternalApi -public fun Attributes.emitBusinessMetric(metric: BusinessMetric) { - if (this is ExecutionContext) this.emitBusinessMetric(metric) -} From 2c09d379eed14c1a38b4ddade4971ae9f2c5f44d Mon Sep 17 00:00:00 2001 From: 0marperez Date: Thu, 31 Oct 2024 16:48:12 -0400 Subject: [PATCH 10/18] Remove TODO --- .../kotlin/runtime/http/operation/SdkOperationExecution.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/protocol/http-client/common/src/aws/smithy/kotlin/runtime/http/operation/SdkOperationExecution.kt b/runtime/protocol/http-client/common/src/aws/smithy/kotlin/runtime/http/operation/SdkOperationExecution.kt index bc6f19212e..a765595b03 100644 --- a/runtime/protocol/http-client/common/src/aws/smithy/kotlin/runtime/http/operation/SdkOperationExecution.kt +++ b/runtime/protocol/http-client/common/src/aws/smithy/kotlin/runtime/http/operation/SdkOperationExecution.kt @@ -419,7 +419,7 @@ private class InterceptorTransmitMiddleware( /** * Emits an [Identity]'s attributes [BusinessMetrics] into an [ExecutionContext] */ -private fun emitIdentityBusinessMetrics(identity: Identity, context: ExecutionContext) { // TODO: Add some E2E tests for this ! +private fun emitIdentityBusinessMetrics(identity: Identity, context: ExecutionContext) { val identityAttributes = identity.attributes if (identityAttributes.contains(BusinessMetrics)) { From 69466cfe4aac066dbfab1180069dfb542aca5f99 Mon Sep 17 00:00:00 2001 From: 0marperez Date: Thu, 31 Oct 2024 17:12:00 -0400 Subject: [PATCH 11/18] Undo commit from main --- .../runtime/http/operation/SdkOperationExecution.kt | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/runtime/protocol/http-client/common/src/aws/smithy/kotlin/runtime/http/operation/SdkOperationExecution.kt b/runtime/protocol/http-client/common/src/aws/smithy/kotlin/runtime/http/operation/SdkOperationExecution.kt index a765595b03..abd49a7628 100644 --- a/runtime/protocol/http-client/common/src/aws/smithy/kotlin/runtime/http/operation/SdkOperationExecution.kt +++ b/runtime/protocol/http-client/common/src/aws/smithy/kotlin/runtime/http/operation/SdkOperationExecution.kt @@ -423,8 +423,11 @@ private fun emitIdentityBusinessMetrics(identity: Identity, context: ExecutionCo val identityAttributes = identity.attributes if (identityAttributes.contains(BusinessMetrics)) { - identityAttributes[BusinessMetrics].forEach { metric -> - context.emitBusinessMetric(metric) - } + identityAttributes[BusinessMetrics] + .toList() + .reversed() + .forEach { metric -> + context.emitBusinessMetric(metric) + } } } From 598b7150277752a775563439ec7cc5acddc27a1b Mon Sep 17 00:00:00 2001 From: 0marperez Date: Thu, 31 Oct 2024 17:21:08 -0400 Subject: [PATCH 12/18] Clean and API dump --- runtime/runtime-core/api/runtime-core.api | 4 ---- 1 file changed, 4 deletions(-) diff --git a/runtime/runtime-core/api/runtime-core.api b/runtime/runtime-core/api/runtime-core.api index b606fa07f4..1fa17d0da0 100644 --- a/runtime/runtime-core/api/runtime-core.api +++ b/runtime/runtime-core/api/runtime-core.api @@ -2050,10 +2050,6 @@ public final class aws/smithy/kotlin/runtime/smoketests/SmokeTestsFunctionsJVMKt public static final fun exitProcess (I)Ljava/lang/Void; } -public final class aws/smithy/kotlin/runtime/smoketests/SmokeTestsFunctionsKt { - public static final fun printExceptionStackTrace (Ljava/lang/Exception;)V -} - public final class aws/smithy/kotlin/runtime/text/Scanner { public fun (Ljava/lang/String;)V public final fun getText ()Ljava/lang/String; From a7e2c7e6d4d9c104d86742d09f76c885fc4abe22 Mon Sep 17 00:00:00 2001 From: 0marperez Date: Thu, 31 Oct 2024 19:48:30 -0400 Subject: [PATCH 13/18] Remove breaking changes --- runtime/runtime-core/api/runtime-core.api | 8 +-- .../businessmetrics/BusinessMetricsUtils.kt | 53 +++++++++++-------- 2 files changed, 36 insertions(+), 25 deletions(-) diff --git a/runtime/runtime-core/api/runtime-core.api b/runtime/runtime-core/api/runtime-core.api index 1fa17d0da0..703a1091dd 100644 --- a/runtime/runtime-core/api/runtime-core.api +++ b/runtime/runtime-core/api/runtime-core.api @@ -82,14 +82,14 @@ public abstract interface class aws/smithy/kotlin/runtime/businessmetrics/Busine } public final class aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtilsKt { - public static final fun containsBusinessMetric (Laws/smithy/kotlin/runtime/collections/Attributes;Laws/smithy/kotlin/runtime/businessmetrics/BusinessMetric;)Z - public static final fun emitBusinessMetric (Laws/smithy/kotlin/runtime/collections/MutableAttributes;Laws/smithy/kotlin/runtime/businessmetrics/BusinessMetric;)V + public static final fun containsBusinessMetric (Laws/smithy/kotlin/runtime/operation/ExecutionContext;Laws/smithy/kotlin/runtime/businessmetrics/BusinessMetric;)Z public static final fun emitBusinessMetric (Laws/smithy/kotlin/runtime/collections/MutableAttributes;Ljava/lang/String;)V - public static final fun emitBusinessMetrics (Laws/smithy/kotlin/runtime/collections/MutableAttributes;Ljava/util/Set;)V + public static final fun emitBusinessMetric (Laws/smithy/kotlin/runtime/operation/ExecutionContext;Laws/smithy/kotlin/runtime/businessmetrics/BusinessMetric;)V + public static final fun emitBusinessMetric (Laws/smithy/kotlin/runtime/operation/ExecutionContext;Ljava/lang/String;)V public static final fun getAccountIdBasedEndpointAccountId ()Laws/smithy/kotlin/runtime/collections/AttributeKey; public static final fun getBusinessMetrics ()Laws/smithy/kotlin/runtime/collections/AttributeKey; public static final fun getServiceEndpointOverride ()Laws/smithy/kotlin/runtime/collections/AttributeKey; - public static final fun removeBusinessMetric (Laws/smithy/kotlin/runtime/collections/MutableAttributes;Laws/smithy/kotlin/runtime/businessmetrics/BusinessMetric;)V + public static final fun removeBusinessMetric (Laws/smithy/kotlin/runtime/operation/ExecutionContext;Laws/smithy/kotlin/runtime/businessmetrics/BusinessMetric;)V } public final class aws/smithy/kotlin/runtime/businessmetrics/SmithyBusinessMetric : java/lang/Enum, aws/smithy/kotlin/runtime/businessmetrics/BusinessMetric { diff --git a/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtils.kt b/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtils.kt index 4c323373bf..136d3784b0 100644 --- a/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtils.kt +++ b/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtils.kt @@ -5,7 +5,10 @@ package aws.smithy.kotlin.runtime.businessmetrics import aws.smithy.kotlin.runtime.InternalApi -import aws.smithy.kotlin.runtime.collections.* +import aws.smithy.kotlin.runtime.collections.AttributeKey +import aws.smithy.kotlin.runtime.collections.MutableAttributes +import aws.smithy.kotlin.runtime.collections.get +import aws.smithy.kotlin.runtime.operation.ExecutionContext /** * Keeps track of all business metrics along an operations execution @@ -26,49 +29,57 @@ public val AccountIdBasedEndpointAccountId: AttributeKey = AttributeKey( public val ServiceEndpointOverride: AttributeKey = AttributeKey("aws.smithy.kotlin#ServiceEndpointOverride") /** - * Emits a business metric into [MutableAttributes] - * @param identifier The identifier of the [BusinessMetric] to be emitted. + * Emit a business metric to the execution context attributes */ @InternalApi -public fun MutableAttributes.emitBusinessMetric(identifier: String) { - if (this.contains(BusinessMetrics)) { - this[BusinessMetrics].add(identifier) +public fun ExecutionContext.emitBusinessMetric(metric: BusinessMetric) { + if (this.attributes.contains(BusinessMetrics)) { + this.attributes[BusinessMetrics].add(metric.identifier) } else { - this[BusinessMetrics] = mutableSetOf(identifier) + this.attributes[BusinessMetrics] = mutableSetOf(metric.identifier) } } /** - * Emits a business metric into [MutableAttributes] - * @param metric The [BusinessMetric] to be emitted. + * Emit a business metric to the execution context attributes using its identifier */ @InternalApi -public fun MutableAttributes.emitBusinessMetric(metric: BusinessMetric): Unit = this.emitBusinessMetric(metric.identifier) +public fun ExecutionContext.emitBusinessMetric(identifier: String) { + if (this.attributes.contains(BusinessMetrics)) { + this.attributes[BusinessMetrics].add(identifier) + } else { + this.attributes[BusinessMetrics] = mutableSetOf(identifier) + } +} /** - * Emits business metrics into [MutableAttributes] - * @param metrics The [BusinessMetric]s to be emitted. + * Emit a business metric to the mutable attributes */ @InternalApi -public fun MutableAttributes.emitBusinessMetrics(metrics: Set): Unit = - metrics.forEach { emitBusinessMetric(it) } +public fun MutableAttributes.emitBusinessMetric(identifier: String) { + if (this.contains(BusinessMetrics)) { + this[BusinessMetrics].add(identifier) + } else { + this[BusinessMetrics] = mutableSetOf(identifier) + } +} /** - * Removes a business metric from the [MutableAttributes] + * Removes a business metric from the execution context attributes */ @InternalApi -public fun MutableAttributes.removeBusinessMetric(metric: BusinessMetric) { - if (this.contains(BusinessMetrics)) { - this[BusinessMetrics].remove(metric.identifier) +public fun ExecutionContext.removeBusinessMetric(metric: BusinessMetric) { + if (this.attributes.contains(BusinessMetrics)) { + this.attributes[BusinessMetrics].remove(metric.identifier) } } /** - * Checks if a business metric exists in the [Attributes] + * Checks if a business metric exists in the execution context attributes */ @InternalApi -public fun Attributes.containsBusinessMetric(metric: BusinessMetric): Boolean = - (this.contains(BusinessMetrics)) && this[BusinessMetrics].contains(metric.identifier) +public fun ExecutionContext.containsBusinessMetric(metric: BusinessMetric): Boolean = + (this.attributes.contains(BusinessMetrics)) && this.attributes[BusinessMetrics].contains(metric.identifier) /** * Valid business metrics From b5a4a389ce15e8a90ba96138c2a15baee7648158 Mon Sep 17 00:00:00 2001 From: 0marperez Date: Thu, 31 Oct 2024 19:59:53 -0400 Subject: [PATCH 14/18] One liner for emitIdentityBusinessMetrics --- .../http/operation/SdkOperationExecution.kt | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/runtime/protocol/http-client/common/src/aws/smithy/kotlin/runtime/http/operation/SdkOperationExecution.kt b/runtime/protocol/http-client/common/src/aws/smithy/kotlin/runtime/http/operation/SdkOperationExecution.kt index abd49a7628..964bb275f7 100644 --- a/runtime/protocol/http-client/common/src/aws/smithy/kotlin/runtime/http/operation/SdkOperationExecution.kt +++ b/runtime/protocol/http-client/common/src/aws/smithy/kotlin/runtime/http/operation/SdkOperationExecution.kt @@ -14,7 +14,6 @@ import aws.smithy.kotlin.runtime.client.endpoints.authOptions import aws.smithy.kotlin.runtime.client.logMode import aws.smithy.kotlin.runtime.collections.attributesOf import aws.smithy.kotlin.runtime.collections.emptyAttributes -import aws.smithy.kotlin.runtime.collections.get import aws.smithy.kotlin.runtime.collections.merge import aws.smithy.kotlin.runtime.http.* import aws.smithy.kotlin.runtime.http.auth.SignHttpRequest @@ -419,15 +418,5 @@ private class InterceptorTransmitMiddleware( /** * Emits an [Identity]'s attributes [BusinessMetrics] into an [ExecutionContext] */ -private fun emitIdentityBusinessMetrics(identity: Identity, context: ExecutionContext) { - val identityAttributes = identity.attributes - - if (identityAttributes.contains(BusinessMetrics)) { - identityAttributes[BusinessMetrics] - .toList() - .reversed() - .forEach { metric -> - context.emitBusinessMetric(metric) - } - } -} +private fun emitIdentityBusinessMetrics(identity: Identity, context: ExecutionContext) = + identity.attributes.getOrNull(BusinessMetrics)?.toList()?.reversed()?.forEach(context::emitBusinessMetric) From 1cc05b14af42d78b0cccd38663102d184e1da2c1 Mon Sep 17 00:00:00 2001 From: 0marperez Date: Thu, 31 Oct 2024 20:02:01 -0400 Subject: [PATCH 15/18] Ktlint --- .../kotlin/runtime/http/operation/SdkOperationExecution.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/protocol/http-client/common/src/aws/smithy/kotlin/runtime/http/operation/SdkOperationExecution.kt b/runtime/protocol/http-client/common/src/aws/smithy/kotlin/runtime/http/operation/SdkOperationExecution.kt index 964bb275f7..86794a641e 100644 --- a/runtime/protocol/http-client/common/src/aws/smithy/kotlin/runtime/http/operation/SdkOperationExecution.kt +++ b/runtime/protocol/http-client/common/src/aws/smithy/kotlin/runtime/http/operation/SdkOperationExecution.kt @@ -419,4 +419,4 @@ private class InterceptorTransmitMiddleware( * Emits an [Identity]'s attributes [BusinessMetrics] into an [ExecutionContext] */ private fun emitIdentityBusinessMetrics(identity: Identity, context: ExecutionContext) = - identity.attributes.getOrNull(BusinessMetrics)?.toList()?.reversed()?.forEach(context::emitBusinessMetric) + identity.attributes.getOrNull(BusinessMetrics)?.toList()?.reversed()?.forEach(context::emitBusinessMetric) From d92f45b6be3036a279ec45818d5e2a6e38b31505 Mon Sep 17 00:00:00 2001 From: 0marperez Date: Thu, 31 Oct 2024 20:57:56 -0400 Subject: [PATCH 16/18] Set -> Set business metrics --- runtime/runtime-core/api/runtime-core.api | 3 +- .../businessmetrics/BusinessMetricsUtils.kt | 28 ++++++------------- .../BusinessMetricsUtilsTest.kt | 10 +++---- 3 files changed, 14 insertions(+), 27 deletions(-) diff --git a/runtime/runtime-core/api/runtime-core.api b/runtime/runtime-core/api/runtime-core.api index 703a1091dd..f096dc5d30 100644 --- a/runtime/runtime-core/api/runtime-core.api +++ b/runtime/runtime-core/api/runtime-core.api @@ -83,9 +83,8 @@ public abstract interface class aws/smithy/kotlin/runtime/businessmetrics/Busine public final class aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtilsKt { public static final fun containsBusinessMetric (Laws/smithy/kotlin/runtime/operation/ExecutionContext;Laws/smithy/kotlin/runtime/businessmetrics/BusinessMetric;)Z - public static final fun emitBusinessMetric (Laws/smithy/kotlin/runtime/collections/MutableAttributes;Ljava/lang/String;)V + public static final fun emitBusinessMetric (Laws/smithy/kotlin/runtime/collections/MutableAttributes;Laws/smithy/kotlin/runtime/businessmetrics/BusinessMetric;)V public static final fun emitBusinessMetric (Laws/smithy/kotlin/runtime/operation/ExecutionContext;Laws/smithy/kotlin/runtime/businessmetrics/BusinessMetric;)V - public static final fun emitBusinessMetric (Laws/smithy/kotlin/runtime/operation/ExecutionContext;Ljava/lang/String;)V public static final fun getAccountIdBasedEndpointAccountId ()Laws/smithy/kotlin/runtime/collections/AttributeKey; public static final fun getBusinessMetrics ()Laws/smithy/kotlin/runtime/collections/AttributeKey; public static final fun getServiceEndpointOverride ()Laws/smithy/kotlin/runtime/collections/AttributeKey; diff --git a/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtils.kt b/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtils.kt index 136d3784b0..394fa6be1a 100644 --- a/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtils.kt +++ b/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtils.kt @@ -14,7 +14,7 @@ import aws.smithy.kotlin.runtime.operation.ExecutionContext * Keeps track of all business metrics along an operations execution */ @InternalApi -public val BusinessMetrics: AttributeKey> = AttributeKey("aws.sdk.kotlin#BusinessMetrics") +public val BusinessMetrics: AttributeKey> = AttributeKey("aws.sdk.kotlin#BusinessMetrics") /** * The account ID in an account ID based endpoint @@ -34,21 +34,9 @@ public val ServiceEndpointOverride: AttributeKey = AttributeKey("aws.sm @InternalApi public fun ExecutionContext.emitBusinessMetric(metric: BusinessMetric) { if (this.attributes.contains(BusinessMetrics)) { - this.attributes[BusinessMetrics].add(metric.identifier) + this.attributes[BusinessMetrics].add(metric) } else { - this.attributes[BusinessMetrics] = mutableSetOf(metric.identifier) - } -} - -/** - * Emit a business metric to the execution context attributes using its identifier - */ -@InternalApi -public fun ExecutionContext.emitBusinessMetric(identifier: String) { - if (this.attributes.contains(BusinessMetrics)) { - this.attributes[BusinessMetrics].add(identifier) - } else { - this.attributes[BusinessMetrics] = mutableSetOf(identifier) + this.attributes[BusinessMetrics] = mutableSetOf(metric) } } @@ -56,11 +44,11 @@ public fun ExecutionContext.emitBusinessMetric(identifier: String) { * Emit a business metric to the mutable attributes */ @InternalApi -public fun MutableAttributes.emitBusinessMetric(identifier: String) { +public fun MutableAttributes.emitBusinessMetric(metric: BusinessMetric) { if (this.contains(BusinessMetrics)) { - this[BusinessMetrics].add(identifier) + this[BusinessMetrics].add(metric) } else { - this[BusinessMetrics] = mutableSetOf(identifier) + this[BusinessMetrics] = mutableSetOf(metric) } } @@ -70,7 +58,7 @@ public fun MutableAttributes.emitBusinessMetric(identifier: String) { @InternalApi public fun ExecutionContext.removeBusinessMetric(metric: BusinessMetric) { if (this.attributes.contains(BusinessMetrics)) { - this.attributes[BusinessMetrics].remove(metric.identifier) + this.attributes[BusinessMetrics].remove(metric) } } @@ -79,7 +67,7 @@ public fun ExecutionContext.removeBusinessMetric(metric: BusinessMetric) { */ @InternalApi public fun ExecutionContext.containsBusinessMetric(metric: BusinessMetric): Boolean = - (this.attributes.contains(BusinessMetrics)) && this.attributes[BusinessMetrics].contains(metric.identifier) + (this.attributes.contains(BusinessMetrics)) && this.attributes[BusinessMetrics].contains(metric) /** * Valid business metrics diff --git a/runtime/runtime-core/common/test/aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtilsTest.kt b/runtime/runtime-core/common/test/aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtilsTest.kt index f7ac2717c1..f5ff6ac727 100644 --- a/runtime/runtime-core/common/test/aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtilsTest.kt +++ b/runtime/runtime-core/common/test/aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtilsTest.kt @@ -17,7 +17,7 @@ class BusinessMetricsUtilsTest { executionContext.emitBusinessMetric(SmithyBusinessMetric.GZIP_REQUEST_COMPRESSION) assertTrue(executionContext.attributes.contains(BusinessMetrics)) - assertTrue(executionContext.attributes[BusinessMetrics].contains(SmithyBusinessMetric.GZIP_REQUEST_COMPRESSION.identifier)) + assertTrue(executionContext.attributes[BusinessMetrics].contains(SmithyBusinessMetric.GZIP_REQUEST_COMPRESSION)) } @Test @@ -27,8 +27,8 @@ class BusinessMetricsUtilsTest { executionContext.emitBusinessMetric(SmithyBusinessMetric.SIGV4A_SIGNING) assertTrue(executionContext.attributes.contains(BusinessMetrics)) - assertTrue(executionContext.attributes[BusinessMetrics].contains(SmithyBusinessMetric.GZIP_REQUEST_COMPRESSION.identifier)) - assertTrue(executionContext.attributes[BusinessMetrics].contains(SmithyBusinessMetric.SIGV4A_SIGNING.identifier)) + assertTrue(executionContext.attributes[BusinessMetrics].contains(SmithyBusinessMetric.GZIP_REQUEST_COMPRESSION)) + assertTrue(executionContext.attributes[BusinessMetrics].contains(SmithyBusinessMetric.SIGV4A_SIGNING)) } @Test @@ -37,12 +37,12 @@ class BusinessMetricsUtilsTest { executionContext.emitBusinessMetric(SmithyBusinessMetric.GZIP_REQUEST_COMPRESSION) assertTrue(executionContext.attributes.contains(BusinessMetrics)) - assertTrue(executionContext.attributes[BusinessMetrics].contains(SmithyBusinessMetric.GZIP_REQUEST_COMPRESSION.identifier)) + assertTrue(executionContext.attributes[BusinessMetrics].contains(SmithyBusinessMetric.GZIP_REQUEST_COMPRESSION)) executionContext.removeBusinessMetric(SmithyBusinessMetric.GZIP_REQUEST_COMPRESSION) assertTrue(executionContext.attributes.contains(BusinessMetrics)) - assertFalse(executionContext.attributes[BusinessMetrics].contains(SmithyBusinessMetric.GZIP_REQUEST_COMPRESSION.identifier)) + assertFalse(executionContext.attributes[BusinessMetrics].contains(SmithyBusinessMetric.GZIP_REQUEST_COMPRESSION)) } @Test From 266cf9f5289c53f0f2b77df045148c4c4b9146b8 Mon Sep 17 00:00:00 2001 From: 0marperez Date: Thu, 31 Oct 2024 21:12:52 -0400 Subject: [PATCH 17/18] Self review --- .../kotlin/runtime/http/operation/SdkOperationExecution.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/protocol/http-client/common/src/aws/smithy/kotlin/runtime/http/operation/SdkOperationExecution.kt b/runtime/protocol/http-client/common/src/aws/smithy/kotlin/runtime/http/operation/SdkOperationExecution.kt index 86794a641e..d30c7c546b 100644 --- a/runtime/protocol/http-client/common/src/aws/smithy/kotlin/runtime/http/operation/SdkOperationExecution.kt +++ b/runtime/protocol/http-client/common/src/aws/smithy/kotlin/runtime/http/operation/SdkOperationExecution.kt @@ -416,7 +416,7 @@ private class InterceptorTransmitMiddleware( } /** - * Emits an [Identity]'s attributes [BusinessMetrics] into an [ExecutionContext] + * Emits an [Identity]'s attributes' [BusinessMetrics] into the [ExecutionContext] */ private fun emitIdentityBusinessMetrics(identity: Identity, context: ExecutionContext) = identity.attributes.getOrNull(BusinessMetrics)?.toList()?.reversed()?.forEach(context::emitBusinessMetric) From b7e1165b2da4ba6de6f0908c73cb71c346f7be1d Mon Sep 17 00:00:00 2001 From: 0marperez Date: Mon, 4 Nov 2024 13:58:10 -0500 Subject: [PATCH 18/18] Fix attribute key --- .../kotlin/runtime/businessmetrics/BusinessMetricsUtils.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtils.kt b/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtils.kt index 394fa6be1a..774b0c9d9f 100644 --- a/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtils.kt +++ b/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtils.kt @@ -14,7 +14,7 @@ import aws.smithy.kotlin.runtime.operation.ExecutionContext * Keeps track of all business metrics along an operations execution */ @InternalApi -public val BusinessMetrics: AttributeKey> = AttributeKey("aws.sdk.kotlin#BusinessMetrics") +public val BusinessMetrics: AttributeKey> = AttributeKey("aws.smithy.kotlin#BusinessMetrics") /** * The account ID in an account ID based endpoint