Skip to content

Commit f900405

Browse files
Anuraag Agrawaljkwatson
andauthored
Allow users to access the default Clock and TestClock (open-telemetry#3363)
* Allow users to access the default Clock and TestClock * Update TestClock API * Doc * Fix * html * Less newlines * Update sdk/common/src/main/java/io/opentelemetry/sdk/common/Clock.java Co-authored-by: John Watson <jkwatson@gmail.com> Co-authored-by: John Watson <jkwatson@gmail.com>
1 parent 6449a84 commit f900405

42 files changed

Lines changed: 311 additions & 265 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

docs/apidiffs/current_vs_latest/opentelemetry-sdk-common.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
Comparing source compatibility of against
2+
***! MODIFIED INTERFACE: PUBLIC ABSTRACT io.opentelemetry.sdk.common.Clock (not serializable)
3+
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
4+
+++! NEW METHOD: PUBLIC(+) STATIC(+) io.opentelemetry.sdk.common.Clock getDefault()
25
**** MODIFIED CLASS: PUBLIC ABSTRACT io.opentelemetry.sdk.common.InstrumentationLibraryInfo (not serializable)
36
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
47
+++ NEW METHOD: PUBLIC(+) STATIC(+) io.opentelemetry.sdk.common.InstrumentationLibraryInfo create(java.lang.String, java.lang.String, java.lang.String)

docs/apidiffs/current_vs_latest/opentelemetry-sdk-testing.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,13 @@ Comparing source compatibility of against
55
+++ NEW ANNOTATION: java.lang.SafeVarargs
66
+++ NEW METHOD: PUBLIC(+) FINAL(+) io.opentelemetry.sdk.testing.assertj.SpanDataAssert hasEventsSatisfyingExactly(java.util.function.Consumer[])
77
+++ NEW ANNOTATION: java.lang.SafeVarargs
8+
+++ NEW CLASS: PUBLIC(+) FINAL(+) io.opentelemetry.sdk.testing.time.TestClock (not serializable)
9+
+++ CLASS FILE FORMAT VERSION: 52.0 <- n.a.
10+
+++ NEW SUPERCLASS: java.lang.Object
11+
+++ NEW METHOD: PUBLIC(+) void advance(java.time.Duration)
12+
+++ NEW METHOD: PUBLIC(+) void advance(long, java.util.concurrent.TimeUnit)
13+
+++ NEW METHOD: PUBLIC(+) STATIC(+) io.opentelemetry.sdk.testing.time.TestClock create()
14+
+++ NEW METHOD: PUBLIC(+) STATIC(+) io.opentelemetry.sdk.testing.time.TestClock create(java.time.Instant)
15+
+++ NEW METHOD: PUBLIC(+) long nanoTime()
16+
+++ NEW METHOD: PUBLIC(+) long now()
17+
+++ NEW METHOD: PUBLIC(+) void setTime(java.time.Instant)

sdk-extensions/aws/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ dependencies {
2121
implementation("com.fasterxml.jackson.core:jackson-core")
2222
implementation("com.fasterxml.jackson.core:jackson-databind")
2323

24+
testImplementation(project(":sdk:testing"))
2425
testImplementation(project(":sdk-extensions:autoconfigure"))
2526

2627
testImplementation("com.linecorp.armeria:armeria-junit5")

sdk-extensions/jaeger-remote-sampler/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ dependencies {
1717
implementation("io.grpc:grpc-stub")
1818
implementation("com.google.protobuf:protobuf-java")
1919

20+
testImplementation(project(":sdk:testing"))
21+
2022
testImplementation("io.grpc:grpc-testing")
2123
testImplementation("org.testcontainers:junit-jupiter")
2224

sdk-extensions/jaeger-remote-sampler/src/main/java/io/opentelemetry/sdk/extension/trace/jaeger/sampler/RateLimitingSampler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
import io.opentelemetry.api.common.Attributes;
1313
import io.opentelemetry.api.trace.SpanKind;
1414
import io.opentelemetry.context.Context;
15+
import io.opentelemetry.sdk.common.Clock;
1516
import io.opentelemetry.sdk.internal.RateLimiter;
16-
import io.opentelemetry.sdk.internal.SystemClock;
1717
import io.opentelemetry.sdk.trace.data.LinkData;
1818
import io.opentelemetry.sdk.trace.samplers.Sampler;
1919
import io.opentelemetry.sdk.trace.samplers.SamplingDecision;
@@ -42,7 +42,7 @@ class RateLimitingSampler implements Sampler {
4242
RateLimitingSampler(int maxTracesPerSecond) {
4343
this.maxTracesPerSecond = maxTracesPerSecond;
4444
double maxBalance = maxTracesPerSecond < 1.0 ? 1.0 : maxTracesPerSecond;
45-
this.rateLimiter = new RateLimiter(maxTracesPerSecond, maxBalance, SystemClock.getInstance());
45+
this.rateLimiter = new RateLimiter(maxTracesPerSecond, maxBalance, Clock.getDefault());
4646
Attributes attributes =
4747
Attributes.of(SAMPLER_TYPE, TYPE, SAMPLER_PARAM, (double) maxTracesPerSecond);
4848
this.onSamplingResult = SamplingResult.create(SamplingDecision.RECORD_AND_SAMPLE, attributes);

sdk-extensions/zpages/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ dependencies {
1212
implementation(project(":api:all"))
1313
implementation(project(":sdk:all"))
1414

15+
testImplementation(project(":sdk:testing"))
16+
1517
testImplementation("com.google.guava:guava")
1618

1719
compileOnly("com.sun.net.httpserver:http")

sdk-extensions/zpages/src/test/java/io/opentelemetry/sdk/extension/zpages/TracezDataAggregatorTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010
import io.opentelemetry.api.trace.Span;
1111
import io.opentelemetry.api.trace.StatusCode;
1212
import io.opentelemetry.api.trace.Tracer;
13-
import io.opentelemetry.sdk.internal.TestClock;
13+
import io.opentelemetry.sdk.testing.time.TestClock;
1414
import io.opentelemetry.sdk.trace.ReadableSpan;
1515
import io.opentelemetry.sdk.trace.SdkTracerProvider;
1616
import io.opentelemetry.sdk.trace.data.SpanData;
17+
import java.time.Duration;
1718
import java.util.List;
1819
import java.util.Map;
1920
import java.util.Set;
@@ -157,7 +158,7 @@ void getSpanLatencyCounts_noCompletedSpans() {
157158
void getSpanLatencyCounts_oneSpanPerLatencyBucket() {
158159
for (LatencyBoundary bucket : LatencyBoundary.values()) {
159160
Span span = tracer.spanBuilder(SPAN_NAME_ONE).startSpan();
160-
testClock.advanceNanos(bucket.getLatencyLowerBound());
161+
testClock.advance(Duration.ofNanos(bucket.getLatencyLowerBound()));
161162
span.end();
162163
}
163164
/* getSpanLatencyCounts should return 1 span per latency bucket */
@@ -181,7 +182,7 @@ void getOkSpans_oneSpanNameWithDifferentLatencies() {
181182
/* getOkSpans should return an empty List */
182183
assertThat(dataAggregator.getOkSpans(SPAN_NAME_ONE, 0, Long.MAX_VALUE)).isEmpty();
183184
span1.end();
184-
testClock.advanceNanos(1000);
185+
testClock.advance(Duration.ofNanos(1000));
185186
span2.end();
186187
/* getOkSpans should return a List with both spans */
187188
List<SpanData> spans = dataAggregator.getOkSpans(SPAN_NAME_ONE, 0, Long.MAX_VALUE);

sdk-extensions/zpages/src/test/java/io/opentelemetry/sdk/extension/zpages/TracezZPageHandlerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import io.opentelemetry.api.trace.Span;
1313
import io.opentelemetry.api.trace.StatusCode;
1414
import io.opentelemetry.api.trace.Tracer;
15-
import io.opentelemetry.sdk.internal.TestClock;
15+
import io.opentelemetry.sdk.testing.time.TestClock;
1616
import io.opentelemetry.sdk.trace.SdkTracerProvider;
1717
import java.io.ByteArrayOutputStream;
1818
import java.io.OutputStream;

sdk/all/src/test/java/io/opentelemetry/sdk/internal/SystemClockTest.java renamed to sdk/all/src/test/java/io/opentelemetry/sdk/common/SystemClockTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
package io.opentelemetry.sdk.internal;
6+
package io.opentelemetry.sdk.common;
77

88
import static org.assertj.core.api.Assertions.assertThat;
99

sdk/common/src/main/java/io/opentelemetry/sdk/common/Clock.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,36 @@
1010
/** Interface for getting the current time. */
1111
@ThreadSafe
1212
public interface Clock {
13+
14+
/** Returns a default {@link Clock} which reads from {@linkplain System system time}. */
15+
static Clock getDefault() {
16+
return SystemClock.getInstance();
17+
}
18+
1319
/**
14-
* Obtains the current epoch timestamp in nanos from this clock.
20+
* Returns the current epoch timestamp in nanos from this clock. This timestamp should only be
21+
* used to compute a current time. To compute a duration, timestamps should always be obtained
22+
* using {@link #nanoTime()}. For example, this usage is correct.
23+
*
24+
* <pre>{@code
25+
* long startNanos = clock.nanoTime();
26+
* // Spend time...
27+
* long durationNanos = clock.nanoTime() - startNanos;
28+
* }</pre>
1529
*
16-
* @return the current epoch timestamp in nanos.
30+
* <p>This usage is NOT correct.
31+
*
32+
* <pre>{@code
33+
* long startNanos = clock.now();
34+
* // Spend time...
35+
* long durationNanos = clock.now() - startNanos;
36+
* }</pre>
1737
*/
1838
long now();
1939

2040
/**
2141
* Returns a time measurement with nanosecond precision that can only be used to calculate elapsed
2242
* time.
23-
*
24-
* @return a time measurement with nanosecond precision that can only be used to calculate elapsed
25-
* time.
2643
*/
2744
long nanoTime();
2845
}

0 commit comments

Comments
 (0)