2020
2121import com .google .auth .Credentials ;
2222import com .google .auth .oauth2 .GoogleCredentials ;
23+ import com .google .cloud .ServiceOptions ;
2324import com .google .common .annotations .VisibleForTesting ;
2425import io .opencensus .trace .Tracing ;
2526import io .opencensus .trace .export .SpanExporter ;
2627import io .opencensus .trace .export .SpanExporter .Handler ;
2728import java .io .IOException ;
28- import java .util .Collections ;
29- import java .util .List ;
3029import javax .annotation .concurrent .GuardedBy ;
3130
3231/**
3635 *
3736 * <pre><code>
3837 * public static void main(String[] args) {
39- * StackdriverExporter.createAndRegister ("MyStackdriverProjectId");
38+ * StackdriverExporter.createAndRegisterWithProjectId ("MyStackdriverProjectId");
4039 * ... // Do work.
4140 * }
4241 * </code></pre>
4342 */
4443public final class StackdriverExporter {
44+
4545 private static final String REGISTER_NAME = StackdriverExporter .class .getName ();
46- private static final List <String > STACKDRIVER_TRACE_WRITER_SCOPE =
47- Collections .singletonList ("https://www.googleapis.com/auth/trace.append" );
4846 private static final Object monitor = new Object ();
4947
5048 @ GuardedBy ("monitor" )
5149 private static Handler handler = null ;
5250
5351 /**
54- * Creates and registers the Stackdriver Trace exporter to the OpenCensus library. Only one
55- * Stackdriver exporter can be registered at any point.
52+ * Creates and registers the Stackdriver Trace exporter to the OpenCensus library for an explicit
53+ * project ID and using explicit credentials. Only one Stackdriver exporter can be registered at
54+ * any point.
5655 *
5756 * @param credentials a credentials used to authenticate API calls.
5857 * @param projectId the cloud project id.
5958 * @throws IllegalStateException if a Stackdriver exporter is already registered.
6059 */
61- public static void createAndRegisterWithCredentials (Credentials credentials , String projectId )
60+ public static void createAndRegisterWithCredentialsAndProjectId (Credentials credentials ,
61+ String projectId )
6262 throws IOException {
6363 synchronized (monitor ) {
6464 checkState (handler == null , "Stackdriver exporter is already registered." );
@@ -68,24 +68,55 @@ public static void createAndRegisterWithCredentials(Credentials credentials, Str
6868 }
6969
7070 /**
71- * Creates and registers the Stackdriver Trace exporter to the OpenCensus library. Only one
72- * Stackdriver exporter can be registered at any point.
71+ * Creates and registers the Stackdriver Trace exporter to the OpenCensus library for an explicit
72+ * project ID. Only one Stackdriver exporter can be registered at any point.
7373 *
7474 * <p>This uses the default application credentials see {@link
75- * GoogleCredentials#getApplicationDefault()}. If you do not have default application credentials
76- * configured use {@link #createAndRegisterWithCredentials(Credentials, String)}.
75+ * GoogleCredentials#getApplicationDefault}.
76+ *
77+ * <p>This is equivalent with:
78+ *
79+ * <pre>{@code
80+ * StackdriverExporter.createAndRegisterWithCredentialsAndProjectId(
81+ * GoogleCredentials.getApplicationDefault(), projectId);
82+ * }</pre>
7783 *
7884 * @param projectId the cloud project id.
7985 * @throws IllegalStateException if a Stackdriver exporter is already registered.
8086 */
81- public static void createAndRegister (String projectId ) throws IOException {
87+ public static void createAndRegisterWithProjectId (String projectId ) throws IOException {
8288 synchronized (monitor ) {
8389 checkState (handler == null , "Stackdriver exporter is already registered." );
8490 handler = StackdriverV1ExporterHandler .create (projectId );
8591 register (Tracing .getExportComponent ().getSpanExporter (), handler );
8692 }
8793 }
8894
95+ /**
96+ * Creates and registers the Stackdriver Trace exporter to the OpenCensus library. Only one
97+ * Stackdriver exporter can be registered at any point.
98+ *
99+ * <p>This uses the default application credentials see {@link
100+ * GoogleCredentials#getApplicationDefault}.
101+ *
102+ * <p>This uses the default project ID configured see {@link ServiceOptions#getDefaultProjectId}.
103+ *
104+ * <p>This is equivalent with:
105+ *
106+ * <pre>{@code
107+ * StackdriverExporter.createAndRegisterWithProjectId(ServiceOptions.getDefaultProjectId());
108+ * }</pre>
109+ *
110+ * @throws IllegalStateException if a Stackdriver exporter is already registered.
111+ */
112+ public static void createAndRegister () throws IOException {
113+ synchronized (monitor ) {
114+ checkState (handler == null , "Stackdriver exporter is already registered." );
115+ handler = StackdriverV1ExporterHandler .create (ServiceOptions .getDefaultProjectId ());
116+ register (Tracing .getExportComponent ().getSpanExporter (), handler );
117+ }
118+ }
119+
89120 /**
90121 * Registers the {@code StackdriverExporter}.
91122 *
0 commit comments