Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.x] Guard against NPE during early invocation of Span.current() #8256

Merged
merged 3 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading