-
Notifications
You must be signed in to change notification settings - Fork 566
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix unwrap methods to return cast delegates rather than cast 'this' (#…
…8298) * Fix unwrap methods to return cast delegates rather than cast 'this' Signed-off-by: Tim Quinn <tim.quinn@oracle.com> * Use a JUnit extension to set-up and shutdown OTel for tests * Review comments: add explicit type checking and exception throwing in unwrap implementations instead of simply delegating to Class.cast * Enhance unwrap to handle package-local and public implementations * Enhance unwrap for spans and no-op tracer also * Restore default implementations of unwrap --------- Signed-off-by: Tim Quinn <tim.quinn@oracle.com>
- Loading branch information
Showing
14 changed files
with
241 additions
and
29 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
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
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
2 changes: 1 addition & 1 deletion
2
...ry/src/main/java/io/helidon/tracing/providers/opentelemetry/OpenTelemetrySpanContext.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
45 changes: 45 additions & 0 deletions
45
...try/src/test/java/io/helidon/tracing/providers/opentelemetry/OtelTestsJunitExtension.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,45 @@ | ||
/* | ||
* 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.providers.opentelemetry; | ||
|
||
import org.junit.jupiter.api.extension.AfterAllCallback; | ||
import org.junit.jupiter.api.extension.BeforeAllCallback; | ||
import org.junit.jupiter.api.extension.Extension; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
|
||
public class OtelTestsJunitExtension implements Extension, BeforeAllCallback, AfterAllCallback { | ||
|
||
private static final String OTEL_AUTO_CONFIGURE_PROP = "otel.java.global-autoconfigure.enabled"; | ||
private static final String OTEL_SDK_DISABLED_PROP = "otel.sdk.disabled"; | ||
private String originalOtelSdkAutoConfiguredSetting; | ||
private String originalOtelSdkDisabledSetting; | ||
|
||
@Override | ||
public void afterAll(ExtensionContext extensionContext) throws Exception { | ||
if (originalOtelSdkAutoConfiguredSetting != null) { | ||
System.setProperty(OTEL_AUTO_CONFIGURE_PROP, originalOtelSdkAutoConfiguredSetting); | ||
} | ||
if (originalOtelSdkDisabledSetting != null) { | ||
System.setProperty(OTEL_SDK_DISABLED_PROP, originalOtelSdkDisabledSetting); | ||
} | ||
} | ||
|
||
@Override | ||
public void beforeAll(ExtensionContext extensionContext) throws Exception { | ||
originalOtelSdkAutoConfiguredSetting = System.setProperty(OTEL_AUTO_CONFIGURE_PROP, "true"); | ||
originalOtelSdkDisabledSetting = System.setProperty(OTEL_SDK_DISABLED_PROP, "false"); | ||
} | ||
} |
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
51 changes: 51 additions & 0 deletions
51
...rs/opentelemetry/src/test/java/io/helidon/tracing/providers/opentelemetry/TestUnwrap.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,51 @@ | ||
/* | ||
* 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.providers.opentelemetry; | ||
|
||
|
||
import io.opentelemetry.api.trace.Span; | ||
import io.opentelemetry.api.trace.SpanBuilder; | ||
import io.opentelemetry.api.trace.Tracer; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.instanceOf; | ||
|
||
class TestUnwrap { | ||
|
||
@Test | ||
void testTracer() { | ||
var tracer = io.helidon.tracing.Tracer.global(); | ||
assertThat("Tracer unwrapped", | ||
tracer.unwrap(Tracer.class), | ||
instanceOf(Tracer.class)); | ||
} | ||
|
||
@Test | ||
void testSpanAndSpanBuilder() { | ||
var tracer = io.helidon.tracing.Tracer.global(); | ||
var spanBuilder = tracer.spanBuilder("test1"); | ||
assertThat("Span builder unwrapped", | ||
spanBuilder.unwrap(SpanBuilder.class), | ||
instanceOf(SpanBuilder.class)); | ||
|
||
var span = spanBuilder.start(); | ||
assertThat("Span unwrapped", | ||
span.unwrap(Span.class), | ||
instanceOf(Span.class)); | ||
|
||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...ntelemetry/src/test/resources/META-INF/services/org.junit.jupiter.api.extension.Extension
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.providers.opentelemetry.OtelTestsJunitExtension |
2 changes: 1 addition & 1 deletion
2
...pentracing/src/main/java/io/helidon/tracing/providers/opentracing/OpenTracingContext.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
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
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
49 changes: 49 additions & 0 deletions
49
...viders/opentracing/src/test/java/io/helidon/tracing/providers/opentracing/TestUnwrap.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,49 @@ | ||
/* | ||
* 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.providers.opentracing; | ||
|
||
import io.opentracing.Span; | ||
import io.opentracing.Tracer; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.instanceOf; | ||
|
||
class TestUnwrap { | ||
|
||
@Test | ||
void testTracer() { | ||
var tracer = io.helidon.tracing.Tracer.global(); | ||
assertThat("Tracer unwrapped", | ||
tracer.unwrap(Tracer.class), | ||
instanceOf(Tracer.class)); | ||
} | ||
|
||
@Test | ||
void testSpanAndSpanBuilder() { | ||
var tracer = io.helidon.tracing.Tracer.global(); | ||
var spanBuilder = tracer.spanBuilder("test1"); | ||
assertThat("Span builder unwrapped", | ||
spanBuilder.unwrap(Tracer.SpanBuilder.class), | ||
instanceOf(Tracer.SpanBuilder.class)); | ||
|
||
var span = spanBuilder.start(); | ||
assertThat("Span unwrapped", | ||
span.unwrap(Span.class), | ||
instanceOf(Span.class)); | ||
|
||
} | ||
} |
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
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