|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package software.amazon.smithy.rustsdk |
| 7 | + |
| 8 | +import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext |
| 9 | +import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator |
| 10 | +import software.amazon.smithy.rust.codegen.client.smithy.generators.ServiceRuntimePluginCustomization |
| 11 | +import software.amazon.smithy.rust.codegen.client.smithy.generators.ServiceRuntimePluginSection |
| 12 | +import software.amazon.smithy.rust.codegen.core.rustlang.CargoDependency |
| 13 | +import software.amazon.smithy.rust.codegen.core.rustlang.Visibility |
| 14 | +import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate |
| 15 | +import software.amazon.smithy.rust.codegen.core.rustlang.writable |
| 16 | +import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType |
| 17 | + |
| 18 | +/** |
| 19 | + * Decorator that tracks observability business metrics when tracing/metrics providers are configured. |
| 20 | + */ |
| 21 | +class ObservabilityMetricDecorator : ClientCodegenDecorator { |
| 22 | + override val name: String = "ObservabilityMetric" |
| 23 | + override val order: Byte = 0 |
| 24 | + |
| 25 | + override fun serviceRuntimePluginCustomizations( |
| 26 | + codegenContext: ClientCodegenContext, |
| 27 | + baseCustomizations: List<ServiceRuntimePluginCustomization>, |
| 28 | + ): List<ServiceRuntimePluginCustomization> = |
| 29 | + baseCustomizations + listOf(ObservabilityFeatureTrackerInterceptor(codegenContext)) |
| 30 | +} |
| 31 | + |
| 32 | +private class ObservabilityFeatureTrackerInterceptor(private val codegenContext: ClientCodegenContext) : |
| 33 | + ServiceRuntimePluginCustomization() { |
| 34 | + override fun section(section: ServiceRuntimePluginSection) = |
| 35 | + writable { |
| 36 | + if (section is ServiceRuntimePluginSection.RegisterRuntimeComponents) { |
| 37 | + section.registerInterceptor(this) { |
| 38 | + val runtimeConfig = codegenContext.runtimeConfig |
| 39 | + rustTemplate( |
| 40 | + "#{Interceptor}", |
| 41 | + "Interceptor" to |
| 42 | + RuntimeType.forInlineDependency( |
| 43 | + InlineAwsDependency.forRustFile( |
| 44 | + "observability_feature", |
| 45 | + Visibility.PRIVATE, |
| 46 | + CargoDependency.smithyObservability(runtimeConfig), |
| 47 | + ), |
| 48 | + ).resolve("ObservabilityFeatureTrackerInterceptor"), |
| 49 | + ) |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | +} |
0 commit comments