diff --git a/api/src/main/java/io/opentelemetry/OpenTelemetry.java b/api/src/main/java/io/opentelemetry/OpenTelemetry.java index 74049c8e8d3..cbce580e7c4 100644 --- a/api/src/main/java/io/opentelemetry/OpenTelemetry.java +++ b/api/src/main/java/io/opentelemetry/OpenTelemetry.java @@ -22,9 +22,11 @@ import io.opentelemetry.metrics.DefaultMeter; import io.opentelemetry.metrics.Meter; import io.opentelemetry.metrics.spi.MeterProvider; -import io.opentelemetry.trace.DefaultTracer; +import io.opentelemetry.trace.DefaultTracerFactory; +import io.opentelemetry.trace.DefaultTracerFactoryProvider; import io.opentelemetry.trace.Tracer; -import io.opentelemetry.trace.spi.TracerProvider; +import io.opentelemetry.trace.TracerFactory; +import io.opentelemetry.trace.spi.TracerFactoryProvider; import java.util.ServiceLoader; import javax.annotation.Nullable; import javax.annotation.concurrent.ThreadSafe; @@ -35,7 +37,7 @@ * *
The telemetry objects are lazy-loaded singletons resolved via {@link ServiceLoader} mechanism.
*
- * @see TracerProvider
+ * @see TracerFactory
* @see MeterProvider
* @see DistributedContextManagerProvider
*/
@@ -44,19 +46,20 @@ public final class OpenTelemetry {
@Nullable private static volatile OpenTelemetry instance;
- private final Tracer tracer;
+ private final TracerFactory tracerFactory;
private final Meter meter;
private final DistributedContextManager contextManager;
/**
- * Returns a singleton {@link Tracer}.
+ * Returns a singleton {@link TracerFactory}.
*
- * @return registered tracer or default via {@link DefaultTracer#getInstance()}.
- * @throws IllegalStateException if a specified tracer (via system properties) could not be found.
+ * @return registered TracerFactory of default via {@link DefaultTracerFactory#getInstance()}.
+ * @throws IllegalStateException if a specified TracerFactory (via system properties) could not be
+ * found.
* @since 0.1.0
*/
- public static Tracer getTracer() {
- return getInstance().tracer;
+ public static TracerFactory getTracerFactory() {
+ return getInstance().tracerFactory;
}
/**
@@ -83,6 +86,35 @@ public static DistributedContextManager getDistributedContextManager() {
return getInstance().contextManager;
}
+ /** Lazy loads an instance. */
+ private static OpenTelemetry getInstance() {
+ if (instance == null) {
+ synchronized (OpenTelemetry.class) {
+ if (instance == null) {
+ instance = new OpenTelemetry();
+ }
+ }
+ }
+ return instance;
+ }
+
+ private OpenTelemetry() {
+ TracerFactoryProvider tracerFactoryProvider = loadSpi(TracerFactoryProvider.class);
+ this.tracerFactory =
+ tracerFactoryProvider != null
+ ? tracerFactoryProvider.create()
+ : DefaultTracerFactoryProvider.getInstance().create();
+
+ MeterProvider meterProvider = loadSpi(MeterProvider.class);
+ meter = meterProvider != null ? meterProvider.create() : DefaultMeter.getInstance();
+ DistributedContextManagerProvider contextManagerProvider =
+ loadSpi(DistributedContextManagerProvider.class);
+ contextManager =
+ contextManagerProvider != null
+ ? contextManagerProvider.create()
+ : DefaultDistributedContextManager.getInstance();
+ }
+
/**
* Load provider class via {@link ServiceLoader}. A specific provider class can be requested via
* setting a system property with FQCN.
@@ -108,31 +140,6 @@ private static This class is not intended to be used in application code and it is used only by {@link
+ * io.opentelemetry.OpenTelemetry}.
+ */
+public class TracerSdkFactory implements TracerFactory {
+ private final Map This class is not intended to be used in application code and it is used only by {@link
- * io.opentelemetry.OpenTelemetry}.
- */
-public class TracerSdkProvider implements io.opentelemetry.trace.spi.TracerProvider {
+import io.opentelemetry.trace.TracerFactory;
+import io.opentelemetry.trace.spi.TracerFactoryProvider;
+/** SDK implementation of the TracerProviderFactory for SPI. */
+public class TracerSdkFactoryProvider implements TracerFactoryProvider {
@Override
- public Tracer create() {
- return new TracerSdk();
+ public TracerFactory create() {
+ return new TracerSdkFactory();
}
}
diff --git a/sdk/src/main/resources/META-INF/services/io.opentelemetry.trace.spi.TracerFactoryProvider b/sdk/src/main/resources/META-INF/services/io.opentelemetry.trace.spi.TracerFactoryProvider
new file mode 100644
index 00000000000..6be0bc33c27
--- /dev/null
+++ b/sdk/src/main/resources/META-INF/services/io.opentelemetry.trace.spi.TracerFactoryProvider
@@ -0,0 +1 @@
+io.opentelemetry.sdk.trace.TracerSdkFactoryProvider
diff --git a/sdk/src/main/resources/META-INF/services/io.opentelemetry.trace.spi.TracerProvider b/sdk/src/main/resources/META-INF/services/io.opentelemetry.trace.spi.TracerProvider
deleted file mode 100644
index c7d60538e5e..00000000000
--- a/sdk/src/main/resources/META-INF/services/io.opentelemetry.trace.spi.TracerProvider
+++ /dev/null
@@ -1 +0,0 @@
-io.opentelemetry.sdk.trace.TracerSdkProvider
diff --git a/sdk/src/test/java/io/opentelemetry/sdk/OpenTelemetrySdkTest.java b/sdk/src/test/java/io/opentelemetry/sdk/OpenTelemetrySdkTest.java
index d4391c0b5a8..87c543edfb8 100644
--- a/sdk/src/test/java/io/opentelemetry/sdk/OpenTelemetrySdkTest.java
+++ b/sdk/src/test/java/io/opentelemetry/sdk/OpenTelemetrySdkTest.java
@@ -28,7 +28,8 @@ public class OpenTelemetrySdkTest {
@Test
public void testDefault() {
- assertThat(OpenTelemetrySdk.getTracer()).isSameInstanceAs(OpenTelemetry.getTracer());
+ assertThat(OpenTelemetrySdk.getTracerFactory())
+ .isSameInstanceAs(OpenTelemetry.getTracerFactory());
assertThat(OpenTelemetrySdk.getDistributedContextManager())
.isSameInstanceAs(OpenTelemetry.getDistributedContextManager());
assertThat(OpenTelemetrySdk.getMeter()).isSameInstanceAs(OpenTelemetry.getMeter());
diff --git a/sdk/src/test/java/io/opentelemetry/sdk/trace/TracerSdkProviderTest.java b/sdk/src/test/java/io/opentelemetry/sdk/trace/TracerSdkProviderTest.java
index c0431b979d8..b92088bc411 100644
--- a/sdk/src/test/java/io/opentelemetry/sdk/trace/TracerSdkProviderTest.java
+++ b/sdk/src/test/java/io/opentelemetry/sdk/trace/TracerSdkProviderTest.java
@@ -27,6 +27,6 @@ public class TracerSdkProviderTest {
@Test
public void testDefault() {
- assertThat(new TracerSdkProvider().create()).isInstanceOf(TracerSdk.class);
+ assertThat(new TracerSdkFactory().get("testInstrumentation")).isInstanceOf(TracerSdk.class);
}
}
+ * TracerFactoryProvider is a service provider for a {@link TracerFactory}. Fully qualified class
+ * name of the implementation should be registered in {@code
+ * META-INF/services/io.opentelemetry.trace.spi.TracerFactoryProvider}.
*
* A specific implementation can be selected by a system property {@code
- * io.opentelemetry.trace.spi.TracerProvider} with value of fully qualified class name.
+ * io.opentelemetry.trace.spi.TracerFactoryProvider} with value of fully qualified class name.
*
* @see io.opentelemetry.OpenTelemetry
*/
@ThreadSafe
-public interface TracerProvider {
+public interface TracerFactoryProvider {
/**
- * Creates a new tracer instance.
+ * Creates a new TracerFactory.
*
- * @return a tracer instance.
+ * @return a new TracerFactory.
* @since 0.1.0
*/
- Tracer create();
+ TracerFactory create();
}
diff --git a/api/src/test/java/io/opentelemetry/OpenTelemetryTest.java b/api/src/test/java/io/opentelemetry/OpenTelemetryTest.java
index d05f24e4631..7c64e7c3700 100644
--- a/api/src/test/java/io/opentelemetry/OpenTelemetryTest.java
+++ b/api/src/test/java/io/opentelemetry/OpenTelemetryTest.java
@@ -42,7 +42,8 @@
import io.opentelemetry.trace.Span;
import io.opentelemetry.trace.SpanContext;
import io.opentelemetry.trace.Tracer;
-import io.opentelemetry.trace.spi.TracerProvider;
+import io.opentelemetry.trace.TracerFactory;
+import io.opentelemetry.trace.spi.TracerFactoryProvider;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
@@ -70,15 +71,17 @@ public static void beforeClass() {
@After
public void after() {
OpenTelemetry.reset();
- System.clearProperty(TracerProvider.class.getName());
+ System.clearProperty(TracerFactoryProvider.class.getName());
System.clearProperty(MeterProvider.class.getName());
System.clearProperty(DistributedContextManagerProvider.class.getName());
}
@Test
public void testDefault() {
- assertThat(OpenTelemetry.getTracer()).isInstanceOf(DefaultTracer.getInstance().getClass());
- assertThat(OpenTelemetry.getTracer()).isEqualTo(OpenTelemetry.getTracer());
+ assertThat(OpenTelemetry.getTracerFactory().get("testTracer"))
+ .isInstanceOf(DefaultTracer.getInstance().getClass());
+ assertThat(OpenTelemetry.getTracerFactory().get("testTracer"))
+ .isEqualTo(OpenTelemetry.getTracerFactory().get("testTracer"));
assertThat(OpenTelemetry.getMeter()).isInstanceOf(DefaultMeter.getInstance().getClass());
assertThat(OpenTelemetry.getMeter()).isEqualTo(OpenTelemetry.getMeter());
assertThat(OpenTelemetry.getDistributedContextManager())
@@ -89,12 +92,13 @@ public void testDefault() {
@Test
public void testTracerLoadArbitrary() throws IOException {
- File serviceFile = createService(TracerProvider.class, FirstTracer.class, SecondTracer.class);
+ File serviceFile =
+ createService(
+ TracerFactoryProvider.class, FirstTracerFactory.class, SecondTracerFactory.class);
try {
assertTrue(
- (OpenTelemetry.getTracer() instanceof FirstTracer)
- || (OpenTelemetry.getTracer() instanceof SecondTracer));
- assertThat(OpenTelemetry.getTracer()).isEqualTo(OpenTelemetry.getTracer());
+ (OpenTelemetry.getTracerFactory() instanceof FirstTracerFactory)
+ || (OpenTelemetry.getTracerFactory() instanceof SecondTracerFactory));
} finally {
serviceFile.delete();
}
@@ -102,11 +106,12 @@ public void testTracerLoadArbitrary() throws IOException {
@Test
public void testTracerSystemProperty() throws IOException {
- File serviceFile = createService(TracerProvider.class, FirstTracer.class, SecondTracer.class);
- System.setProperty(TracerProvider.class.getName(), SecondTracer.class.getName());
+ File serviceFile =
+ createService(
+ TracerFactoryProvider.class, FirstTracerFactory.class, SecondTracerFactory.class);
+ System.setProperty(TracerFactoryProvider.class.getName(), SecondTracerFactory.class.getName());
try {
- assertThat(OpenTelemetry.getTracer()).isInstanceOf(SecondTracer.class);
- assertThat(OpenTelemetry.getTracer()).isEqualTo(OpenTelemetry.getTracer());
+ assertThat(OpenTelemetry.getTracerFactory()).isInstanceOf(SecondTracerFactory.class);
} finally {
serviceFile.delete();
}
@@ -114,9 +119,9 @@ public void testTracerSystemProperty() throws IOException {
@Test
public void testTracerNotFound() {
- System.setProperty(TracerProvider.class.getName(), "io.does.not.exists");
+ System.setProperty(TracerFactoryProvider.class.getName(), "io.does.not.exists");
thrown.expect(IllegalStateException.class);
- OpenTelemetry.getTracer();
+ OpenTelemetry.getTracerFactory().get("testTracer");
}
@Test
@@ -212,17 +217,32 @@ private static File createService(Class> service, Class>... impls) throws IO
return file;
}
- public static class SecondTracer extends FirstTracer {
+ public static class SecondTracerFactory extends FirstTracerFactory {
+ @Override
+ public Tracer get(String instrumentationName) {
+ return new SecondTracerFactory();
+ }
+
+ @Override
+ public Tracer get(String instrumentationName, String instrumentationVersion) {
+ return get(instrumentationName);
+ }
+
@Override
- public Tracer create() {
- return new SecondTracer();
+ public TracerFactory create() {
+ return new SecondTracerFactory();
}
}
- public static class FirstTracer implements Tracer, TracerProvider {
+ public static class FirstTracerFactory implements Tracer, TracerFactory, TracerFactoryProvider {
+ @Override
+ public Tracer get(String instrumentationName) {
+ return new FirstTracerFactory();
+ }
+
@Override
- public Tracer create() {
- return new FirstTracer();
+ public Tracer get(String instrumentationName, String instrumentationVersion) {
+ return get(instrumentationName);
}
@Nullable
@@ -254,6 +274,11 @@ public BinaryFormat