aboutsummaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorBogdan Drutu <bdrutu@google.com>2017-03-05 12:36:40 -0800
committerGitHub <noreply@github.com>2017-03-05 12:36:40 -0800
commit85eb690ef659ccc4f163f05daa9a29eaebdfb787 (patch)
tree8f93d4a4b471add0ac73a18a1f2ceaa9e92d616a /core
parent224619bdf81fd5288f51e7ba940d9abdd04c239e (diff)
downloadplatform_external_opencensus-java-85eb690ef659ccc4f163f05daa9a29eaebdfb787.tar.gz
platform_external_opencensus-java-85eb690ef659ccc4f163f05daa9a29eaebdfb787.tar.bz2
platform_external_opencensus-java-85eb690ef659ccc4f163f05daa9a29eaebdfb787.zip
Cleanup trace API (#121)
* Cleanups: Use directly the static final variable for INVALID and DEFAULT. Add a SpanId class. deps for trace in BUILD. Fix samplers to use the new SpanId.
Diffstat (limited to 'core')
-rw-r--r--core/src/main/java/com/google/instrumentation/trace/BlankSpan.java2
-rw-r--r--core/src/main/java/com/google/instrumentation/trace/EndSpanOptions.java17
-rw-r--r--core/src/main/java/com/google/instrumentation/trace/Sampler.java2
-rw-r--r--core/src/main/java/com/google/instrumentation/trace/Samplers.java4
-rw-r--r--core/src/main/java/com/google/instrumentation/trace/ScopedSpanHandle.java2
-rw-r--r--core/src/main/java/com/google/instrumentation/trace/Span.java4
-rw-r--r--core/src/main/java/com/google/instrumentation/trace/SpanContext.java38
-rw-r--r--core/src/main/java/com/google/instrumentation/trace/SpanId.java87
-rw-r--r--core/src/main/java/com/google/instrumentation/trace/TraceId.java17
-rw-r--r--core/src/test/java/com/google/instrumentation/trace/BlankSpanTest.java4
-rw-r--r--core/src/test/java/com/google/instrumentation/trace/EndSpanOptionsTest.java6
-rw-r--r--core/src/test/java/com/google/instrumentation/trace/SamplersTest.java20
-rw-r--r--core/src/test/java/com/google/instrumentation/trace/ScopedSpanHandleTest.java2
-rw-r--r--core/src/test/java/com/google/instrumentation/trace/SpanContextTest.java33
-rw-r--r--core/src/test/java/com/google/instrumentation/trace/SpanIdTest.java59
-rw-r--r--core/src/test/java/com/google/instrumentation/trace/SpanTest.java6
-rw-r--r--core/src/test/java/com/google/instrumentation/trace/TraceIdTest.java10
-rw-r--r--core/src/test/java/com/google/instrumentation/trace/TracerTest.java11
18 files changed, 229 insertions, 95 deletions
diff --git a/core/src/main/java/com/google/instrumentation/trace/BlankSpan.java b/core/src/main/java/com/google/instrumentation/trace/BlankSpan.java
index bbe31df2..a77b4af0 100644
--- a/core/src/main/java/com/google/instrumentation/trace/BlankSpan.java
+++ b/core/src/main/java/com/google/instrumentation/trace/BlankSpan.java
@@ -29,7 +29,7 @@ public final class BlankSpan extends Span {
public static final BlankSpan INSTANCE = new BlankSpan();
private BlankSpan() {
- super(SpanContext.getInvalid(), null);
+ super(SpanContext.INVALID, null);
}
/**
diff --git a/core/src/main/java/com/google/instrumentation/trace/EndSpanOptions.java b/core/src/main/java/com/google/instrumentation/trace/EndSpanOptions.java
index b7abfaa6..2b74d0ca 100644
--- a/core/src/main/java/com/google/instrumentation/trace/EndSpanOptions.java
+++ b/core/src/main/java/com/google/instrumentation/trace/EndSpanOptions.java
@@ -28,27 +28,20 @@ import javax.annotation.concurrent.Immutable;
*/
@Immutable
public final class EndSpanOptions {
- // Default set of options.
- private static final EndSpanOptions DEFAULT_OPTIONS = builder().build();
-
private final Status status;
private final Timestamp endTime;
+ /**
+ * The default {@code EndSpanOptions}.
+ */
+ public static final EndSpanOptions DEFAULT = builder().build();
+
private EndSpanOptions(Timestamp endTime, Status status) {
this.endTime = endTime;
this.status = status;
}
/**
- * Returns default {@code EndSpanOptions}.
- *
- * @return default {@code EndSpanOptions}.
- */
- public static EndSpanOptions getDefault() {
- return DEFAULT_OPTIONS;
- }
-
- /**
* Returns a new {@link Builder} with default options.
*
* @return a new {@code Builder} with default options.
diff --git a/core/src/main/java/com/google/instrumentation/trace/Sampler.java b/core/src/main/java/com/google/instrumentation/trace/Sampler.java
index c1d5b303..bf294a5c 100644
--- a/core/src/main/java/com/google/instrumentation/trace/Sampler.java
+++ b/core/src/main/java/com/google/instrumentation/trace/Sampler.java
@@ -37,7 +37,7 @@ public abstract class Sampler {
@Nullable SpanContext parentContext,
boolean remoteParent,
TraceId traceId,
- long spanId,
+ SpanId spanId,
String name,
List<Span> parentLinks);
}
diff --git a/core/src/main/java/com/google/instrumentation/trace/Samplers.java b/core/src/main/java/com/google/instrumentation/trace/Samplers.java
index 59983b2d..0c7ef3cd 100644
--- a/core/src/main/java/com/google/instrumentation/trace/Samplers.java
+++ b/core/src/main/java/com/google/instrumentation/trace/Samplers.java
@@ -53,7 +53,7 @@ public final class Samplers {
@Nullable SpanContext parentContext,
boolean remoteParent,
TraceId traceId,
- long spanId,
+ SpanId spanId,
String name,
List<Span> parentLinks) {
return true;
@@ -74,7 +74,7 @@ public final class Samplers {
@Nullable SpanContext parentContext,
boolean remoteParent,
TraceId traceId,
- long spanId,
+ SpanId spanId,
String name,
List<Span> parentLinks) {
return false;
diff --git a/core/src/main/java/com/google/instrumentation/trace/ScopedSpanHandle.java b/core/src/main/java/com/google/instrumentation/trace/ScopedSpanHandle.java
index 5c24d1fa..1ac67a45 100644
--- a/core/src/main/java/com/google/instrumentation/trace/ScopedSpanHandle.java
+++ b/core/src/main/java/com/google/instrumentation/trace/ScopedSpanHandle.java
@@ -44,6 +44,6 @@ final class ScopedSpanHandle implements NonThrowingCloseable {
@Override
public void close() {
withSpan.close();
- span.end(EndSpanOptions.getDefault());
+ span.end(EndSpanOptions.DEFAULT);
}
}
diff --git a/core/src/main/java/com/google/instrumentation/trace/Span.java b/core/src/main/java/com/google/instrumentation/trace/Span.java
index b883cf62..26de28f7 100644
--- a/core/src/main/java/com/google/instrumentation/trace/Span.java
+++ b/core/src/main/java/com/google/instrumentation/trace/Span.java
@@ -122,7 +122,7 @@ public abstract class Span implements NonThrowingCloseable {
* implementations are free to ignore all further calls.
*/
public final void end() {
- end(EndSpanOptions.getDefault());
+ end(EndSpanOptions.DEFAULT);
}
/**
@@ -148,6 +148,6 @@ public abstract class Span implements NonThrowingCloseable {
*/
@Override
public final void close() {
- end(EndSpanOptions.getDefault());
+ end(EndSpanOptions.DEFAULT);
}
}
diff --git a/core/src/main/java/com/google/instrumentation/trace/SpanContext.java b/core/src/main/java/com/google/instrumentation/trace/SpanContext.java
index fba02939..916ad030 100644
--- a/core/src/main/java/com/google/instrumentation/trace/SpanContext.java
+++ b/core/src/main/java/com/google/instrumentation/trace/SpanContext.java
@@ -16,35 +16,34 @@ package com.google.instrumentation.trace;
import com.google.common.base.MoreObjects;
import com.google.common.base.Objects;
-import java.util.Formatter;
-import java.util.Locale;
import javax.annotation.concurrent.Immutable;
/**
* A class that represents a span context. A span context contains the state that must propagate
* to child {@link Span}s and across process boundaries. It contains the identifiers (a {@link
- * TraceId trace_id} and span_id) associated with the {@link Span} and a set of {@link TraceOptions
- * options}.
+ * TraceId trace_id} and {@link SpanId span_id}) associated with the {@link Span} and a set of
+ * {@link TraceOptions options}.
*/
@Immutable
public final class SpanContext {
- private static final long INVALID_SPAN_ID = 0;
-
- private static final SpanContext INVALID_CONTEXT =
- new SpanContext(TraceId.getInvalid(), INVALID_SPAN_ID, TraceOptions.getDefault());
-
private final TraceId traceId;
- private final long spanId;
+ private final SpanId spanId;
private final TraceOptions traceOptions;
/**
+ * The invalid {@code SpanContext}.
+ */
+ public static final SpanContext INVALID =
+ new SpanContext(TraceId.INVALID, SpanId.INVALID, TraceOptions.getDefault());
+
+ /**
* Creates a new {@code SpanContext} with the given identifiers and options.
*
* @param traceId the trace identifier of the span context.
* @param spanId the span identifier of the span context.
* @param traceOptions the trace options for the span context.
*/
- SpanContext(TraceId traceId, long spanId, TraceOptions traceOptions) {
+ SpanContext(TraceId traceId, SpanId spanId, TraceOptions traceOptions) {
this.traceId = traceId;
this.spanId = spanId;
this.traceOptions = traceOptions;
@@ -64,7 +63,7 @@ public final class SpanContext {
*
* @return the span identifier associated with this {@code SpanContext}.
*/
- public long getSpanId() {
+ public SpanId getSpanId() {
return spanId;
}
@@ -83,16 +82,7 @@ public final class SpanContext {
* @return true if this {@code SpanContext} is valid.
*/
public boolean isValid() {
- return traceId.isValid() && spanId != INVALID_SPAN_ID;
- }
-
- /**
- * Returns an invalid {@code SpanContext}.
- *
- * @return an invalid {@code SpanContext}.
- */
- public static SpanContext getInvalid() {
- return INVALID_CONTEXT;
+ return traceId.isValid() && spanId.isValid();
}
@Override
@@ -107,7 +97,7 @@ public final class SpanContext {
SpanContext that = (SpanContext) obj;
return traceId.equals(that.traceId)
- && spanId == that.spanId
+ && spanId.equals(that.spanId)
&& traceOptions.equals(that.traceOptions);
}
@@ -120,7 +110,7 @@ public final class SpanContext {
public String toString() {
return MoreObjects.toStringHelper(this)
.add("traceId", traceId)
- .add("spanId", new Formatter(Locale.US).format("%016x", spanId).toString())
+ .add("spanId", spanId)
.add("traceOptions", traceOptions)
.toString();
}
diff --git a/core/src/main/java/com/google/instrumentation/trace/SpanId.java b/core/src/main/java/com/google/instrumentation/trace/SpanId.java
new file mode 100644
index 00000000..302aea35
--- /dev/null
+++ b/core/src/main/java/com/google/instrumentation/trace/SpanId.java
@@ -0,0 +1,87 @@
+/*
+ * Copyright 2017, Google Inc.
+ * 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 com.google.instrumentation.trace;
+
+import com.google.common.base.MoreObjects;
+import com.google.common.base.Objects;
+
+import java.util.Formatter;
+import java.util.Locale;
+import javax.annotation.concurrent.Immutable;
+
+/**
+ * A class that represents a span identifier. A span identifier is a 64-bit, unsigned integer.
+ */
+@Immutable
+public final class SpanId {
+ private static final long INVALID_SPAN_ID = 0;
+ private final long spanId;
+
+ /**
+ * The invalid {@code SpanId}.
+ */
+ public static final SpanId INVALID = new SpanId(INVALID_SPAN_ID);
+
+ /**
+ * Creates a span identifier whose value is taken from the given {@code long}.
+ *
+ * @param spanId a long used as the span identifier.
+ */
+ SpanId(long spanId) {
+ this.spanId = spanId;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (obj == this) {
+ return true;
+ }
+
+ if (!(obj instanceof SpanId)) {
+ return false;
+ }
+
+ SpanId that = (SpanId)obj;
+ return spanId == that.spanId;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hashCode(spanId);
+ }
+
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(this)
+ .add("spanId", new Formatter(Locale.US).format("%016x", spanId))
+ .toString();
+ }
+ /**
+ * Returns whether the span identifier is valid. A valid span identifer is non-zero long value.
+ *
+ * @return whether the span identifier is valid.
+ */
+ public boolean isValid() {
+ return spanId != INVALID_SPAN_ID;
+ }
+
+ /**
+ * Returns the long that represents the span identifier.
+ *
+ * @return the span identifier.
+ */
+ public long getSpanId() {
+ return spanId;
+ }
+}
diff --git a/core/src/main/java/com/google/instrumentation/trace/TraceId.java b/core/src/main/java/com/google/instrumentation/trace/TraceId.java
index 551e0d17..46492dbd 100644
--- a/core/src/main/java/com/google/instrumentation/trace/TraceId.java
+++ b/core/src/main/java/com/google/instrumentation/trace/TraceId.java
@@ -26,11 +26,15 @@ import javax.annotation.concurrent.Immutable;
*/
@Immutable
public final class TraceId {
- private static final TraceId INVALID_ID = new TraceId(0, 0);
private final long traceIdHi;
private final long traceIdLo;
/**
+ * The invalid {@code TraceId}.
+ */
+ public static final TraceId INVALID = new TraceId(0, 0);
+
+ /**
* Creates a new {@code TraceId} whose value is taken from the given params.
*
* @param traceIdHi the higher bits.
@@ -65,16 +69,7 @@ public final class TraceId {
* @return true if the {@code TraceId} is valid.
*/
public boolean isValid() {
- return !this.equals(INVALID_ID);
- }
-
- /**
- * Returns the invalid {@code TraceId}.
- *
- * @return the invalid {@code TraceId}.
- */
- public static TraceId getInvalid() {
- return INVALID_ID;
+ return traceIdLo != 0 || traceIdHi != 0;
}
@Override
diff --git a/core/src/test/java/com/google/instrumentation/trace/BlankSpanTest.java b/core/src/test/java/com/google/instrumentation/trace/BlankSpanTest.java
index 72d48f7e..b6fb865f 100644
--- a/core/src/test/java/com/google/instrumentation/trace/BlankSpanTest.java
+++ b/core/src/test/java/com/google/instrumentation/trace/BlankSpanTest.java
@@ -24,7 +24,7 @@ import org.junit.runners.JUnit4;
public class BlankSpanTest {
@Test
public void hasInvalidContextAndDefaultSpanOptions() {
- assertThat(BlankSpan.INSTANCE.getContext()).isEqualTo(SpanContext.getInvalid());
+ assertThat(BlankSpan.INSTANCE.getContext()).isEqualTo(SpanContext.INVALID);
assertThat(BlankSpan.INSTANCE.getOptions().isEmpty()).isTrue();
}
@@ -40,7 +40,7 @@ public class BlankSpanTest {
BlankSpan.INSTANCE
.addNetworkEvent(NetworkEvent.builder(NetworkEvent.Type.SENT, 1L).build());
BlankSpan.INSTANCE.addChildLink(BlankSpan.INSTANCE);
- BlankSpan.INSTANCE.end(EndSpanOptions.getDefault());
+ BlankSpan.INSTANCE.end(EndSpanOptions.DEFAULT);
}
@Test
diff --git a/core/src/test/java/com/google/instrumentation/trace/EndSpanOptionsTest.java b/core/src/test/java/com/google/instrumentation/trace/EndSpanOptionsTest.java
index 67aa36ac..a2fb4f63 100644
--- a/core/src/test/java/com/google/instrumentation/trace/EndSpanOptionsTest.java
+++ b/core/src/test/java/com/google/instrumentation/trace/EndSpanOptionsTest.java
@@ -31,8 +31,8 @@ public class EndSpanOptionsTest {
@Test
public void endSpanOptions_DefaultOptions() {
- assertThat(EndSpanOptions.getDefault().getStatus()).isEqualTo(Status.OK);
- assertThat(EndSpanOptions.getDefault().getEndTime()).isNull();
+ assertThat(EndSpanOptions.DEFAULT.getStatus()).isEqualTo(Status.OK);
+ assertThat(EndSpanOptions.DEFAULT.getEndTime()).isNull();
}
@Test
@@ -70,7 +70,7 @@ public class EndSpanOptionsTest {
.setEndTime(Timestamp.fromMillis(123456L))
.setStatus(Status.CANCELLED.withDescription("ThisIsAnError"))
.build());
- tester.addEqualityGroup(EndSpanOptions.builder().build(), EndSpanOptions.getDefault());
+ tester.addEqualityGroup(EndSpanOptions.builder().build(), EndSpanOptions.DEFAULT);
tester.testEquals();
}
diff --git a/core/src/test/java/com/google/instrumentation/trace/SamplersTest.java b/core/src/test/java/com/google/instrumentation/trace/SamplersTest.java
index ece59813..8266bcb2 100644
--- a/core/src/test/java/com/google/instrumentation/trace/SamplersTest.java
+++ b/core/src/test/java/com/google/instrumentation/trace/SamplersTest.java
@@ -26,14 +26,16 @@ public class SamplersTest {
@Test
public void alwaysSampleSampler_AlwaysReturnTrue() {
TraceId traceId = new TraceId(2, 3);
+ SpanId parentSpanId = new SpanId(17);
+ SpanId spanId = new SpanId(42);
// Traced parent.
assertThat(
Samplers.alwaysSample()
.shouldSample(
- new SpanContext(traceId, 17, new TraceOptions(TraceOptions.IS_SAMPLED)),
+ new SpanContext(traceId, parentSpanId, new TraceOptions(TraceOptions.IS_SAMPLED)),
false,
traceId,
- 42,
+ spanId,
"Another name",
Collections.emptyList()))
.isTrue();
@@ -41,10 +43,10 @@ public class SamplersTest {
assertThat(
Samplers.alwaysSample()
.shouldSample(
- new SpanContext(traceId, 19, new TraceOptions(0)),
+ new SpanContext(traceId, parentSpanId, new TraceOptions(0)),
false,
traceId,
- 42,
+ spanId,
"Yet another name",
Collections.emptyList()))
.isTrue();
@@ -58,14 +60,16 @@ public class SamplersTest {
@Test
public void neverSampleSampler_AlwaysReturnFalse() {
TraceId traceId = new TraceId(4, 5);
+ SpanId parentSpanId = new SpanId(19);
+ SpanId spanId = new SpanId(42);
// Traced parent.
assertThat(
Samplers.neverSample()
.shouldSample(
- new SpanContext(traceId, 17, new TraceOptions(TraceOptions.IS_SAMPLED)),
+ new SpanContext(traceId, parentSpanId, new TraceOptions(TraceOptions.IS_SAMPLED)),
false,
traceId,
- 42,
+ spanId,
"bar",
Collections.emptyList()))
.isFalse();
@@ -73,10 +77,10 @@ public class SamplersTest {
assertThat(
Samplers.neverSample()
.shouldSample(
- new SpanContext(traceId, 17, new TraceOptions(0)),
+ new SpanContext(traceId, parentSpanId, new TraceOptions(0)),
false,
traceId,
- 42,
+ spanId,
"quux",
Collections.emptyList()))
.isFalse();
diff --git a/core/src/test/java/com/google/instrumentation/trace/ScopedSpanHandleTest.java b/core/src/test/java/com/google/instrumentation/trace/ScopedSpanHandleTest.java
index cca32519..971904b1 100644
--- a/core/src/test/java/com/google/instrumentation/trace/ScopedSpanHandleTest.java
+++ b/core/src/test/java/com/google/instrumentation/trace/ScopedSpanHandleTest.java
@@ -46,6 +46,6 @@ public class ScopedSpanHandleTest {
// Do nothing.
}
verify(ntc).close();
- verify(span).end(same(EndSpanOptions.getDefault()));
+ verify(span).end(same(EndSpanOptions.DEFAULT));
}
}
diff --git a/core/src/test/java/com/google/instrumentation/trace/SpanContextTest.java b/core/src/test/java/com/google/instrumentation/trace/SpanContextTest.java
index 30ca1cb4..4cc10bee 100644
--- a/core/src/test/java/com/google/instrumentation/trace/SpanContextTest.java
+++ b/core/src/test/java/com/google/instrumentation/trace/SpanContextTest.java
@@ -24,23 +24,26 @@ import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class SpanContextTest {
private static final SpanContext first =
- new SpanContext(new TraceId(0, 10), 30, TraceOptions.getDefault());
+ new SpanContext(new TraceId(0, 10), new SpanId(30), TraceOptions.getDefault());
private static final SpanContext second =
- new SpanContext(new TraceId(0, 20), 40, new TraceOptions(TraceOptions.IS_SAMPLED));
+ new SpanContext(
+ new TraceId(0, 20), new SpanId(40), new TraceOptions(TraceOptions.IS_SAMPLED));
@Test
public void invalidSpanContext() {
- assertThat(SpanContext.getInvalid().getTraceId()).isEqualTo(TraceId.getInvalid());
- assertThat(SpanContext.getInvalid().getSpanId()).isEqualTo(0);
- assertThat(SpanContext.getInvalid().getTraceOptions()).isEqualTo(TraceOptions.getDefault());
+ assertThat(SpanContext.INVALID.getTraceId()).isEqualTo(TraceId.INVALID);
+ assertThat(SpanContext.INVALID.getSpanId()).isEqualTo(SpanId.INVALID);
+ assertThat(SpanContext.INVALID.getTraceOptions()).isEqualTo(TraceOptions.getDefault());
}
@Test
public void isValid() {
- assertThat(SpanContext.getInvalid().isValid()).isFalse();
- assertThat(new SpanContext(new TraceId(0, 10), 0, TraceOptions.getDefault()).isValid())
+ assertThat(SpanContext.INVALID.isValid()).isFalse();
+ assertThat(
+ new SpanContext(new TraceId(0, 10), SpanId.INVALID, TraceOptions.getDefault()).isValid())
.isFalse();
- assertThat(new SpanContext(TraceId.getInvalid(), 10, TraceOptions.getDefault()).isValid())
+ assertThat(
+ new SpanContext(TraceId.INVALID, new SpanId(10), TraceOptions.getDefault()).isValid())
.isFalse();
assertThat(first.isValid()).isTrue();
assertThat(second.isValid()).isTrue();
@@ -54,8 +57,8 @@ public class SpanContextTest {
@Test
public void getSpanId() {
- assertThat(first.getSpanId()).isEqualTo(30);
- assertThat(second.getSpanId()).isEqualTo(40);
+ assertThat(first.getSpanId()).isEqualTo(new SpanId(30));
+ assertThat(second.getSpanId()).isEqualTo(new SpanId(40));
}
@Test
@@ -68,19 +71,21 @@ public class SpanContextTest {
public void spanContext_EqualsAndHashCode() {
EqualsTester tester = new EqualsTester();
tester.addEqualityGroup(
- first, new SpanContext(new TraceId(0, 10), 30, TraceOptions.getDefault()));
+ first, new SpanContext(new TraceId(0, 10), new SpanId(30), TraceOptions.getDefault()));
tester.addEqualityGroup(
- second, new SpanContext(new TraceId(0, 20), 40, new TraceOptions(TraceOptions.IS_SAMPLED)));
+ second,
+ new SpanContext(
+ new TraceId(0, 20), new SpanId(40), new TraceOptions(TraceOptions.IS_SAMPLED)));
tester.testEquals();
}
@Test
public void spanContext_ToString() {
assertThat(first.toString()).contains(new TraceId(0, 10).toString());
- assertThat(first.toString()).contains("spanId=000000000000001e");
+ assertThat(first.toString()).contains(new SpanId(30).toString());
assertThat(first.toString()).contains(TraceOptions.getDefault().toString());
assertThat(second.toString()).contains(new TraceId(0, 20).toString());
- assertThat(second.toString()).contains("spanId=0000000000000028");
+ assertThat(second.toString()).contains(new SpanId(40).toString());
assertThat(second.toString()).contains(new TraceOptions(TraceOptions.IS_SAMPLED).toString());
}
}
diff --git a/core/src/test/java/com/google/instrumentation/trace/SpanIdTest.java b/core/src/test/java/com/google/instrumentation/trace/SpanIdTest.java
new file mode 100644
index 00000000..f5c50d25
--- /dev/null
+++ b/core/src/test/java/com/google/instrumentation/trace/SpanIdTest.java
@@ -0,0 +1,59 @@
+/*
+ * Copyright 2017, Google Inc.
+ * 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 com.google.instrumentation.trace;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import com.google.common.testing.EqualsTester;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Unit tests for {@link SpanId}. */
+@RunWith(JUnit4.class)
+public class SpanIdTest {
+ private static final SpanId first = new SpanId(123);
+
+ @Test
+ public void invalidSpanId() {
+ assertThat(SpanId.INVALID.getSpanId()).isEqualTo(0);
+ }
+
+ @Test
+ public void isValid() {
+ assertThat(SpanId.INVALID.isValid()).isFalse();
+ assertThat(first.isValid()).isTrue();
+ }
+
+ @Test
+ public void getSpanId() {
+ assertThat(first.getSpanId()).isEqualTo(123);
+ }
+
+ @Test
+ public void traceId_EqualsAndHashCode() {
+ EqualsTester tester = new EqualsTester();
+ tester.addEqualityGroup(SpanId.INVALID, SpanId.INVALID);
+ tester.addEqualityGroup(first, new SpanId(123));
+ tester.testEquals();
+ }
+
+ @Test
+ public void traceId_ToString() {
+ assertThat(SpanId.INVALID.toString()).contains("0000000000000000");
+ assertThat(new SpanId(0xFEDCBA9876543210L).toString())
+ .contains("fedcba9876543210");
+ assertThat(new SpanId(-1).toString()).contains("ffffffffffffffff");
+ }
+}
diff --git a/core/src/test/java/com/google/instrumentation/trace/SpanTest.java b/core/src/test/java/com/google/instrumentation/trace/SpanTest.java
index 0c628253..1864b1a7 100644
--- a/core/src/test/java/com/google/instrumentation/trace/SpanTest.java
+++ b/core/src/test/java/com/google/instrumentation/trace/SpanTest.java
@@ -27,7 +27,7 @@ import org.mockito.Mockito;
@RunWith(JUnit4.class)
public class SpanTest {
private static final SpanContext spanContext =
- new SpanContext(new TraceId(10, 20), 30, TraceOptions.getDefault());
+ new SpanContext(new TraceId(10, 20), new SpanId(30), TraceOptions.getDefault());
private static final EnumSet<Span.Options> spanOptions = EnumSet.of(Span.Options.RECORD_EVENTS);
@Test(expected = NullPointerException.class)
@@ -52,14 +52,14 @@ public class SpanTest {
public void endCallsEndWithDefaultOptions() {
Span span = Mockito.spy(new NoopSpan(spanContext, spanOptions));
span.end();
- verify(span).end(same(EndSpanOptions.getDefault()));
+ verify(span).end(same(EndSpanOptions.DEFAULT));
}
@Test
public void closeCallsEndWithDefaultOptions() {
Span span = Mockito.spy(new NoopSpan(spanContext, spanOptions));
span.close();
- verify(span).end(same(EndSpanOptions.getDefault()));
+ verify(span).end(same(EndSpanOptions.DEFAULT));
}
// No-op implementation of the Span for testing only.
diff --git a/core/src/test/java/com/google/instrumentation/trace/TraceIdTest.java b/core/src/test/java/com/google/instrumentation/trace/TraceIdTest.java
index b611a281..fc9708eb 100644
--- a/core/src/test/java/com/google/instrumentation/trace/TraceIdTest.java
+++ b/core/src/test/java/com/google/instrumentation/trace/TraceIdTest.java
@@ -29,13 +29,13 @@ public class TraceIdTest {
@Test
public void invalidTraceId() {
- assertThat(TraceId.getInvalid().getTraceIdLo()).isEqualTo(0);
- assertThat(TraceId.getInvalid().getTraceIdHi()).isEqualTo(0);
+ assertThat(TraceId.INVALID.getTraceIdLo()).isEqualTo(0);
+ assertThat(TraceId.INVALID.getTraceIdHi()).isEqualTo(0);
}
@Test
public void isValid() {
- assertThat(TraceId.getInvalid().isValid()).isFalse();
+ assertThat(TraceId.INVALID.isValid()).isFalse();
assertThat(second.isValid()).isTrue();
assertThat(third.isValid()).isTrue();
assertThat(first.isValid()).isTrue();
@@ -58,7 +58,7 @@ public class TraceIdTest {
@Test
public void traceId_EqualsAndHashCode() {
EqualsTester tester = new EqualsTester();
- tester.addEqualityGroup(TraceId.getInvalid(), TraceId.getInvalid());
+ tester.addEqualityGroup(TraceId.INVALID, TraceId.INVALID);
tester.addEqualityGroup(first, new TraceId(123, 456));
tester.addEqualityGroup(second, new TraceId(0, 789));
tester.addEqualityGroup(third, new TraceId(987, 0));
@@ -67,7 +67,7 @@ public class TraceIdTest {
@Test
public void traceId_ToString() {
- assertThat(TraceId.getInvalid().toString()).contains("00000000000000000000000000000000");
+ assertThat(TraceId.INVALID.toString()).contains("00000000000000000000000000000000");
assertThat(new TraceId(0, 0xFEDCBA9876543210L).toString())
.contains("0000000000000000fedcba9876543210");
assertThat(new TraceId(0x0123456789ABCDEFL, 0).toString())
diff --git a/core/src/test/java/com/google/instrumentation/trace/TracerTest.java b/core/src/test/java/com/google/instrumentation/trace/TracerTest.java
index c5f07722..de67c6ec 100644
--- a/core/src/test/java/com/google/instrumentation/trace/TracerTest.java
+++ b/core/src/test/java/com/google/instrumentation/trace/TracerTest.java
@@ -229,7 +229,7 @@ public class TracerTest {
when(contextSpanHandler.withSpan(same(span))).thenReturn(withSpan);
try (NonThrowingCloseable ss = mockTracer.startScopedSpan("MySpanName")) {}
verify(withSpan).close();
- verify(span).end(same(EndSpanOptions.getDefault()));
+ verify(span).end(same(EndSpanOptions.DEFAULT));
}
@Test
@@ -243,7 +243,7 @@ public class TracerTest {
when(contextSpanHandler.withSpan(same(span))).thenReturn(withSpan);
try (NonThrowingCloseable ss = mockTracer.startScopedSpan("MySpanName", startSpanOptions)) {}
verify(withSpan).close();
- verify(span).end(same(EndSpanOptions.getDefault()));
+ verify(span).end(same(EndSpanOptions.DEFAULT));
}
@Test
@@ -254,7 +254,7 @@ public class TracerTest {
when(contextSpanHandler.withSpan(same(span))).thenReturn(withSpan);
try (NonThrowingCloseable ss = mockTracer.startScopedSpan(span, "MySpanName")) {}
verify(withSpan).close();
- verify(span).end(same(EndSpanOptions.getDefault()));
+ verify(span).end(same(EndSpanOptions.DEFAULT));
}
@Test
@@ -267,7 +267,7 @@ public class TracerTest {
try (NonThrowingCloseable ss =
mockTracer.startScopedSpan(span, "MySpanName", startSpanOptions)) {}
verify(withSpan).close();
- verify(span).end(same(EndSpanOptions.getDefault()));
+ verify(span).end(same(EndSpanOptions.DEFAULT));
}
@Test
@@ -291,7 +291,8 @@ public class TracerTest {
@Test
public void startSpanWitRemoteParent() {
Tracer mockTracer = newTracerWithMocks();
- SpanContext spanContext = new SpanContext(new TraceId(10, 20), 30, TraceOptions.getDefault());
+ SpanContext spanContext = new SpanContext(new TraceId(10, 20), new SpanId(30),
+ TraceOptions.getDefault());
StartSpanOptions startSpanOptions = StartSpanOptions.builder().build();
when(spanFactory.startSpanWithRemoteParent(
same(spanContext), eq("MySpanName"), same(startSpanOptions)))