From b78a7b5d8e8720d550b72e9a636900dc7c08774f Mon Sep 17 00:00:00 2001 From: Richard Li Date: Tue, 29 Oct 2024 16:29:45 -0700 Subject: [PATCH 1/6] jetbrains: Generate OpenTelemetry-style metric builders --- telemetry/jetbrains/.editorconfig | 6 +- telemetry/jetbrains/build.gradle.kts | 9 +- telemetry/jetbrains/gradle/libs.versions.toml | 8 +- .../generator/OTelTelemetryGenerator.kt | 339 ++++++++++++++++++ .../telemetry/generator/TelemetryGenerator.kt | 38 +- .../telemetry/generator/TelemetryParser.kt | 29 +- .../generator/gradle/GenerateTelemetry.kt | 22 +- .../telemetry/generator/NewGeneratorTest.kt | 83 +++++ .../{GeneratorTest.kt => OldGeneratorTest.kt} | 44 ++- .../telemetry/generator/ParserTest.kt | 2 +- .../resources/generateOTelBaseSpan/input.json | 3 + .../aws/toolkits/telemetry/Telemetry.kt | 10 + .../aws/toolkits/telemetry/impl/BaseSpan.kt | 32 ++ .../toolkits/telemetry/MetadataTelemetry.kt | 13 +- .../aws/toolkits/telemetry/TelemetryEnums.kt | 3 +- .../aws/toolkits/telemetry/TelemetryEnums.kt | 3 +- .../aws/toolkits/telemetry/TestTelemetry.kt | 16 +- .../generatesOTelWithNormalInput/input.json | 61 ++++ .../aws/toolkits/telemetry/Telemetry.kt | 23 ++ .../aws/toolkits/telemetry/impl/BaseSpan.kt | 48 +++ .../toolkits/telemetry/impl/LambdaTracer.kt | 146 ++++++++ .../aws/toolkits/telemetry/impl/NoTracer.kt | 48 +++ .../toolkits/telemetry/impl/PassiveTracer.kt | 44 +++ .../aws/toolkits/telemetry/LambdaTelemetry.kt | 5 + .../aws/toolkits/telemetry/NoTelemetry.kt | 5 + .../toolkits/telemetry/PassiveTelemetry.kt | 5 + .../aws/toolkits/telemetry/TelemetryEnums.kt | 3 +- .../aws/toolkits/telemetry/TelemetryEnums.kt | 7 +- .../toolkits/telemetry/MetadataTelemetry.kt | 13 +- .../aws/toolkits/telemetry/TelemetryEnums.kt | 3 +- 30 files changed, 976 insertions(+), 95 deletions(-) create mode 100644 telemetry/jetbrains/src/main/kotlin/software/aws/toolkits/telemetry/generator/OTelTelemetryGenerator.kt create mode 100644 telemetry/jetbrains/src/test/kotlin/software/aws/toolkits/telemetry/generator/NewGeneratorTest.kt rename telemetry/jetbrains/src/test/kotlin/software/aws/toolkits/telemetry/generator/{GeneratorTest.kt => OldGeneratorTest.kt} (74%) create mode 100644 telemetry/jetbrains/src/test/resources/generateOTelBaseSpan/input.json create mode 100644 telemetry/jetbrains/src/test/resources/generateOTelBaseSpan/output/software/aws/toolkits/telemetry/Telemetry.kt create mode 100644 telemetry/jetbrains/src/test/resources/generateOTelBaseSpan/output/software/aws/toolkits/telemetry/impl/BaseSpan.kt create mode 100644 telemetry/jetbrains/src/test/resources/generatesOTelWithNormalInput/input.json create mode 100644 telemetry/jetbrains/src/test/resources/generatesOTelWithNormalInput/output/software/aws/toolkits/telemetry/Telemetry.kt create mode 100644 telemetry/jetbrains/src/test/resources/generatesOTelWithNormalInput/output/software/aws/toolkits/telemetry/impl/BaseSpan.kt create mode 100644 telemetry/jetbrains/src/test/resources/generatesOTelWithNormalInput/output/software/aws/toolkits/telemetry/impl/LambdaTracer.kt create mode 100644 telemetry/jetbrains/src/test/resources/generatesOTelWithNormalInput/output/software/aws/toolkits/telemetry/impl/NoTracer.kt create mode 100644 telemetry/jetbrains/src/test/resources/generatesOTelWithNormalInput/output/software/aws/toolkits/telemetry/impl/PassiveTracer.kt diff --git a/telemetry/jetbrains/.editorconfig b/telemetry/jetbrains/.editorconfig index 08885399..15b6a9b0 100644 --- a/telemetry/jetbrains/.editorconfig +++ b/telemetry/jetbrains/.editorconfig @@ -9,4 +9,8 @@ tab_width = 4 ij_continuation_indent_size = 4 [*.{kt,kts}] -ktlint_code_style = ktlint_official +ij_kotlin_allow_trailing_comma = true +ij_kotlin_code_style_defaults = KOTLIN_OFFICIAL +ij_kotlin_name_count_to_use_star_import = 2147483647 +ij_kotlin_name_count_to_use_star_import_for_members = 2147483647 +ij_kotlin_packages_to_use_import_on_demand = unset diff --git a/telemetry/jetbrains/build.gradle.kts b/telemetry/jetbrains/build.gradle.kts index f66dae42..924d61bb 100644 --- a/telemetry/jetbrains/build.gradle.kts +++ b/telemetry/jetbrains/build.gradle.kts @@ -39,15 +39,18 @@ dependencies { implementation(libs.kotlin.poet) implementation(libs.jackson.module.kotlin) implementation(libs.json.schema) - testImplementation(libs.junit4) + + testImplementation(platform(libs.junit5.bom)) + testImplementation(libs.junit5.jupiter) testImplementation(libs.assertj) + + testRuntimeOnly(libs.junit5.launcher) } tasks { withType { compilerOptions { jvmTarget = JvmTarget.JVM_17 - freeCompilerArgs.add("-Xcontext-receivers") } } @@ -108,6 +111,8 @@ tasks.withType { } tasks.withType { + useJUnitPlatform() + testLogging { exceptionFormat = TestExceptionFormat.FULL } diff --git a/telemetry/jetbrains/gradle/libs.versions.toml b/telemetry/jetbrains/gradle/libs.versions.toml index 88bac014..44c51779 100644 --- a/telemetry/jetbrains/gradle/libs.versions.toml +++ b/telemetry/jetbrains/gradle/libs.versions.toml @@ -4,9 +4,9 @@ jackson = "2.17.2" jlleitschuh-ktlint = "12.1.1" # deprecated; should move to json-skema jsonSchema = "1.14.4" -junit4 = "4.13.2" +junit5 = "5.11.3" kotlin = "2.0.20" -kotlin-poet = "1.18.1" +kotlin-poet = "2.0.0" nexus = "2.0.0" [libraries] @@ -14,7 +14,9 @@ assertj = { module = "org.assertj:assertj-core", version.ref = "assertJ" } kotlin-poet = { module = "com.squareup:kotlinpoet", version.ref = "kotlin-poet" } jackson-module-kotlin = { module = "com.fasterxml.jackson.module:jackson-module-kotlin", version.ref = "jackson" } json-schema = { module = "com.github.erosb:everit-json-schema", version.ref = "jsonSchema" } -junit4 = { module = "junit:junit", version.ref = "junit4" } +junit5-bom = { module = "org.junit:junit-bom", version.ref = "junit5" } +junit5-jupiter = { module = "org.junit.jupiter:junit-jupiter" } +junit5-launcher = { module = "org.junit.platform:junit-platform-launcher" } [plugins] jlleitschuh-ktlint = { id = "org.jlleitschuh.gradle.ktlint", version.ref = "jlleitschuh-ktlint" } diff --git a/telemetry/jetbrains/src/main/kotlin/software/aws/toolkits/telemetry/generator/OTelTelemetryGenerator.kt b/telemetry/jetbrains/src/main/kotlin/software/aws/toolkits/telemetry/generator/OTelTelemetryGenerator.kt new file mode 100644 index 00000000..46f5bd47 --- /dev/null +++ b/telemetry/jetbrains/src/main/kotlin/software/aws/toolkits/telemetry/generator/OTelTelemetryGenerator.kt @@ -0,0 +1,339 @@ +// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package software.aws.toolkits.telemetry.generator + +import com.squareup.kotlinpoet.BOOLEAN +import com.squareup.kotlinpoet.ClassName +import com.squareup.kotlinpoet.CodeBlock +import com.squareup.kotlinpoet.FileSpec +import com.squareup.kotlinpoet.FunSpec +import com.squareup.kotlinpoet.KModifier +import com.squareup.kotlinpoet.NUMBER +import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy +import com.squareup.kotlinpoet.PropertySpec +import com.squareup.kotlinpoet.STRING +import com.squareup.kotlinpoet.TypeSpec +import com.squareup.kotlinpoet.TypeVariableName +import java.io.File + +object OTelTelemetryGenerator { + private const val PACKAGE_NAME_IMPL = "software.aws.toolkits.telemetry.impl" + + private val OTEL_CONTEXT = ClassName("io.opentelemetry.context", "Context") + private val OTEL_TRACER = ClassName("io.opentelemetry.api.trace", "Tracer") + private val OTEL_SPAN_BUILDER = ClassName("io.opentelemetry.api.trace", "SpanBuilder") + private val OTEL_SPAN = ClassName("io.opentelemetry.api.trace", "Span") + private val OTEL_RW_SPAN = ClassName("io.opentelemetry.sdk.trace", "ReadWriteSpan") + + private const val TOOLKIT_OTEL_PACKAGE = "software.aws.toolkits.jetbrains.services.telemetry.otel" + private val TOOLKIT_ABSTRACT_BASE_SPAN = ClassName(TOOLKIT_OTEL_PACKAGE, "AbstractBaseSpan") + private val TOOLKIT_ABSTRACT_SPAN_BUILDER = ClassName(TOOLKIT_OTEL_PACKAGE, "AbstractSpanBuilder") + private val TOOLKIT_DEFAULT_SPAN_BUILDER = ClassName(TOOLKIT_OTEL_PACKAGE, "DefaultSpanBuilder") + private val TOOLKIT_OTEL_SERVICE = ClassName(TOOLKIT_OTEL_PACKAGE, "OTelService") + + private val GENERATED_BASE_SPAN = ClassName(PACKAGE_NAME_IMPL, "BaseSpan") + + private val indent = " ".repeat(4) + private val commonMetadataTypes = + setOf( + "duration", + "httpStatusCode", + "reason", + "reasonDesc", + "requestId", + "requestServiceType", + "result", + // handled by OpenTelemetry emitter +// "traceId", +// "metricId", +// "parentId", + // handled as special generator cases +// "passive", +// "value", +// "unit", + ) + + fun generateTelemetryFromFiles( + inputFiles: List, + defaultDefinitions: List = ResourceLoader.DEFINITIONS_FILES, + outputFolder: File, + ) { + val telemetryDefinitions = TelemetryParser.parseFiles(defaultDefinitions, inputFiles) + + FileSpec.builder(GENERATED_BASE_SPAN) + .indent(indent) + .generateHeader() + .addType(baseSpan(telemetryDefinitions)) + .build() + .writeTo(outputFolder) + + val telemetryKt = + FileSpec.builder(PACKAGE_NAME, "Telemetry") + .indent(indent) + .generateHeader() + + val telemetryRootBuilder = TypeSpec.objectBuilder("Telemetry") + + telemetryDefinitions.metrics.groupBy { it.namespace() } + .toSortedMap() + .forEach { (namespace, metrics) -> + generateMetrics(telemetryRootBuilder, outputFolder, namespace, metrics) + } + + telemetryKt + .addType(telemetryRootBuilder.build()) + .build() + .writeTo(outputFolder) + } + + // public open class BaseSpan>( + // context: Context?, + // `delegate`: Span, + // ) : AbstractBaseSpan(context, delegate as ReadWriteSpan) { + private fun baseSpan(telemetryDefinitions: TelemetrySchema) = + TypeSpec.classBuilder(GENERATED_BASE_SPAN) + .addModifiers(KModifier.OPEN) + .primaryConstructor( + FunSpec.constructorBuilder() + .addParameter("context", OTEL_CONTEXT.copy(nullable = true)) + .addParameter("delegate", OTEL_SPAN) + .build(), + ) + .addTypeVariable(TypeVariableName("SpanType", listOf(GENERATED_BASE_SPAN.parameterizedBy(TypeVariableName("SpanType"))))) + .superclass( + TOOLKIT_ABSTRACT_BASE_SPAN + .parameterizedBy(TypeVariableName("SpanType")), + ) + .addSuperclassConstructorParameter("context, delegate as %T", OTEL_RW_SPAN) + .apply { + commonMetadataTypes.forEach { t -> + val type = telemetryDefinitions.types.firstOrNull { it.name == t } ?: return@forEach + + addFunctions(MetadataSchema(type, false).overloadedFunSpec()) + } + + // special case attributes + listOf( + "passive" to BOOLEAN, + "unit" to METRIC_UNIT, + ).forEach { pair -> + addFunction( + FunSpec.builder(pair.first) + .addParameter(pair.first, pair.second) + .addStatement("this._%1L = %1N", pair.first) + .build(), + ) + } + // special-special case + addFunction( + FunSpec.builder("value") + .addParameter("value", NUMBER) + .addStatement("this._%1L = %1N.toDouble()", "value") + .build(), + ) + } + .build() + + private fun generateMetrics( + rootBuilder: TypeSpec.Builder, + outputFolder: File, + namespace: String, + metrics: List, + ) { + val tracerName = ClassName(PACKAGE_NAME_IMPL, "${namespace.capitalize()}Tracer") + + val tracerKt = + FileSpec.builder(tracerName) + .indent(indent) + .generateHeader() + + // public class AmazonqTracer internal constructor( + // private val `delegate`: Tracer, + // ) : Tracer { + // /** + // * When user opens CWSPR chat panel + // */ + // public val openChat: AmazonqopenChatSpanBuilder + // get() = AmazonqopenChatSpanBuilder(delegate.spanBuilder("amazonq_openChat")) + val tracer = + TypeSpec.classBuilder(tracerName) + .addSuperinterface(OTEL_TRACER) + .primaryConstructor( + FunSpec.constructorBuilder() + .addModifiers(KModifier.INTERNAL) + .addParameter("delegate", OTEL_TRACER) + .build(), + ) + .addProperty( + PropertySpec.builder("delegate", OTEL_TRACER) + .initializer("delegate") + .addModifiers(KModifier.PRIVATE) + .build(), + ) + .addFunction( + FunSpec.builder("spanBuilder") + .addModifiers(KModifier.OVERRIDE) + .addParameter("spanName", String::class) + .returns(TOOLKIT_DEFAULT_SPAN_BUILDER) + .addStatement("return %T(delegate.spanBuilder(spanName))", TOOLKIT_DEFAULT_SPAN_BUILDER) + .build(), + ) + .apply { + metrics.forEach { metricSchema -> + val metricName = metricSchema.name.split("_", limit = 2)[1] + val metricSpanName = ClassName(PACKAGE_NAME_IMPL, "${namespace.capitalize()}${metricName}Span") + val metricSpanBuilderName = ClassName(PACKAGE_NAME_IMPL, "${namespace.capitalize()}${metricName}SpanBuilder") + + tracerKt.generateMetricSpan(metricSchema, metricSpanName) + tracerKt.generateMetricSpanBuilder(metricSpanName, metricSpanBuilderName) + + // /** + // * When user opens CWSPR chat panel + // */ + // public val openChat: AmazonqopenChatSpanBuilder + // get() = AmazonqopenChatSpanBuilder(delegate.spanBuilder("amazonq_openChat")) + addProperty( + PropertySpec.builder(metricName, metricSpanBuilderName) + .getter( + FunSpec.builder("get()") + .addStatement("""return %T(delegate.spanBuilder(%S))""", metricSpanBuilderName, metricSchema.name) + .build(), + ) + .addKdoc(metricSchema.description) + .build(), + ) + } + } + .build() + + tracerKt + .addType(tracer) + .build() + .writeTo(outputFolder) + + rootBuilder.addProperty( + PropertySpec.builder(namespace, tracerName) + .getter( + FunSpec.builder("get()") + .addStatement("return %T(%T.getSdk().getTracer(%S))", tracerName, TOOLKIT_OTEL_SERVICE, namespace) + .build(), + ) + .build(), + ) + } + + private fun FileSpec.Builder.generateMetricSpanBuilder( + metricSpanName: ClassName, + metricSpanBuilderName: ClassName, + ) { + // public class AmazonqopenChatSpanBuilder internal constructor( + // `delegate`: SpanBuilder, + // ) : AbstractSpanBuilder(delegate) { + // override fun doStartSpan(): AmazonqopenChatSpan = AmazonqopenChatSpan(parent, delegate.startSpan()) + // } + val metricSpanBuilder = + TypeSpec.classBuilder(metricSpanBuilderName) + .primaryConstructor( + FunSpec.constructorBuilder() + .addModifiers(KModifier.INTERNAL) + .addParameter("delegate", OTEL_SPAN_BUILDER) + .build(), + ) + .superclass( + TOOLKIT_ABSTRACT_SPAN_BUILDER.parameterizedBy(metricSpanBuilderName, metricSpanName), + ) + .addSuperclassConstructorParameter("delegate") + .addFunction( + FunSpec.builder("doStartSpan") + .returns(metricSpanName) + .addModifiers(KModifier.OVERRIDE) + .addStatement("return %T(parent, delegate.startSpan())", metricSpanName) + .build(), + ) + .build() + addType(metricSpanBuilder) + } + + private fun FileSpec.Builder.generateMetricSpan( + metricSchema: MetricSchema, + metricSpanName: ClassName, + ) { + // public class AmazonqopenChatSpan internal constructor( + // context: Context?, + // span: Span, + // ) : BaseSpan(context, span) { + // init { + // passive(false) + // } + // + // override val requiredFields: Collection = setOf() + // } + val metricSpan = + TypeSpec.classBuilder(metricSpanName) + .primaryConstructor( + FunSpec.constructorBuilder() + .addModifiers(KModifier.INTERNAL) + .addParameter("context", OTEL_CONTEXT.copy(nullable = true)) + .addParameter("span", OTEL_SPAN) + .build(), + ) + .addKdoc(metricSchema.description) + .superclass(GENERATED_BASE_SPAN.parameterizedBy(metricSpanName)) + .addSuperclassConstructorParameter("context, span") + .apply { + if (!metricSchema.passive) { + addInitializerBlock(CodeBlock.builder().addStatement("passive(false)").build()) + } + + metricSchema.metadata.filterNot { it.type.name in commonMetadataTypes }.forEach { metadata -> + addFunctions(metadata.overloadedFunSpec()) + } + + val requiredAttributes = metricSchema.metadata.filter { it.required != false } + addProperty( + PropertySpec.builder("requiredFields", Collection::class.parameterizedBy(String::class), KModifier.OVERRIDE) + .initializer( + """setOf(${ "%S,".repeat(requiredAttributes.size) })""", + *requiredAttributes.map { it.type.name }.toTypedArray(), + ) + .build(), + ) + } + .build() + addType(metricSpan) + } + + private fun MetadataSchema.overloadedFunSpec(): List { + val types = + if (type.allowedValues?.isNotEmpty() == true) { + listOf(ClassName(PACKAGE_NAME, type.typeName)) + } else { + type.type.kotlinTypes() + } + + return types.map { t -> + val needsToString = (t != STRING || type.allowedValues?.isNotEmpty() == true) + val nullable = required == false + + FunSpec.builder(type.name) + .addParameter(type.name, t.copy(nullable = nullable)) + .apply { + val valueParam = + if (needsToString) { + if (nullable) { + "%N?.let { it.toString() }" + } else { + "%N.toString()" + } + } else { + "%N" + } + + addStatement("metadata(%S, $valueParam)", type.name, type.name) + } + .addKdoc(type.description) + .build() + } + } +} diff --git a/telemetry/jetbrains/src/main/kotlin/software/aws/toolkits/telemetry/generator/TelemetryGenerator.kt b/telemetry/jetbrains/src/main/kotlin/software/aws/toolkits/telemetry/generator/TelemetryGenerator.kt index f6031ea4..99c2416e 100644 --- a/telemetry/jetbrains/src/main/kotlin/software/aws/toolkits/telemetry/generator/TelemetryGenerator.kt +++ b/telemetry/jetbrains/src/main/kotlin/software/aws/toolkits/telemetry/generator/TelemetryGenerator.kt @@ -26,6 +26,7 @@ val METRIC_METADATA = ClassName(JETBRAINS_TELEMETRY_PACKAGE_NAME, "MetricEventMe val TELEMETRY_SERVICE = ClassName(JETBRAINS_TELEMETRY_PACKAGE_NAME, "TelemetryService") val PROJECT = ClassName("com.intellij.openapi.project", "Project").copy(nullable = true) val CONNECTION_SETTINGS = ClassName("software.aws.toolkits.core", "ConnectionSettings").copy(nullable = true) +val METRIC_UNIT = ClassName("software.amazon.awssdk.services.toolkittelemetry.model", "MetricUnit") const val RESULT = "result" const val SUCCESS = "success" @@ -82,6 +83,7 @@ fun generateTelemetryFromFiles( .indent(indent) .generateHeader() .generateTelemetryEnumTypes(telemetry.types) + .generateDeprecatedOverloads(RESULT) .build() .writeTo(outputFolder) @@ -98,7 +100,7 @@ fun generateTelemetryFromFiles( } } -private fun FileSpec.Builder.generateHeader(): FileSpec.Builder { +internal fun FileSpec.Builder.generateHeader(): FileSpec.Builder { addFileComment("Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n") addFileComment("SPDX-License-Identifier: Apache-2.0\n") addFileComment("THIS FILE IS GENERATED! DO NOT EDIT BY HAND!") @@ -120,16 +122,7 @@ private fun FileSpec.Builder.generateTelemetryEnumTypes(items: List): FileSpec.Builder { - schema.groupBy { it.namespace() } - .toSortedMap() - .forEach { (namespace, metrics) -> generateNamespaces(namespace, metrics) } - - return this -} - private fun FileSpec.Builder.generateNamespaces( namespace: String, metrics: List, ): FileSpec.Builder { - val telemetryObject = TypeSpec.objectBuilder("${namespace.toTypeFormat()}Telemetry") - + val telemetryObject = + TypeSpec.objectBuilder("${namespace.toTypeFormat()}Telemetry") + .addAnnotation( + AnnotationSpec.builder(Deprecated::class) + .addMember(""""Use type-safe metric builders"""") + .addMember("""ReplaceWith("Telemetry.$namespace", "software.aws.toolkits.telemetry.Telemetry")""") + .build(), + ) metrics.sortedBy { it.name }.forEach { telemetryObject.generateRecordFunctions(it) } @@ -220,7 +211,6 @@ private fun FileSpec.Builder.generateNamespaces( return this } -context(FileSpec.Builder) private fun TypeSpec.Builder.generateRecordFunctions(metric: MetricSchema) { // metric.name.split("_")[1] is guaranteed to work at this point because the schema requires the metric name to have at least 1 underscore val functionName = metric.name.split("_")[1] @@ -233,7 +223,6 @@ private fun TypeSpec.Builder.generateRecordFunctions(metric: MetricSchema) { if (metric.metadata.none { it.type.name == RESULT }) { return } - generateDeprecatedOverloads(RESULT) addFunction(buildProjectOverloadFunction(functionName, metric)) addFunction(buildConnectionSettingsOverloadFunction(functionName, metric)) @@ -314,11 +303,10 @@ private fun FunSpec.Builder.generateFunctionBody( metadataParameter: ParameterSpec, metric: MetricSchema, ): FunSpec.Builder { - val metricUnit = ClassName("software.amazon.awssdk.services.toolkittelemetry.model", "Unit") beginControlFlow("%T.getInstance().record(${metadataParameter.name})", TELEMETRY_SERVICE) beginControlFlow("datum(%S)", metric.name) addStatement("createTime(createTime)") - addStatement("unit(%T.${(metric.unit ?: MetricUnit.NONE).name})", metricUnit) + addStatement("unit(%T.${(metric.unit ?: MetricUnit.NONE).name})", METRIC_UNIT) addStatement("value(value)") addStatement("passive(passive)") metric.metadata.forEach { diff --git a/telemetry/jetbrains/src/main/kotlin/software/aws/toolkits/telemetry/generator/TelemetryParser.kt b/telemetry/jetbrains/src/main/kotlin/software/aws/toolkits/telemetry/generator/TelemetryParser.kt index 813bb543..81064cfb 100644 --- a/telemetry/jetbrains/src/main/kotlin/software/aws/toolkits/telemetry/generator/TelemetryParser.kt +++ b/telemetry/jetbrains/src/main/kotlin/software/aws/toolkits/telemetry/generator/TelemetryParser.kt @@ -18,26 +18,51 @@ enum class MetricMetadataTypes( ) { STRING("string") { override fun kotlinType(): TypeName = com.squareup.kotlinpoet.STRING + + override fun kotlinTypes() = listOf(kotlinType()) }, INT("int") { override fun kotlinType(): TypeName = com.squareup.kotlinpoet.LONG + + override fun kotlinTypes() = listOf(com.squareup.kotlinpoet.LONG, com.squareup.kotlinpoet.INT) }, DOUBLE("double") { override fun kotlinType(): TypeName = com.squareup.kotlinpoet.DOUBLE + + override fun kotlinTypes() = listOf(com.squareup.kotlinpoet.FLOAT, com.squareup.kotlinpoet.DOUBLE) }, BOOLEAN("boolean") { override fun kotlinType(): TypeName = com.squareup.kotlinpoet.BOOLEAN + + override fun kotlinTypes() = listOf(kotlinType()) }, ; abstract fun kotlinType(): TypeName + + abstract fun kotlinTypes(): Iterable } data class TelemetryMetricType( val name: String, val description: String, val type: MetricMetadataTypes = MetricMetadataTypes.STRING, - val allowedValues: List?, -) + val allowedValues: List?, +) { + private val clazz = name.toTypeFormat() + + private val existsInKotlinStdlib by lazy { + try { + Class.forName("kotlin.$clazz") + true + } catch (_: ClassNotFoundException) { + false + } + } + + val typeName by lazy { + if (existsInKotlinStdlib) "Metric$clazz" else clazz + } +} enum class MetricUnit( @get:JsonValue val type: String, diff --git a/telemetry/jetbrains/src/main/kotlin/software/aws/toolkits/telemetry/generator/gradle/GenerateTelemetry.kt b/telemetry/jetbrains/src/main/kotlin/software/aws/toolkits/telemetry/generator/gradle/GenerateTelemetry.kt index 95f2c487..19718df7 100644 --- a/telemetry/jetbrains/src/main/kotlin/software/aws/toolkits/telemetry/generator/gradle/GenerateTelemetry.kt +++ b/telemetry/jetbrains/src/main/kotlin/software/aws/toolkits/telemetry/generator/gradle/GenerateTelemetry.kt @@ -4,24 +4,30 @@ package software.aws.toolkits.telemetry.generator.gradle import org.gradle.api.DefaultTask +import org.gradle.api.file.ConfigurableFileCollection +import org.gradle.api.file.DirectoryProperty import org.gradle.api.tasks.InputFiles import org.gradle.api.tasks.OutputDirectory +import org.gradle.api.tasks.PathSensitive +import org.gradle.api.tasks.PathSensitivity import org.gradle.api.tasks.TaskAction +import software.aws.toolkits.telemetry.generator.OTelTelemetryGenerator import software.aws.toolkits.telemetry.generator.generateTelemetryFromFiles -import java.io.File -open class GenerateTelemetry : DefaultTask() { - @InputFiles - lateinit var inputFiles: List +abstract class GenerateTelemetry : DefaultTask() { + @get:InputFiles + @get:PathSensitive(PathSensitivity.RELATIVE) + abstract val inputFiles: ConfigurableFileCollection - @OutputDirectory - lateinit var outputDirectory: File + @get:OutputDirectory + abstract val outputDirectory: DirectoryProperty @TaskAction fun generate() { - println("Generating telemetry using packaged file and additional files:\n ${inputFiles.map { it.absolutePath }.joinToString("\n")}") + println("Generating telemetry using packaged file and additional files:\n ${inputFiles.joinToString("\n") { it.absolutePath }}") try { - generateTelemetryFromFiles(inputFiles = inputFiles, outputFolder = outputDirectory) + generateTelemetryFromFiles(inputFiles = inputFiles.toList(), outputFolder = outputDirectory.get().asFile) + OTelTelemetryGenerator.generateTelemetryFromFiles(inputFiles = inputFiles.toList(), outputFolder = outputDirectory.get().asFile) } catch (e: Exception) { System.err.println("Generating telemetry threw an exception! $e\n") throw e diff --git a/telemetry/jetbrains/src/test/kotlin/software/aws/toolkits/telemetry/generator/NewGeneratorTest.kt b/telemetry/jetbrains/src/test/kotlin/software/aws/toolkits/telemetry/generator/NewGeneratorTest.kt new file mode 100644 index 00000000..b553445b --- /dev/null +++ b/telemetry/jetbrains/src/test/kotlin/software/aws/toolkits/telemetry/generator/NewGeneratorTest.kt @@ -0,0 +1,83 @@ +package software.aws.toolkits.telemetry.generator + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.TestInfo +import org.junit.jupiter.api.io.TempDir +import java.io.File +import java.nio.file.Files +import java.nio.file.Paths +import kotlin.io.path.isRegularFile +import kotlin.io.path.readText +import kotlin.io.path.toPath + +class NewGeneratorTest { + @TempDir + private lateinit var folder: File + + private lateinit var methodName: String + + @BeforeEach + fun setUp(info: TestInfo) { + methodName = info.testMethod.get().name + } + + @Test + fun generateOTelBaseSpan() { + testGenerator() + } + + @Test + fun generatesOTelWithNormalInput() { + testGenerator() + } + + @Test + fun generatesWithDefaultDefinitions() { + OTelTelemetryGenerator.generateTelemetryFromFiles(inputFiles = listOf(), outputFolder = folder) + val outputFile = Paths.get(folder.absolutePath, "software", "aws", "toolkits", "telemetry") + assertThat(Files.walk(outputFile).toList()).isNotEmpty + } + + // inputPath and outputPath must be in test resources + private fun testGenerator( + definitionsFile: String? = null, + definitionsOverrides: List = listOf(), + ) { + OTelTelemetryGenerator.generateTelemetryFromFiles( + defaultDefinitions = + listOf( + this.javaClass.getResourceAsStream(definitionsFile ?: "/$methodName/input.json").use { + it.bufferedReader().readText() + }, + ), + inputFiles = definitionsOverrides.map { File(javaClass.getResource(it).toURI()) }, + outputFolder = folder, + ) + + val outputRoot = Paths.get(folder.absolutePath) + val outputFiles = Files.walk(outputRoot).filter { it.isRegularFile() }.toList() + val expectedRoot = this.javaClass.getResource("/$methodName").toURI().toPath().resolve("output") + val expectedFiles = Files.walk(expectedRoot).filter { it.isRegularFile() }.toList() + + if (expectedFiles.isEmpty()) { + // for dev convenience, populate the test files and consider it a pass + val root = Paths.get("src", "test", "resources", methodName, "output") + outputFiles.forEach { + val dest = root.resolve(outputRoot.relativize(it)) + Files.createDirectories(dest.parent) + Files.copy(it, dest) + } + return + } + + assertThat(outputFiles.map { outputRoot.relativize(it) }) + .containsExactlyInAnyOrder(*expectedFiles.map { expectedRoot.relativize(it) }.toTypedArray()) + + outputFiles.forEach { + val relPath = outputRoot.relativize(it) + assertThat(it.readText()).isEqualTo(expectedRoot.resolve(relPath).readText()) + } + } +} diff --git a/telemetry/jetbrains/src/test/kotlin/software/aws/toolkits/telemetry/generator/GeneratorTest.kt b/telemetry/jetbrains/src/test/kotlin/software/aws/toolkits/telemetry/generator/OldGeneratorTest.kt similarity index 74% rename from telemetry/jetbrains/src/test/kotlin/software/aws/toolkits/telemetry/generator/GeneratorTest.kt rename to telemetry/jetbrains/src/test/kotlin/software/aws/toolkits/telemetry/generator/OldGeneratorTest.kt index 8f2a7e9b..37ef9008 100644 --- a/telemetry/jetbrains/src/test/kotlin/software/aws/toolkits/telemetry/generator/GeneratorTest.kt +++ b/telemetry/jetbrains/src/test/kotlin/software/aws/toolkits/telemetry/generator/OldGeneratorTest.kt @@ -5,10 +5,10 @@ package software.aws.toolkits.telemetry.generator import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThatThrownBy -import org.junit.Rule -import org.junit.Test -import org.junit.rules.TemporaryFolder -import org.junit.rules.TestName +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.TestInfo +import org.junit.jupiter.api.io.TempDir import java.io.File import java.nio.file.Files import java.nio.file.Paths @@ -16,19 +16,21 @@ import kotlin.io.path.isRegularFile import kotlin.io.path.readText import kotlin.io.path.toPath -class GeneratorTest { - @JvmField - @Rule - val folder = TemporaryFolder() +class OldGeneratorTest { + @TempDir + private lateinit var folder: File - @Rule - @JvmField - val testName = TestName() + private lateinit var methodName: String + + @BeforeEach + fun setUp(info: TestInfo) { + methodName = info.testMethod.get().name + } @Test fun generateFailsWhenValidationFails() { assertThatThrownBy { - generateTelemetryFromFiles(inputFiles = listOf(), defaultDefinitions = listOf("{}"), outputFolder = folder.root) + generateTelemetryFromFiles(inputFiles = listOf(), defaultDefinitions = listOf("{}"), outputFolder = folder) }.hasMessageContaining("required key [metrics] not found") } @@ -49,7 +51,10 @@ class GeneratorTest { @Test fun generateOverrides() { - testGenerator(definitionsFile = "/resultGeneratesTwoFunctions/input.json", definitionsOverrides = listOf("/generateOverrides/overrideInput.json")) + testGenerator( + definitionsFile = "/resultGeneratesTwoFunctions/input.json", + definitionsOverrides = listOf("/generateOverrides/overrideInput.json"), + ) } @Test @@ -59,8 +64,8 @@ class GeneratorTest { @Test fun generateGeneratesWithDefaultDefinitions() { - generateTelemetryFromFiles(inputFiles = listOf(), outputFolder = folder.root) - val outputFile = Paths.get(folder.root.absolutePath, "software", "aws", "toolkits", "telemetry") + generateTelemetryFromFiles(inputFiles = listOf(), outputFolder = folder) + val outputFile = Paths.get(folder.absolutePath, "software", "aws", "toolkits", "telemetry") assertThat(Files.walk(outputFile).toList()).isNotEmpty } @@ -69,17 +74,18 @@ class GeneratorTest { definitionsFile: String? = null, definitionsOverrides: List = listOf(), ) { - val methodName = testName.methodName generateTelemetryFromFiles( defaultDefinitions = listOf( - this.javaClass.getResourceAsStream(definitionsFile ?: "/$methodName/input.json").use { it.bufferedReader().readText() }, + this.javaClass.getResourceAsStream(definitionsFile ?: "/$methodName/input.json").use { + it.bufferedReader().readText() + }, ), inputFiles = definitionsOverrides.map { File(javaClass.getResource(it).toURI()) }, - outputFolder = folder.root, + outputFolder = folder, ) - val outputRoot = Paths.get(folder.root.absolutePath) + val outputRoot = Paths.get(folder.absolutePath) val outputFiles = Files.walk(outputRoot).filter { it.isRegularFile() }.toList() val expectedRoot = this.javaClass.getResource("/$methodName").toURI().toPath().resolve("output") val expectedFiles = Files.walk(expectedRoot).filter { it.isRegularFile() }.toList() diff --git a/telemetry/jetbrains/src/test/kotlin/software/aws/toolkits/telemetry/generator/ParserTest.kt b/telemetry/jetbrains/src/test/kotlin/software/aws/toolkits/telemetry/generator/ParserTest.kt index bcd30c63..ba0aa3a4 100644 --- a/telemetry/jetbrains/src/test/kotlin/software/aws/toolkits/telemetry/generator/ParserTest.kt +++ b/telemetry/jetbrains/src/test/kotlin/software/aws/toolkits/telemetry/generator/ParserTest.kt @@ -4,7 +4,7 @@ package software.aws.toolkits.telemetry.generator import org.assertj.core.api.Assertions.assertThatThrownBy -import org.junit.Test +import org.junit.jupiter.api.Test class ParserTest { @Test diff --git a/telemetry/jetbrains/src/test/resources/generateOTelBaseSpan/input.json b/telemetry/jetbrains/src/test/resources/generateOTelBaseSpan/input.json new file mode 100644 index 00000000..f0ccaf47 --- /dev/null +++ b/telemetry/jetbrains/src/test/resources/generateOTelBaseSpan/input.json @@ -0,0 +1,3 @@ +{ + "metrics": [] +} diff --git a/telemetry/jetbrains/src/test/resources/generateOTelBaseSpan/output/software/aws/toolkits/telemetry/Telemetry.kt b/telemetry/jetbrains/src/test/resources/generateOTelBaseSpan/output/software/aws/toolkits/telemetry/Telemetry.kt new file mode 100644 index 00000000..288794da --- /dev/null +++ b/telemetry/jetbrains/src/test/resources/generateOTelBaseSpan/output/software/aws/toolkits/telemetry/Telemetry.kt @@ -0,0 +1,10 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +// THIS FILE IS GENERATED! DO NOT EDIT BY HAND! +@file:Suppress("unused", "MemberVisibilityCanBePrivate") + +package software.aws.toolkits.telemetry + +import kotlin.Suppress + +public object Telemetry diff --git a/telemetry/jetbrains/src/test/resources/generateOTelBaseSpan/output/software/aws/toolkits/telemetry/impl/BaseSpan.kt b/telemetry/jetbrains/src/test/resources/generateOTelBaseSpan/output/software/aws/toolkits/telemetry/impl/BaseSpan.kt new file mode 100644 index 00000000..a1fbacbf --- /dev/null +++ b/telemetry/jetbrains/src/test/resources/generateOTelBaseSpan/output/software/aws/toolkits/telemetry/impl/BaseSpan.kt @@ -0,0 +1,32 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +// THIS FILE IS GENERATED! DO NOT EDIT BY HAND! +@file:Suppress("unused", "MemberVisibilityCanBePrivate") + +package software.aws.toolkits.telemetry.`impl` + +import io.opentelemetry.api.trace.Span +import io.opentelemetry.context.Context +import io.opentelemetry.sdk.trace.ReadWriteSpan +import kotlin.Boolean +import kotlin.Number +import kotlin.Suppress +import software.amazon.awssdk.services.toolkittelemetry.model.MetricUnit +import software.aws.toolkits.jetbrains.services.telemetry.otel.AbstractBaseSpan + +public open class BaseSpan>( + context: Context?, + `delegate`: Span, +) : AbstractBaseSpan(context, delegate as ReadWriteSpan) { + public fun passive(passive: Boolean) { + this._passive = passive + } + + public fun unit(unit: MetricUnit) { + this._unit = unit + } + + public fun `value`(`value`: Number) { + this._value = `value`.toDouble() + } +} diff --git a/telemetry/jetbrains/src/test/resources/generateOverrides/output/software/aws/toolkits/telemetry/MetadataTelemetry.kt b/telemetry/jetbrains/src/test/resources/generateOverrides/output/software/aws/toolkits/telemetry/MetadataTelemetry.kt index 71ddc7e4..cbb69a99 100644 --- a/telemetry/jetbrains/src/test/resources/generateOverrides/output/software/aws/toolkits/telemetry/MetadataTelemetry.kt +++ b/telemetry/jetbrains/src/test/resources/generateOverrides/output/software/aws/toolkits/telemetry/MetadataTelemetry.kt @@ -22,6 +22,10 @@ import software.aws.toolkits.jetbrains.services.telemetry.TelemetryService ) public typealias Result = MetricResult +@Deprecated( + "Use type-safe metric builders", + ReplaceWith("Telemetry.metadata", "software.aws.toolkits.telemetry.Telemetry"), +) public object MetadataTelemetry { /** * It does not actually have a result, yep @@ -102,8 +106,7 @@ public object MetadataTelemetry { `value`: Double = 1.0, createTime: Instant = Instant.now(), ) { - hasResult(project, if(success) MetricResult.Succeeded else MetricResult.Failed, passive, - value, createTime) + hasResult(project, if(success) MetricResult.Succeeded else MetricResult.Failed, passive, value, createTime) } /** @@ -116,8 +119,7 @@ public object MetadataTelemetry { `value`: Double = 1.0, createTime: Instant = Instant.now(), ) { - hasResult(connectionSettings, if(success) MetricResult.Succeeded else MetricResult.Failed, - passive, value, createTime) + hasResult(connectionSettings, if(success) MetricResult.Succeeded else MetricResult.Failed, passive, value, createTime) } /** @@ -130,7 +132,6 @@ public object MetadataTelemetry { `value`: Double = 1.0, createTime: Instant = Instant.now(), ) { - hasResult(metadata, if(success) MetricResult.Succeeded else MetricResult.Failed, passive, - value, createTime) + hasResult(metadata, if(success) MetricResult.Succeeded else MetricResult.Failed, passive, value, createTime) } } diff --git a/telemetry/jetbrains/src/test/resources/generateOverrides/output/software/aws/toolkits/telemetry/TelemetryEnums.kt b/telemetry/jetbrains/src/test/resources/generateOverrides/output/software/aws/toolkits/telemetry/TelemetryEnums.kt index deee6dd8..379e1900 100644 --- a/telemetry/jetbrains/src/test/resources/generateOverrides/output/software/aws/toolkits/telemetry/TelemetryEnums.kt +++ b/telemetry/jetbrains/src/test/resources/generateOverrides/output/software/aws/toolkits/telemetry/TelemetryEnums.kt @@ -21,7 +21,6 @@ public enum class MetricResult( override fun toString(): String = value public companion object { - public fun from(type: String): MetricResult = values().firstOrNull { it.value == type } - ?: Unknown + public fun from(type: String): MetricResult = values().firstOrNull { it.value == type } ?: Unknown } } diff --git a/telemetry/jetbrains/src/test/resources/generateWithGlobalMetadata/output/software/aws/toolkits/telemetry/TelemetryEnums.kt b/telemetry/jetbrains/src/test/resources/generateWithGlobalMetadata/output/software/aws/toolkits/telemetry/TelemetryEnums.kt index 027f36dc..110ac3f7 100644 --- a/telemetry/jetbrains/src/test/resources/generateWithGlobalMetadata/output/software/aws/toolkits/telemetry/TelemetryEnums.kt +++ b/telemetry/jetbrains/src/test/resources/generateWithGlobalMetadata/output/software/aws/toolkits/telemetry/TelemetryEnums.kt @@ -23,7 +23,6 @@ public enum class MetricResult( override fun toString(): String = value public companion object { - public fun from(type: String): MetricResult = values().firstOrNull { it.value == type } - ?: Unknown + public fun from(type: String): MetricResult = values().firstOrNull { it.value == type } ?: Unknown } } diff --git a/telemetry/jetbrains/src/test/resources/generateWithGlobalMetadata/output/software/aws/toolkits/telemetry/TestTelemetry.kt b/telemetry/jetbrains/src/test/resources/generateWithGlobalMetadata/output/software/aws/toolkits/telemetry/TestTelemetry.kt index 21290d1a..f8cbd949 100644 --- a/telemetry/jetbrains/src/test/resources/generateWithGlobalMetadata/output/software/aws/toolkits/telemetry/TestTelemetry.kt +++ b/telemetry/jetbrains/src/test/resources/generateWithGlobalMetadata/output/software/aws/toolkits/telemetry/TestTelemetry.kt @@ -23,6 +23,10 @@ import software.aws.toolkits.jetbrains.services.telemetry.TelemetryService ) public typealias Result = MetricResult +@Deprecated( + "Use type-safe metric builders", + ReplaceWith("Telemetry.test", "software.aws.toolkits.telemetry.Telemetry"), +) public object TestTelemetry { /** * Testing metric with global metadata fields @@ -220,9 +224,7 @@ public object TestTelemetry { `value`: Double = 1.0, createTime: Instant = Instant.now(), ) { - metric(project, duration, httpStatusCode, reason, reasonDesc, requestId, requestServiceType, - if(success) MetricResult.Succeeded else MetricResult.Failed, traceId, metricId, - parentId, passive, value, createTime) + metric(project, duration, httpStatusCode, reason, reasonDesc, requestId, requestServiceType, if(success) MetricResult.Succeeded else MetricResult.Failed, traceId, metricId, parentId, passive, value, createTime) } /** @@ -244,9 +246,7 @@ public object TestTelemetry { `value`: Double = 1.0, createTime: Instant = Instant.now(), ) { - metric(connectionSettings, duration, httpStatusCode, reason, reasonDesc, requestId, - requestServiceType, if(success) MetricResult.Succeeded else MetricResult.Failed, - traceId, metricId, parentId, passive, value, createTime) + metric(connectionSettings, duration, httpStatusCode, reason, reasonDesc, requestId, requestServiceType, if(success) MetricResult.Succeeded else MetricResult.Failed, traceId, metricId, parentId, passive, value, createTime) } /** @@ -268,8 +268,6 @@ public object TestTelemetry { `value`: Double = 1.0, createTime: Instant = Instant.now(), ) { - metric(metadata, duration, httpStatusCode, reason, reasonDesc, requestId, - requestServiceType, if(success) MetricResult.Succeeded else MetricResult.Failed, - traceId, metricId, parentId, passive, value, createTime) + metric(metadata, duration, httpStatusCode, reason, reasonDesc, requestId, requestServiceType, if(success) MetricResult.Succeeded else MetricResult.Failed, traceId, metricId, parentId, passive, value, createTime) } } diff --git a/telemetry/jetbrains/src/test/resources/generatesOTelWithNormalInput/input.json b/telemetry/jetbrains/src/test/resources/generatesOTelWithNormalInput/input.json new file mode 100644 index 00000000..dc363e9c --- /dev/null +++ b/telemetry/jetbrains/src/test/resources/generatesOTelWithNormalInput/input.json @@ -0,0 +1,61 @@ +{ + "types": [ + { + "name": "lambdaRuntime", + "type": "string", + "allowedValues": [ + "dotnetcore2.1", + "nodejs12.x" + ], + "description": "The lambda runtime" + }, + { + "name": "duration", + "type": "double", + "description": "The duration of the operation in miliseconds" + }, + { + "name": "inttype", + "description": "a test int type", + "type": "int" + }, + { + "name": "booltype", + "description": "a test boolean type", + "type": "boolean" + }, + { + "name": "arbitraryString", + "description": "untyped string type" + } + ], + "metrics": [ + { + "name": "lambda_delete", + "description": "called when deleting lambdas remotely", + "unit": "None", + "metadata": [{ "type": "duration" }, { "type": "booltype" }] + }, + { + "name": "lambda_create", + "description": "called when creating lambdas remotely", + "unit": "None", + "metadata": [{ "type": "lambdaRuntime" }, { "type": "arbitraryString" } ] + }, + { + "name": "lambda_remoteinvoke", + "description": "called when invoking lambdas remotely", + "unit": "None", + "metadata": [{ "type": "lambdaRuntime", "required": false }, { "type": "inttype" }] + }, + { + "name": "no_metadata", + "description": "called when invoking lambdas remotely" + }, + { + "name": "passive_passive", + "description": "a passive metric", + "passive": true + } + ] +} diff --git a/telemetry/jetbrains/src/test/resources/generatesOTelWithNormalInput/output/software/aws/toolkits/telemetry/Telemetry.kt b/telemetry/jetbrains/src/test/resources/generatesOTelWithNormalInput/output/software/aws/toolkits/telemetry/Telemetry.kt new file mode 100644 index 00000000..0e838a59 --- /dev/null +++ b/telemetry/jetbrains/src/test/resources/generatesOTelWithNormalInput/output/software/aws/toolkits/telemetry/Telemetry.kt @@ -0,0 +1,23 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +// THIS FILE IS GENERATED! DO NOT EDIT BY HAND! +@file:Suppress("unused", "MemberVisibilityCanBePrivate") + +package software.aws.toolkits.telemetry + +import kotlin.Suppress +import software.aws.toolkits.jetbrains.services.telemetry.otel.OTelService +import software.aws.toolkits.telemetry.`impl`.LambdaTracer +import software.aws.toolkits.telemetry.`impl`.NoTracer +import software.aws.toolkits.telemetry.`impl`.PassiveTracer + +public object Telemetry { + public val lambda: LambdaTracer + get() = LambdaTracer(OTelService.getSdk().getTracer("lambda")) + + public val no: NoTracer + get() = NoTracer(OTelService.getSdk().getTracer("no")) + + public val passive: PassiveTracer + get() = PassiveTracer(OTelService.getSdk().getTracer("passive")) +} diff --git a/telemetry/jetbrains/src/test/resources/generatesOTelWithNormalInput/output/software/aws/toolkits/telemetry/impl/BaseSpan.kt b/telemetry/jetbrains/src/test/resources/generatesOTelWithNormalInput/output/software/aws/toolkits/telemetry/impl/BaseSpan.kt new file mode 100644 index 00000000..b9a9fdb3 --- /dev/null +++ b/telemetry/jetbrains/src/test/resources/generatesOTelWithNormalInput/output/software/aws/toolkits/telemetry/impl/BaseSpan.kt @@ -0,0 +1,48 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +// THIS FILE IS GENERATED! DO NOT EDIT BY HAND! +@file:Suppress("unused", "MemberVisibilityCanBePrivate") + +package software.aws.toolkits.telemetry.`impl` + +import io.opentelemetry.api.trace.Span +import io.opentelemetry.context.Context +import io.opentelemetry.sdk.trace.ReadWriteSpan +import kotlin.Boolean +import kotlin.Double +import kotlin.Float +import kotlin.Number +import kotlin.Suppress +import software.amazon.awssdk.services.toolkittelemetry.model.MetricUnit +import software.aws.toolkits.jetbrains.services.telemetry.otel.AbstractBaseSpan + +public open class BaseSpan>( + context: Context?, + `delegate`: Span, +) : AbstractBaseSpan(context, delegate as ReadWriteSpan) { + /** + * The duration of the operation in miliseconds + */ + public fun duration(duration: Float?) { + metadata("duration", duration?.let { it.toString() }) + } + + /** + * The duration of the operation in miliseconds + */ + public fun duration(duration: Double?) { + metadata("duration", duration?.let { it.toString() }) + } + + public fun passive(passive: Boolean) { + this._passive = passive + } + + public fun unit(unit: MetricUnit) { + this._unit = unit + } + + public fun `value`(`value`: Number) { + this._value = `value`.toDouble() + } +} diff --git a/telemetry/jetbrains/src/test/resources/generatesOTelWithNormalInput/output/software/aws/toolkits/telemetry/impl/LambdaTracer.kt b/telemetry/jetbrains/src/test/resources/generatesOTelWithNormalInput/output/software/aws/toolkits/telemetry/impl/LambdaTracer.kt new file mode 100644 index 00000000..414a592d --- /dev/null +++ b/telemetry/jetbrains/src/test/resources/generatesOTelWithNormalInput/output/software/aws/toolkits/telemetry/impl/LambdaTracer.kt @@ -0,0 +1,146 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +// THIS FILE IS GENERATED! DO NOT EDIT BY HAND! +@file:Suppress("unused", "MemberVisibilityCanBePrivate") + +package software.aws.toolkits.telemetry.`impl` + +import io.opentelemetry.api.trace.Span +import io.opentelemetry.api.trace.SpanBuilder +import io.opentelemetry.api.trace.Tracer +import io.opentelemetry.context.Context +import kotlin.Boolean +import kotlin.Int +import kotlin.Long +import kotlin.String +import kotlin.Suppress +import kotlin.collections.Collection +import software.aws.toolkits.jetbrains.services.telemetry.otel.AbstractSpanBuilder +import software.aws.toolkits.jetbrains.services.telemetry.otel.DefaultSpanBuilder +import software.aws.toolkits.telemetry.LambdaRuntime + +/** + * called when deleting lambdas remotely + */ +public class LambdadeleteSpan internal constructor( + context: Context?, + span: Span, +) : BaseSpan(context, span) { + init { + passive(false) + } + + override val requiredFields: Collection = setOf("duration","booltype",) + + /** + * a test boolean type + */ + public fun booltype(booltype: Boolean) { + metadata("booltype", booltype.toString()) + } +} + +public class LambdadeleteSpanBuilder internal constructor( + `delegate`: SpanBuilder, +) : AbstractSpanBuilder(delegate) { + override fun doStartSpan(): LambdadeleteSpan = LambdadeleteSpan(parent, delegate.startSpan()) +} + +/** + * called when creating lambdas remotely + */ +public class LambdacreateSpan internal constructor( + context: Context?, + span: Span, +) : BaseSpan(context, span) { + init { + passive(false) + } + + override val requiredFields: Collection = setOf("lambdaRuntime","arbitraryString",) + + /** + * The lambda runtime + */ + public fun lambdaRuntime(lambdaRuntime: LambdaRuntime) { + metadata("lambdaRuntime", lambdaRuntime.toString()) + } + + /** + * untyped string type + */ + public fun arbitraryString(arbitraryString: String) { + metadata("arbitraryString", arbitraryString) + } +} + +public class LambdacreateSpanBuilder internal constructor( + `delegate`: SpanBuilder, +) : AbstractSpanBuilder(delegate) { + override fun doStartSpan(): LambdacreateSpan = LambdacreateSpan(parent, delegate.startSpan()) +} + +/** + * called when invoking lambdas remotely + */ +public class LambdaremoteinvokeSpan internal constructor( + context: Context?, + span: Span, +) : BaseSpan(context, span) { + init { + passive(false) + } + + override val requiredFields: Collection = setOf("inttype",) + + /** + * The lambda runtime + */ + public fun lambdaRuntime(lambdaRuntime: LambdaRuntime?) { + metadata("lambdaRuntime", lambdaRuntime?.let { it.toString() }) + } + + /** + * a test int type + */ + public fun inttype(inttype: Long) { + metadata("inttype", inttype.toString()) + } + + /** + * a test int type + */ + public fun inttype(inttype: Int) { + metadata("inttype", inttype.toString()) + } +} + +public class LambdaremoteinvokeSpanBuilder internal constructor( + `delegate`: SpanBuilder, +) : AbstractSpanBuilder(delegate) { + override fun doStartSpan(): LambdaremoteinvokeSpan = LambdaremoteinvokeSpan(parent, delegate.startSpan()) +} + +public class LambdaTracer internal constructor( + private val `delegate`: Tracer, +) : Tracer { + /** + * called when deleting lambdas remotely + */ + public val delete: LambdadeleteSpanBuilder + get() = LambdadeleteSpanBuilder(delegate.spanBuilder("lambda_delete")) + + /** + * called when creating lambdas remotely + */ + public val create: LambdacreateSpanBuilder + get() = LambdacreateSpanBuilder(delegate.spanBuilder("lambda_create")) + + /** + * called when invoking lambdas remotely + */ + public val remoteinvoke: LambdaremoteinvokeSpanBuilder + get() = LambdaremoteinvokeSpanBuilder(delegate.spanBuilder("lambda_remoteinvoke")) + + override fun spanBuilder(spanName: String): DefaultSpanBuilder = DefaultSpanBuilder(delegate.spanBuilder(spanName)) +} diff --git a/telemetry/jetbrains/src/test/resources/generatesOTelWithNormalInput/output/software/aws/toolkits/telemetry/impl/NoTracer.kt b/telemetry/jetbrains/src/test/resources/generatesOTelWithNormalInput/output/software/aws/toolkits/telemetry/impl/NoTracer.kt new file mode 100644 index 00000000..ad9950fe --- /dev/null +++ b/telemetry/jetbrains/src/test/resources/generatesOTelWithNormalInput/output/software/aws/toolkits/telemetry/impl/NoTracer.kt @@ -0,0 +1,48 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +// THIS FILE IS GENERATED! DO NOT EDIT BY HAND! +@file:Suppress("unused", "MemberVisibilityCanBePrivate") + +package software.aws.toolkits.telemetry.`impl` + +import io.opentelemetry.api.trace.Span +import io.opentelemetry.api.trace.SpanBuilder +import io.opentelemetry.api.trace.Tracer +import io.opentelemetry.context.Context +import kotlin.String +import kotlin.Suppress +import kotlin.collections.Collection +import software.aws.toolkits.jetbrains.services.telemetry.otel.AbstractSpanBuilder +import software.aws.toolkits.jetbrains.services.telemetry.otel.DefaultSpanBuilder + +/** + * called when invoking lambdas remotely + */ +public class NometadataSpan internal constructor( + context: Context?, + span: Span, +) : BaseSpan(context, span) { + init { + passive(false) + } + + override val requiredFields: Collection = setOf() +} + +public class NometadataSpanBuilder internal constructor( + `delegate`: SpanBuilder, +) : AbstractSpanBuilder(delegate) { + override fun doStartSpan(): NometadataSpan = NometadataSpan(parent, delegate.startSpan()) +} + +public class NoTracer internal constructor( + private val `delegate`: Tracer, +) : Tracer { + /** + * called when invoking lambdas remotely + */ + public val metadata: NometadataSpanBuilder + get() = NometadataSpanBuilder(delegate.spanBuilder("no_metadata")) + + override fun spanBuilder(spanName: String): DefaultSpanBuilder = DefaultSpanBuilder(delegate.spanBuilder(spanName)) +} diff --git a/telemetry/jetbrains/src/test/resources/generatesOTelWithNormalInput/output/software/aws/toolkits/telemetry/impl/PassiveTracer.kt b/telemetry/jetbrains/src/test/resources/generatesOTelWithNormalInput/output/software/aws/toolkits/telemetry/impl/PassiveTracer.kt new file mode 100644 index 00000000..c4cfe22e --- /dev/null +++ b/telemetry/jetbrains/src/test/resources/generatesOTelWithNormalInput/output/software/aws/toolkits/telemetry/impl/PassiveTracer.kt @@ -0,0 +1,44 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +// THIS FILE IS GENERATED! DO NOT EDIT BY HAND! +@file:Suppress("unused", "MemberVisibilityCanBePrivate") + +package software.aws.toolkits.telemetry.`impl` + +import io.opentelemetry.api.trace.Span +import io.opentelemetry.api.trace.SpanBuilder +import io.opentelemetry.api.trace.Tracer +import io.opentelemetry.context.Context +import kotlin.String +import kotlin.Suppress +import kotlin.collections.Collection +import software.aws.toolkits.jetbrains.services.telemetry.otel.AbstractSpanBuilder +import software.aws.toolkits.jetbrains.services.telemetry.otel.DefaultSpanBuilder + +/** + * a passive metric + */ +public class PassivepassiveSpan internal constructor( + context: Context?, + span: Span, +) : BaseSpan(context, span) { + override val requiredFields: Collection = setOf() +} + +public class PassivepassiveSpanBuilder internal constructor( + `delegate`: SpanBuilder, +) : AbstractSpanBuilder(delegate) { + override fun doStartSpan(): PassivepassiveSpan = PassivepassiveSpan(parent, delegate.startSpan()) +} + +public class PassiveTracer internal constructor( + private val `delegate`: Tracer, +) : Tracer { + /** + * a passive metric + */ + public val passive: PassivepassiveSpanBuilder + get() = PassivepassiveSpanBuilder(delegate.spanBuilder("passive_passive")) + + override fun spanBuilder(spanName: String): DefaultSpanBuilder = DefaultSpanBuilder(delegate.spanBuilder(spanName)) +} diff --git a/telemetry/jetbrains/src/test/resources/generatesWithNormalInput/output/software/aws/toolkits/telemetry/LambdaTelemetry.kt b/telemetry/jetbrains/src/test/resources/generatesWithNormalInput/output/software/aws/toolkits/telemetry/LambdaTelemetry.kt index 2c0894ff..fb419738 100644 --- a/telemetry/jetbrains/src/test/resources/generatesWithNormalInput/output/software/aws/toolkits/telemetry/LambdaTelemetry.kt +++ b/telemetry/jetbrains/src/test/resources/generatesWithNormalInput/output/software/aws/toolkits/telemetry/LambdaTelemetry.kt @@ -8,6 +8,7 @@ package software.aws.toolkits.telemetry import com.intellij.openapi.project.Project import java.time.Instant import kotlin.Boolean +import kotlin.Deprecated import kotlin.Double import kotlin.Long import kotlin.String @@ -17,6 +18,10 @@ import software.aws.toolkits.core.ConnectionSettings import software.aws.toolkits.jetbrains.services.telemetry.MetricEventMetadata import software.aws.toolkits.jetbrains.services.telemetry.TelemetryService +@Deprecated( + "Use type-safe metric builders", + ReplaceWith("Telemetry.lambda", "software.aws.toolkits.telemetry.Telemetry"), +) public object LambdaTelemetry { /** * called when creating lambdas remotely diff --git a/telemetry/jetbrains/src/test/resources/generatesWithNormalInput/output/software/aws/toolkits/telemetry/NoTelemetry.kt b/telemetry/jetbrains/src/test/resources/generatesWithNormalInput/output/software/aws/toolkits/telemetry/NoTelemetry.kt index cb9bf646..d18c3856 100644 --- a/telemetry/jetbrains/src/test/resources/generatesWithNormalInput/output/software/aws/toolkits/telemetry/NoTelemetry.kt +++ b/telemetry/jetbrains/src/test/resources/generatesWithNormalInput/output/software/aws/toolkits/telemetry/NoTelemetry.kt @@ -8,6 +8,7 @@ package software.aws.toolkits.telemetry import com.intellij.openapi.project.Project import java.time.Instant import kotlin.Boolean +import kotlin.Deprecated import kotlin.Double import kotlin.Suppress import software.amazon.awssdk.services.toolkittelemetry.model.Unit @@ -15,6 +16,10 @@ import software.aws.toolkits.core.ConnectionSettings import software.aws.toolkits.jetbrains.services.telemetry.MetricEventMetadata import software.aws.toolkits.jetbrains.services.telemetry.TelemetryService +@Deprecated( + "Use type-safe metric builders", + ReplaceWith("Telemetry.no", "software.aws.toolkits.telemetry.Telemetry"), +) public object NoTelemetry { /** * called when invoking lambdas remotely diff --git a/telemetry/jetbrains/src/test/resources/generatesWithNormalInput/output/software/aws/toolkits/telemetry/PassiveTelemetry.kt b/telemetry/jetbrains/src/test/resources/generatesWithNormalInput/output/software/aws/toolkits/telemetry/PassiveTelemetry.kt index 9519f306..65d6b441 100644 --- a/telemetry/jetbrains/src/test/resources/generatesWithNormalInput/output/software/aws/toolkits/telemetry/PassiveTelemetry.kt +++ b/telemetry/jetbrains/src/test/resources/generatesWithNormalInput/output/software/aws/toolkits/telemetry/PassiveTelemetry.kt @@ -8,6 +8,7 @@ package software.aws.toolkits.telemetry import com.intellij.openapi.project.Project import java.time.Instant import kotlin.Boolean +import kotlin.Deprecated import kotlin.Double import kotlin.Suppress import software.amazon.awssdk.services.toolkittelemetry.model.Unit @@ -15,6 +16,10 @@ import software.aws.toolkits.core.ConnectionSettings import software.aws.toolkits.jetbrains.services.telemetry.MetricEventMetadata import software.aws.toolkits.jetbrains.services.telemetry.TelemetryService +@Deprecated( + "Use type-safe metric builders", + ReplaceWith("Telemetry.passive", "software.aws.toolkits.telemetry.Telemetry"), +) public object PassiveTelemetry { /** * a passive metric diff --git a/telemetry/jetbrains/src/test/resources/generatesWithNormalInput/output/software/aws/toolkits/telemetry/TelemetryEnums.kt b/telemetry/jetbrains/src/test/resources/generatesWithNormalInput/output/software/aws/toolkits/telemetry/TelemetryEnums.kt index 31b7b686..9a5ecdf7 100644 --- a/telemetry/jetbrains/src/test/resources/generatesWithNormalInput/output/software/aws/toolkits/telemetry/TelemetryEnums.kt +++ b/telemetry/jetbrains/src/test/resources/generatesWithNormalInput/output/software/aws/toolkits/telemetry/TelemetryEnums.kt @@ -22,7 +22,6 @@ public enum class LambdaRuntime( override fun toString(): String = value public companion object { - public fun from(type: String): LambdaRuntime = values().firstOrNull { it.value == type } - ?: Unknown + public fun from(type: String): LambdaRuntime = values().firstOrNull { it.value == type } ?: Unknown } } diff --git a/telemetry/jetbrains/src/test/resources/longEnum/output/software/aws/toolkits/telemetry/TelemetryEnums.kt b/telemetry/jetbrains/src/test/resources/longEnum/output/software/aws/toolkits/telemetry/TelemetryEnums.kt index 04e8ef68..5a42acc4 100644 --- a/telemetry/jetbrains/src/test/resources/longEnum/output/software/aws/toolkits/telemetry/TelemetryEnums.kt +++ b/telemetry/jetbrains/src/test/resources/longEnum/output/software/aws/toolkits/telemetry/TelemetryEnums.kt @@ -11,8 +11,7 @@ import kotlin.Suppress /** * an enum */ -public enum class - Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa( +public enum class Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa( private val `value`: String, ) { A("A"), @@ -25,8 +24,6 @@ public enum class override fun toString(): String = value public companion object { - public fun from(type: String): - Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa - = values().firstOrNull { it.value == type } ?: Unknown + public fun from(type: String): Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = values().firstOrNull { it.value == type } ?: Unknown } } diff --git a/telemetry/jetbrains/src/test/resources/resultGeneratesTwoFunctions/output/software/aws/toolkits/telemetry/MetadataTelemetry.kt b/telemetry/jetbrains/src/test/resources/resultGeneratesTwoFunctions/output/software/aws/toolkits/telemetry/MetadataTelemetry.kt index 2536874e..006bf6c8 100644 --- a/telemetry/jetbrains/src/test/resources/resultGeneratesTwoFunctions/output/software/aws/toolkits/telemetry/MetadataTelemetry.kt +++ b/telemetry/jetbrains/src/test/resources/resultGeneratesTwoFunctions/output/software/aws/toolkits/telemetry/MetadataTelemetry.kt @@ -22,6 +22,10 @@ import software.aws.toolkits.jetbrains.services.telemetry.TelemetryService ) public typealias Result = MetricResult +@Deprecated( + "Use type-safe metric builders", + ReplaceWith("Telemetry.metadata", "software.aws.toolkits.telemetry.Telemetry"), +) public object MetadataTelemetry { /** * It has a result @@ -96,8 +100,7 @@ public object MetadataTelemetry { `value`: Double = 1.0, createTime: Instant = Instant.now(), ) { - hasResult(project, if(success) MetricResult.Succeeded else MetricResult.Failed, passive, - value, createTime) + hasResult(project, if(success) MetricResult.Succeeded else MetricResult.Failed, passive, value, createTime) } /** @@ -110,8 +113,7 @@ public object MetadataTelemetry { `value`: Double = 1.0, createTime: Instant = Instant.now(), ) { - hasResult(connectionSettings, if(success) MetricResult.Succeeded else MetricResult.Failed, - passive, value, createTime) + hasResult(connectionSettings, if(success) MetricResult.Succeeded else MetricResult.Failed, passive, value, createTime) } /** @@ -124,7 +126,6 @@ public object MetadataTelemetry { `value`: Double = 1.0, createTime: Instant = Instant.now(), ) { - hasResult(metadata, if(success) MetricResult.Succeeded else MetricResult.Failed, passive, - value, createTime) + hasResult(metadata, if(success) MetricResult.Succeeded else MetricResult.Failed, passive, value, createTime) } } diff --git a/telemetry/jetbrains/src/test/resources/resultGeneratesTwoFunctions/output/software/aws/toolkits/telemetry/TelemetryEnums.kt b/telemetry/jetbrains/src/test/resources/resultGeneratesTwoFunctions/output/software/aws/toolkits/telemetry/TelemetryEnums.kt index 03c86db1..ff524001 100644 --- a/telemetry/jetbrains/src/test/resources/resultGeneratesTwoFunctions/output/software/aws/toolkits/telemetry/TelemetryEnums.kt +++ b/telemetry/jetbrains/src/test/resources/resultGeneratesTwoFunctions/output/software/aws/toolkits/telemetry/TelemetryEnums.kt @@ -26,7 +26,6 @@ public enum class MetricResult( override fun toString(): String = value public companion object { - public fun from(type: String): MetricResult = values().firstOrNull { it.value == type } - ?: Unknown + public fun from(type: String): MetricResult = values().firstOrNull { it.value == type } ?: Unknown } } From 9214f562f13805506fdd4fab71ff593f74e083bc Mon Sep 17 00:00:00 2001 From: Richard Li Date: Thu, 31 Oct 2024 11:38:04 -0700 Subject: [PATCH 2/6] tst --- .../toolkits/telemetry/MetadataTelemetry.kt | 14 ++++--------- .../aws/toolkits/telemetry/TelemetryEnums.kt | 7 +++++++ .../aws/toolkits/telemetry/TelemetryEnums.kt | 7 +++++++ .../aws/toolkits/telemetry/TestTelemetry.kt | 14 ++++--------- .../aws/toolkits/telemetry/LambdaTelemetry.kt | 20 +++++++++---------- .../aws/toolkits/telemetry/NoTelemetry.kt | 8 ++++---- .../toolkits/telemetry/PassiveTelemetry.kt | 8 ++++---- .../aws/toolkits/telemetry/TelemetryEnums.kt | 7 +++++++ .../aws/toolkits/telemetry/TelemetryEnums.kt | 7 +++++++ .../toolkits/telemetry/MetadataTelemetry.kt | 14 ++++--------- .../aws/toolkits/telemetry/TelemetryEnums.kt | 7 +++++++ 11 files changed, 65 insertions(+), 48 deletions(-) diff --git a/telemetry/jetbrains/src/test/resources/generateOverrides/output/software/aws/toolkits/telemetry/MetadataTelemetry.kt b/telemetry/jetbrains/src/test/resources/generateOverrides/output/software/aws/toolkits/telemetry/MetadataTelemetry.kt index cbb69a99..33fd7b91 100644 --- a/telemetry/jetbrains/src/test/resources/generateOverrides/output/software/aws/toolkits/telemetry/MetadataTelemetry.kt +++ b/telemetry/jetbrains/src/test/resources/generateOverrides/output/software/aws/toolkits/telemetry/MetadataTelemetry.kt @@ -11,17 +11,11 @@ import kotlin.Boolean import kotlin.Deprecated import kotlin.Double import kotlin.Suppress -import software.amazon.awssdk.services.toolkittelemetry.model.Unit +import software.amazon.awssdk.services.toolkittelemetry.model.MetricUnit import software.aws.toolkits.core.ConnectionSettings import software.aws.toolkits.jetbrains.services.telemetry.MetricEventMetadata import software.aws.toolkits.jetbrains.services.telemetry.TelemetryService -@Deprecated( - message = "Name conflicts with the Kotlin standard library", - replaceWith = ReplaceWith("MetricResult", "software.aws.toolkits.telemetry.MetricResult"), -) -public typealias Result = MetricResult - @Deprecated( "Use type-safe metric builders", ReplaceWith("Telemetry.metadata", "software.aws.toolkits.telemetry.Telemetry"), @@ -40,7 +34,7 @@ public object MetadataTelemetry { TelemetryService.getInstance().record(project) { datum("metadata_hasResult") { createTime(createTime) - unit(Unit.NONE) + unit(MetricUnit.NONE) value(value) passive(passive) if(result != null) { @@ -63,7 +57,7 @@ public object MetadataTelemetry { TelemetryService.getInstance().record(connectionSettings) { datum("metadata_hasResult") { createTime(createTime) - unit(Unit.NONE) + unit(MetricUnit.NONE) value(value) passive(passive) if(result != null) { @@ -86,7 +80,7 @@ public object MetadataTelemetry { TelemetryService.getInstance().record(metadata) { datum("metadata_hasResult") { createTime(createTime) - unit(Unit.NONE) + unit(MetricUnit.NONE) value(value) passive(passive) if(result != null) { diff --git a/telemetry/jetbrains/src/test/resources/generateOverrides/output/software/aws/toolkits/telemetry/TelemetryEnums.kt b/telemetry/jetbrains/src/test/resources/generateOverrides/output/software/aws/toolkits/telemetry/TelemetryEnums.kt index 379e1900..baacba28 100644 --- a/telemetry/jetbrains/src/test/resources/generateOverrides/output/software/aws/toolkits/telemetry/TelemetryEnums.kt +++ b/telemetry/jetbrains/src/test/resources/generateOverrides/output/software/aws/toolkits/telemetry/TelemetryEnums.kt @@ -5,6 +5,7 @@ package software.aws.toolkits.telemetry +import kotlin.Deprecated import kotlin.String import kotlin.Suppress @@ -24,3 +25,9 @@ public enum class MetricResult( public fun from(type: String): MetricResult = values().firstOrNull { it.value == type } ?: Unknown } } + +@Deprecated( + message = "Name conflicts with the Kotlin standard library", + replaceWith = ReplaceWith("MetricResult", "software.aws.toolkits.telemetry.MetricResult"), +) +public typealias Result = MetricResult diff --git a/telemetry/jetbrains/src/test/resources/generateWithGlobalMetadata/output/software/aws/toolkits/telemetry/TelemetryEnums.kt b/telemetry/jetbrains/src/test/resources/generateWithGlobalMetadata/output/software/aws/toolkits/telemetry/TelemetryEnums.kt index 110ac3f7..bdf9acae 100644 --- a/telemetry/jetbrains/src/test/resources/generateWithGlobalMetadata/output/software/aws/toolkits/telemetry/TelemetryEnums.kt +++ b/telemetry/jetbrains/src/test/resources/generateWithGlobalMetadata/output/software/aws/toolkits/telemetry/TelemetryEnums.kt @@ -5,6 +5,7 @@ package software.aws.toolkits.telemetry +import kotlin.Deprecated import kotlin.String import kotlin.Suppress @@ -26,3 +27,9 @@ public enum class MetricResult( public fun from(type: String): MetricResult = values().firstOrNull { it.value == type } ?: Unknown } } + +@Deprecated( + message = "Name conflicts with the Kotlin standard library", + replaceWith = ReplaceWith("MetricResult", "software.aws.toolkits.telemetry.MetricResult"), +) +public typealias Result = MetricResult diff --git a/telemetry/jetbrains/src/test/resources/generateWithGlobalMetadata/output/software/aws/toolkits/telemetry/TestTelemetry.kt b/telemetry/jetbrains/src/test/resources/generateWithGlobalMetadata/output/software/aws/toolkits/telemetry/TestTelemetry.kt index f8cbd949..73258201 100644 --- a/telemetry/jetbrains/src/test/resources/generateWithGlobalMetadata/output/software/aws/toolkits/telemetry/TestTelemetry.kt +++ b/telemetry/jetbrains/src/test/resources/generateWithGlobalMetadata/output/software/aws/toolkits/telemetry/TestTelemetry.kt @@ -12,17 +12,11 @@ import kotlin.Deprecated import kotlin.Double import kotlin.String import kotlin.Suppress -import software.amazon.awssdk.services.toolkittelemetry.model.Unit +import software.amazon.awssdk.services.toolkittelemetry.model.MetricUnit import software.aws.toolkits.core.ConnectionSettings import software.aws.toolkits.jetbrains.services.telemetry.MetricEventMetadata import software.aws.toolkits.jetbrains.services.telemetry.TelemetryService -@Deprecated( - message = "Name conflicts with the Kotlin standard library", - replaceWith = ReplaceWith("MetricResult", "software.aws.toolkits.telemetry.MetricResult"), -) -public typealias Result = MetricResult - @Deprecated( "Use type-safe metric builders", ReplaceWith("Telemetry.test", "software.aws.toolkits.telemetry.Telemetry"), @@ -50,7 +44,7 @@ public object TestTelemetry { TelemetryService.getInstance().record(project) { datum("test_metric") { createTime(createTime) - unit(Unit.NONE) + unit(MetricUnit.NONE) value(value) passive(passive) if(duration != null) { @@ -109,7 +103,7 @@ public object TestTelemetry { TelemetryService.getInstance().record(connectionSettings) { datum("test_metric") { createTime(createTime) - unit(Unit.NONE) + unit(MetricUnit.NONE) value(value) passive(passive) if(duration != null) { @@ -168,7 +162,7 @@ public object TestTelemetry { TelemetryService.getInstance().record(metadata) { datum("test_metric") { createTime(createTime) - unit(Unit.NONE) + unit(MetricUnit.NONE) value(value) passive(passive) if(duration != null) { diff --git a/telemetry/jetbrains/src/test/resources/generatesWithNormalInput/output/software/aws/toolkits/telemetry/LambdaTelemetry.kt b/telemetry/jetbrains/src/test/resources/generatesWithNormalInput/output/software/aws/toolkits/telemetry/LambdaTelemetry.kt index fb419738..d146fac0 100644 --- a/telemetry/jetbrains/src/test/resources/generatesWithNormalInput/output/software/aws/toolkits/telemetry/LambdaTelemetry.kt +++ b/telemetry/jetbrains/src/test/resources/generatesWithNormalInput/output/software/aws/toolkits/telemetry/LambdaTelemetry.kt @@ -13,7 +13,7 @@ import kotlin.Double import kotlin.Long import kotlin.String import kotlin.Suppress -import software.amazon.awssdk.services.toolkittelemetry.model.Unit +import software.amazon.awssdk.services.toolkittelemetry.model.MetricUnit import software.aws.toolkits.core.ConnectionSettings import software.aws.toolkits.jetbrains.services.telemetry.MetricEventMetadata import software.aws.toolkits.jetbrains.services.telemetry.TelemetryService @@ -38,7 +38,7 @@ public object LambdaTelemetry { TelemetryService.getInstance().record(project) { datum("lambda_create") { createTime(createTime) - unit(Unit.NONE) + unit(MetricUnit.NONE) value(value) passive(passive) metadata("lambdaRuntime", lambdaRuntime.toString()) @@ -65,7 +65,7 @@ public object LambdaTelemetry { TelemetryService.getInstance().record(connectionSettings) { datum("lambda_create") { createTime(createTime) - unit(Unit.NONE) + unit(MetricUnit.NONE) value(value) passive(passive) metadata("lambdaRuntime", lambdaRuntime.toString()) @@ -92,7 +92,7 @@ public object LambdaTelemetry { TelemetryService.getInstance().record(metadata) { datum("lambda_create") { createTime(createTime) - unit(Unit.NONE) + unit(MetricUnit.NONE) value(value) passive(passive) metadata("lambdaRuntime", lambdaRuntime.toString()) @@ -118,7 +118,7 @@ public object LambdaTelemetry { TelemetryService.getInstance().record(project) { datum("lambda_delete") { createTime(createTime) - unit(Unit.NONE) + unit(MetricUnit.NONE) value(value) passive(passive) metadata("duration", duration.toString()) @@ -141,7 +141,7 @@ public object LambdaTelemetry { TelemetryService.getInstance().record(connectionSettings) { datum("lambda_delete") { createTime(createTime) - unit(Unit.NONE) + unit(MetricUnit.NONE) value(value) passive(passive) metadata("duration", duration.toString()) @@ -164,7 +164,7 @@ public object LambdaTelemetry { TelemetryService.getInstance().record(metadata) { datum("lambda_delete") { createTime(createTime) - unit(Unit.NONE) + unit(MetricUnit.NONE) value(value) passive(passive) metadata("duration", duration.toString()) @@ -188,7 +188,7 @@ public object LambdaTelemetry { TelemetryService.getInstance().record(project) { datum("lambda_remoteinvoke") { createTime(createTime) - unit(Unit.NONE) + unit(MetricUnit.NONE) value(value) passive(passive) if(lambdaRuntime != null) { @@ -217,7 +217,7 @@ public object LambdaTelemetry { TelemetryService.getInstance().record(connectionSettings) { datum("lambda_remoteinvoke") { createTime(createTime) - unit(Unit.NONE) + unit(MetricUnit.NONE) value(value) passive(passive) if(lambdaRuntime != null) { @@ -246,7 +246,7 @@ public object LambdaTelemetry { TelemetryService.getInstance().record(metadata) { datum("lambda_remoteinvoke") { createTime(createTime) - unit(Unit.NONE) + unit(MetricUnit.NONE) value(value) passive(passive) if(lambdaRuntime != null) { diff --git a/telemetry/jetbrains/src/test/resources/generatesWithNormalInput/output/software/aws/toolkits/telemetry/NoTelemetry.kt b/telemetry/jetbrains/src/test/resources/generatesWithNormalInput/output/software/aws/toolkits/telemetry/NoTelemetry.kt index d18c3856..f0323114 100644 --- a/telemetry/jetbrains/src/test/resources/generatesWithNormalInput/output/software/aws/toolkits/telemetry/NoTelemetry.kt +++ b/telemetry/jetbrains/src/test/resources/generatesWithNormalInput/output/software/aws/toolkits/telemetry/NoTelemetry.kt @@ -11,7 +11,7 @@ import kotlin.Boolean import kotlin.Deprecated import kotlin.Double import kotlin.Suppress -import software.amazon.awssdk.services.toolkittelemetry.model.Unit +import software.amazon.awssdk.services.toolkittelemetry.model.MetricUnit import software.aws.toolkits.core.ConnectionSettings import software.aws.toolkits.jetbrains.services.telemetry.MetricEventMetadata import software.aws.toolkits.jetbrains.services.telemetry.TelemetryService @@ -34,7 +34,7 @@ public object NoTelemetry { TelemetryService.getInstance().record(project) { datum("no_metadata") { createTime(createTime) - unit(Unit.NONE) + unit(MetricUnit.NONE) value(value) passive(passive) if(duration != null) { @@ -57,7 +57,7 @@ public object NoTelemetry { TelemetryService.getInstance().record(connectionSettings) { datum("no_metadata") { createTime(createTime) - unit(Unit.NONE) + unit(MetricUnit.NONE) value(value) passive(passive) if(duration != null) { @@ -80,7 +80,7 @@ public object NoTelemetry { TelemetryService.getInstance().record(metadata) { datum("no_metadata") { createTime(createTime) - unit(Unit.NONE) + unit(MetricUnit.NONE) value(value) passive(passive) if(duration != null) { diff --git a/telemetry/jetbrains/src/test/resources/generatesWithNormalInput/output/software/aws/toolkits/telemetry/PassiveTelemetry.kt b/telemetry/jetbrains/src/test/resources/generatesWithNormalInput/output/software/aws/toolkits/telemetry/PassiveTelemetry.kt index 65d6b441..487ec051 100644 --- a/telemetry/jetbrains/src/test/resources/generatesWithNormalInput/output/software/aws/toolkits/telemetry/PassiveTelemetry.kt +++ b/telemetry/jetbrains/src/test/resources/generatesWithNormalInput/output/software/aws/toolkits/telemetry/PassiveTelemetry.kt @@ -11,7 +11,7 @@ import kotlin.Boolean import kotlin.Deprecated import kotlin.Double import kotlin.Suppress -import software.amazon.awssdk.services.toolkittelemetry.model.Unit +import software.amazon.awssdk.services.toolkittelemetry.model.MetricUnit import software.aws.toolkits.core.ConnectionSettings import software.aws.toolkits.jetbrains.services.telemetry.MetricEventMetadata import software.aws.toolkits.jetbrains.services.telemetry.TelemetryService @@ -34,7 +34,7 @@ public object PassiveTelemetry { TelemetryService.getInstance().record(project) { datum("passive_passive") { createTime(createTime) - unit(Unit.NONE) + unit(MetricUnit.NONE) value(value) passive(passive) if(duration != null) { @@ -57,7 +57,7 @@ public object PassiveTelemetry { TelemetryService.getInstance().record(connectionSettings) { datum("passive_passive") { createTime(createTime) - unit(Unit.NONE) + unit(MetricUnit.NONE) value(value) passive(passive) if(duration != null) { @@ -80,7 +80,7 @@ public object PassiveTelemetry { TelemetryService.getInstance().record(metadata) { datum("passive_passive") { createTime(createTime) - unit(Unit.NONE) + unit(MetricUnit.NONE) value(value) passive(passive) if(duration != null) { diff --git a/telemetry/jetbrains/src/test/resources/generatesWithNormalInput/output/software/aws/toolkits/telemetry/TelemetryEnums.kt b/telemetry/jetbrains/src/test/resources/generatesWithNormalInput/output/software/aws/toolkits/telemetry/TelemetryEnums.kt index 9a5ecdf7..cd357e12 100644 --- a/telemetry/jetbrains/src/test/resources/generatesWithNormalInput/output/software/aws/toolkits/telemetry/TelemetryEnums.kt +++ b/telemetry/jetbrains/src/test/resources/generatesWithNormalInput/output/software/aws/toolkits/telemetry/TelemetryEnums.kt @@ -5,6 +5,7 @@ package software.aws.toolkits.telemetry +import kotlin.Deprecated import kotlin.String import kotlin.Suppress @@ -25,3 +26,9 @@ public enum class LambdaRuntime( public fun from(type: String): LambdaRuntime = values().firstOrNull { it.value == type } ?: Unknown } } + +@Deprecated( + message = "Name conflicts with the Kotlin standard library", + replaceWith = ReplaceWith("MetricResult", "software.aws.toolkits.telemetry.MetricResult"), +) +public typealias Result = MetricResult diff --git a/telemetry/jetbrains/src/test/resources/longEnum/output/software/aws/toolkits/telemetry/TelemetryEnums.kt b/telemetry/jetbrains/src/test/resources/longEnum/output/software/aws/toolkits/telemetry/TelemetryEnums.kt index 5a42acc4..33f2a8b8 100644 --- a/telemetry/jetbrains/src/test/resources/longEnum/output/software/aws/toolkits/telemetry/TelemetryEnums.kt +++ b/telemetry/jetbrains/src/test/resources/longEnum/output/software/aws/toolkits/telemetry/TelemetryEnums.kt @@ -5,6 +5,7 @@ package software.aws.toolkits.telemetry +import kotlin.Deprecated import kotlin.String import kotlin.Suppress @@ -27,3 +28,9 @@ public enum class Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa public fun from(type: String): Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = values().firstOrNull { it.value == type } ?: Unknown } } + +@Deprecated( + message = "Name conflicts with the Kotlin standard library", + replaceWith = ReplaceWith("MetricResult", "software.aws.toolkits.telemetry.MetricResult"), +) +public typealias Result = MetricResult diff --git a/telemetry/jetbrains/src/test/resources/resultGeneratesTwoFunctions/output/software/aws/toolkits/telemetry/MetadataTelemetry.kt b/telemetry/jetbrains/src/test/resources/resultGeneratesTwoFunctions/output/software/aws/toolkits/telemetry/MetadataTelemetry.kt index 006bf6c8..0d7415b5 100644 --- a/telemetry/jetbrains/src/test/resources/resultGeneratesTwoFunctions/output/software/aws/toolkits/telemetry/MetadataTelemetry.kt +++ b/telemetry/jetbrains/src/test/resources/resultGeneratesTwoFunctions/output/software/aws/toolkits/telemetry/MetadataTelemetry.kt @@ -11,17 +11,11 @@ import kotlin.Boolean import kotlin.Deprecated import kotlin.Double import kotlin.Suppress -import software.amazon.awssdk.services.toolkittelemetry.model.Unit +import software.amazon.awssdk.services.toolkittelemetry.model.MetricUnit import software.aws.toolkits.core.ConnectionSettings import software.aws.toolkits.jetbrains.services.telemetry.MetricEventMetadata import software.aws.toolkits.jetbrains.services.telemetry.TelemetryService -@Deprecated( - message = "Name conflicts with the Kotlin standard library", - replaceWith = ReplaceWith("MetricResult", "software.aws.toolkits.telemetry.MetricResult"), -) -public typealias Result = MetricResult - @Deprecated( "Use type-safe metric builders", ReplaceWith("Telemetry.metadata", "software.aws.toolkits.telemetry.Telemetry"), @@ -40,7 +34,7 @@ public object MetadataTelemetry { TelemetryService.getInstance().record(project) { datum("metadata_hasResult") { createTime(createTime) - unit(Unit.NONE) + unit(MetricUnit.NONE) value(value) passive(passive) metadata("result", result.toString()) @@ -61,7 +55,7 @@ public object MetadataTelemetry { TelemetryService.getInstance().record(connectionSettings) { datum("metadata_hasResult") { createTime(createTime) - unit(Unit.NONE) + unit(MetricUnit.NONE) value(value) passive(passive) metadata("result", result.toString()) @@ -82,7 +76,7 @@ public object MetadataTelemetry { TelemetryService.getInstance().record(metadata) { datum("metadata_hasResult") { createTime(createTime) - unit(Unit.NONE) + unit(MetricUnit.NONE) value(value) passive(passive) metadata("result", result.toString()) diff --git a/telemetry/jetbrains/src/test/resources/resultGeneratesTwoFunctions/output/software/aws/toolkits/telemetry/TelemetryEnums.kt b/telemetry/jetbrains/src/test/resources/resultGeneratesTwoFunctions/output/software/aws/toolkits/telemetry/TelemetryEnums.kt index ff524001..631f241e 100644 --- a/telemetry/jetbrains/src/test/resources/resultGeneratesTwoFunctions/output/software/aws/toolkits/telemetry/TelemetryEnums.kt +++ b/telemetry/jetbrains/src/test/resources/resultGeneratesTwoFunctions/output/software/aws/toolkits/telemetry/TelemetryEnums.kt @@ -5,6 +5,7 @@ package software.aws.toolkits.telemetry +import kotlin.Deprecated import kotlin.String import kotlin.Suppress @@ -29,3 +30,9 @@ public enum class MetricResult( public fun from(type: String): MetricResult = values().firstOrNull { it.value == type } ?: Unknown } } + +@Deprecated( + message = "Name conflicts with the Kotlin standard library", + replaceWith = ReplaceWith("MetricResult", "software.aws.toolkits.telemetry.MetricResult"), +) +public typealias Result = MetricResult From 2b5d519ab56d3c4f7024c84ad2a829b04b5d9fda Mon Sep 17 00:00:00 2001 From: Richard Li Date: Thu, 31 Oct 2024 14:49:51 -0700 Subject: [PATCH 3/6] success method / metadata chaining --- .../generator/OTelTelemetryGenerator.kt | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/telemetry/jetbrains/src/main/kotlin/software/aws/toolkits/telemetry/generator/OTelTelemetryGenerator.kt b/telemetry/jetbrains/src/main/kotlin/software/aws/toolkits/telemetry/generator/OTelTelemetryGenerator.kt index 46f5bd47..e886b4d4 100644 --- a/telemetry/jetbrains/src/main/kotlin/software/aws/toolkits/telemetry/generator/OTelTelemetryGenerator.kt +++ b/telemetry/jetbrains/src/main/kotlin/software/aws/toolkits/telemetry/generator/OTelTelemetryGenerator.kt @@ -13,6 +13,7 @@ import com.squareup.kotlinpoet.NUMBER import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy import com.squareup.kotlinpoet.PropertySpec import com.squareup.kotlinpoet.STRING +import com.squareup.kotlinpoet.TypeName import com.squareup.kotlinpoet.TypeSpec import com.squareup.kotlinpoet.TypeVariableName import java.io.File @@ -110,7 +111,7 @@ object OTelTelemetryGenerator { commonMetadataTypes.forEach { t -> val type = telemetryDefinitions.types.firstOrNull { it.name == t } ?: return@forEach - addFunctions(MetadataSchema(type, false).overloadedFunSpec()) + addFunctions(MetadataSchema(type, false).overloadedFunSpec(TypeVariableName("SpanType"))) } // special case attributes @@ -132,6 +133,12 @@ object OTelTelemetryGenerator { .addStatement("this._%1L = %1N.toDouble()", "value") .build(), ) + addFunction( + FunSpec.builder("success") + .addParameter("success", BOOLEAN) + .addStatement("result(if(success) MetricResult.Succeeded else MetricResult.Failed)") + .build(), + ) } .build() @@ -287,7 +294,7 @@ object OTelTelemetryGenerator { } metricSchema.metadata.filterNot { it.type.name in commonMetadataTypes }.forEach { metadata -> - addFunctions(metadata.overloadedFunSpec()) + addFunctions(metadata.overloadedFunSpec(metricSpanName)) } val requiredAttributes = metricSchema.metadata.filter { it.required != false } @@ -304,7 +311,7 @@ object OTelTelemetryGenerator { addType(metricSpan) } - private fun MetadataSchema.overloadedFunSpec(): List { + private fun MetadataSchema.overloadedFunSpec(returnType: TypeName): List { val types = if (type.allowedValues?.isNotEmpty() == true) { listOf(ClassName(PACKAGE_NAME, type.typeName)) @@ -318,6 +325,7 @@ object OTelTelemetryGenerator { FunSpec.builder(type.name) .addParameter(type.name, t.copy(nullable = nullable)) + .returns(returnType) .apply { val valueParam = if (needsToString) { @@ -330,7 +338,7 @@ object OTelTelemetryGenerator { "%N" } - addStatement("metadata(%S, $valueParam)", type.name, type.name) + addStatement("return metadata(%S, $valueParam)", type.name, type.name) } .addKdoc(type.description) .build() From 6b2f599204dba85e7802c5689e46c729eb76b182 Mon Sep 17 00:00:00 2001 From: Richard Li Date: Fri, 1 Nov 2024 12:23:52 -0700 Subject: [PATCH 4/6] cleanup --- .../generator/OTelTelemetryGenerator.kt | 32 ++++++------------- 1 file changed, 9 insertions(+), 23 deletions(-) diff --git a/telemetry/jetbrains/src/main/kotlin/software/aws/toolkits/telemetry/generator/OTelTelemetryGenerator.kt b/telemetry/jetbrains/src/main/kotlin/software/aws/toolkits/telemetry/generator/OTelTelemetryGenerator.kt index e886b4d4..cc655605 100644 --- a/telemetry/jetbrains/src/main/kotlin/software/aws/toolkits/telemetry/generator/OTelTelemetryGenerator.kt +++ b/telemetry/jetbrains/src/main/kotlin/software/aws/toolkits/telemetry/generator/OTelTelemetryGenerator.kt @@ -33,6 +33,8 @@ object OTelTelemetryGenerator { private val TOOLKIT_DEFAULT_SPAN_BUILDER = ClassName(TOOLKIT_OTEL_PACKAGE, "DefaultSpanBuilder") private val TOOLKIT_OTEL_SERVICE = ClassName(TOOLKIT_OTEL_PACKAGE, "OTelService") + private val SPAN_TYPE_TYPEVAR = TypeVariableName("SpanType") + private val GENERATED_BASE_SPAN = ClassName(PACKAGE_NAME_IMPL, "BaseSpan") private val indent = " ".repeat(4) @@ -49,7 +51,7 @@ object OTelTelemetryGenerator { // "traceId", // "metricId", // "parentId", - // handled as special generator cases + // handled as special cases in base // "passive", // "value", // "unit", @@ -101,42 +103,26 @@ object OTelTelemetryGenerator { .addParameter("delegate", OTEL_SPAN) .build(), ) - .addTypeVariable(TypeVariableName("SpanType", listOf(GENERATED_BASE_SPAN.parameterizedBy(TypeVariableName("SpanType"))))) + .addTypeVariable(SPAN_TYPE_TYPEVAR.copy(bounds = listOf(GENERATED_BASE_SPAN.parameterizedBy(SPAN_TYPE_TYPEVAR)))) .superclass( TOOLKIT_ABSTRACT_BASE_SPAN - .parameterizedBy(TypeVariableName("SpanType")), + .parameterizedBy(SPAN_TYPE_TYPEVAR), ) .addSuperclassConstructorParameter("context, delegate as %T", OTEL_RW_SPAN) .apply { commonMetadataTypes.forEach { t -> val type = telemetryDefinitions.types.firstOrNull { it.name == t } ?: return@forEach - addFunctions(MetadataSchema(type, false).overloadedFunSpec(TypeVariableName("SpanType"))) + addFunctions(MetadataSchema(type, false).overloadedFunSpec(SPAN_TYPE_TYPEVAR)) } - // special case attributes - listOf( - "passive" to BOOLEAN, - "unit" to METRIC_UNIT, - ).forEach { pair -> - addFunction( - FunSpec.builder(pair.first) - .addParameter(pair.first, pair.second) - .addStatement("this._%1L = %1N", pair.first) - .build(), - ) - } - // special-special case - addFunction( - FunSpec.builder("value") - .addParameter("value", NUMBER) - .addStatement("this._%1L = %1N.toDouble()", "value") - .build(), - ) + // special case addFunction( FunSpec.builder("success") .addParameter("success", BOOLEAN) + .returns(SPAN_TYPE_TYPEVAR) .addStatement("result(if(success) MetricResult.Succeeded else MetricResult.Failed)") + .addStatement("return this as %T", SPAN_TYPE_TYPEVAR) .build(), ) } From 27f4ac44b2622f7986efc1b932d6cf08f6d07652 Mon Sep 17 00:00:00 2001 From: Richard Li Date: Fri, 1 Nov 2024 12:51:19 -0700 Subject: [PATCH 5/6] tst --- .../generator/OTelTelemetryGenerator.kt | 1 - .../aws/toolkits/telemetry/impl/BaseSpan.kt | 15 +++--------- .../aws/toolkits/telemetry/impl/BaseSpan.kt | 23 ++++-------------- .../toolkits/telemetry/impl/LambdaTracer.kt | 24 +++++-------------- 4 files changed, 14 insertions(+), 49 deletions(-) diff --git a/telemetry/jetbrains/src/main/kotlin/software/aws/toolkits/telemetry/generator/OTelTelemetryGenerator.kt b/telemetry/jetbrains/src/main/kotlin/software/aws/toolkits/telemetry/generator/OTelTelemetryGenerator.kt index cc655605..29d5de78 100644 --- a/telemetry/jetbrains/src/main/kotlin/software/aws/toolkits/telemetry/generator/OTelTelemetryGenerator.kt +++ b/telemetry/jetbrains/src/main/kotlin/software/aws/toolkits/telemetry/generator/OTelTelemetryGenerator.kt @@ -9,7 +9,6 @@ import com.squareup.kotlinpoet.CodeBlock import com.squareup.kotlinpoet.FileSpec import com.squareup.kotlinpoet.FunSpec import com.squareup.kotlinpoet.KModifier -import com.squareup.kotlinpoet.NUMBER import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy import com.squareup.kotlinpoet.PropertySpec import com.squareup.kotlinpoet.STRING diff --git a/telemetry/jetbrains/src/test/resources/generateOTelBaseSpan/output/software/aws/toolkits/telemetry/impl/BaseSpan.kt b/telemetry/jetbrains/src/test/resources/generateOTelBaseSpan/output/software/aws/toolkits/telemetry/impl/BaseSpan.kt index a1fbacbf..b8970ae2 100644 --- a/telemetry/jetbrains/src/test/resources/generateOTelBaseSpan/output/software/aws/toolkits/telemetry/impl/BaseSpan.kt +++ b/telemetry/jetbrains/src/test/resources/generateOTelBaseSpan/output/software/aws/toolkits/telemetry/impl/BaseSpan.kt @@ -9,24 +9,15 @@ import io.opentelemetry.api.trace.Span import io.opentelemetry.context.Context import io.opentelemetry.sdk.trace.ReadWriteSpan import kotlin.Boolean -import kotlin.Number import kotlin.Suppress -import software.amazon.awssdk.services.toolkittelemetry.model.MetricUnit import software.aws.toolkits.jetbrains.services.telemetry.otel.AbstractBaseSpan public open class BaseSpan>( context: Context?, `delegate`: Span, ) : AbstractBaseSpan(context, delegate as ReadWriteSpan) { - public fun passive(passive: Boolean) { - this._passive = passive - } - - public fun unit(unit: MetricUnit) { - this._unit = unit - } - - public fun `value`(`value`: Number) { - this._value = `value`.toDouble() + public fun success(success: Boolean): SpanType { + result(if(success) MetricResult.Succeeded else MetricResult.Failed) + return this as SpanType } } diff --git a/telemetry/jetbrains/src/test/resources/generatesOTelWithNormalInput/output/software/aws/toolkits/telemetry/impl/BaseSpan.kt b/telemetry/jetbrains/src/test/resources/generatesOTelWithNormalInput/output/software/aws/toolkits/telemetry/impl/BaseSpan.kt index b9a9fdb3..ee92acf6 100644 --- a/telemetry/jetbrains/src/test/resources/generatesOTelWithNormalInput/output/software/aws/toolkits/telemetry/impl/BaseSpan.kt +++ b/telemetry/jetbrains/src/test/resources/generatesOTelWithNormalInput/output/software/aws/toolkits/telemetry/impl/BaseSpan.kt @@ -11,9 +11,7 @@ import io.opentelemetry.sdk.trace.ReadWriteSpan import kotlin.Boolean import kotlin.Double import kotlin.Float -import kotlin.Number import kotlin.Suppress -import software.amazon.awssdk.services.toolkittelemetry.model.MetricUnit import software.aws.toolkits.jetbrains.services.telemetry.otel.AbstractBaseSpan public open class BaseSpan>( @@ -23,26 +21,15 @@ public open class BaseSpan>( /** * The duration of the operation in miliseconds */ - public fun duration(duration: Float?) { - metadata("duration", duration?.let { it.toString() }) - } + public fun duration(duration: Float?): SpanType = metadata("duration", duration?.let { it.toString() }) /** * The duration of the operation in miliseconds */ - public fun duration(duration: Double?) { - metadata("duration", duration?.let { it.toString() }) - } - - public fun passive(passive: Boolean) { - this._passive = passive - } - - public fun unit(unit: MetricUnit) { - this._unit = unit - } + public fun duration(duration: Double?): SpanType = metadata("duration", duration?.let { it.toString() }) - public fun `value`(`value`: Number) { - this._value = `value`.toDouble() + public fun success(success: Boolean): SpanType { + result(if(success) MetricResult.Succeeded else MetricResult.Failed) + return this as SpanType } } diff --git a/telemetry/jetbrains/src/test/resources/generatesOTelWithNormalInput/output/software/aws/toolkits/telemetry/impl/LambdaTracer.kt b/telemetry/jetbrains/src/test/resources/generatesOTelWithNormalInput/output/software/aws/toolkits/telemetry/impl/LambdaTracer.kt index 414a592d..10864cb6 100644 --- a/telemetry/jetbrains/src/test/resources/generatesOTelWithNormalInput/output/software/aws/toolkits/telemetry/impl/LambdaTracer.kt +++ b/telemetry/jetbrains/src/test/resources/generatesOTelWithNormalInput/output/software/aws/toolkits/telemetry/impl/LambdaTracer.kt @@ -35,9 +35,7 @@ public class LambdadeleteSpan internal constructor( /** * a test boolean type */ - public fun booltype(booltype: Boolean) { - metadata("booltype", booltype.toString()) - } + public fun booltype(booltype: Boolean): LambdadeleteSpan = metadata("booltype", booltype.toString()) } public class LambdadeleteSpanBuilder internal constructor( @@ -62,16 +60,12 @@ public class LambdacreateSpan internal constructor( /** * The lambda runtime */ - public fun lambdaRuntime(lambdaRuntime: LambdaRuntime) { - metadata("lambdaRuntime", lambdaRuntime.toString()) - } + public fun lambdaRuntime(lambdaRuntime: LambdaRuntime): LambdacreateSpan = metadata("lambdaRuntime", lambdaRuntime.toString()) /** * untyped string type */ - public fun arbitraryString(arbitraryString: String) { - metadata("arbitraryString", arbitraryString) - } + public fun arbitraryString(arbitraryString: String): LambdacreateSpan = metadata("arbitraryString", arbitraryString) } public class LambdacreateSpanBuilder internal constructor( @@ -96,23 +90,17 @@ public class LambdaremoteinvokeSpan internal constructor( /** * The lambda runtime */ - public fun lambdaRuntime(lambdaRuntime: LambdaRuntime?) { - metadata("lambdaRuntime", lambdaRuntime?.let { it.toString() }) - } + public fun lambdaRuntime(lambdaRuntime: LambdaRuntime?): LambdaremoteinvokeSpan = metadata("lambdaRuntime", lambdaRuntime?.let { it.toString() }) /** * a test int type */ - public fun inttype(inttype: Long) { - metadata("inttype", inttype.toString()) - } + public fun inttype(inttype: Long): LambdaremoteinvokeSpan = metadata("inttype", inttype.toString()) /** * a test int type */ - public fun inttype(inttype: Int) { - metadata("inttype", inttype.toString()) - } + public fun inttype(inttype: Int): LambdaremoteinvokeSpan = metadata("inttype", inttype.toString()) } public class LambdaremoteinvokeSpanBuilder internal constructor( From 0ab78adefb5468ba09bcf4dec6683d9fc90fbfd5 Mon Sep 17 00:00:00 2001 From: Richard Li Date: Mon, 4 Nov 2024 16:53:45 -0800 Subject: [PATCH 6/6] typo --- .../test/resources/generatesOTelWithNormalInput/input.json | 2 +- .../output/software/aws/toolkits/telemetry/impl/BaseSpan.kt | 4 ++-- .../src/test/resources/generatesWithNormalInput/input.json | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/telemetry/jetbrains/src/test/resources/generatesOTelWithNormalInput/input.json b/telemetry/jetbrains/src/test/resources/generatesOTelWithNormalInput/input.json index dc363e9c..48d9b365 100644 --- a/telemetry/jetbrains/src/test/resources/generatesOTelWithNormalInput/input.json +++ b/telemetry/jetbrains/src/test/resources/generatesOTelWithNormalInput/input.json @@ -12,7 +12,7 @@ { "name": "duration", "type": "double", - "description": "The duration of the operation in miliseconds" + "description": "The duration of the operation in milliseconds" }, { "name": "inttype", diff --git a/telemetry/jetbrains/src/test/resources/generatesOTelWithNormalInput/output/software/aws/toolkits/telemetry/impl/BaseSpan.kt b/telemetry/jetbrains/src/test/resources/generatesOTelWithNormalInput/output/software/aws/toolkits/telemetry/impl/BaseSpan.kt index ee92acf6..3bb2e2bd 100644 --- a/telemetry/jetbrains/src/test/resources/generatesOTelWithNormalInput/output/software/aws/toolkits/telemetry/impl/BaseSpan.kt +++ b/telemetry/jetbrains/src/test/resources/generatesOTelWithNormalInput/output/software/aws/toolkits/telemetry/impl/BaseSpan.kt @@ -19,12 +19,12 @@ public open class BaseSpan>( `delegate`: Span, ) : AbstractBaseSpan(context, delegate as ReadWriteSpan) { /** - * The duration of the operation in miliseconds + * The duration of the operation in milliseconds */ public fun duration(duration: Float?): SpanType = metadata("duration", duration?.let { it.toString() }) /** - * The duration of the operation in miliseconds + * The duration of the operation in milliseconds */ public fun duration(duration: Double?): SpanType = metadata("duration", duration?.let { it.toString() }) diff --git a/telemetry/jetbrains/src/test/resources/generatesWithNormalInput/input.json b/telemetry/jetbrains/src/test/resources/generatesWithNormalInput/input.json index dc363e9c..48d9b365 100644 --- a/telemetry/jetbrains/src/test/resources/generatesWithNormalInput/input.json +++ b/telemetry/jetbrains/src/test/resources/generatesWithNormalInput/input.json @@ -12,7 +12,7 @@ { "name": "duration", "type": "double", - "description": "The duration of the operation in miliseconds" + "description": "The duration of the operation in milliseconds" }, { "name": "inttype",