Skip to content

Commit b78bc01

Browse files
authored
[4.x] Guard against NPE during early invocation of Span.current() (#8257)
* Guard against NPE during early invocation of Span.current() Signed-off-by: Tim Quinn <[email protected]> * Add null checking to other static methods --------- Signed-off-by: Tim Quinn <[email protected]>
1 parent 368d147 commit b78bc01

File tree

4 files changed

+103
-2
lines changed

4 files changed

+103
-2
lines changed

tracing/tracing/src/main/java/io/helidon/tracing/TracerProviderHelper.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2022 Oracle and/or its affiliates.
2+
* Copyright (c) 2019, 2024 Oracle and/or its affiliates.
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.
@@ -58,18 +58,32 @@ private TracerProviderHelper() {
5858
}
5959

6060
public static Optional<Span> currentSpan() {
61-
return TRACER_PROVIDER.currentSpan();
61+
/*
62+
If a custom TracerProvider implementation indirectly tries to access the current span (for example, by triggering custom
63+
logging that adds the current span to each message), then this method can be invoked before the static initializer has
64+
completed and, therefore, before TRACER_PROVIDER is assigned.
65+
*/
66+
return (TRACER_PROVIDER == null) ? Optional.empty() : TRACER_PROVIDER.currentSpan();
6267
}
6368

6469
static Tracer global() {
70+
if (TRACER_PROVIDER == null) {
71+
throw new IllegalStateException("Use before initialization has completed");
72+
}
6573
return TRACER_PROVIDER.global();
6674
}
6775

6876
static void global(Tracer tracer) {
77+
if (TRACER_PROVIDER == null) {
78+
throw new IllegalStateException("Use before initialization has completed");
79+
}
6980
TRACER_PROVIDER.global(tracer);
7081
}
7182

7283
static TracerBuilder<?> findTracerBuilder() {
84+
if (TRACER_PROVIDER == null) {
85+
throw new IllegalStateException("Use before initialization has completed");
86+
}
7387
return TRACER_PROVIDER.createBuilder();
7488
}
7589
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2024 Oracle and/or its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.helidon.tracing;
17+
18+
import org.junit.jupiter.api.Test;
19+
20+
import static org.hamcrest.MatcherAssert.assertThat;
21+
import static org.hamcrest.Matchers.notNullValue;
22+
class TestEarlyUseOfSpanCurrent {
23+
24+
@Test
25+
void ensureCurrentSpanNonNull() {
26+
27+
Span.current(); // Trigger the service loading of the test provider which will itself invoke Span.current().
28+
assertThat("Early current span", TestTracerProvider.earlyCurrentSpan(), notNullValue());
29+
}
30+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (c) 2024 Oracle and/or its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.helidon.tracing;
17+
18+
import java.util.Optional;
19+
20+
/**
21+
* Approximates the behavior of a custom tracer provider with a constructor that invokes a logger which invokes Span.current().
22+
*/
23+
public class TestTracerProvider extends NoOpTracerProvider {
24+
25+
private static Optional<Span> earlyCurrentSpan;
26+
27+
public TestTracerProvider() {
28+
Optional<Span> currentSpan;
29+
try {
30+
currentSpan = Span.current();
31+
} catch (NullPointerException e) {
32+
// silent
33+
currentSpan = null;
34+
}
35+
earlyCurrentSpan = currentSpan;
36+
}
37+
38+
static Optional<Span> earlyCurrentSpan() {
39+
return earlyCurrentSpan;
40+
}
41+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#
2+
# Copyright (c) 2024 Oracle and/or its affiliates.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
io.helidon.tracing.TestTracerProvider

0 commit comments

Comments
 (0)