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

Move the TraceFlags isSampled boolean into the SpanContext #1628

Merged
merged 6 commits into from
Sep 14, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
Expand Up @@ -20,7 +20,6 @@
import io.opentelemetry.context.propagation.TextMapPropagator.Setter;
import io.opentelemetry.trace.DefaultSpan;
import io.opentelemetry.trace.SpanContext;
import io.opentelemetry.trace.TraceFlags;
import io.opentelemetry.trace.TraceState;
import io.opentelemetry.trace.TracingContextUtils;
import java.util.ArrayList;
Expand Down Expand Up @@ -78,8 +77,7 @@ public Map<String, String> measureInject() {
}

private static SpanContext createTestSpanContext(String traceId, String spanId) {
byte sampledTraceOptionsBytes = 1;
TraceFlags sampledTraceOptions = TraceFlags.fromByte(sampledTraceOptionsBytes);
boolean sampledTraceOptions = true;
TraceState traceStateDefault = TraceState.builder().build();
return SpanContext.create(traceId, spanId, sampledTraceOptions, traceStateDefault);
}
Expand Down
38 changes: 19 additions & 19 deletions api/src/main/java/io/opentelemetry/trace/SpanContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
/**
* 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 {@link SpanId span_id}) associated with the {@link Span} and a set of {@link
* TraceFlags options}, as well as the {@link TraceState traceState} and the {@link boolean remote}
* flag.
* trace_id} and {@link SpanId span_id}) associated with the {@link Span} and a set of options
* (currently only whether the context is sampled or not), as well as the {@link TraceState
* traceState} and the {@link boolean remote} flag.
*
* @since 0.1.0
*/
Expand All @@ -37,7 +37,7 @@ public abstract class SpanContext {
create(
TraceId.getInvalid(),
SpanId.getInvalid(),
TraceFlags.getDefault(),
/* isSampled=*/ false,
TraceState.getDefault());

/**
Expand All @@ -54,24 +54,24 @@ public static SpanContext getInvalid() {
*
* @param traceIdHex the trace identifier of the span context.
* @param spanIdHex the span identifier of the span context.
* @param traceFlags the trace options for the span context.
* @param isSampled if the underlying span should be sampled.
* @param traceState the trace state for the span context.
* @return a new {@code SpanContext} with the given identifiers and options.
* @since 0.1.0
*/
public static SpanContext create(
String traceIdHex, String spanIdHex, TraceFlags traceFlags, TraceState traceState) {
return create(traceIdHex, spanIdHex, traceFlags, traceState, /* remote=*/ false);
String traceIdHex, String spanIdHex, boolean isSampled, TraceState traceState) {
return create(traceIdHex, spanIdHex, isSampled, traceState, /* remote=*/ false);
}

private static SpanContext create(
String traceIdHex,
String spanIdHex,
TraceFlags traceFlags,
boolean isSampled,
TraceState traceState,
boolean remote) {
return new AutoValue_SpanContext(
traceIdHex, spanIdHex, traceFlags, traceState, /* remote$=*/ remote);
traceIdHex, spanIdHex, isSampled, traceState, /* remote$=*/ remote);
}

/**
Expand All @@ -80,14 +80,14 @@ private static SpanContext create(
*
* @param traceIdHex the trace identifier of the span context.
* @param spanIdHex the span identifier of the span context.
* @param traceFlags the trace options for the span context.
* @param isSampled if the underlying span should be sampled.
* @param traceState the trace state for the span context.
* @return a new {@code SpanContext} with the given identifiers and options.
* @since 0.1.0
*/
public static SpanContext createFromRemoteParent(
String traceIdHex, String spanIdHex, TraceFlags traceFlags, TraceState traceState) {
return create(traceIdHex, spanIdHex, traceFlags, traceState, /* remote=*/ true);
String traceIdHex, String spanIdHex, boolean isSampled, TraceState traceState) {
return create(traceIdHex, spanIdHex, isSampled, traceState, /* remote=*/ true);
}

abstract String getTraceIdHex();
Expand Down Expand Up @@ -136,13 +136,13 @@ public byte[] getSpanIdBytes() {
return SpanId.bytesFromHex(getSpanIdHex(), 0);
}

/**
* Returns the {@code TraceFlags} associated with this {@code SpanContext}.
*
* @return the {@code TraceFlags} associated with this {@code SpanContext}.
* @since 0.1.0
*/
public abstract TraceFlags getTraceFlags();
/** Whether the span in this context is sampled. */
public abstract boolean isSampled();

public void copyTraceFlagsHexTo(char[] dest, int destOffset) {
dest[destOffset] = '0';
dest[destOffset + 1] = isSampled() ? '1' : '0';
}

/**
* Returns the {@code TraceState} associated with this {@code SpanContext}.
Expand Down
222 changes: 16 additions & 206 deletions api/src/main/java/io/opentelemetry/trace/TraceFlags.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,45 +16,32 @@

package io.opentelemetry.trace;

import io.opentelemetry.internal.Utils;
import java.util.Arrays;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;

/**
* A class that represents global trace options. These options are propagated to all child {@link
* Span spans}. These determine features such as whether a {@code Span} should be traced. It is
* implemented as a bitmask.
* Helper methods for dealing with trace flags options. These options are propagated to all child
* {@link Span spans}. These determine features such as whether a {@code Span} should be traced. It
* is implemented as a bitmask.
*
* @since 0.1.0
*/
@Immutable
public final class TraceFlags {
// Default options. Nothing set.
private static final byte DEFAULT_OPTIONS = 0;
private TraceFlags() {}

// Bit to represent whether trace is sampled or not.
private static final byte IS_SAMPLED = 0x1;

private static final int SIZE = 1;
private static final int BASE16_SIZE = 2 * SIZE;
private static final TraceFlags DEFAULT = fromByte(DEFAULT_OPTIONS);

// The set of enabled features is determined by all the enabled bits.
private final byte options;

// Creates a new {@code TraceFlags} with the given options.
private TraceFlags(byte options) {
this.options = options;
}

/**
* Returns the size in bytes of the {@code TraceFlags}.
* Returns the size in Hex of trace flags.
*
* @return the size in bytes of the {@code TraceFlags}.
* @since 0.1.0
* @since 0.9.0
*/
public static int getSize() {
return SIZE;
public static int getBase16Size() {
return BASE16_SIZE;
}

/**
Expand All @@ -63,191 +50,14 @@ public static int getSize() {
* @return the default {@code TraceFlags}.
* @since 0.1.0
*/
public static TraceFlags getDefault() {
return DEFAULT;
}

/**
* Returns a {@code TraceFlags} whose representation is {@code src}.
*
* @param src the byte representation of the {@code TraceFlags}.
* @return a {@code TraceFlags} whose representation is {@code src}.
* @since 0.1.0
*/
public static TraceFlags fromByte(byte src) {
return new TraceFlags(src);
}

/**
* Returns a {@code TraceOption} built from a lowercase base16 representation.
*
* @param src the lowercase base16 representation.
* @param srcOffset the offset in the buffer where the representation of the {@code TraceFlags}
* begins.
* @return a {@code TraceOption} built from a lowercase base16 representation.
* @throws NullPointerException if {@code src} is null.
* @throws IllegalArgumentException if {@code src.length} is not {@code 2 * TraceOption.SIZE} OR
* if the {@code str} has invalid characters.
* @since 0.1.0
*/
public static TraceFlags fromLowerBase16(CharSequence src, int srcOffset) {
return new TraceFlags(BigendianEncoding.byteFromBase16String(src, srcOffset));
}

/**
* Returns the one byte representation of the {@code TraceFlags}.
*
* @return the one byte representation of the {@code TraceFlags}.
* @since 0.1.0
*/
public byte getByte() {
return options;
}

/**
* Copies the byte representations of the {@code TraceFlags} into the {@code dest} beginning at
* the {@code destOffset} offset.
*
* <p>Equivalent with (but faster because it avoids any new allocations):
*
* <pre>{@code
* System.arraycopy(getBytes(), 0, dest, destOffset, TraceFlags.getSize());
* }</pre>
*
* @param dest the destination buffer.
* @param destOffset the starting offset in the destination buffer.
* @throws NullPointerException if {@code dest} is null.
* @throws IndexOutOfBoundsException if {@code destOffset+TraceFlags.getSize()} is greater than
* {@code dest.length}.
* @since 0.1.0
*/
public void copyBytesTo(byte[] dest, int destOffset) {
Utils.checkIndex(destOffset, dest.length);
dest[destOffset] = options;
}

/**
* Copies the lowercase base16 representations of the {@code TraceId} into the {@code dest}
* beginning at the {@code destOffset} offset.
*
* @param dest the destination buffer.
* @param destOffset the starting offset in the destination buffer.
* @throws IndexOutOfBoundsException if {@code destOffset + 2} is greater than {@code
* dest.length}.
* @since 0.1.0
*/
public void copyLowerBase16To(char[] dest, int destOffset) {
BigendianEncoding.byteToBase16String(options, dest, destOffset);
}

/**
* Returns the lowercase base16 encoding of this {@code TraceFlags}.
*
* @return the lowercase base16 encoding of this {@code TraceFlags}.
* @since 0.1.0
*/
public String toLowerBase16() {
char[] chars = new char[BASE16_SIZE];
copyLowerBase16To(chars, 0);
return new String(chars);
}

/**
* Returns a new {@link Builder} with default options.
*
* @return a new {@code Builder} with default options.
* @since 0.1.0
*/
public static Builder builder() {
return new Builder(DEFAULT_OPTIONS);
}

/**
* Returns a new {@link Builder} with all given options set.
*
* @param traceFlags the given options set.
* @return a new {@code Builder} with all given options set.
* @since 0.1.0
*/
public static Builder builder(TraceFlags traceFlags) {
return new Builder(traceFlags.options);
}

/**
* Returns a boolean indicating whether this {@code Span} is part of a sampled trace and data
* should be exported to a persistent store.
*
* @return a boolean indicating whether the trace is sampled.
* @since 0.1.0
*/
public boolean isSampled() {
return hasOption(IS_SAMPLED);
}

@Override
public boolean equals(@Nullable Object obj) {
if (obj == this) {
return true;
}

if (!(obj instanceof TraceFlags)) {
return false;
}

TraceFlags that = (TraceFlags) obj;
return options == that.options;
}

@Override
public int hashCode() {
return Arrays.hashCode(new byte[] {options});
}

@Override
public String toString() {
return "TraceFlags{sampled=" + isSampled() + "}";
}

/**
* Builder class for {@link TraceFlags}.
*
* @since 0.1.0
*/
public static final class Builder {
private byte options;

private Builder(byte options) {
this.options = options;
}

/**
* Sets the sampling bit in the options.
*
* @param isSampled the sampling bit.
* @return this.
* @since 0.1.0
*/
public Builder setIsSampled(boolean isSampled) {
if (isSampled) {
options = (byte) (options | IS_SAMPLED);
} else {
options = (byte) (options & ~IS_SAMPLED);
}
return this;
}

/**
* Builds and returns a {@code TraceFlags} with the desired options.
*
* @return a {@code TraceFlags} with the desired options.
* @since 0.1.0
*/
public TraceFlags build() {
return fromByte(options);
}
public static boolean getDefault() {
return false;
}

private boolean hasOption(int mask) {
return (this.options & mask) != 0;
/** Extract the sampled flag from hex-based trace-flags. */
public static boolean isSampledFromHex(CharSequence src, int srcOffset) {
// todo bypass the byte conversion and look directly at the hex.
byte b = BigendianEncoding.byteFromBase16String(src, srcOffset);
return (b & IS_SAMPLED) != 0;
}
}
Loading