Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweak otelbase classes slightly to meet requirements from generator #5027

Merged
merged 27 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ mockitoKotlin = "5.4.0"
mockk = "1.13.10"
nimbus-jose-jwt = "9.40"
node-gradle = "7.0.2"
telemetryGenerator = "1.0.272"
telemetryGenerator = "1.0.278"
testLogger = "4.0.0"
testRetry = "1.5.10"
# test-only; platform provides slf4j transitively at runtime
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
package software.aws.toolkits.core.telemetry

import software.amazon.awssdk.services.toolkittelemetry.model.AWSProduct
import software.amazon.awssdk.services.toolkittelemetry.model.MetricUnit
import software.aws.toolkits.core.telemetry.MetricEvent.Companion.illegalCharsRegex
import software.aws.toolkits.core.utils.getLogger
import software.aws.toolkits.core.utils.warn
import java.time.Instant
import software.amazon.awssdk.services.toolkittelemetry.model.Unit as MetricUnit

interface MetricEvent {
val createTime: Instant
Expand Down
6 changes: 3 additions & 3 deletions plugins/core/jetbrains-community/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ idea {
}

val generateTelemetry = tasks.register<GenerateTelemetry>("generateTelemetry") {
inputFiles = listOf(file("${project.projectDir}/resources/telemetryOverride.json"))
outputDirectory = generatedSrcDir.get().asFile
inputFiles.setFrom(file("${project.projectDir}/resources/telemetryOverride.json"))
outputDirectory.set(generatedSrcDir)

doFirst {
outputDirectory.deleteRecursively()
outputDirectory.get().asFile.deleteRecursively()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.intellij.openapi.project.Project
import migration.software.aws.toolkits.jetbrains.services.telemetry.TelemetryService
import software.amazon.awssdk.services.ssooidc.model.SsoOidcException
import software.amazon.awssdk.services.toolkittelemetry.model.MetricUnit
import software.aws.toolkits.core.ClientConnectionSettings
import software.aws.toolkits.core.ConnectionSettings
import software.aws.toolkits.core.TokenConnectionSettings
Expand All @@ -27,10 +28,10 @@
import software.aws.toolkits.jetbrains.core.credentials.sso.bearer.InteractiveBearerTokenProvider
import software.aws.toolkits.jetbrains.utils.runUnderProgressIfNeeded
import software.aws.toolkits.resources.AwsCoreBundle
import software.aws.toolkits.telemetry.AuthTelemetry

Check warning on line 31 in plugins/core/jetbrains-community/src/software/aws/toolkits/jetbrains/core/credentials/ToolkitAuthManager.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Usage of redundant or deprecated syntax or deprecated symbols

'AuthTelemetry' is deprecated. Use type-safe metric builders

Check warning on line 31 in plugins/core/jetbrains-community/src/software/aws/toolkits/jetbrains/core/credentials/ToolkitAuthManager.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Usage of redundant or deprecated syntax or deprecated symbols

Remove deprecated symbol import
import software.aws.toolkits.telemetry.CredentialSourceId
import software.aws.toolkits.telemetry.CredentialType
import software.aws.toolkits.telemetry.Result

Check warning on line 34 in plugins/core/jetbrains-community/src/software/aws/toolkits/jetbrains/core/credentials/ToolkitAuthManager.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Usage of redundant or deprecated syntax or deprecated symbols

'typealias Result = MetricResult' is deprecated. Name conflicts with the Kotlin standard library

Check warning on line 34 in plugins/core/jetbrains-community/src/software/aws/toolkits/jetbrains/core/credentials/ToolkitAuthManager.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Usage of redundant or deprecated syntax or deprecated symbols

Remove deprecated symbol import
import java.time.Instant

sealed interface ToolkitConnection {
Expand Down Expand Up @@ -262,19 +263,19 @@
credentialStartUrl = startUrl,
credentialSourceId = getCredentialIdForTelemetry(connection),
isReAuth = true,
result = Result.Succeeded,

Check warning on line 266 in plugins/core/jetbrains-community/src/software/aws/toolkits/jetbrains/core/credentials/ToolkitAuthManager.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Usage of redundant or deprecated syntax or deprecated symbols

'typealias Result = MetricResult' is deprecated. Name conflicts with the Kotlin standard library
source = source,
)
recordAddConnection(
credentialSourceId = getCredentialIdForTelemetry(connection),
isReAuth = true,
result = Result.Succeeded,

Check warning on line 272 in plugins/core/jetbrains-community/src/software/aws/toolkits/jetbrains/core/credentials/ToolkitAuthManager.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Usage of redundant or deprecated syntax or deprecated symbols

'typealias Result = MetricResult' is deprecated. Name conflicts with the Kotlin standard library
source = source,
)
}
} catch (e: Exception) {
if (isReAuth) {
val result = if (e is ProcessCanceledException) Result.Cancelled else Result.Failed

Check warning on line 278 in plugins/core/jetbrains-community/src/software/aws/toolkits/jetbrains/core/credentials/ToolkitAuthManager.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Usage of redundant or deprecated syntax or deprecated symbols

'typealias Result = MetricResult' is deprecated. Name conflicts with the Kotlin standard library

Check warning on line 278 in plugins/core/jetbrains-community/src/software/aws/toolkits/jetbrains/core/credentials/ToolkitAuthManager.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Usage of redundant or deprecated syntax or deprecated symbols

'typealias Result = MetricResult' is deprecated. Name conflicts with the Kotlin standard library
recordLoginWithBrowser(
credentialStartUrl = startUrl,
credentialSourceId = getCredentialIdForTelemetry(connection),
Expand Down Expand Up @@ -326,7 +327,7 @@
return@runUnderProgressIfNeeded false
}
} catch (e: SsoOidcException) {
AuthTelemetry.sourceOfRefresh(authRefreshSource = reauthSource.toString())

Check warning on line 330 in plugins/core/jetbrains-community/src/software/aws/toolkits/jetbrains/core/credentials/ToolkitAuthManager.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Usage of redundant or deprecated syntax or deprecated symbols

'AuthTelemetry' is deprecated. Use type-safe metric builders
getLogger<ToolkitAuthManager>().warn(e) { "Redriving bearer token login flow since token could not be refreshed" }
onReauthRequired(e)
return true
Expand Down Expand Up @@ -368,13 +369,13 @@
credentialSourceId: CredentialSourceId? = null,
reason: String? = null,
isReAuth: Boolean,
result: Result,

Check warning on line 372 in plugins/core/jetbrains-community/src/software/aws/toolkits/jetbrains/core/credentials/ToolkitAuthManager.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Usage of redundant or deprecated syntax or deprecated symbols

'typealias Result = MetricResult' is deprecated. Name conflicts with the Kotlin standard library
source: String? = null,
) {
TelemetryService.getInstance().record(null as Project?) {
datum("aws_loginWithBrowser") {
createTime(Instant.now())
unit(software.amazon.awssdk.services.toolkittelemetry.model.Unit.NONE)
unit(MetricUnit.NONE)
value(1.0)
passive(false)
credentialSourceId?.let { metadata("credentialSourceId", it.toString()) }
Expand All @@ -392,13 +393,13 @@
credentialSourceId: CredentialSourceId? = null,
reason: String? = null,
isReAuth: Boolean,
result: Result,

Check warning on line 396 in plugins/core/jetbrains-community/src/software/aws/toolkits/jetbrains/core/credentials/ToolkitAuthManager.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Usage of redundant or deprecated syntax or deprecated symbols

'typealias Result = MetricResult' is deprecated. Name conflicts with the Kotlin standard library
source: String? = null,
) {
TelemetryService.getInstance().record(null as Project?) {
datum("auth_addConnection") {
createTime(Instant.now())
unit(software.amazon.awssdk.services.toolkittelemetry.model.Unit.NONE)
unit(MetricUnit.NONE)
value(1.0)
passive(false)
credentialSourceId?.let { metadata("credentialSourceId", it.toString()) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
import io.opentelemetry.sdk.trace.ReadWriteSpan
import kotlinx.coroutines.CoroutineScope
import software.amazon.awssdk.services.toolkittelemetry.model.AWSProduct
import software.amazon.awssdk.services.toolkittelemetry.model.MetricUnit
import software.aws.toolkits.core.utils.error
import software.aws.toolkits.core.utils.getLogger
import software.aws.toolkits.core.utils.warn
import software.aws.toolkits.jetbrains.isDeveloperMode
import software.aws.toolkits.jetbrains.services.telemetry.PluginResolver
import software.aws.toolkits.telemetry.impl.BaseSpan
import java.time.Instant
import java.util.concurrent.TimeUnit
import kotlin.coroutines.CoroutineContext
Expand All @@ -39,12 +41,6 @@
override fun doStartSpan() = DefaultSpan(parent, delegate.startSpan())
}

// temporary; will be generated
abstract class BaseSpan<SpanType : AbstractBaseSpan<SpanType>>(
context: Context?,
delegate: Span,
) : AbstractBaseSpan<SpanType>(context, delegate as ReadWriteSpan)

abstract class AbstractSpanBuilder<
BuilderType : AbstractSpanBuilder<BuilderType, SpanType>,
SpanType : AbstractBaseSpan<SpanType>,
Expand Down Expand Up @@ -195,6 +191,9 @@

abstract class AbstractBaseSpan<SpanType : AbstractBaseSpan<SpanType>>(internal val context: Context?, private val delegate: ReadWriteSpan) : Span by delegate {
protected open val requiredFields: Collection<String> = emptySet()
private var passive: Boolean = false
private var unit: MetricUnit = MetricUnit.NONE
private var value: Double = 0.0

/**
* Same as [com.intellij.platform.diagnostic.telemetry.helpers.use] except downcasts to specific subclass of [BaseSpan]
Expand All @@ -221,6 +220,21 @@
delegate.end()
}

fun passive(passive: Boolean): SpanType {
this.passive = passive
return this as SpanType
}

fun unit(unit: MetricUnit): SpanType {
this.unit = unit
return this as SpanType

Check warning on line 230 in plugins/core/jetbrains-community/src/software/aws/toolkits/jetbrains/services/telemetry/otel/OtelBase.kt

View check run for this annotation

Codecov / codecov/patch

plugins/core/jetbrains-community/src/software/aws/toolkits/jetbrains/services/telemetry/otel/OtelBase.kt#L229-L230

Added lines #L229 - L230 were not covered by tests
}

fun value(value: Number): SpanType {
this.value = value.toDouble()
return this as SpanType

Check warning on line 235 in plugins/core/jetbrains-community/src/software/aws/toolkits/jetbrains/services/telemetry/otel/OtelBase.kt

View check run for this annotation

Codecov / codecov/patch

plugins/core/jetbrains-community/src/software/aws/toolkits/jetbrains/services/telemetry/otel/OtelBase.kt#L234-L235

Added lines #L234 - L235 were not covered by tests
}

private fun validateRequiredAttributes() {
val missingFields = requiredFields.filter { delegate.getAttribute(AttributeKey.stringKey(it)) == null }
val message = { "${delegate.name} is missing required fields: ${missingFields.joinToString(", ")}" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package software.aws.toolkits.jetbrains.services.telemetry.otel

import com.intellij.openapi.application.ApplicationManager
import com.intellij.testFramework.ApplicationExtension
import io.opentelemetry.api.trace.Span
import io.opentelemetry.api.trace.TraceId
import io.opentelemetry.context.Context
import io.opentelemetry.extension.kotlin.asContextElement
Expand All @@ -17,25 +18,46 @@ import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.runTest
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertDoesNotThrow
import org.junit.jupiter.api.assertThrows
import org.junit.jupiter.api.extension.AfterAllCallback
import org.junit.jupiter.api.extension.AfterEachCallback
import org.junit.jupiter.api.extension.ExtendWith
import org.junit.jupiter.api.extension.ExtensionContext
import org.junit.jupiter.api.extension.RegisterExtension
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.Arguments
import org.junit.jupiter.params.provider.MethodSource
import software.amazon.awssdk.services.toolkittelemetry.model.AWSProduct
import software.aws.toolkits.core.utils.getLogger
import software.aws.toolkits.core.utils.warn
import software.aws.toolkits.jetbrains.core.coroutines.getCoroutineBgContext
import software.aws.toolkits.jetbrains.utils.pluginAwareExecuteOnPooledThread
import software.aws.toolkits.jetbrains.utils.satisfiesKt
import software.aws.toolkits.jetbrains.utils.spinUntil
import software.aws.toolkits.telemetry.MetricResult
import software.aws.toolkits.telemetry.Telemetry
import java.time.Instant
import java.util.concurrent.TimeUnit
import java.util.stream.Stream

@ExtendWith(ApplicationExtension::class)
class OtelBaseTest {
private companion object {
@RegisterExtension
val otelExtension = OtelExtension()

private fun spanEndArgs() = Stream.of(
Arguments.of("end()", { span: Span -> span.end() }),
Arguments.of("end(long, TimeUnit)", { span: Span -> span.end(1, TimeUnit.SECONDS) }),
Arguments.of("end(Instant)", { span: Span -> span.end(Instant.now()) }),
)

@JvmStatic
fun `AbstractBaseSpan#end() throws if attributes are missing`() = spanEndArgs()

@JvmStatic
fun `AbstractBaseSpan#end() does not throw if all required attributes are present`() = spanEndArgs()
}

@Test
Expand Down Expand Up @@ -268,6 +290,22 @@ class OtelBaseTest {
}
}

@ParameterizedTest(name = "{0}")
@MethodSource
fun `AbstractBaseSpan#end() throws if attributes are missing`(ignored: String, block: Span.() -> Unit) {
val span = Telemetry.aws.openUrl.startSpan()
val e = assertThrows<Exception> { block(span) }
assertThat(e.message).contains("aws_openUrl is missing required fields: result")
}

@ParameterizedTest(name = "{0}")
@MethodSource
fun `AbstractBaseSpan#end() does not throw if all required attributes are present`(ignored: String, block: Span.() -> Unit) {
val span = Telemetry.aws.openUrl.startSpan()
span.result(MetricResult.Succeeded)
assertDoesNotThrow { block(span) }
}

private fun spanBuilder(tracer: String, spanName: String) = DefaultSpanBuilder(otelExtension.sdk.sdk.getTracer(tracer).spanBuilder(spanName))
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"renameShapes": {
// "Unit" conflicts with Kotlin stdlib [kotlin.Unit]
"Unit" : "MetricUnit"
}
}
Loading