Skip to content

Commit 169c96a

Browse files
committed
Bug fixes & refactoring of new business metrics utils
1 parent 980abe6 commit 169c96a

File tree

4 files changed

+19
-29
lines changed

4 files changed

+19
-29
lines changed

runtime/auth/identity-api/common/src/aws/smithy/kotlin/runtime/identity/IdentityProviderChain.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
package aws.smithy.kotlin.runtime.identity
77

88
import aws.smithy.kotlin.runtime.InternalApi
9+
import aws.smithy.kotlin.runtime.businessmetrics.BusinessMetrics
10+
import aws.smithy.kotlin.runtime.businessmetrics.copyBusinessMetrics
911
import aws.smithy.kotlin.runtime.collections.*
1012
import aws.smithy.kotlin.runtime.io.Closeable
1113
import aws.smithy.kotlin.runtime.telemetry.logging.logger
@@ -38,15 +40,14 @@ public abstract class IdentityProviderChain<P : IdentityProvider, I : Identity>(
3840
for (provider in providers) {
3941
logger.trace { "Attempting to resolve identity from $provider" }
4042

41-
val attributesAreMutable = attributes is MutableAttributes
42-
val attributesSnapshot = if (attributesAreMutable) attributes.copy() else null
43+
val businessMetricsSnapshot = attributes.copyBusinessMetrics()
4344

4445
try {
4546
@Suppress("UNCHECKED_CAST")
4647
return@withSpan provider.resolve(attributes) as I
4748
} catch (ex: Exception) {
4849
logger.debug { "unable to resolve identity from $provider: ${ex.message}" }
49-
if (attributesAreMutable) (attributes as MutableAttributes).resetTo(attributesSnapshot!!)
50+
if (attributes is MutableAttributes) attributes[BusinessMetrics] = businessMetricsSnapshot
5051
chainException.value.addSuppressed(ex)
5152
}
5253
}

runtime/runtime-core/api/runtime-core.api

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public abstract interface class aws/smithy/kotlin/runtime/businessmetrics/Busine
8383

8484
public final class aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtilsKt {
8585
public static final fun containsBusinessMetric (Laws/smithy/kotlin/runtime/operation/ExecutionContext;Laws/smithy/kotlin/runtime/businessmetrics/BusinessMetric;)Z
86+
public static final fun copyBusinessMetrics (Laws/smithy/kotlin/runtime/collections/Attributes;)Ljava/util/Set;
8687
public static final fun emitBusinessMetric (Laws/smithy/kotlin/runtime/collections/Attributes;Laws/smithy/kotlin/runtime/businessmetrics/BusinessMetric;)V
8788
public static final fun emitBusinessMetric (Laws/smithy/kotlin/runtime/operation/ExecutionContext;Laws/smithy/kotlin/runtime/businessmetrics/BusinessMetric;)V
8889
public static final fun getAccountIdBasedEndpointAccountId ()Laws/smithy/kotlin/runtime/collections/AttributeKey;
@@ -134,7 +135,6 @@ public final class aws/smithy/kotlin/runtime/collections/AttributesBuilder {
134135

135136
public final class aws/smithy/kotlin/runtime/collections/AttributesKt {
136137
public static final fun attributesOf (Lkotlin/jvm/functions/Function1;)Laws/smithy/kotlin/runtime/collections/Attributes;
137-
public static final fun copy (Laws/smithy/kotlin/runtime/collections/Attributes;)Laws/smithy/kotlin/runtime/collections/Attributes;
138138
public static final fun emptyAttributes ()Laws/smithy/kotlin/runtime/collections/Attributes;
139139
public static final fun get (Laws/smithy/kotlin/runtime/collections/Attributes;Laws/smithy/kotlin/runtime/collections/AttributeKey;)Ljava/lang/Object;
140140
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 {
143143
public static final fun mutableAttributesOf (Lkotlin/jvm/functions/Function1;)Laws/smithy/kotlin/runtime/collections/MutableAttributes;
144144
public static final fun putIfAbsent (Laws/smithy/kotlin/runtime/collections/MutableAttributes;Laws/smithy/kotlin/runtime/collections/AttributeKey;Ljava/lang/Object;)V
145145
public static final fun putIfAbsentNotNull (Laws/smithy/kotlin/runtime/collections/MutableAttributes;Laws/smithy/kotlin/runtime/collections/AttributeKey;Ljava/lang/Object;)V
146-
public static final fun resetTo (Laws/smithy/kotlin/runtime/collections/MutableAttributes;Laws/smithy/kotlin/runtime/collections/Attributes;)V
147146
public static final fun setIfValueNotNull (Laws/smithy/kotlin/runtime/collections/MutableAttributes;Laws/smithy/kotlin/runtime/collections/AttributeKey;Ljava/lang/Object;)V
148147
public static final fun take (Laws/smithy/kotlin/runtime/collections/MutableAttributes;Laws/smithy/kotlin/runtime/collections/AttributeKey;)Ljava/lang/Object;
149148
public static final fun takeOrNull (Laws/smithy/kotlin/runtime/collections/MutableAttributes;Laws/smithy/kotlin/runtime/collections/AttributeKey;)Ljava/lang/Object;

runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtils.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,17 @@ public enum class SmithyBusinessMetric(public override val identifier: String) :
8484
public fun Attributes.emitBusinessMetric(metric: BusinessMetric) {
8585
if (this is ExecutionContext) this.emitBusinessMetric(metric)
8686
}
87+
88+
/**
89+
* Creates a copy of an [Attributes] business metrics.
90+
* Returns an empty set if no business metrics are available.
91+
*/
92+
public fun Attributes.copyBusinessMetrics(): MutableSet<String> {
93+
val copy = mutableSetOf<String>()
94+
if (!this.contains(BusinessMetrics)) return copy
95+
96+
this[BusinessMetrics].forEach { metric ->
97+
copy.add(metric)
98+
}
99+
return copy
100+
}

runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/collections/Attributes.kt

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -235,27 +235,3 @@ public inline fun attributesOf(block: AttributesBuilder.() -> Unit): Attributes
235235
* Returns a new [MutableAttributes] instance with elements from this set of attributes.
236236
*/
237237
public fun Attributes.toMutableAttributes(): MutableAttributes = AttributesImpl(this)
238-
239-
/**
240-
* Creates a copy of this [Attributes] instance.
241-
*
242-
* This function generates a new [Attributes] instance containing all the key-value pairs
243-
* from the current set of attributes. The new instance is independent of the original,
244-
* meaning changes to the copied attributes will not affect the original set.
245-
*/
246-
public fun Attributes.copy(): Attributes {
247-
val copy = mutableAttributes()
248-
(this.keys as Set<AttributeKey<Any>>).forEach { key ->
249-
copy[key] = this[key]
250-
}
251-
return copy
252-
}
253-
254-
/**
255-
* Resets this [MutableAttributes] instance to the state of the specified [Attributes].
256-
*/
257-
public fun MutableAttributes.resetTo(attributes: Attributes) {
258-
val obsoleteKeys = this.keys.subtract(attributes.keys)
259-
obsoleteKeys.forEach { key -> this.remove(key as AttributeKey<Any>) }
260-
this.merge(attributes)
261-
}

0 commit comments

Comments
 (0)