Remove Java9VersionSpecific clock implementation (#7221)

This commit is contained in:
jack-berg 2025-04-07 16:50:55 -05:00 committed by GitHub
parent aa17528ec1
commit 10eda198c0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 15 additions and 241 deletions

View File

@ -9,26 +9,21 @@ import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnJre;
import org.junit.jupiter.api.condition.EnabledOnJre;
import org.junit.jupiter.api.condition.JRE;
// This test is placed in the all artifact instead of the common one so it uses the dependency jar
// instead of the classes directly, which allows verifying mrjar behavior.
class SystemClockTest {
@EnabledOnJre(JRE.JAVA_8)
@Test
void now_millisPrecision() {
// If we test many times, we can be fairly sure we didn't just get lucky with having a rounded
// result on a higher than expected precision timestamp.
for (int i = 0; i < 100; i++) {
long now = SystemClock.getInstance().now();
assertThat(now % 1000000).isZero();
}
void now() {
assertThat(SystemClock.getInstance().now()).isNotZero();
assertThat(SystemClock.getInstance().now(true)).isNotZero();
assertThat(SystemClock.getInstance().now(false)).isNotZero();
}
@DisabledOnJre(JRE.JAVA_8)
@Test
// On java 8, the APIs used to produce micro precision are available but still only produce millis
// precision
@DisabledOnJre(JRE.JAVA_8)
void now_microsPrecision() {
// If we test many times, we can be fairly sure we get at least one timestamp that isn't
// coincidentally rounded to millis precision.
@ -52,8 +47,10 @@ class SystemClockTest {
}
}
@DisabledOnJre(JRE.JAVA_8)
@Test
// On java 8, the APIs used to produce micro precision are available but still only produce millis
// precision
@DisabledOnJre(JRE.JAVA_8)
void now_highPrecision() {
// If we test many times, we can be fairly sure we get at least one timestamp that isn't
// coincidentally rounded to millis precision.

View File

@ -10,8 +10,6 @@ apply<OtelVersionClassPlugin>()
description = "OpenTelemetry SDK Common"
otelJava.moduleName.set("io.opentelemetry.sdk.common")
val mrJarVersions = listOf(9)
dependencies {
api(project(":api:all"))
@ -23,61 +21,9 @@ dependencies {
testImplementation("com.google.guava:guava-testlib")
}
for (version in mrJarVersions) {
sourceSets {
create("java$version") {
java {
setSrcDirs(listOf("src/main/java$version"))
}
}
}
tasks {
named<JavaCompile>("compileJava${version}Java") {
sourceCompatibility = "$version"
targetCompatibility = "$version"
options.release.set(version)
}
}
configurations {
named("java${version}Implementation") {
extendsFrom(configurations["implementation"])
}
}
dependencies {
// Common to reference classes in main sourceset from Java 9 one (e.g., to return a common interface)
add("java${version}Implementation", files(sourceSets.main.get().output.classesDirs))
}
}
tasks {
withType(Jar::class) {
val sourcePathProvider = if (name.equals("jar")) {
{ ss: SourceSet? -> ss?.output }
} else if (name.equals("sourcesJar")) {
{ ss: SourceSet? -> ss?.java }
} else {
{ _: SourceSet -> project.objects.fileCollection() }
}
for (version in mrJarVersions) {
into("META-INF/versions/$version") {
from(sourcePathProvider(sourceSets["java$version"]))
}
}
manifest.attributes(
"Multi-Release" to "true",
)
}
test {
// For checking version number included in Resource.
systemProperty("otel.test.project-version", project.version.toString())
}
check {
dependsOn(testing.suites)
}
}

View File

@ -5,13 +5,13 @@
package io.opentelemetry.sdk.common;
import io.opentelemetry.sdk.internal.JavaVersionSpecific;
import java.time.Instant;
import java.util.concurrent.TimeUnit;
import javax.annotation.concurrent.ThreadSafe;
/**
* A {@link Clock} that uses {@link JavaVersionSpecific#currentTimeNanos()} and {@link
* System#nanoTime()}.
* A {@link Clock} that uses {@link java.time.Clock#systemUTC()}, {@link
* System#currentTimeMillis()}, and {@link System#nanoTime()}.
*/
@ThreadSafe
final class SystemClock implements Clock {
@ -33,7 +33,8 @@ final class SystemClock implements Clock {
@Override
public long now(boolean highPrecision) {
if (highPrecision) {
return JavaVersionSpecific.get().currentTimeNanos();
Instant now = java.time.Clock.systemUTC().instant();
return TimeUnit.SECONDS.toNanos(now.getEpochSecond()) + now.getNano();
}
return TimeUnit.MILLISECONDS.toNanos(System.currentTimeMillis());
}

View File

@ -1,32 +0,0 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
// Includes work from:
/*
* Copyright 2019 LINE Corporation
*
* LINE Corporation licenses this file to you 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:
*
* https://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.opentelemetry.sdk.internal;
final class CurrentJavaVersionSpecific {
static JavaVersionSpecific get() {
return new JavaVersionSpecific();
}
private CurrentJavaVersionSpecific() {}
}

View File

@ -1,64 +0,0 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
// Includes work from:
/*
* Copyright 2019 LINE Corporation
*
* LINE Corporation licenses this file to you 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:
*
* https://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.opentelemetry.sdk.internal;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Contains APIs that are implemented differently based on the version of Java being run. This class
* implements the default, using Java 8 APIs, the minimum version supported by OpenTelemetry. All
* implementations in this class must be forwards-compatible on all Java versions because this class
* may be used outside the multi-release JAR, e.g., in testing or when a user shades without
* creating their own multi-release JAR.
*
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change
* at any time.
*/
public class JavaVersionSpecific {
private static final Logger logger = Logger.getLogger(JavaVersionSpecific.class.getName());
private static final JavaVersionSpecific CURRENT = CurrentJavaVersionSpecific.get();
static {
if (CURRENT.getClass() != JavaVersionSpecific.class) {
logger.log(Level.FINE, "Using the APIs optimized for: {0}", CURRENT.name());
}
}
/** Returns the {@link JavaVersionSpecific} for the current version of Java. */
public static JavaVersionSpecific get() {
return CURRENT;
}
String name() {
return "Java 8";
}
/** Returns the number of nanoseconds since the epoch (00:00:00, 01-Jan-1970, GMT). */
public long currentTimeNanos() {
return TimeUnit.MILLISECONDS.toNanos(System.currentTimeMillis());
}
}

View File

@ -1,32 +0,0 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
// Includes work from:
/*
* Copyright 2019 LINE Corporation
*
* LINE Corporation licenses this file to you 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:
*
* https://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.opentelemetry.sdk.internal;
final class CurrentJavaVersionSpecific {
static JavaVersionSpecific get() {
return new Java9VersionSpecific();
}
private CurrentJavaVersionSpecific() {}
}

View File

@ -1,42 +0,0 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
// Includes work from:
/*
* Copyright 2019 LINE Corporation
*
* LINE Corporation licenses this file to you 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:
*
* https://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.opentelemetry.sdk.internal;
import java.time.Clock;
import java.time.Instant;
import java.util.concurrent.TimeUnit;
/** Implementation of {@link JavaVersionSpecific} using Java 9 APIs. */
class Java9VersionSpecific extends JavaVersionSpecific {
@Override
String name() {
return "Java 9+";
}
@Override
public long currentTimeNanos() {
Instant now = Clock.systemUTC().instant();
return TimeUnit.SECONDS.toNanos(now.getEpochSecond()) + now.getNano();
}
}