Skip to content

@Scheduled task instrumentation does not work for Kotlin suspend functions #32165

Closed as not planned
@pwestlin

Description

@pwestlin

Spring docs for Scheduled tasks instrumentation states:

An Observation is created for each execution of an @Scheduled task.

This function get an automatic observation:

@Scheduled(fixedDelay = 5, timeUnit = TimeUnit.SECONDS)
fun nonSuspendable() {
    logger.info("Not suspendable")
}

but this suspend function does not:

@Scheduled(fixedDelay = 5, timeUnit = TimeUnit.SECONDS)
suspend fun suspendable() {
    logger.info("Suspendable")
}

I use Spring Boot 3.2.2 and I've also tried 3.2.3-SNAPSHOT and 3.3.0-M1.

build.gradle.kts (shortened):

plugins {
    id("org.springframework.boot") version "3.2.2"
    id("io.spring.dependency-management") version "1.1.4"
    kotlin("jvm") version "1.9.22"
    kotlin("plugin.spring") version "1.9.22"
}

java {
    sourceCompatibility = JavaVersion.VERSION_21
}

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-webflux")
    implementation("org.springframework.boot:spring-boot-starter-actuator")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("io.projectreactor.kotlin:reactor-kotlin-extensions")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor")

    implementation("io.micrometer:micrometer-tracing")
    implementation("io.micrometer:micrometer-tracing-bridge-brave")

    testImplementation("org.springframework.boot:spring-boot-starter-test")
    testImplementation("io.projectreactor:reactor-test")
}

Application code:

@SpringBootApplication
@EnableScheduling
class Application

fun main(args: Array<String>) {
    Hooks.enableAutomaticContextPropagation()
    runApplication<Application>(*args)
}

@Service
class SchedulingService {

    private val logger: Logger = LoggerFactory.getLogger(this.javaClass)

    @Scheduled(fixedDelay = 5, timeUnit = TimeUnit.SECONDS)
    fun nonSuspendable() {
        logger.info("Not suspendable")
    }

    @Scheduled(fixedDelay = 5, timeUnit = TimeUnit.SECONDS)
    suspend fun suspendable() {
        logger.info("Suspendable")
    }
}

When I run the application I get the following output:

2024-01-31T07:33:26.925+01:00  INFO [65b9e9b65a4ecd1702feecf2dbdd6be4,02feecf2dbdd6be4] 926648 --- [   scheduling-1] [65b9e9b65a4ecd1702feecf2dbdd6be4-02feecf2dbdd6be4] n.w.s.SchedulingService                  : Not suspendable
2024-01-31T07:33:26.946+01:00  INFO [,] 926648 --- [   scheduling-1] [                                                 ] n.w.s.SchedulingService                  : Suspendable

There we can se that "65b9e9b65a4ecd1702feecf2dbdd6be4,02feecf2dbdd6be4" means that the function "nonSuspendable" gets an observation but "suspendable" doesn't.

Regards Peter

Metadata

Metadata

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)status: supersededAn issue that has been superseded by anothertheme: observabilityAn issue related to observability and tracing

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions