Skip to content

Commit 864dc65

Browse files
committed
Set<String> -> Set<BusinessMetric> business metrics
1 parent b74a50b commit 864dc65

File tree

6 files changed

+33
-61
lines changed

6 files changed

+33
-61
lines changed

aws-runtime/aws-config/common/src/aws/sdk/kotlin/runtime/auth/credentials/ProfileCredentialsProvider.kt

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import aws.sdk.kotlin.runtime.http.interceptors.businessmetrics.AwsBusinessMetri
1919
import aws.sdk.kotlin.runtime.http.interceptors.businessmetrics.emitBusinessMetrics
2020
import aws.sdk.kotlin.runtime.region.resolveRegion
2121
import aws.smithy.kotlin.runtime.auth.awscredentials.*
22+
import aws.smithy.kotlin.runtime.businessmetrics.BusinessMetric
2223
import aws.smithy.kotlin.runtime.businessmetrics.BusinessMetrics
2324
import aws.smithy.kotlin.runtime.collections.Attributes
2425
import aws.smithy.kotlin.runtime.http.engine.HttpClientEngine
@@ -87,7 +88,7 @@ public class ProfileCredentialsProvider @InternalSdkApi constructor(
8788
public val httpClient: HttpClientEngine? = null,
8889
public val configurationSource: AwsConfigurationSource? = null,
8990
) : CloseableCredentialsProvider {
90-
private val credentialsBusinessMetrics: MutableSet<String> = mutableSetOf()
91+
private val credentialsBusinessMetrics: MutableSet<BusinessMetric> = mutableSetOf()
9192

9293
public constructor(
9394
profileName: String? = null,
@@ -134,7 +135,7 @@ public class ProfileCredentialsProvider @InternalSdkApi constructor(
134135
chain.roles.forEach { roleArn ->
135136
logger.debug { "Assuming role `${roleArn.roleArn}`" }
136137
if (roleArn.source == RoleArnSource.SOURCE_PROFILE) {
137-
credentialsBusinessMetrics.add(AwsBusinessMetric.Credentials.CREDENTIALS_PROFILE_SOURCE_PROFILE.identifier)
138+
credentialsBusinessMetrics.add(AwsBusinessMetric.Credentials.CREDENTIALS_PROFILE_SOURCE_PROFILE)
138139
}
139140

140141
val assumeProvider = roleArn.toCredentialsProvider(creds, region)
@@ -159,11 +160,11 @@ public class ProfileCredentialsProvider @InternalSdkApi constructor(
159160
private suspend fun LeafProvider.toCredentialsProvider(region: LazyAsyncValue<String?>): CredentialsProvider =
160161
when (this) {
161162
is LeafProvider.NamedSource -> namedProviders[name].also {
162-
credentialsBusinessMetrics.add(AwsBusinessMetric.Credentials.CREDENTIALS_PROFILE_NAMED_PROVIDER.identifier)
163+
credentialsBusinessMetrics.add(AwsBusinessMetric.Credentials.CREDENTIALS_PROFILE_NAMED_PROVIDER)
163164
} ?: throw ProviderConfigurationException("unknown credentials source: $name")
164165

165166
is LeafProvider.AccessKey -> StaticCredentialsProvider(credentials).also {
166-
credentialsBusinessMetrics.add(AwsBusinessMetric.Credentials.CREDENTIALS_PROFILE.identifier)
167+
credentialsBusinessMetrics.add(AwsBusinessMetric.Credentials.CREDENTIALS_PROFILE)
167168
}
168169

169170
is LeafProvider.WebIdentityTokenRole -> StsWebIdentityCredentialsProvider(
@@ -174,7 +175,7 @@ public class ProfileCredentialsProvider @InternalSdkApi constructor(
174175
platformProvider = platformProvider,
175176
httpClient = httpClient,
176177
).also {
177-
credentialsBusinessMetrics.add(AwsBusinessMetric.Credentials.CREDENTIALS_PROFILE_STS_WEB_ID_TOKEN.identifier)
178+
credentialsBusinessMetrics.add(AwsBusinessMetric.Credentials.CREDENTIALS_PROFILE_STS_WEB_ID_TOKEN)
178179
}
179180

180181
is LeafProvider.SsoSession -> SsoCredentialsProvider(
@@ -186,7 +187,7 @@ public class ProfileCredentialsProvider @InternalSdkApi constructor(
186187
httpClient = httpClient,
187188
platformProvider = platformProvider,
188189
).also {
189-
credentialsBusinessMetrics.add(AwsBusinessMetric.Credentials.CREDENTIALS_PROFILE_SSO.identifier)
190+
credentialsBusinessMetrics.add(AwsBusinessMetric.Credentials.CREDENTIALS_PROFILE_SSO)
190191
}
191192

192193
is LeafProvider.LegacySso -> SsoCredentialsProvider(
@@ -197,11 +198,11 @@ public class ProfileCredentialsProvider @InternalSdkApi constructor(
197198
httpClient = httpClient,
198199
platformProvider = platformProvider,
199200
).also {
200-
credentialsBusinessMetrics.add(AwsBusinessMetric.Credentials.CREDENTIALS_PROFILE_SSO_LEGACY.identifier)
201+
credentialsBusinessMetrics.add(AwsBusinessMetric.Credentials.CREDENTIALS_PROFILE_SSO_LEGACY)
201202
}
202203

203204
is LeafProvider.Process -> ProcessCredentialsProvider(command).also {
204-
credentialsBusinessMetrics.add(AwsBusinessMetric.Credentials.CREDENTIALS_PROFILE_PROCESS.identifier)
205+
credentialsBusinessMetrics.add(AwsBusinessMetric.Credentials.CREDENTIALS_PROFILE_PROCESS)
205206
}
206207
}
207208

aws-runtime/aws-config/common/test/aws/sdk/kotlin/runtime/util/AwsBusinessMetricsTestUtils.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package aws.sdk.kotlin.runtime.util
22

3+
import aws.sdk.kotlin.runtime.http.interceptors.businessmetrics.AwsBusinessMetric
34
import aws.sdk.kotlin.runtime.http.interceptors.businessmetrics.emitBusinessMetrics
45
import aws.smithy.kotlin.runtime.auth.awscredentials.Credentials
56
import aws.smithy.kotlin.runtime.businessmetrics.BusinessMetric
@@ -14,7 +15,7 @@ import aws.smithy.kotlin.runtime.collections.toMutableAttributes
1415
internal fun testAttributes(attributes: Attributes? = null, vararg metrics: BusinessMetric): Attributes {
1516
val testAttributes = attributes?.toMutableAttributes() ?: mutableAttributes()
1617
metrics.forEach { metric ->
17-
testAttributes.emitBusinessMetric(metric.identifier)
18+
testAttributes.emitBusinessMetric(metric)
1819
}
1920
return testAttributes
2021
}
@@ -25,7 +26,7 @@ internal fun testAttributes(attributes: Attributes? = null, vararg metrics: Busi
2526
internal fun testAttributes(vararg metrics: BusinessMetric): Attributes {
2627
val testAttributes = mutableAttributes()
2728
metrics.forEach { metric ->
28-
testAttributes.emitBusinessMetric(metric.identifier)
29+
testAttributes.emitBusinessMetric(metric)
2930
}
3031
return testAttributes
3132
}
@@ -35,3 +36,6 @@ internal fun testAttributes(vararg metrics: BusinessMetric): Attributes {
3536
*/
3637
internal fun Credentials.withBusinessMetrics(vararg metrics: BusinessMetric): Credentials =
3738
emitBusinessMetrics(metrics.toSet())
39+
40+
internal fun String.toAwsBusinessMetric(): BusinessMetric =
41+
AwsBusinessMetric.Credentials.entries.find { it.identifier == this } ?: throw Exception("String '$this' is not an AWS business metric")

aws-runtime/aws-config/jvm/test/aws/sdk/kotlin/runtime/auth/credentials/DefaultChainCredentialsProviderTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package aws.sdk.kotlin.runtime.auth.credentials
77

88
import aws.sdk.kotlin.runtime.auth.credentials.internal.credentials
99
import aws.sdk.kotlin.runtime.http.interceptors.businessmetrics.emitBusinessMetrics
10+
import aws.sdk.kotlin.runtime.util.toAwsBusinessMetric
1011
import aws.smithy.kotlin.runtime.auth.awscredentials.Credentials
1112
import aws.smithy.kotlin.runtime.auth.awscredentials.copy
1213
import aws.smithy.kotlin.runtime.httptest.TestConnection
@@ -102,7 +103,7 @@ class DefaultChainCredentialsProviderTest {
102103
o["session_token"]?.jsonPrimitive?.content,
103104
o["expiry"]?.jsonPrimitive?.longOrNull?.let { Instant.fromEpochSeconds(it) },
104105
accountId = o["accountId"]?.jsonPrimitive?.content,
105-
).emitBusinessMetrics(expectedBusinessMetrics)
106+
).emitBusinessMetrics(expectedBusinessMetrics.map { it.toAwsBusinessMetric() }.toSet())
106107
Ok(name, docs, expectedCreds)
107108
}
108109
"ErrorContains" in result -> ErrorContains(name, docs, checkNotNull(result["ErrorContains"]).jsonPrimitive.content)

aws-runtime/aws-http/api/aws-http.api

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,7 @@ public final class aws/sdk/kotlin/runtime/http/interceptors/businessmetrics/AwsB
221221

222222
public final class aws/sdk/kotlin/runtime/http/interceptors/businessmetrics/AwsBusinessMetricsUtilsKt {
223223
public static final fun emitBusinessMetric (Laws/smithy/kotlin/runtime/auth/awscredentials/Credentials;Laws/smithy/kotlin/runtime/businessmetrics/BusinessMetric;)Laws/smithy/kotlin/runtime/auth/awscredentials/Credentials;
224-
public static final fun emitBusinessMetric (Laws/smithy/kotlin/runtime/auth/awscredentials/Credentials;Ljava/lang/String;)Laws/smithy/kotlin/runtime/auth/awscredentials/Credentials;
225-
public static final fun emitBusinessMetricsWithSetOfBusinessMetrics (Laws/smithy/kotlin/runtime/auth/awscredentials/Credentials;Ljava/util/Set;)Laws/smithy/kotlin/runtime/auth/awscredentials/Credentials;
226-
public static final fun emitBusinessMetricsWithSetOfString (Laws/smithy/kotlin/runtime/auth/awscredentials/Credentials;Ljava/util/Set;)Laws/smithy/kotlin/runtime/auth/awscredentials/Credentials;
224+
public static final fun emitBusinessMetrics (Laws/smithy/kotlin/runtime/auth/awscredentials/Credentials;Ljava/util/Set;)Laws/smithy/kotlin/runtime/auth/awscredentials/Credentials;
227225
}
228226

229227
public final class aws/sdk/kotlin/runtime/http/interceptors/businessmetrics/BusinessMetricsInterceptor : aws/smithy/kotlin/runtime/client/Interceptor {

aws-runtime/aws-http/common/src/aws/sdk/kotlin/runtime/http/interceptors/businessmetrics/AwsBusinessMetricsUtils.kt

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@ import aws.smithy.kotlin.runtime.businessmetrics.BusinessMetric
88
import aws.smithy.kotlin.runtime.businessmetrics.emitBusinessMetric
99
import aws.smithy.kotlin.runtime.collections.MutableAttributes
1010
import aws.smithy.kotlin.runtime.collections.toMutableAttributes
11-
import kotlin.jvm.JvmName
1211

1312
/**
1413
* Makes sure the metrics do not exceed the maximum size and truncates them if so.
1514
*/
16-
internal fun formatMetrics(metrics: MutableSet<String>): String {
15+
internal fun formatMetrics(metrics: MutableSet<BusinessMetric>): String {
1716
if (metrics.isEmpty()) return ""
18-
val metricsString = metrics.joinToString(",", "m/")
17+
val metricsString = metrics.joinToString(",", "m/") { it.identifier }
1918
val metricsByteArray = metricsString.encodeToByteArray()
2019

2120
if (metricsByteArray.size <= BUSINESS_METRICS_MAX_LENGTH) return metricsString
@@ -69,50 +68,31 @@ public enum class AwsBusinessMetric(public override val identifier: String) : Bu
6968

7069
/**
7170
* Emits a business metric into [Credentials.attributes]
72-
* @param identifier The identifier of the [BusinessMetric] to be emitted.
71+
* @param metric The [BusinessMetric] to be emitted.
7372
*/
7473
@InternalApi
75-
public fun Credentials.emitBusinessMetric(identifier: String): Credentials =
74+
public fun Credentials.emitBusinessMetric(metric: BusinessMetric): Credentials =
7675
when (val credentialsAttributes = this.attributes) {
7776
is MutableAttributes -> {
78-
credentialsAttributes.emitBusinessMetric(identifier)
77+
credentialsAttributes.emitBusinessMetric(metric)
7978
this
8079
}
8180
else -> {
8281
val newCredentialsAttributes = credentialsAttributes.toMutableAttributes()
83-
newCredentialsAttributes.emitBusinessMetric(identifier)
82+
newCredentialsAttributes.emitBusinessMetric(metric)
8483
this.copy(attributes = newCredentialsAttributes)
8584
}
8685
}
8786

88-
/**
89-
* Emits a business metric into [Credentials.attributes]
90-
* @param metric The [BusinessMetric] to be emitted.
91-
*/
92-
@InternalApi
93-
public fun Credentials.emitBusinessMetric(metric: BusinessMetric): Credentials = this.emitBusinessMetric(metric.identifier)
94-
9587
/**
9688
* Emits business metrics into [Credentials.attributes]
97-
* @param identifiers The identifiers of the [BusinessMetric]s to be emitted.
89+
* @param metrics The [BusinessMetric]s to be emitted.
9890
*/
9991
@InternalApi
100-
@JvmName("emitBusinessMetricsWithSetOfString")
101-
public fun Credentials.emitBusinessMetrics(identifiers: Set<String>): Credentials {
92+
public fun Credentials.emitBusinessMetrics(metrics: Set<BusinessMetric>): Credentials {
10293
var credentials = this
103-
identifiers.forEach { identifier ->
94+
metrics.forEach { identifier ->
10495
credentials = this.emitBusinessMetric(identifier)
10596
}
10697
return credentials
10798
}
108-
109-
/**
110-
* Emits business metrics into [Credentials.attributes]
111-
* @param metrics The [BusinessMetric]s to be emitted.
112-
*/
113-
@InternalApi
114-
@JvmName("emitBusinessMetricsWithSetOfBusinessMetrics")
115-
public fun Credentials.emitBusinessMetrics(metrics: Set<BusinessMetric>): Credentials =
116-
this.emitBusinessMetrics(
117-
metrics.map { it.identifier }.toSet(),
118-
)

aws-runtime/aws-http/common/test/aws/sdk/kotlin/runtime/http/interceptors/BusinessMetricsInterceptorTest.kt

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import aws.sdk.kotlin.runtime.http.BUSINESS_METRICS_MAX_LENGTH
88
import aws.sdk.kotlin.runtime.http.interceptors.businessmetrics.AwsBusinessMetric
99
import aws.sdk.kotlin.runtime.http.interceptors.businessmetrics.BusinessMetricsInterceptor
1010
import aws.sdk.kotlin.runtime.http.middleware.USER_AGENT
11+
import aws.smithy.kotlin.runtime.businessmetrics.BusinessMetric
1112
import aws.smithy.kotlin.runtime.businessmetrics.SmithyBusinessMetric
1213
import aws.smithy.kotlin.runtime.businessmetrics.emitBusinessMetric
1314
import aws.smithy.kotlin.runtime.client.ProtocolRequestInterceptorContext
@@ -18,7 +19,6 @@ import aws.smithy.kotlin.runtime.net.url.Url
1819
import aws.smithy.kotlin.runtime.operation.ExecutionContext
1920
import kotlinx.coroutines.test.runTest
2021
import kotlin.test.Test
21-
import kotlin.test.assertFailsWith
2222
import kotlin.test.assertFalse
2323
import kotlin.test.assertTrue
2424

@@ -69,10 +69,13 @@ class BusinessMetricsInterceptorTest {
6969
@Test
7070
fun truncateBusinessMetrics() = runTest {
7171
val executionContext = ExecutionContext()
72-
executionContext.attributes[aws.smithy.kotlin.runtime.businessmetrics.BusinessMetrics] = mutableSetOf()
7372

7473
for (i in 0..1024) {
75-
executionContext.attributes[aws.smithy.kotlin.runtime.businessmetrics.BusinessMetrics].add(i.toString())
74+
executionContext.emitBusinessMetric(
75+
object : BusinessMetric {
76+
override val identifier: String = i.toString()
77+
},
78+
)
7679
}
7780

7881
val rawMetrics = executionContext[aws.smithy.kotlin.runtime.businessmetrics.BusinessMetrics]
@@ -89,21 +92,6 @@ class BusinessMetricsInterceptorTest {
8992
assertTrue(truncatedMetrics.encodeToByteArray().size <= BUSINESS_METRICS_MAX_LENGTH)
9093
assertFalse(truncatedMetrics.endsWith(","))
9194
}
92-
93-
@Test
94-
fun malformedBusinessMetrics() = runTest {
95-
val executionContext = ExecutionContext()
96-
97-
executionContext.attributes[aws.smithy.kotlin.runtime.businessmetrics.BusinessMetrics] = mutableSetOf(
98-
"A".repeat(BUSINESS_METRICS_MAX_LENGTH),
99-
)
100-
101-
val interceptor = BusinessMetricsInterceptor()
102-
103-
assertFailsWith<IllegalStateException>("Business metrics are incorrectly formatted:") {
104-
interceptor.modifyBeforeTransmit(interceptorContext(executionContext))
105-
}
106-
}
10795
}
10896

10997
private fun interceptorContext(executionContext: ExecutionContext): ProtocolRequestInterceptorContext<Any, HttpRequest> =

0 commit comments

Comments
 (0)