Skip to content

Commit

Permalink
[3.x] Guard against NPE during early invocation of Span.current() (#8256
Browse files Browse the repository at this point in the history
)

* 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
tjquinno authored Jan 18, 2024
1 parent 9cae35d commit 75f8f1b
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2022 Oracle and/or its affiliates.
* Copyright (c) 2019, 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.
Expand Down Expand Up @@ -58,18 +58,32 @@ private TracerProviderHelper() {
}

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

static Tracer global() {
if (TRACER_PROVIDER == null) {
throw new IllegalStateException("Use before initialization has completed");
}
return TRACER_PROVIDER.global();
}

static void global(Tracer tracer) {
if (TRACER_PROVIDER == null) {
throw new IllegalStateException("Use before initialization has completed");
}
TRACER_PROVIDER.global(tracer);
}

static TracerBuilder<?> findTracerBuilder() {
if (TRACER_PROVIDER == null) {
throw new IllegalStateException("Use before initialization has completed");
}
return TRACER_PROVIDER.createBuilder();
}
}
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());
}
}
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;
}
}
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

0 comments on commit 75f8f1b

Please sign in to comment.