diff --git a/build.gradle b/build.gradle index 4b83aa5acc..0d357bab94 100644 --- a/build.gradle +++ b/build.gradle @@ -19,6 +19,7 @@ buildscript { classpath 'me.champeau.gradle:japicmp-gradle-plugin:0.4.0' classpath 'de.undercouch:gradle-download-task:5.0.2' classpath 'io.spring.javaformat:spring-javaformat-gradle-plugin:0.0.34' + classpath 'com.diffplug.spotless:spotless-plugin-gradle:6.8.0' constraints { classpath('org.ow2.asm:asm:7.3.1') { @@ -48,6 +49,7 @@ allprojects { subprojects { apply plugin: 'signing' apply plugin: 'io.spring.javaformat' + apply plugin: 'com.diffplug.spotless' if (project.name != 'micrometer-bom') { if (project.name.contains('samples') || project.name.contains('benchmarks')) { @@ -184,6 +186,12 @@ subprojects { exclude '**/*.json' // comments not supported } + spotless { + kotlin { + ktlint().editorConfigOverride([disabled_rules: "no-wildcard-imports"]) + } + } + // Publish resolved versions. plugins.withId('maven-publish') { sourceCompatibility = JavaVersion.VERSION_1_8 diff --git a/buildscript-gradle.lockfile b/buildscript-gradle.lockfile index b2c942a319..4faef1af71 100644 --- a/buildscript-gradle.lockfile +++ b/buildscript-gradle.lockfile @@ -4,6 +4,12 @@ antlr:antlr:2.7.7=classpath ch.qos.logback:logback-classic:1.2.3=classpath ch.qos.logback:logback-core:1.2.3=classpath +com.diffplug.durian:durian-collect:1.2.0=classpath +com.diffplug.durian:durian-core:1.2.0=classpath +com.diffplug.durian:durian-io:1.2.0=classpath +com.diffplug.spotless:spotless-lib-extra:2.27.0=classpath +com.diffplug.spotless:spotless-lib:2.27.0=classpath +com.diffplug.spotless:spotless-plugin-gradle:6.8.0=classpath com.fasterxml.jackson:jackson-bom:2.11.0=classpath com.github.siom79.japicmp:japicmp:0.15.7=classpath com.github.zafarkhaja:java-semver:0.9.0=classpath @@ -13,6 +19,7 @@ com.google.guava:failureaccess:1.0.1=classpath com.google.guava:guava:29.0-jre=classpath com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava=classpath com.google.j2objc:j2objc-annotations:1.3=classpath +com.googlecode.concurrent-trees:concurrent-trees:2.6.1=classpath com.googlecode.javaewah:JavaEWAH:1.1.12=classpath com.jcraft:jsch.agentproxy.connector-factory:0.0.7=classpath com.jcraft:jsch.agentproxy.core:0.0.7=classpath @@ -70,6 +77,8 @@ org.apache.commons:commons-pool2:2.2=classpath org.apache.maven:maven-settings-builder:3.0.4=classpath org.apache.maven:maven-settings:3.0.4=classpath org.checkerframework:checker-qual:2.11.1=classpath +org.codehaus.groovy:groovy-xml:3.0.9=classpath +org.codehaus.groovy:groovy:3.0.9=classpath org.codehaus.plexus:plexus-component-annotations:1.5.5=classpath org.codehaus.plexus:plexus-interpolation:1.14=classpath org.codehaus.plexus:plexus-utils:2.0.6=classpath diff --git a/micrometer-core/src/main/kotlin/io/micrometer/core/instrument/kotlin/asContextElement.kt b/micrometer-core/src/main/kotlin/io/micrometer/core/instrument/kotlin/AsContextElement.kt similarity index 83% rename from micrometer-core/src/main/kotlin/io/micrometer/core/instrument/kotlin/asContextElement.kt rename to micrometer-core/src/main/kotlin/io/micrometer/core/instrument/kotlin/AsContextElement.kt index 65b263f65b..683e0e916e 100644 --- a/micrometer-core/src/main/kotlin/io/micrometer/core/instrument/kotlin/asContextElement.kt +++ b/micrometer-core/src/main/kotlin/io/micrometer/core/instrument/kotlin/AsContextElement.kt @@ -28,7 +28,7 @@ import kotlin.coroutines.CoroutineContext * @since 1.10.0 */ fun ObservationRegistry.asContextElement(): CoroutineContext { - return KotlinObservationContextElement(this, ContextRegistry.getInstance()) + return KotlinObservationContextElement(this, ContextRegistry.getInstance()) } /** @@ -38,9 +38,9 @@ fun ObservationRegistry.asContextElement(): CoroutineContext { * @since 1.10.0 */ fun CoroutineContext.currentObservation(): Observation? { - val element = get(KotlinObservationContextElement.KEY) - if (element is KotlinObservationContextElement) { - return element.currentObservation - } - return null + val element = get(KotlinObservationContextElement.KEY) + if (element is KotlinObservationContextElement) { + return element.currentObservation + } + return null } diff --git a/micrometer-core/src/test/kotlin/io/micrometer/core/instrument/kotlin/AsContextElementKtTests.kt b/micrometer-core/src/test/kotlin/io/micrometer/core/instrument/kotlin/AsContextElementKtTests.kt index 9b9be30d16..74eb4ed1bd 100644 --- a/micrometer-core/src/test/kotlin/io/micrometer/core/instrument/kotlin/AsContextElementKtTests.kt +++ b/micrometer-core/src/test/kotlin/io/micrometer/core/instrument/kotlin/AsContextElementKtTests.kt @@ -25,41 +25,40 @@ import org.junit.jupiter.api.Test internal class AsContextElementKtTests { - @Test - fun `should return current observation from context`(): Unit = runBlocking { - val observationRegistry = ObservationRegistry.create() - observationRegistry.observationConfig().observationHandler { true } - val nextObservation = Observation.start("name", observationRegistry) - val inScope = nextObservation.openScope() - var observationInGlobalScopeLaunch: Observation? = null - var observationInGlobalScopeAsync: Observation? = null - val asContextElement = observationRegistry.asContextElement() + @Test + fun `should return current observation from context`(): Unit = runBlocking { + val observationRegistry = ObservationRegistry.create() + observationRegistry.observationConfig().observationHandler { true } + val nextObservation = Observation.start("name", observationRegistry) + val inScope = nextObservation.openScope() + var observationInGlobalScopeLaunch: Observation? = null + var observationInGlobalScopeAsync: Observation? = null + val asContextElement = observationRegistry.asContextElement() - @OptIn(DelicateCoroutinesApi::class) - GlobalScope.launch(asContextElement) { - observationInGlobalScopeLaunch = coroutineContext.currentObservation() - } - @OptIn(DelicateCoroutinesApi::class) - GlobalScope.async(asContextElement) { - observationInGlobalScopeAsync = coroutineContext.currentObservation() - }.await() + @OptIn(DelicateCoroutinesApi::class) + GlobalScope.launch(asContextElement) { + observationInGlobalScopeLaunch = coroutineContext.currentObservation() + } + @OptIn(DelicateCoroutinesApi::class) + GlobalScope.async(asContextElement) { + observationInGlobalScopeAsync = coroutineContext.currentObservation() + }.await() - inScope.close(); + inScope.close() - then(observationInGlobalScopeLaunch).isSameAs(nextObservation) - then(observationInGlobalScopeAsync).isSameAs(nextObservation) - } + then(observationInGlobalScopeLaunch).isSameAs(nextObservation) + then(observationInGlobalScopeAsync).isSameAs(nextObservation) + } - @Test - fun `should return observation from coroutine context when KotlinContextElement present`(): Unit = runBlocking { - val observationRegistry = ObservationRegistry.create() - observationRegistry.observationConfig().observationHandler { true } - val nextObservation = Observation.start("name", observationRegistry) - val inScope = nextObservation.openScope() - val element = KotlinObservationContextElement(observationRegistry, ContextRegistry.getInstance()) - - then(element.currentObservation()).isSameAs(nextObservation) - inScope.close() - } + @Test + fun `should return observation from coroutine context when KotlinContextElement present`(): Unit = runBlocking { + val observationRegistry = ObservationRegistry.create() + observationRegistry.observationConfig().observationHandler { true } + val nextObservation = Observation.start("name", observationRegistry) + val inScope = nextObservation.openScope() + val element = KotlinObservationContextElement(observationRegistry, ContextRegistry.getInstance()) + then(element.currentObservation()).isSameAs(nextObservation) + inScope.close() + } }