Skip to content

Commit 593d2cc

Browse files
committed
Polish "Add support for virtual threads in OtlpMetricRegistry configuration"
See gh-42407
1 parent e615eb3 commit 593d2cc

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/otlp/OtlpMetricsExportAutoConfiguration.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -76,15 +76,15 @@ OtlpConfig otlpConfig(OpenTelemetryProperties openTelemetryProperties,
7676
@Bean
7777
@ConditionalOnMissingBean
7878
@ConditionalOnThreading(Threading.PLATFORM)
79-
public OtlpMeterRegistry otlpMeterRegistryPlatformThreads(OtlpConfig otlpConfig, Clock clock) {
79+
public OtlpMeterRegistry otlpMeterRegistry(OtlpConfig otlpConfig, Clock clock) {
8080
return new OtlpMeterRegistry(otlpConfig, clock);
8181
}
8282

8383
@Bean
8484
@ConditionalOnMissingBean
8585
@ConditionalOnThreading(Threading.VIRTUAL)
8686
public OtlpMeterRegistry otlpMeterRegistryVirtualThreads(OtlpConfig otlpConfig, Clock clock) {
87-
VirtualThreadTaskExecutor taskExecutor = new VirtualThreadTaskExecutor("otlp-meter-registry");
87+
VirtualThreadTaskExecutor taskExecutor = new VirtualThreadTaskExecutor("otlp-meter-registry-");
8888
return new OtlpMeterRegistry(otlpConfig, clock, taskExecutor.getVirtualThreadFactory());
8989
}
9090

spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/assertj/ScheduledExecutorServiceAssert.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.testsupport.assertj;
1818

19+
import java.lang.reflect.Method;
1920
import java.util.concurrent.ExecutionException;
2021
import java.util.concurrent.ScheduledExecutorService;
2122
import java.util.concurrent.ScheduledThreadPoolExecutor;
@@ -24,6 +25,8 @@
2425
import org.assertj.core.api.AbstractAssert;
2526
import org.assertj.core.api.Assert;
2627

28+
import org.springframework.util.ReflectionUtils;
29+
2730
/**
2831
* AssertJ {@link Assert} for {@link ScheduledThreadPoolExecutor}.
2932
*
@@ -66,12 +69,11 @@ public ScheduledExecutorServiceAssert usesVirtualThreads() {
6669
private boolean producesVirtualThreads() {
6770
try {
6871
return this.actual.schedule(() -> {
69-
// https://openjdk.org/jeps/444
70-
// jep 444 specifies that virtual threads will belong to
71-
// a special thread group given the name "VirtualThreads"
72-
ThreadGroup threadGroup = Thread.currentThread().getThreadGroup();
73-
String threadGroupName = (threadGroup != null) ? threadGroup.getName() : "";
74-
return threadGroupName.equalsIgnoreCase("VirtualThreads");
72+
Method isVirtual = ReflectionUtils.findMethod(Thread.class, "isVirtual");
73+
if (isVirtual == null) {
74+
return false;
75+
}
76+
return (boolean) ReflectionUtils.invokeMethod(isVirtual, Thread.currentThread());
7577
}, 0, TimeUnit.SECONDS).get();
7678
}
7779
catch (InterruptedException | ExecutionException ex) {

0 commit comments

Comments
 (0)