Skip to content

Commit 77ccfa9

Browse files
authored
Merge branch 'main' into remove-more-deprecated-props
2 parents 038f63c + 85b39ac commit 77ccfa9

32 files changed

Lines changed: 157 additions & 105 deletions

File tree

.github/agents/knowledge/gradle-conventions.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,24 @@ should be active in the test agent.
102102
- **Sibling cross-version references are required**: every javaagent module in a version
103103
family must list all other sibling `:javaagent` modules via `testInstrumentation`.
104104

105+
### Exception: modules already bundled into `agent-for-testing`
106+
107+
A small set of javaagent modules are bundled directly into the main agent via
108+
`baseJavaagentLibs(...)` in `javaagent/build.gradle.kts`, and therefore into
109+
`agent-for-testing` as well. For these, the sibling cross-version rule does **not** apply:
110+
they are already loaded in every test JVM, so adding them via `testInstrumentation`
111+
from a sibling's `build.gradle.kts` is redundant and should be rejected in review.
112+
113+
In particular, do not add `testInstrumentation(project(":instrumentation:opentelemetry-api:opentelemetry-api-1.N:javaagent"))`
114+
entries to sibling `opentelemetry-api-*` modules — all `opentelemetry-api-1.*:javaagent`
115+
versions are already bundled in the test agent. The same applies to the other modules listed
116+
under `baseJavaagentLibs` in `javaagent/build.gradle.kts` (e.g. `executors`,
117+
`opentelemetry-instrumentation-annotations-1.16`, and the `internal/*` modules).
118+
119+
Before adding a `testInstrumentation` for a sibling, check
120+
[`javaagent/build.gradle.kts`](../../../javaagent/build.gradle.kts): if the sibling appears
121+
in a `baseJavaagentLibs(...)` line, omit the `testInstrumentation` entry.
122+
105123
### How to check for missing siblings (step by step)
106124

107125
When reviewing a `javaagent/` module:

.github/agents/knowledge/testing-general-patterns.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
`throws` clause. That noisy try/catch inside the lambda is worse than leaving
3636
`throws Exception` (or `throws IOException`) on the `@Test` method. Only introduce such
3737
wrapping when the lambda already needs its own error handling for behavioral reasons.
38+
- Do **not** choose `CompletableFuture.runAsync(...)` over the simpler
39+
`executor.submit(runnable).get()` just to avoid the checked exceptions thrown by `Future.get()`.
3840

3941
## Test Resource Cleanup
4042

instrumentation-docs/build.gradle.kts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,21 @@ dependencies {
1818
}
1919

2020
tasks {
21+
test {
22+
// DeclarativeConfigValidationTest walks ../instrumentation for metadata.yaml files,
23+
// so changes to those files must invalidate this task's build cache entry.
24+
// Eagerly resolve to a concrete file list (rather than passing a FileTree rooted at
25+
// the instrumentation directory) to avoid Gradle's implicit-dependency validation
26+
// flagging overlap with sibling subprojects' build/ output directories.
27+
val metadataYamlFiles = fileTree(rootDir.resolve("instrumentation")) {
28+
include("**/metadata.yaml")
29+
exclude("**/build/**")
30+
}.files
31+
inputs.files(metadataYamlFiles)
32+
.withPropertyName("instrumentationMetadataYamlFiles")
33+
.withPathSensitivity(PathSensitivity.RELATIVE)
34+
}
35+
2136
val runAnalysis by registering(JavaExec::class) {
2237
dependsOn(classes)
2338

instrumentation-docs/src/main/java/io/opentelemetry/instrumentation/docs/utils/FileManager.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@ private static InstrumentationPath parseInstrumentationPath(String filePath) {
3838
return null;
3939
}
4040

41+
String normalized = normalizeSeparators(filePath);
4142
String instrumentationSegment = "/instrumentation/";
42-
int startIndex = filePath.indexOf(instrumentationSegment) + instrumentationSegment.length();
43-
String[] parts = filePath.substring(startIndex).split("/");
43+
int startIndex = normalized.indexOf(instrumentationSegment) + instrumentationSegment.length();
44+
String[] parts = normalized.substring(startIndex).split("/");
4445

4546
if (parts.length < 2) {
4647
return null;
@@ -58,23 +59,28 @@ public static boolean isValidInstrumentationPath(String filePath) {
5859
if (filePath == null || filePath.isEmpty()) {
5960
return false;
6061
}
62+
String normalized = normalizeSeparators(filePath);
6163
String instrumentationSegment = "instrumentation/";
6264

63-
if (!filePath.contains(instrumentationSegment)) {
65+
if (!normalized.contains(instrumentationSegment)) {
6466
return false;
6567
}
6668

67-
int javaagentCount = filePath.split("/javaagent", -1).length - 1;
69+
int javaagentCount = normalized.split("/javaagent", -1).length - 1;
6870
if (javaagentCount > 1) {
6971
return false;
7072
}
7173

72-
if (filePath.matches(
74+
if (normalized.matches(
7375
".*(/test/|/testing|/build/|-common/|-common-|common-|/compile-stub/|-testing|bootstrap/src).*")) {
7476
return false;
7577
}
7678

77-
return filePath.endsWith("javaagent") || filePath.endsWith("library");
79+
return normalized.endsWith("javaagent") || normalized.endsWith("library");
80+
}
81+
82+
private static String normalizeSeparators(String filePath) {
83+
return filePath.replace('\\', '/');
7884
}
7985

8086
public List<String> findBuildGradleFiles(String instrumentationDirectory) {
@@ -85,7 +91,7 @@ public List<String> findBuildGradleFiles(String instrumentationDirectory) {
8591
.filter(
8692
path ->
8793
path.getFileName().toString().equals("build.gradle.kts")
88-
&& !path.toString().contains("/testing/")
94+
&& !normalizeSeparators(path.toString()).contains("/testing/")
8995
&& !isInNestedInstrumentationModule(path, rootPath))
9096
.map(Path::toString)
9197
.collect(toList());
@@ -105,13 +111,11 @@ public List<String> findBuildGradleFiles(String instrumentationDirectory) {
105111
*/
106112
private static boolean isInNestedInstrumentationModule(Path filePath, Path rootPath) {
107113
Path relativePath = rootPath.relativize(filePath);
108-
String relativeStr = relativePath.toString();
109-
110-
String[] segments = relativeStr.split("/");
111114

112115
// Find the first javaagent or library segment
113-
for (int i = 0; i < segments.length; i++) {
114-
if (segments[i].equals("javaagent") || segments[i].equals("library")) {
116+
for (int i = 0; i < relativePath.getNameCount(); i++) {
117+
String segment = relativePath.getName(i).toString();
118+
if (segment.equals("javaagent") || segment.equals("library")) {
115119
// If javaagent/library is not the first segment, it's a nested module
116120
return i > 0;
117121
}

instrumentation/opensearch/opensearch-rest-1.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/opensearch/rest/v1_0/OpenSearchRestSingletons.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import io.opentelemetry.javaagent.instrumentation.opensearch.rest.OpenSearchRestRequest;
1111
import io.opentelemetry.javaagent.instrumentation.opensearch.rest.OpenSearchRestResponse;
1212
import java.net.InetAddress;
13+
import javax.annotation.Nullable;
1314
import org.opensearch.client.Response;
1415

1516
public class OpenSearchRestSingletons {
@@ -21,7 +22,11 @@ public static Instrumenter<OpenSearchRestRequest, OpenSearchRestResponse> instru
2122
return instrumenter;
2223
}
2324

24-
public static OpenSearchRestResponse convertResponse(Response response) {
25+
@Nullable
26+
public static OpenSearchRestResponse convertResponse(@Nullable Response response) {
27+
if (response == null) {
28+
return null;
29+
}
2530
return new OpenSearchRestResponse() {
2631

2732
@Override
@@ -30,6 +35,7 @@ public int getStatusCode() {
3035
}
3136

3237
@Override
38+
@Nullable
3339
public InetAddress getAddress() {
3440
return response.getHost().getAddress();
3541
}

instrumentation/opensearch/opensearch-rest-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/opensearch/rest/v3_0/RestClientInstrumentation.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ public void endWithListener(@Nullable Throwable throwable) {
103103
public static class PerformRequestAdvice {
104104

105105
@Advice.OnMethodEnter(suppress = Throwable.class, inline = false)
106+
@Nullable
106107
public static AdviceScope onEnter(@Advice.Argument(0) Request request) {
107108
return AdviceScope.start(request);
108109
}

instrumentation/opentelemetry-api/opentelemetry-api-1.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/opentelemetryapi/ContextBridgeTest.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import io.opentelemetry.instrumentation.annotations.WithSpan;
2222
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
2323
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
24-
import java.util.concurrent.CountDownLatch;
24+
import java.util.concurrent.ExecutorService;
2525
import java.util.concurrent.Executors;
2626
import java.util.concurrent.atomic.AtomicReference;
2727
import org.junit.jupiter.api.DisplayName;
@@ -43,9 +43,12 @@ void agentPropagatesApplicationsContext() throws Exception {
4343
Context context = Context.current().with(ANIMAL, "cat");
4444
AtomicReference<String> captured = new AtomicReference<>();
4545
try (Scope ignored = context.makeCurrent()) {
46-
Executors.newSingleThreadExecutor()
47-
.submit(() -> captured.set(Context.current().get(ANIMAL)))
48-
.get();
46+
ExecutorService executor = Executors.newSingleThreadExecutor();
47+
try {
48+
executor.submit(() -> captured.set(Context.current().get(ANIMAL))).get();
49+
} finally {
50+
executor.shutdownNow();
51+
}
4952
}
5053

5154
// Then
@@ -97,12 +100,12 @@ void agentPropagatesApplicationsSpan() throws Exception {
97100

98101
Span testSpan = tracer.spanBuilder("test").startSpan();
99102
try (Scope ignored = testSpan.makeCurrent()) {
100-
Executors.newSingleThreadExecutor()
101-
.submit(
102-
() -> {
103-
Span.current().setAttribute("cat", "yes");
104-
})
105-
.get();
103+
ExecutorService executor = Executors.newSingleThreadExecutor();
104+
try {
105+
executor.submit(() -> Span.current().setAttribute("cat", "yes")).get();
106+
} finally {
107+
executor.shutdownNow();
108+
}
106109
}
107110
testSpan.end();
108111

@@ -158,19 +161,16 @@ void agentPropagatesApplicationsBaggage() throws Exception {
158161
// When
159162
Baggage testBaggage = Baggage.builder().put("cat", "yes").build();
160163
AtomicReference<Baggage> ref = new AtomicReference<>();
161-
CountDownLatch latch = new CountDownLatch(1);
162164
try (Scope ignored = testBaggage.makeCurrent()) {
163-
Executors.newSingleThreadExecutor()
164-
.submit(
165-
() -> {
166-
ref.set(Baggage.current());
167-
latch.countDown();
168-
})
169-
.get();
165+
ExecutorService executor = Executors.newSingleThreadExecutor();
166+
try {
167+
executor.submit(() -> ref.set(Baggage.current())).get();
168+
} finally {
169+
executor.shutdownNow();
170+
}
170171
}
171172

172173
// Then
173-
latch.await();
174174
assertThat(ref.get().size()).isEqualTo(1);
175175
assertThat(ref.get().getEntryValue("cat")).isEqualTo("yes");
176176
}

instrumentation/opentelemetry-api/opentelemetry-api-1.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/opentelemetryapi/TracerTest.java

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,11 @@ void captureSpanWithImplicitParentUsingTracerWithSpan() {
7575
// When
7676
Tracer tracer = GlobalOpenTelemetry.getTracer("test");
7777
Span parentSpan = tracer.spanBuilder("parent").startSpan();
78-
Scope parentScope = Context.current().with(parentSpan).makeCurrent();
79-
80-
Span testSpan = tracer.spanBuilder("test").startSpan();
81-
testSpan.end();
82-
78+
try (Scope parentScope = Context.current().with(parentSpan).makeCurrent()) {
79+
Span testSpan = tracer.spanBuilder("test").startSpan();
80+
testSpan.end();
81+
}
8382
parentSpan.end();
84-
parentScope.close();
8583

8684
// Then
8785
testing.waitAndAssertTraces(
@@ -98,13 +96,11 @@ void captureSpanWithImplicitParentUsingMakeCurrent() {
9896
// When
9997
Tracer tracer = GlobalOpenTelemetry.getTracer("test");
10098
Span parentSpan = tracer.spanBuilder("parent").startSpan();
101-
Scope parentScope = parentSpan.makeCurrent();
102-
103-
Span testSpan = tracer.spanBuilder("test").startSpan();
104-
testSpan.end();
105-
99+
try (Scope parentScope = parentSpan.makeCurrent()) {
100+
Span testSpan = tracer.spanBuilder("test").startSpan();
101+
testSpan.end();
102+
}
106103
parentSpan.end();
107-
parentScope.close();
108104

109105
// Then
110106
testing.waitAndAssertTraces(
@@ -123,13 +119,11 @@ void captureSpanWithImplicitParentUsingTracingContextUtilsWithSpanAndMakeCurrent
123119
Tracer tracer = GlobalOpenTelemetry.getTracer("test");
124120
Span parentSpan = tracer.spanBuilder("parent").startSpan();
125121
Context parentContext = Context.current().with(parentSpan);
126-
Scope parentScope = parentContext.makeCurrent();
127-
128-
Span testSpan = tracer.spanBuilder("test").startSpan();
129-
testSpan.end();
130-
122+
try (Scope parentScope = parentContext.makeCurrent()) {
123+
Span testSpan = tracer.spanBuilder("test").startSpan();
124+
testSpan.end();
125+
}
131126
parentSpan.end();
132-
parentScope.close();
133127

134128
// Then
135129
testing.waitAndAssertTraces(
@@ -166,11 +160,11 @@ void captureSpanWithExplicitNoParent() {
166160
// When
167161
Tracer tracer = GlobalOpenTelemetry.getTracer("test");
168162
Span parentSpan = tracer.spanBuilder("parent").startSpan();
169-
Scope parentScope = parentSpan.makeCurrent();
170-
Span testSpan = tracer.spanBuilder("test").setNoParent().startSpan();
171-
testSpan.end();
163+
try (Scope parentScope = parentSpan.makeCurrent()) {
164+
Span testSpan = tracer.spanBuilder("test").setNoParent().startSpan();
165+
testSpan.end();
166+
}
172167
parentSpan.end();
173-
parentScope.close();
174168

175169
// Then
176170
testing.waitAndAssertSortedTraces(
@@ -255,9 +249,9 @@ void captureNameUpdateUsingTracingContextUtilsGetCurrentSpan() {
255249
// When
256250
Tracer tracer = GlobalOpenTelemetry.getTracer("test");
257251
Span testSpan = tracer.spanBuilder("test").startSpan();
258-
Scope testScope = Context.current().with(testSpan).makeCurrent();
259-
Span.current().updateName("test2");
260-
testScope.close();
252+
try (Scope testScope = Context.current().with(testSpan).makeCurrent()) {
253+
Span.current().updateName("test2");
254+
}
261255
testSpan.end();
262256

263257
// Then
@@ -273,9 +267,9 @@ void captureNameUpdateUsingTracingContextUtilsSpanFromContextCurrent() {
273267
// When
274268
Tracer tracer = GlobalOpenTelemetry.getTracer("test");
275269
Span testSpan = tracer.spanBuilder("test").startSpan();
276-
Scope testScope = Context.current().with(testSpan).makeCurrent();
277-
Span.fromContext(Context.current()).updateName("test2");
278-
testScope.close();
270+
try (Scope testScope = Context.current().with(testSpan).makeCurrent()) {
271+
Span.fromContext(Context.current()).updateName("test2");
272+
}
279273
testSpan.end();
280274

281275
// Then

instrumentation/opentelemetry-api/opentelemetry-api-1.10/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/opentelemetryapi/v1_10/OpenTelemetryInstrumentation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public void transform(TypeTransformer transformer) {
2828

2929
@SuppressWarnings({"ReturnValueIgnored", "unused"})
3030
public static class InitAdvice {
31-
@Advice.OnMethodEnter(suppress = Throwable.class, inline = false)
31+
@Advice.OnMethodEnter(inline = false)
3232
public static void init() {
3333
// the sole purpose of this advice is to ensure that ApplicationOpenTelemetry110 is recognized
3434
// as helper class and injected into class loader

instrumentation/opentelemetry-api/opentelemetry-api-1.10/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/opentelemetryapi/v1_10/metrics/CallbackGcCloseTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundE
159159
resolveClass(c);
160160
}
161161
return c;
162-
} catch (ClassNotFoundException e) {
162+
} catch (ClassNotFoundException ignored) {
163163
// fall through to parent
164164
}
165165
}

0 commit comments

Comments
 (0)