-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
247 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
...ws-http/common/src/aws/sdk/kotlin/runtime/http/interceptors/BusinessMetricsInterceptor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package aws.sdk.kotlin.runtime.http.interceptors | ||
|
||
import aws.sdk.kotlin.runtime.http.middleware.USER_AGENT | ||
import aws.smithy.kotlin.runtime.businessmetrics.businessMetrics | ||
import aws.smithy.kotlin.runtime.client.ProtocolRequestInterceptorContext | ||
import aws.smithy.kotlin.runtime.http.interceptors.HttpInterceptor | ||
import aws.smithy.kotlin.runtime.http.request.HttpRequest | ||
import aws.smithy.kotlin.runtime.http.request.toBuilder | ||
|
||
/** | ||
* Appends business metrics to the `User-Agent` header. | ||
*/ | ||
public class BusinessMetricsInterceptor : HttpInterceptor { | ||
override suspend fun modifyBeforeTransmit(context: ProtocolRequestInterceptorContext<Any, HttpRequest>): HttpRequest { | ||
context.executionContext.getOrNull(businessMetrics)?.let { metrics -> | ||
val metricsString = formatMetrics(metrics) | ||
val currentUserAgentHeader = context.protocolRequest.headers[USER_AGENT] | ||
val modifiedRequest = context.protocolRequest.toBuilder() | ||
|
||
modifiedRequest.headers[USER_AGENT] = currentUserAgentHeader + metricsString | ||
|
||
return modifiedRequest.build() | ||
} | ||
return context.protocolRequest | ||
} | ||
} | ||
|
||
/** | ||
* Makes sure the metrics do not exceed the maximum size and truncates them if so. | ||
*/ | ||
private fun formatMetrics(metrics: MutableSet<String>): String { | ||
val metricsString = metrics.joinToString(",", "m/") | ||
val metricsByteArray = metricsString.toByteArray() | ||
val maxSize = 1024 | ||
|
||
if (metricsByteArray.size <= maxSize) return metricsString | ||
|
||
val commaByte = ','.code.toByte() | ||
var lastCommaIndex: Int? = null | ||
|
||
for (i in 0..1023) { | ||
if (metricsByteArray[i] == commaByte) { | ||
lastCommaIndex = i | ||
} | ||
} | ||
|
||
lastCommaIndex?.let { | ||
return metricsByteArray.decodeToString( | ||
0, | ||
lastCommaIndex, | ||
true, | ||
) | ||
} | ||
|
||
throw IllegalStateException("Business metrics are incorrectly formatted: $metricsString") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
...tp/common/test/aws/sdk/kotlin/runtime/http/interceptors/BusinessMetricsInterceptorTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package aws.sdk.kotlin.runtime.http.interceptors | ||
|
||
import aws.sdk.kotlin.runtime.http.middleware.USER_AGENT | ||
import aws.smithy.kotlin.runtime.businessmetrics.BusinessMetrics | ||
import aws.smithy.kotlin.runtime.businessmetrics.emitBusinessMetric | ||
import aws.smithy.kotlin.runtime.client.ProtocolRequestInterceptorContext | ||
import aws.smithy.kotlin.runtime.collections.get | ||
import aws.smithy.kotlin.runtime.http.* | ||
import aws.smithy.kotlin.runtime.http.request.HttpRequest | ||
import aws.smithy.kotlin.runtime.net.url.Url | ||
import aws.smithy.kotlin.runtime.operation.ExecutionContext | ||
import kotlinx.coroutines.test.runTest | ||
import kotlin.test.Test | ||
import kotlin.test.assertFalse | ||
import kotlin.test.assertTrue | ||
|
||
class BusinessMetricsInterceptorTest { | ||
@Test | ||
fun noBusinessMetrics() = runTest { | ||
val executionContext = ExecutionContext() | ||
val interceptor = BusinessMetricsInterceptor() | ||
val request = interceptor.modifyBeforeTransmit(interceptorContext(executionContext)) | ||
val userAgentHeader = request.headers[USER_AGENT]!! | ||
|
||
assertFalse(userAgentHeader.endsWith("m/")) | ||
} | ||
|
||
@Test | ||
fun businessMetrics() = runTest { | ||
val executionContext = ExecutionContext() | ||
executionContext.emitBusinessMetric(BusinessMetrics.S3_EXPRESS_BUCKET) | ||
|
||
val interceptor = BusinessMetricsInterceptor() | ||
val request = interceptor.modifyBeforeTransmit(interceptorContext(executionContext)) | ||
val userAgentHeader = request.headers[USER_AGENT]!! | ||
|
||
assertTrue( | ||
userAgentHeader.endsWith( | ||
"m/${BusinessMetrics.S3_EXPRESS_BUCKET.identifier}", | ||
), | ||
) | ||
} | ||
|
||
@Test | ||
fun multipleBusinessMetrics() = runTest { | ||
val executionContext = ExecutionContext() | ||
executionContext.emitBusinessMetric(BusinessMetrics.S3_EXPRESS_BUCKET) | ||
executionContext.emitBusinessMetric(BusinessMetrics.GZIP_REQUEST_COMPRESSION) | ||
|
||
val interceptor = BusinessMetricsInterceptor() | ||
val request = interceptor.modifyBeforeTransmit(interceptorContext(executionContext)) | ||
val userAgentHeader = request.headers[USER_AGENT]!! | ||
|
||
assertTrue( | ||
userAgentHeader.endsWith( | ||
"m/${BusinessMetrics.S3_EXPRESS_BUCKET.identifier},${BusinessMetrics.GZIP_REQUEST_COMPRESSION.identifier}", | ||
), | ||
) | ||
} | ||
|
||
@Test | ||
fun truncateBusinessMetrics() = runTest { | ||
val executionContext = ExecutionContext() | ||
executionContext.attributes[aws.smithy.kotlin.runtime.businessmetrics.businessMetrics] = mutableSetOf() | ||
|
||
for (i in 0..1024) { | ||
executionContext.attributes[aws.smithy.kotlin.runtime.businessmetrics.businessMetrics].add(i.toString()) | ||
} | ||
|
||
val rawMetrics = executionContext[aws.smithy.kotlin.runtime.businessmetrics.businessMetrics] | ||
val rawMetricsString = rawMetrics.joinToString(",", "m/") | ||
val rawMetricsByteArray = rawMetricsString.toByteArray() | ||
val maxSize = 1024 | ||
|
||
assertTrue(rawMetricsByteArray.size >= maxSize) | ||
|
||
val interceptor = BusinessMetricsInterceptor() | ||
val request = interceptor.modifyBeforeTransmit(interceptorContext(executionContext)) | ||
val userAgentHeader = request.headers[USER_AGENT]!! | ||
val truncatedMetrics = "m/" + userAgentHeader.substringAfter("m/") | ||
|
||
assertTrue(truncatedMetrics.toByteArray().size <= maxSize) | ||
assertFalse(truncatedMetrics.endsWith(",")) | ||
} | ||
} | ||
|
||
private fun interceptorContext(executionContext: ExecutionContext): ProtocolRequestInterceptorContext<Any, HttpRequest> = | ||
object : ProtocolRequestInterceptorContext<Any, HttpRequest> { | ||
override val protocolRequest: HttpRequest = HttpRequest( | ||
HttpMethod.GET, | ||
Url.parse("https://test.aws.com?foo=bar"), | ||
Headers { | ||
append(USER_AGENT, "aws-sdk-kotlin/1.2.3 ua/2.1 api/test-service#1.2.3...") | ||
}, | ||
) | ||
override val executionContext: ExecutionContext = executionContext | ||
override val request: Any = Unit | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
codegen/aws-sdk-codegen/src/main/kotlin/aws/sdk/kotlin/codegen/BusinessMetricsIntegration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package aws.sdk.kotlin.codegen | ||
|
||
import software.amazon.smithy.kotlin.codegen.core.KotlinWriter | ||
import software.amazon.smithy.kotlin.codegen.core.RuntimeTypes | ||
import software.amazon.smithy.kotlin.codegen.integration.KotlinIntegration | ||
import software.amazon.smithy.kotlin.codegen.integration.SectionWriter | ||
import software.amazon.smithy.kotlin.codegen.integration.SectionWriterBinding | ||
import software.amazon.smithy.kotlin.codegen.rendering.endpoints.EndpointBusinessMetrics | ||
import software.amazon.smithy.kotlin.codegen.rendering.protocol.ProtocolGenerator | ||
import software.amazon.smithy.kotlin.codegen.rendering.protocol.ProtocolMiddleware | ||
import software.amazon.smithy.model.shapes.OperationShape | ||
|
||
/** | ||
* Renders the addition of the [BusinessMetricsInterceptor] and endpoint business metrics emitters | ||
*/ | ||
class BusinessMetricsIntegration : KotlinIntegration { | ||
override val sectionWriters: List<SectionWriterBinding> | ||
get() = listOf( | ||
SectionWriterBinding(EndpointBusinessMetrics, endpointBusinessMetricsSectionWriter), | ||
) | ||
|
||
private val endpointBusinessMetricsSectionWriter = SectionWriter { writer, _ -> | ||
writer.write( | ||
"if (endpoint.attributes.contains(#T)) request.context.#T(#T.SERVICE_ENDPOINT_OVERRIDE)", | ||
RuntimeTypes.Core.BusinessMetrics.serviceEndpointOverride, | ||
RuntimeTypes.Core.BusinessMetrics.EmitBusinessMetrics, | ||
RuntimeTypes.Core.BusinessMetrics.BusinessMetrics, | ||
) | ||
|
||
writer.write( | ||
"if (endpoint.attributes.contains(#T)) request.context.#T(#T.ACCOUNT_ID_BASED_ENDPOINT)", | ||
RuntimeTypes.Core.BusinessMetrics.accountIdBasedEndPoint, | ||
RuntimeTypes.Core.BusinessMetrics.EmitBusinessMetrics, | ||
RuntimeTypes.Core.BusinessMetrics.BusinessMetrics, | ||
) | ||
} | ||
|
||
override fun customizeMiddleware( | ||
ctx: ProtocolGenerator.GenerationContext, | ||
resolved: List<ProtocolMiddleware>, | ||
): List<ProtocolMiddleware> = resolved + userAgentBusinessMetricsMiddleware | ||
|
||
private val userAgentBusinessMetricsMiddleware = object : ProtocolMiddleware { | ||
override val name: String = "UserAgentBusinessMetrics" | ||
override fun render(ctx: ProtocolGenerator.GenerationContext, op: OperationShape, writer: KotlinWriter) { | ||
writer.write( | ||
"op.interceptors.add(#T())", | ||
AwsRuntimeTypes.Http.Interceptors.BusinessMetricsInterceptor, | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters