From d18ff00b33537dc402e22efaf1547f3733293594 Mon Sep 17 00:00:00 2001 From: Hailong Wen Date: Mon, 22 Jan 2018 10:23:51 -0800 Subject: Rename trace exporters that have inconsistent naming. (#942) * Rename trace exporters [Logging|Stackdriver|Zipkin]Exporter to [Logging|Stackdriver|Zipkin]TraceExporter. * Add old [Logging|Stackdriver|Zipkin]Exporter back and deprecate them to maintain compatibility. * Implement old trace exporters using renamed ones. --- .../trace/stackdriver/StackdriverExporter.java | 47 ++---- .../stackdriver/StackdriverTraceExporter.java | 160 +++++++++++++++++++++ .../trace/stackdriver/StackdriverExporterTest.java | 53 ------- .../stackdriver/StackdriverTraceExporterTest.java | 53 +++++++ 4 files changed, 222 insertions(+), 91 deletions(-) create mode 100644 exporters/trace/stackdriver/src/main/java/io/opencensus/exporter/trace/stackdriver/StackdriverTraceExporter.java delete mode 100644 exporters/trace/stackdriver/src/test/java/io/opencensus/exporter/trace/stackdriver/StackdriverExporterTest.java create mode 100644 exporters/trace/stackdriver/src/test/java/io/opencensus/exporter/trace/stackdriver/StackdriverTraceExporterTest.java (limited to 'exporters/trace/stackdriver') diff --git a/exporters/trace/stackdriver/src/main/java/io/opencensus/exporter/trace/stackdriver/StackdriverExporter.java b/exporters/trace/stackdriver/src/main/java/io/opencensus/exporter/trace/stackdriver/StackdriverExporter.java index efe1c85b..2a77541f 100644 --- a/exporters/trace/stackdriver/src/main/java/io/opencensus/exporter/trace/stackdriver/StackdriverExporter.java +++ b/exporters/trace/stackdriver/src/main/java/io/opencensus/exporter/trace/stackdriver/StackdriverExporter.java @@ -16,18 +16,13 @@ package io.opencensus.exporter.trace.stackdriver; -import static com.google.common.base.Preconditions.checkState; - import com.google.auth.Credentials; import com.google.auth.oauth2.GoogleCredentials; import com.google.cloud.ServiceOptions; import com.google.common.annotations.VisibleForTesting; -import io.opencensus.trace.Tracing; import io.opencensus.trace.export.SpanExporter; import io.opencensus.trace.export.SpanExporter.Handler; import java.io.IOException; -import javax.annotation.Nullable; -import javax.annotation.concurrent.GuardedBy; /** * An OpenCensus span exporter implementation which exports data to Stackdriver Trace. @@ -40,16 +35,12 @@ import javax.annotation.concurrent.GuardedBy; * ... // Do work. * } * } + * + * @deprecated Deprecated due to inconsistent naming. Use {@link StackdriverTraceExporter}. */ +@Deprecated public final class StackdriverExporter { - private static final String REGISTER_NAME = StackdriverExporter.class.getName(); - private static final Object monitor = new Object(); - - @GuardedBy("monitor") - @Nullable - private static Handler handler = null; - /** * Creates and registers the Stackdriver Trace exporter to the OpenCensus library for an explicit * project ID and using explicit credentials. Only one Stackdriver exporter can be registered at @@ -61,10 +52,7 @@ public final class StackdriverExporter { */ public static void createAndRegisterWithCredentialsAndProjectId( Credentials credentials, String projectId) throws IOException { - synchronized (monitor) { - checkState(handler == null, "Stackdriver exporter is already registered."); - registerInternal(StackdriverV2ExporterHandler.createWithCredentials(credentials, projectId)); - } + StackdriverTraceExporter.createAndRegisterWithCredentialsAndProjectId(credentials, projectId); } /** @@ -85,10 +73,7 @@ public final class StackdriverExporter { * @throws IllegalStateException if a Stackdriver exporter is already registered. */ public static void createAndRegisterWithProjectId(String projectId) throws IOException { - synchronized (monitor) { - checkState(handler == null, "Stackdriver exporter is already registered."); - registerInternal(StackdriverV2ExporterHandler.create(projectId)); - } + StackdriverTraceExporter.createAndRegisterWithProjectId(projectId); } /** @@ -109,17 +94,7 @@ public final class StackdriverExporter { * @throws IllegalStateException if a Stackdriver exporter is already registered. */ public static void createAndRegister() throws IOException { - synchronized (monitor) { - checkState(handler == null, "Stackdriver exporter is already registered."); - registerInternal(StackdriverV2ExporterHandler.create(ServiceOptions.getDefaultProjectId())); - } - } - - private static void registerInternal(Handler newHandler) { - synchronized (monitor) { - handler = newHandler; - register(Tracing.getExportComponent().getSpanExporter(), newHandler); - } + StackdriverTraceExporter.createAndRegister(); } /** @@ -129,7 +104,7 @@ public final class StackdriverExporter { */ @VisibleForTesting static void register(SpanExporter spanExporter, Handler handler) { - spanExporter.registerHandler(REGISTER_NAME, handler); + StackdriverTraceExporter.register(spanExporter, handler); } /** @@ -138,11 +113,7 @@ public final class StackdriverExporter { * @throws IllegalStateException if a Stackdriver exporter is not registered. */ public static void unregister() { - synchronized (monitor) { - checkState(handler != null, "Stackdriver exporter is not registered."); - unregister(Tracing.getExportComponent().getSpanExporter()); - handler = null; - } + StackdriverTraceExporter.unregister(); } /** @@ -153,7 +124,7 @@ public final class StackdriverExporter { */ @VisibleForTesting static void unregister(SpanExporter spanExporter) { - spanExporter.unregisterHandler(REGISTER_NAME); + StackdriverTraceExporter.unregister(spanExporter); } private StackdriverExporter() {} diff --git a/exporters/trace/stackdriver/src/main/java/io/opencensus/exporter/trace/stackdriver/StackdriverTraceExporter.java b/exporters/trace/stackdriver/src/main/java/io/opencensus/exporter/trace/stackdriver/StackdriverTraceExporter.java new file mode 100644 index 00000000..6ad17c66 --- /dev/null +++ b/exporters/trace/stackdriver/src/main/java/io/opencensus/exporter/trace/stackdriver/StackdriverTraceExporter.java @@ -0,0 +1,160 @@ +/* + * Copyright 2017, OpenCensus Authors + * + * 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 io.opencensus.exporter.trace.stackdriver; + +import static com.google.common.base.Preconditions.checkState; + +import com.google.auth.Credentials; +import com.google.auth.oauth2.GoogleCredentials; +import com.google.cloud.ServiceOptions; +import com.google.common.annotations.VisibleForTesting; +import io.opencensus.trace.Tracing; +import io.opencensus.trace.export.SpanExporter; +import io.opencensus.trace.export.SpanExporter.Handler; +import java.io.IOException; +import javax.annotation.Nullable; +import javax.annotation.concurrent.GuardedBy; + +/** + * An OpenCensus span exporter implementation which exports data to Stackdriver Trace. + * + *

Example of usage on Google Cloud VMs: + * + *

{@code
+ * public static void main(String[] args) {
+ *   StackdriverTraceExporter.createAndRegisterWithProjectId("MyStackdriverProjectId");
+ *   ... // Do work.
+ * }
+ * }
+ */ +public final class StackdriverTraceExporter { + + private static final String REGISTER_NAME = StackdriverTraceExporter.class.getName(); + private static final Object monitor = new Object(); + + @GuardedBy("monitor") + @Nullable + private static Handler handler = null; + + /** + * Creates and registers the Stackdriver Trace exporter to the OpenCensus library for an explicit + * project ID and using explicit credentials. Only one Stackdriver exporter can be registered at + * any point. + * + * @param credentials a credentials used to authenticate API calls. + * @param projectId the cloud project id. + * @throws IllegalStateException if a Stackdriver exporter is already registered. + */ + public static void createAndRegisterWithCredentialsAndProjectId( + Credentials credentials, String projectId) throws IOException { + synchronized (monitor) { + checkState(handler == null, "Stackdriver exporter is already registered."); + registerInternal(StackdriverV2ExporterHandler.createWithCredentials(credentials, projectId)); + } + } + + /** + * Creates and registers the Stackdriver Trace exporter to the OpenCensus library for an explicit + * project ID. Only one Stackdriver exporter can be registered at any point. + * + *

This uses the default application credentials see {@link + * GoogleCredentials#getApplicationDefault}. + * + *

This is equivalent with: + * + *

{@code
+   * StackdriverExporter.createAndRegisterWithCredentialsAndProjectId(
+   *     GoogleCredentials.getApplicationDefault(), projectId);
+   * }
+ * + * @param projectId the cloud project id. + * @throws IllegalStateException if a Stackdriver exporter is already registered. + */ + public static void createAndRegisterWithProjectId(String projectId) throws IOException { + synchronized (monitor) { + checkState(handler == null, "Stackdriver exporter is already registered."); + registerInternal(StackdriverV2ExporterHandler.create(projectId)); + } + } + + /** + * Creates and registers the Stackdriver Trace exporter to the OpenCensus library. Only one + * Stackdriver exporter can be registered at any point. + * + *

This uses the default application credentials see {@link + * GoogleCredentials#getApplicationDefault}. + * + *

This uses the default project ID configured see {@link ServiceOptions#getDefaultProjectId}. + * + *

This is equivalent with: + * + *

{@code
+   * StackdriverExporter.createAndRegisterWithProjectId(ServiceOptions.getDefaultProjectId());
+   * }
+ * + * @throws IllegalStateException if a Stackdriver exporter is already registered. + */ + public static void createAndRegister() throws IOException { + synchronized (monitor) { + checkState(handler == null, "Stackdriver exporter is already registered."); + registerInternal(StackdriverV2ExporterHandler.create(ServiceOptions.getDefaultProjectId())); + } + } + + private static void registerInternal(Handler newHandler) { + synchronized (monitor) { + handler = newHandler; + register(Tracing.getExportComponent().getSpanExporter(), newHandler); + } + } + + /** + * Registers the {@code StackdriverExporter}. + * + * @param spanExporter the instance of the {@code SpanExporter} where this service is registered. + */ + @VisibleForTesting + static void register(SpanExporter spanExporter, Handler handler) { + spanExporter.registerHandler(REGISTER_NAME, handler); + } + + /** + * Unregisters the Stackdriver Trace exporter from the OpenCensus library. + * + * @throws IllegalStateException if a Stackdriver exporter is not registered. + */ + public static void unregister() { + synchronized (monitor) { + checkState(handler != null, "Stackdriver exporter is not registered."); + unregister(Tracing.getExportComponent().getSpanExporter()); + handler = null; + } + } + + /** + * Unregisters the {@code StackdriverExporter}. + * + * @param spanExporter the instance of the {@code SpanExporter} from where this service is + * unregistered. + */ + @VisibleForTesting + static void unregister(SpanExporter spanExporter) { + spanExporter.unregisterHandler(REGISTER_NAME); + } + + private StackdriverTraceExporter() {} +} diff --git a/exporters/trace/stackdriver/src/test/java/io/opencensus/exporter/trace/stackdriver/StackdriverExporterTest.java b/exporters/trace/stackdriver/src/test/java/io/opencensus/exporter/trace/stackdriver/StackdriverExporterTest.java deleted file mode 100644 index 71790586..00000000 --- a/exporters/trace/stackdriver/src/test/java/io/opencensus/exporter/trace/stackdriver/StackdriverExporterTest.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright 2017, OpenCensus Authors - * - * 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 io.opencensus.exporter.trace.stackdriver; - -import static org.mockito.Matchers.eq; -import static org.mockito.Matchers.same; -import static org.mockito.Mockito.verify; - -import io.opencensus.trace.export.SpanExporter; -import io.opencensus.trace.export.SpanExporter.Handler; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; - -/** Unit tests for {@link StackdriverExporter}. */ -@RunWith(JUnit4.class) -public class StackdriverExporterTest { - @Mock private SpanExporter spanExporter; - @Mock private Handler handler; - - @Before - public void setUp() { - MockitoAnnotations.initMocks(this); - } - - @Test - public void registerUnregisterStackdriverExporter() { - StackdriverExporter.register(spanExporter, handler); - verify(spanExporter) - .registerHandler( - eq("io.opencensus.exporter.trace.stackdriver.StackdriverExporter"), same(handler)); - StackdriverExporter.unregister(spanExporter); - verify(spanExporter) - .unregisterHandler(eq("io.opencensus.exporter.trace.stackdriver.StackdriverExporter")); - } -} diff --git a/exporters/trace/stackdriver/src/test/java/io/opencensus/exporter/trace/stackdriver/StackdriverTraceExporterTest.java b/exporters/trace/stackdriver/src/test/java/io/opencensus/exporter/trace/stackdriver/StackdriverTraceExporterTest.java new file mode 100644 index 00000000..6a12a899 --- /dev/null +++ b/exporters/trace/stackdriver/src/test/java/io/opencensus/exporter/trace/stackdriver/StackdriverTraceExporterTest.java @@ -0,0 +1,53 @@ +/* + * Copyright 2017, OpenCensus Authors + * + * 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 io.opencensus.exporter.trace.stackdriver; + +import static org.mockito.Matchers.eq; +import static org.mockito.Matchers.same; +import static org.mockito.Mockito.verify; + +import io.opencensus.trace.export.SpanExporter; +import io.opencensus.trace.export.SpanExporter.Handler; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +/** Unit tests for {@link StackdriverTraceExporter}. */ +@RunWith(JUnit4.class) +public class StackdriverTraceExporterTest { + @Mock private SpanExporter spanExporter; + @Mock private Handler handler; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + } + + @Test + public void registerUnregisterStackdriverExporter() { + StackdriverTraceExporter.register(spanExporter, handler); + verify(spanExporter) + .registerHandler( + eq("io.opencensus.exporter.trace.stackdriver.StackdriverTraceExporter"), same(handler)); + StackdriverTraceExporter.unregister(spanExporter); + verify(spanExporter) + .unregisterHandler(eq("io.opencensus.exporter.trace.stackdriver.StackdriverTraceExporter")); + } +} -- cgit v1.2.3