-
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.
emit DDB_MAPPER business metric (#1426)
- Loading branch information
Showing
10 changed files
with
212 additions
and
7 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
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
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
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
109 changes: 109 additions & 0 deletions
109
...apper/dynamodb-mapper/common/test/aws/sdk/kotlin/hll/dynamodbmapper/DynamoDbMapperTest.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,109 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package aws.sdk.kotlin.hll.dynamodbmapper | ||
|
||
import aws.sdk.kotlin.hll.dynamodbmapper.items.AttributeDescriptor | ||
import aws.sdk.kotlin.hll.dynamodbmapper.items.ItemSchema | ||
import aws.sdk.kotlin.hll.dynamodbmapper.items.KeySpec | ||
import aws.sdk.kotlin.hll.dynamodbmapper.items.SimpleItemConverter | ||
import aws.sdk.kotlin.hll.dynamodbmapper.operations.scanPaginated | ||
import aws.sdk.kotlin.hll.dynamodbmapper.testutils.DdbLocalTest | ||
import aws.sdk.kotlin.hll.dynamodbmapper.values.scalars.IntConverter | ||
import aws.sdk.kotlin.hll.dynamodbmapper.values.scalars.StringConverter | ||
import aws.sdk.kotlin.runtime.http.interceptors.AwsBusinessMetric | ||
import aws.sdk.kotlin.services.dynamodb.scan | ||
import aws.sdk.kotlin.services.dynamodb.withConfig | ||
import aws.smithy.kotlin.runtime.businessmetrics.BusinessMetric | ||
import aws.smithy.kotlin.runtime.businessmetrics.BusinessMetrics | ||
import aws.smithy.kotlin.runtime.client.ProtocolRequestInterceptorContext | ||
import aws.smithy.kotlin.runtime.collections.get | ||
import aws.smithy.kotlin.runtime.http.interceptors.HttpInterceptor | ||
import aws.smithy.kotlin.runtime.http.request.HttpRequest | ||
import kotlinx.coroutines.flow.collect | ||
import kotlinx.coroutines.test.runTest | ||
import kotlin.test.assertFalse | ||
import kotlin.test.assertTrue | ||
|
||
class DynamoDbMapperTest : DdbLocalTest() { | ||
companion object { | ||
private const val TABLE_NAME = "dummy" | ||
|
||
private data class DummyData(var foo: String = "", var bar: Int = 0) | ||
|
||
private val dummyConverter = SimpleItemConverter( | ||
::DummyData, | ||
{ this }, | ||
AttributeDescriptor("foo", DummyData::foo, DummyData::foo::set, StringConverter), | ||
AttributeDescriptor("bar", DummyData::bar, DummyData::bar::set, IntConverter), | ||
) | ||
|
||
private val dummySchema = ItemSchema(dummyConverter, KeySpec.String("foo"), KeySpec.Number("bar")) | ||
} | ||
|
||
@BeforeAll | ||
fun setUp() = runTest { | ||
createTable(TABLE_NAME, dummySchema) | ||
} | ||
|
||
@Test | ||
fun testBusinessMetricEmission() = runTest { | ||
val interceptor = MetricCapturingInterceptor() | ||
|
||
val ddb = lowLevelAccess { withConfig { interceptors += interceptor } } | ||
interceptor.assertEmpty() | ||
|
||
// No metric for low-level client | ||
lowLevelAccess { scan { tableName = TABLE_NAME } } | ||
interceptor.assertMetric(AwsBusinessMetric.DDB_MAPPER, exists = false) | ||
interceptor.reset() | ||
|
||
// Metric for high-level client | ||
val mapper = mapper(ddb) | ||
val table = mapper.getTable(TABLE_NAME, dummySchema) | ||
table.scanPaginated { }.collect() | ||
interceptor.assertMetric(AwsBusinessMetric.DDB_MAPPER) | ||
interceptor.reset() | ||
|
||
// Still no metric for low-level client (i.e., LL wasn't modified by HL) | ||
lowLevelAccess { scan { tableName = TABLE_NAME } } | ||
interceptor.assertMetric(AwsBusinessMetric.DDB_MAPPER, exists = false) | ||
interceptor.reset() | ||
|
||
// Original client can be closed, mapper is unaffected | ||
lowLevelAccess { close() } | ||
table.scanPaginated { }.collect() | ||
interceptor.assertMetric(AwsBusinessMetric.DDB_MAPPER) | ||
} | ||
} | ||
|
||
private class MetricCapturingInterceptor : HttpInterceptor { | ||
private val capturedMetrics = mutableSetOf<String>() | ||
|
||
override fun readBeforeTransmit(context: ProtocolRequestInterceptorContext<Any, HttpRequest>) { | ||
capturedMetrics += context.executionContext[BusinessMetrics] | ||
} | ||
|
||
fun assertMetric(metric: BusinessMetric, exists: Boolean = true) { | ||
if (exists) { | ||
assertTrue( | ||
metric.identifier in capturedMetrics, | ||
"Expected metrics to contain $metric. Actual values: $capturedMetrics", | ||
) | ||
} else { | ||
assertFalse( | ||
metric.identifier in capturedMetrics, | ||
"Expected metrics *not* to contain $metric. Actual values: $capturedMetrics", | ||
) | ||
} | ||
} | ||
|
||
fun assertEmpty() { | ||
assertTrue(capturedMetrics.isEmpty(), "Expected metrics to be empty. Actual values: $capturedMetrics") | ||
} | ||
|
||
fun reset() { | ||
capturedMetrics.clear() | ||
} | ||
} |
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