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

TracerProvider: Make format settable. #914

Closed
Show file tree
Hide file tree
Changes from all 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 @@ -93,6 +93,12 @@ public final class BinaryTraceContext implements BinaryFormat<SpanContext> {
private static final int ALL_FORMAT_LENGTH =
REQUIRED_FORMAT_LENGTH + ID_SIZE + TraceFlags.getSize();

private static final BinaryTraceContext INSTANCE = new BinaryTraceContext();

public static BinaryTraceContext getInstance() {
return INSTANCE;
}

@Override
public byte[] toByteArray(SpanContext spanContext) {
Utils.checkNotNull(spanContext, "spanContext");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ public class HttpTraceContext implements HttpTextFormat<SpanContext> {
private static final Pattern TRACESTATE_ENTRY_DELIMITER_SPLIT_PATTERN =
Pattern.compile("[ \t]*" + TRACESTATE_ENTRY_DELIMITER + "[ \t]*");

private static final HttpTraceContext INSTANCE = new HttpTraceContext();

public static HttpTraceContext getInstance() {
return INSTANCE;
}

@Override
public List<String> fields() {
return FIELDS;
Expand Down
8 changes: 2 additions & 6 deletions sdk/src/main/java/io/opentelemetry/sdk/trace/TracerSdk.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,10 @@
import io.opentelemetry.trace.Span;
import io.opentelemetry.trace.SpanContext;
import io.opentelemetry.trace.Tracer;
import io.opentelemetry.trace.propagation.BinaryTraceContext;
import io.opentelemetry.trace.propagation.HttpTraceContext;
import io.opentelemetry.trace.unsafe.ContextUtils;

/** {@link TracerSdk} is SDK implementation of {@link Tracer}. */
public final class TracerSdk implements Tracer {
private static final BinaryFormat<SpanContext> BINARY_FORMAT = new BinaryTraceContext();
private static final HttpTextFormat<SpanContext> HTTP_TEXT_FORMAT = new HttpTraceContext();
private final TracerSharedState sharedState;
private final InstrumentationLibraryInfo instrumentationLibraryInfo;

Expand Down Expand Up @@ -67,12 +63,12 @@ public Span.Builder spanBuilder(String spanName) {

@Override
public BinaryFormat<SpanContext> getBinaryFormat() {
return BINARY_FORMAT;
return sharedState.getBinaryFormat();
}

@Override
public HttpTextFormat<SpanContext> getHttpTextFormat() {
return HTTP_TEXT_FORMAT;
return sharedState.getTextFormat();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package io.opentelemetry.sdk.trace;

import io.opentelemetry.context.propagation.BinaryFormat;
import io.opentelemetry.context.propagation.HttpTextFormat;
import io.opentelemetry.internal.Utils;
import io.opentelemetry.sdk.common.Clock;
import io.opentelemetry.sdk.common.InstrumentationLibraryInfo;
Expand All @@ -24,8 +26,11 @@
import io.opentelemetry.sdk.resources.EnvVarResource;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.trace.config.TraceConfig;
import io.opentelemetry.trace.SpanContext;
import io.opentelemetry.trace.Tracer;
import io.opentelemetry.trace.TracerProvider;
import io.opentelemetry.trace.propagation.BinaryTraceContext;
import io.opentelemetry.trace.propagation.HttpTraceContext;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand All @@ -50,8 +55,14 @@ public static Builder builder() {
return new Builder();
}

private TracerSdkProvider(Clock clock, IdsGenerator idsGenerator, Resource resource) {
this.sharedState = new TracerSharedState(clock, idsGenerator, resource);
private TracerSdkProvider(
Clock clock,
IdsGenerator idsGenerator,
Resource resource,
HttpTextFormat<SpanContext> textFormat,
BinaryFormat<SpanContext> binaryFormat) {
this.sharedState =
new TracerSharedState(clock, idsGenerator, resource, textFormat, binaryFormat);
this.tracerSdkComponentRegistry = new TracerSdkComponentRegistry(sharedState);
}

Expand Down Expand Up @@ -124,6 +135,8 @@ public static class Builder {
private Clock clock = MillisClock.getInstance();
private IdsGenerator idsGenerator = new RandomIdsGenerator();
private Resource resource = EnvVarResource.getResource();
private HttpTextFormat<SpanContext> textFormat = HttpTraceContext.getInstance();
private BinaryFormat<SpanContext> binaryFormat = BinaryTraceContext.getInstance();

/**
* Assign a {@link Clock}.
Expand Down Expand Up @@ -163,12 +176,36 @@ public Builder setResource(Resource resource) {
}

/**
* Create a new TracerSdkFactory instance.
* Assign an {@link HttpTextFormat}.
*
* @return An initialized TracerSdkFactory.
* @param textFormat The text format to use for propagation.
* @return this
*/
public Builder setTextFormat(HttpTextFormat<SpanContext> textFormat) {
Utils.checkNotNull(textFormat, "textFormat");
this.textFormat = textFormat;
return this;
}

/**
* Assign a {@link BinaryFormat}.
*
* @param binaryFormat The binary format to use for propagation.
* @return this
*/
public Builder setBinaryFormat(BinaryFormat<SpanContext> binaryFormat) {
Utils.checkNotNull(binaryFormat, "binaryFormat");
this.binaryFormat = binaryFormat;
return this;
}

/**
* Create a new TracerSdkProvider instance.
*
* @return An initialized TracerSdkProvider.
*/
public TracerSdkProvider build() {
return new TracerSdkProvider(clock, idsGenerator, resource);
return new TracerSdkProvider(clock, idsGenerator, resource, textFormat, binaryFormat);
}

private Builder() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@

package io.opentelemetry.sdk.trace;

import io.opentelemetry.context.propagation.BinaryFormat;
import io.opentelemetry.context.propagation.HttpTextFormat;
import io.opentelemetry.sdk.common.Clock;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.trace.config.TraceConfig;
import io.opentelemetry.trace.SpanContext;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.concurrent.GuardedBy;
Expand All @@ -29,6 +32,8 @@ final class TracerSharedState {
private final Clock clock;
private final IdsGenerator idsGenerator;
private final Resource resource;
private final HttpTextFormat<SpanContext> textFormat;
private final BinaryFormat<SpanContext> binaryFormat;

// Reads and writes are atomic for reference variables. Use volatile to ensure that these
// operations are visible on other CPUs as well.
Expand All @@ -39,10 +44,17 @@ final class TracerSharedState {
@GuardedBy("lock")
private final List<SpanProcessor> registeredSpanProcessors = new ArrayList<>();

TracerSharedState(Clock clock, IdsGenerator idsGenerator, Resource resource) {
TracerSharedState(
Clock clock,
IdsGenerator idsGenerator,
Resource resource,
HttpTextFormat<SpanContext> textFormat,
BinaryFormat<SpanContext> binaryFormat) {
this.clock = clock;
this.idsGenerator = idsGenerator;
this.resource = resource;
this.textFormat = textFormat;
this.binaryFormat = binaryFormat;
}

Clock getClock() {
Expand All @@ -57,6 +69,10 @@ Resource getResource() {
return resource;
}

HttpTextFormat<SpanContext> getTextFormat() {
return textFormat;
}

/**
* Returns the active {@code TraceConfig}.
*
Expand Down Expand Up @@ -117,4 +133,8 @@ void stop() {
isStopped = true;
}
}

public BinaryFormat<SpanContext> getBinaryFormat() {
return binaryFormat;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.mock;

import io.opentelemetry.context.propagation.BinaryFormat;
import io.opentelemetry.context.propagation.HttpTextFormat;
import io.opentelemetry.sdk.common.Clock;
import io.opentelemetry.sdk.common.InstrumentationLibraryInfo;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.trace.config.TraceConfig;
import io.opentelemetry.trace.DefaultSpan;
import io.opentelemetry.trace.Span;
import io.opentelemetry.trace.SpanContext;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -80,6 +83,36 @@ public void builder_NullIdsGenerator() {
TracerSdkProvider.builder().setIdsGenerator(null);
}

@Test
public void builder_NullTextFormat() {
thrown.expect(NullPointerException.class);
thrown.expectMessage("textFormat");
TracerSdkProvider.builder().setTextFormat(null);
}

@Test
public void builder_NullBinaryFormat() {
thrown.expect(NullPointerException.class);
thrown.expectMessage("binaryFormat");
TracerSdkProvider.builder().setBinaryFormat(null);
}

@Test
public void builder_setTextFormat() {
@SuppressWarnings("unchecked")
final HttpTextFormat<SpanContext> format = Mockito.mock(HttpTextFormat.class);
final TracerSdkProvider registry = TracerSdkProvider.builder().setTextFormat(format).build();
assertThat(registry.get("test").getHttpTextFormat()).isSameInstanceAs(format);
}

@Test
public void builder_setBinaryFormat() {
@SuppressWarnings("unchecked")
final BinaryFormat<SpanContext> format = Mockito.mock(BinaryFormat.class);
final TracerSdkProvider registry = TracerSdkProvider.builder().setBinaryFormat(format).build();
assertThat(registry.get("test").getBinaryFormat()).isSameInstanceAs(format);
}

@Test
public void defaultGet() {
assertThat(tracerFactory.get("test")).isInstanceOf(TracerSdk.class);
Expand Down