Skip to content

Commit 96ccfee

Browse files
otelbot[bot]trask
andauthored
Code review sweep (run 25083201560) (#18392)
Co-authored-by: otelbot <197425009+otelbot@users.noreply.github.com> Co-authored-by: Trask Stalnaker <trask.stalnaker@gmail.com>
1 parent e092a2e commit 96ccfee

10 files changed

Lines changed: 65 additions & 64 deletions

File tree

instrumentation/spring/spring-boot-actuator-autoconfigure-2.0/javaagent/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ muzzle {
88
group.set("org.springframework.boot")
99
module.set("spring-boot-actuator-autoconfigure")
1010
versions.set("[2.0.0.RELEASE,)")
11-
extraDependency("io.micrometer:micrometer-core:1.5.0")
1211
assertInverse.set(true)
12+
extraDependency("io.micrometer:micrometer-core:1.5.0")
1313
}
1414
}
1515

instrumentation/spring/spring-boot-resources/javaagent/src/main/java/io/opentelemetry/instrumentation/spring/resources/SpringBootServiceNameDetector.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,19 @@
3737
* strategy wins:
3838
*
3939
* <ul>
40-
* <li>Check for the SPRING_APPLICATION_NAME environment variable
40+
* <li>Check for --spring.application.name program argument (not jvm arg) via ProcessHandle
41+
* <li>Check for --spring.application.name program argument via sun.java.command system property
4142
* <li>Check for spring.application.name system property
42-
* <li>Check for application.properties file on the classpath
43+
* <li>Check for the SPRING_APPLICATION_NAME environment variable
4344
* <li>Check for application.properties in the current working dir
44-
* <li>Check for application.yml on the classpath
4545
* <li>Check for application.yml in the current working dir
46-
* <li>Check for --spring.application.name program argument (not jvm arg) via ProcessHandle
47-
* <li>Check for --spring.application.name program argument via sun.java.command system property
46+
* <li>Check for application.yaml in the current working dir
47+
* <li>Check for application.properties file on the classpath
48+
* <li>Check for application.yml on the classpath
49+
* <li>Check for application.yaml on the classpath
50+
* <li>Check for bootstrap.properties file on the classpath
51+
* <li>Check for bootstrap.yml on the classpath
52+
* <li>Check for bootstrap.yaml on the classpath
4853
* </ul>
4954
*
5055
* <p>Note: The spring starter already includes provider in
@@ -57,7 +62,7 @@ public class SpringBootServiceNameDetector implements ConditionalResourceProvide
5762
Logger.getLogger(SpringBootServiceNameDetector.class.getName());
5863
private static final String COMMANDLINE_ARG_PREFIX = "--spring.application.name=";
5964
private static final Pattern COMMANDLINE_PATTERN =
60-
Pattern.compile("--spring\\.application\\.name=([a-zA-Z.\\-_]+)");
65+
Pattern.compile("--spring\\.application\\.name=(\\S+)");
6166
private final SystemHelper system;
6267

6368
@SuppressWarnings("unused")

instrumentation/spring/spring-boot-resources/javaagent/src/test/java/io/opentelemetry/instrumentation/spring/resources/SpringBootServiceNameDetectorTest.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import static io.opentelemetry.semconv.ServiceAttributes.SERVICE_NAME;
99
import static java.nio.charset.StandardCharsets.UTF_8;
1010
import static java.util.Collections.singletonMap;
11+
import static java.util.Objects.requireNonNull;
1112
import static org.assertj.core.api.Assertions.assertThat;
1213
import static org.mockito.Mockito.lenient;
1314
import static org.mockito.Mockito.when;
@@ -18,7 +19,6 @@
1819
import java.io.IOException;
1920
import java.io.InputStream;
2021
import java.io.OutputStream;
21-
import java.net.URL;
2222
import java.nio.file.Files;
2323
import java.nio.file.Path;
2424
import java.nio.file.Paths;
@@ -132,12 +132,12 @@ void classpathApplicationYamlContainingMultipleYamlDefinitions(String fileName)
132132

133133
@ParameterizedTest
134134
@ValueSource(strings = {"application.yaml", APPLICATION_YML})
135-
void yamlFileInCurrentDir(String fileName) throws Exception {
135+
void yamlFileInCurrentDir(String fileName) throws IOException {
136136
Path yamlPath = Paths.get(fileName);
137137
try {
138-
URL url = getClass().getClassLoader().getResource(APPLICATION_YML);
139-
String content = readString(Paths.get(url.toURI()));
140-
writeString(yamlPath, content);
138+
try (InputStream in = requireNonNull(openClasspathResource(APPLICATION_YML))) {
139+
Files.copy(in, yamlPath);
140+
}
141141
when(system.openFile(fileName)).thenCallRealMethod();
142142
SpringBootServiceNameDetector guesser = new SpringBootServiceNameDetector(system);
143143
Resource result = guesser.createResource(config);
@@ -165,10 +165,11 @@ void getFromCommandlineArgsWithProcessHandle() throws ReflectiveOperationExcepti
165165
@Test
166166
void getFromCommandlineArgsWithSystemProperty() {
167167
when(system.getProperty("sun.java.command"))
168-
.thenReturn("/bin/java sweet-spring.jar --spring.application.name=bullpen --quiet=never");
168+
.thenReturn(
169+
"/bin/java sweet-spring.jar --spring.application.name=bullpen-v2 --quiet=never");
169170
SpringBootServiceNameDetector guesser = new SpringBootServiceNameDetector(system);
170171
Resource result = guesser.createResource(config);
171-
expectServiceName(result, "bullpen");
172+
expectServiceName(result, "bullpen-v2");
172173
}
173174

174175
@Test
@@ -210,11 +211,6 @@ private static void writeString(Path path, String value) throws IOException {
210211
}
211212
}
212213

213-
private static String readString(Path path) throws Exception {
214-
byte[] allBytes = Files.readAllBytes(path);
215-
return new String(allBytes, UTF_8);
216-
}
217-
218214
private InputStream openClasspathResource(String resource) {
219215
return getClass().getClassLoader().getResourceAsStream(resource);
220216
}

instrumentation/spring/spring-cloud-gateway/spring-cloud-gateway-2.0/javaagent/build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ muzzle {
1616
pass {
1717
group.set("org.springframework.cloud")
1818
module.set("spring-cloud-starter-gateway-server-webflux")
19-
versions.set("[4.3.0,]")
19+
versions.set("[4.3.0,)")
2020
assertInverse.set(true)
2121
}
2222
}
@@ -39,7 +39,7 @@ dependencies {
3939
latestDepTestLibrary("org.springframework.boot:spring-boot-starter-test:2.1.+") // see spring-cloud-gateway-2.2:testing module
4040
}
4141

42-
tasks.withType<Test>().configureEach {
42+
tasks.test {
4343
jvmArgs("-Dotel.instrumentation.spring-cloud-gateway.experimental-span-attributes=true")
4444

4545
// required on jdk17

instrumentation/spring/spring-cloud-gateway/spring-cloud-gateway-2.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/gateway/v2_0/GatewayTestApplication.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
import org.springframework.context.annotation.Bean;
1414

1515
@SpringBootApplication
16-
public class GatewayTestApplication {
16+
class GatewayTestApplication {
1717

1818
@Bean
19-
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
19+
RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
2020
// A simple echo gateway.
2121
return builder
2222
.routes()

instrumentation/spring/spring-cloud-gateway/spring-cloud-gateway-webflux-4.3/testing/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ dependencies {
1818
testLibrary("org.springframework.boot:spring-boot-webflux")
1919
}
2020

21-
tasks.withType<Test>().configureEach {
21+
tasks.test {
2222
jvmArgs("-Dotel.instrumentation.spring-cloud-gateway.experimental-span-attributes=true")
2323
jvmArgs("-Dotel.instrumentation.common.experimental.controller-telemetry.enabled=true")
2424
}

instrumentation/spring/spring-cloud-gateway/spring-cloud-gateway-webmvc-4.3/javaagent/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ dependencies {
2626
testLibrary("org.springframework.boot:spring-boot-starter-test:4.0.0")
2727
}
2828

29-
tasks.withType<Test>().configureEach {
29+
tasks.test {
3030
jvmArgs("-Dotel.instrumentation.spring-cloud-gateway.experimental-span-attributes=true")
3131
jvmArgs("-Dotel.instrumentation.common.experimental.controller-telemetry.enabled=true")
3232
}

instrumentation/spring/spring-data/spring-data-1.8/javaagent/build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ muzzle {
88
pass {
99
group.set("org.springframework.data")
1010
module.set("spring-data-commons")
11-
versions.set("[1.8.0.RELEASE,]")
11+
versions.set("[1.8.0.RELEASE,)")
1212
extraDependency("org.springframework:spring-aop:1.2")
1313
assertInverse.set(true)
1414
}
1515
pass {
1616
group.set("org.springframework")
1717
module.set("spring-aop")
18-
versions.set("[1.2,]")
18+
versions.set("[1.2,)")
1919
extraDependency("org.springframework.data:spring-data-commons:1.8.0.RELEASE")
2020
assertInverse.set(true)
2121
}

instrumentation/spring/spring-data/spring-data-3.0/kotlin-testing/src/test/kotlin/io/opentelemetry/javaagent/instrumentation/spring/data/v3_0/KotlinSpringDataTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class KotlinSpringDataTest {
2424
companion object {
2525
@JvmStatic
2626
@RegisterExtension
27-
val testing = AgentInstrumentationExtension.create()
27+
private val testing = AgentInstrumentationExtension.create()
2828
}
2929

3030
private lateinit var applicationContext: ConfigurableApplicationContext

instrumentation/spring/spring-integration-4.1/library/src/main/java/io/opentelemetry/instrumentation/spring/integration/v4_1/TracingChannelInterceptor.java

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,42 @@ final class TracingChannelInterceptor implements ExecutorChannelInterceptor {
3333
private static final ThreadLocal<Map<MessageChannel, ContextAndScope>> localContextAndScope =
3434
ThreadLocal.withInitial(IdentityHashMap::new);
3535

36+
@Nullable
37+
private static final Class<?> directWithAttributesChannelClass =
38+
getDirectWithAttributesChannelClass();
39+
40+
@Nullable
41+
private static final MethodHandle channelGetAttributeMh =
42+
getChannelAttributeMh(directWithAttributesChannelClass);
43+
44+
@Nullable
45+
private static Class<?> getDirectWithAttributesChannelClass() {
46+
try {
47+
return Class.forName(
48+
"org.springframework.cloud.stream.messaging.DirectWithAttributesChannel");
49+
} catch (ClassNotFoundException ignored) {
50+
return null;
51+
}
52+
}
53+
54+
@Nullable
55+
private static MethodHandle getChannelAttributeMh(
56+
@Nullable Class<?> directWithAttributesChannelClass) {
57+
if (directWithAttributesChannelClass == null) {
58+
return null;
59+
}
60+
61+
try {
62+
return MethodHandles.lookup()
63+
.findVirtual(
64+
directWithAttributesChannelClass,
65+
"getAttribute",
66+
MethodType.methodType(Object.class, String.class));
67+
} catch (NoSuchMethodException | IllegalAccessException ignored) {
68+
return null;
69+
}
70+
}
71+
3672
private final ContextPropagators propagators;
3773
private final Instrumenter<MessageWithChannel, Void> consumerInstrumenter;
3874
private final Instrumenter<MessageWithChannel, Void> producerInstrumenter;
@@ -203,42 +239,6 @@ private static Message<?> createMessageWithHeaders(
203239
.build();
204240
}
205241

206-
@Nullable
207-
private static final Class<?> directWithAttributesChannelClass =
208-
getDirectWithAttributesChannelClass();
209-
210-
@Nullable
211-
private static final MethodHandle channelGetAttributeMh =
212-
getChannelAttributeMh(directWithAttributesChannelClass);
213-
214-
@Nullable
215-
private static Class<?> getDirectWithAttributesChannelClass() {
216-
try {
217-
return Class.forName(
218-
"org.springframework.cloud.stream.messaging.DirectWithAttributesChannel");
219-
} catch (ClassNotFoundException ignored) {
220-
return null;
221-
}
222-
}
223-
224-
@Nullable
225-
private static MethodHandle getChannelAttributeMh(
226-
@Nullable Class<?> directWithAttributesChannelClass) {
227-
if (directWithAttributesChannelClass == null) {
228-
return null;
229-
}
230-
231-
try {
232-
return MethodHandles.lookup()
233-
.findVirtual(
234-
directWithAttributesChannelClass,
235-
"getAttribute",
236-
MethodType.methodType(Object.class, String.class));
237-
} catch (NoSuchMethodException | IllegalAccessException ignored) {
238-
return null;
239-
}
240-
}
241-
242242
private boolean createProducerSpan(MessageChannel messageChannel) {
243243
if (!producerSpanEnabled || directWithAttributesChannelClass == null) {
244244
return false;

0 commit comments

Comments
 (0)