-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
emit DDB_MAPPER business metric #1426
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,12 +8,27 @@ import aws.sdk.kotlin.hll.dynamodbmapper.DynamoDbMapper | |
import aws.sdk.kotlin.hll.dynamodbmapper.items.ItemSchema | ||
import aws.sdk.kotlin.hll.dynamodbmapper.model.internal.tableImpl | ||
import aws.sdk.kotlin.hll.dynamodbmapper.pipeline.InterceptorAny | ||
import aws.sdk.kotlin.runtime.http.interceptors.AwsBusinessMetric | ||
import aws.sdk.kotlin.services.dynamodb.DynamoDbClient | ||
import aws.sdk.kotlin.services.dynamodb.withConfig | ||
import aws.smithy.kotlin.runtime.businessmetrics.emitBusinessMetric | ||
import aws.smithy.kotlin.runtime.client.RequestInterceptorContext | ||
import aws.smithy.kotlin.runtime.http.interceptors.HttpInterceptor | ||
|
||
internal data class DynamoDbMapperImpl( | ||
override val client: DynamoDbClient, | ||
override val config: DynamoDbMapper.Config, | ||
) : DynamoDbMapper { | ||
internal companion object { | ||
/** | ||
* Wraps a low-level [DynamoDbClient] to add additional features before instantiating a new | ||
* [DynamoDbMapperImpl]. | ||
*/ | ||
fun wrapping(client: DynamoDbClient, config: DynamoDbMapper.Config): DynamoDbMapperImpl { | ||
val wrappedClient = client.withConfig { interceptors += DdbMapperMetricInterceptor } | ||
return DynamoDbMapperImpl(wrappedClient, config) | ||
} | ||
} | ||
override fun <T, PK> getTable(name: String, schema: ItemSchema.PartitionKey<T, PK>) = | ||
tableImpl(this, name, schema) | ||
|
||
|
@@ -35,3 +50,13 @@ internal class MapperConfigBuilderImpl : DynamoDbMapper.Config.Builder { | |
|
||
override fun build() = MapperConfigImpl(interceptors.toList()) | ||
} | ||
|
||
/** | ||
* An interceptor that emits the DynamoDB Mapper business metric | ||
*/ | ||
private object DdbMapperMetricInterceptor : HttpInterceptor { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit/naming: |
||
override suspend fun modifyBeforeSerialization(context: RequestInterceptorContext<Any>): Any { | ||
context.executionContext.emitBusinessMetric(AwsBusinessMetric.DDB_MAPPER) | ||
return context.request | ||
} | ||
} |
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 = super.ddb.withConfig { interceptors += interceptor } | ||
interceptor.assertEmpty() | ||
|
||
// No metric for low-level client | ||
ddb.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) | ||
ddb.scan { tableName = TABLE_NAME } | ||
interceptor.assertMetric(AwsBusinessMetric.DDB_MAPPER, exists = false) | ||
interceptor.reset() | ||
|
||
// Original client can be closed, mapper is unaffected | ||
ddb.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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would a name like
withConfig
be clearer?