Skip to content
This repository was archived by the owner on Dec 23, 2023. It is now read-only.

Commit 821b15f

Browse files
author
Bogdan Drutu
authored
Add an API that does not require user to write a project_id. (#589)
* Add an API that does not require user to write a project_id. * Resolve comments. * Fix javadoc example.
1 parent e8e4d4b commit 821b15f

File tree

3 files changed

+76
-24
lines changed

3 files changed

+76
-24
lines changed

exporters/trace_stackdriver/README.md

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,6 @@ In order to be able to push your traces to [Stackdriver Trace][stackdriver-trace
2222

2323
These steps enable the API but don't require that your app is hosted on Google Cloud Platform.
2424

25-
### Authentication
26-
27-
This exporter uses [google-cloud-java](https://github.com/GoogleCloudPlatform/google-cloud-java),
28-
for details see [here](https://github.com/GoogleCloudPlatform/google-cloud-java#authentication).
29-
30-
### Java Versions
31-
32-
Java 7 or above is required for using this exporter.
33-
3425
### Hello "Stackdriver Trace"
3526

3627
#### Add the dependencies to your project
@@ -53,21 +44,50 @@ For Maven add to your `pom.xml`:
5344
```
5445

5546
For Gradle add to your dependencies:
56-
```gradle
47+
```groovy
5748
compile 'io.opencensus:opencensus-exporter-trace-stackdriver:0.6.0'
5849
runtime 'io.opencensus:opencensus-impl:0.6.0'
5950
```
6051

6152
#### Register the exporter
53+
54+
This uses the default configuration for authentication and project ID.
55+
6256
```java
6357
public class MyMainClass {
6458
public static void main(String[] args) throws Exception {
65-
StackdriverExporter.createAndRegister("MyStackdriverProjectId");
59+
StackdriverExporter.createAndRegister();
6660
// ...
6761
}
6862
}
6963
```
7064

65+
#### Authentication
66+
67+
This exporter uses [google-cloud-java](https://github.com/GoogleCloudPlatform/google-cloud-java),
68+
for details about how to configure the authentication see [here](https://github.com/GoogleCloudPlatform/google-cloud-java#authentication).
69+
70+
If you prefer to manually set the credentials use:
71+
```
72+
StackdriverExporter.createAndRegisterWithCredentialsAndProjectId(
73+
new GoogleCredentials(new AccessToken(accessToken, expirationTime)),
74+
"MyStackdriverProjectId");
75+
```
76+
77+
#### Specifying a Project ID
78+
79+
This exporter uses [google-cloud-java](https://github.com/GoogleCloudPlatform/google-cloud-java),
80+
for details about how to configure the project ID see [here](https://github.com/GoogleCloudPlatform/google-cloud-java#specifying-a-project-id).
81+
82+
If you prefer to manually set the project ID use:
83+
```
84+
StackdriverExporter.createAndRegisterWithProjectId("MyStackdriverProjectId");
85+
```
86+
87+
#### Java Versions
88+
89+
Java 7 or above is required for using this exporter.
90+
7191
## FAQ
7292
### Why do I not see some trace events in Stackdriver?
7393
The current Stackdriver Trace exporter is implemented using the [v1 API][stackdriver-v1-api-url]

exporters/trace_stackdriver/src/main/java/io/opencensus/exporter/trace/stackdriver/StackdriverExporter.java

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,12 @@
2020

2121
import com.google.auth.Credentials;
2222
import com.google.auth.oauth2.GoogleCredentials;
23+
import com.google.cloud.ServiceOptions;
2324
import com.google.common.annotations.VisibleForTesting;
2425
import io.opencensus.trace.Tracing;
2526
import io.opencensus.trace.export.SpanExporter;
2627
import io.opencensus.trace.export.SpanExporter.Handler;
2728
import java.io.IOException;
28-
import java.util.Collections;
29-
import java.util.List;
3029
import javax.annotation.concurrent.GuardedBy;
3130

3231
/**
@@ -36,29 +35,30 @@
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
*/
4443
public 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
*

exporters/trace_stackdriver/src/main/java/io/opencensus/exporter/trace/stackdriver/StackdriverV1ExporterHandler.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
* messages.
4949
*/
5050
final class StackdriverV1ExporterHandler extends SpanExporter.Handler {
51+
5152
private static final String STATUS_CODE = "g.co/status/code";
5253
private static final String STATUS_DESCRIPTION = "g.co/status/description";
5354

0 commit comments

Comments
 (0)