-
Notifications
You must be signed in to change notification settings - Fork 564
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[3.x] Guard against NPE during early invocation of Span.current() (#8256
) * Guard against NPE during early invocation of Span.current() * Add null checking to other static methods --------- Signed-off-by: Tim Quinn <[email protected]>
- Loading branch information
Showing
4 changed files
with
103 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
tracing/tracing/src/test/java/io/helidon/tracing/TestEarlyUseOfSpanCurrent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright (c) 2024 Oracle and/or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.helidon.tracing; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.notNullValue; | ||
class TestEarlyUseOfSpanCurrent { | ||
|
||
@Test | ||
void ensureCurrentSpanNonNull() { | ||
|
||
Span.current(); // Trigger the service loading of the test provider which will itself invoke Span.current(). | ||
assertThat("Early current span", TestTracerProvider.earlyCurrentSpan(), notNullValue()); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
tracing/tracing/src/test/java/io/helidon/tracing/TestTracerProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright (c) 2024 Oracle and/or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.helidon.tracing; | ||
|
||
import java.util.Optional; | ||
|
||
/** | ||
* Approximates the behavior of a custom tracer provider with a constructor that invokes a logger which invokes Span.current(). | ||
*/ | ||
public class TestTracerProvider extends NoOpTracerProvider { | ||
|
||
private static Optional<Span> earlyCurrentSpan; | ||
|
||
public TestTracerProvider() { | ||
Optional<Span> currentSpan; | ||
try { | ||
currentSpan = Span.current(); | ||
} catch (NullPointerException e) { | ||
// silent | ||
currentSpan = null; | ||
} | ||
earlyCurrentSpan = currentSpan; | ||
} | ||
|
||
static Optional<Span> earlyCurrentSpan() { | ||
return earlyCurrentSpan; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
tracing/tracing/src/test/resources/META-INF/services/io.helidon.tracing.spi.TracerProvider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# | ||
# Copyright (c) 2024 Oracle and/or its affiliates. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
io.helidon.tracing.TestTracerProvider |