Skip to content

Commit 86ea1b3

Browse files
committed
Refresh credentials and add readme
1 parent f337fe5 commit 86ea1b3

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

examples/otlptrace/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# OTLP Trace with Google Auth Example
2+
3+
Run this sample to connect to an endpoint that is protected by GCP authentication.
4+
5+
First, get GCP credentials on your machine:
6+
7+
```shell
8+
gcloud auth application-default login
9+
```
10+
Executing this command will save your application credentials to default path which will depend on the type of machine -
11+
- Linux, macOS: `$HOME/.config/gcloud/application_default_credentials.json`
12+
- Windows: `%APPDATA%\gcloud\application_default_credentials.json`
13+
14+
Next, set your endpoint with the `OTEL_EXPORTER_OTLP_ENDPOINT` environment variable:
15+
16+
```shell
17+
export OTEL_EXPORTER_OTLP_ENDPOINT="http://your-endpoint:port"
18+
```
19+
20+
Next, update [`build.gradle`](build.grade) to set the following:
21+
22+
```
23+
'-Dotel.resource.attributes=gcp.project_id=<YOUR_PROJECT_ID>,
24+
'-Dotel.exporter.otlp.headers=X-Goog-User-Project=<YOUR_QUOTA_PROJECT>',
25+
```
26+
27+
Finally, run `gradle run` to run the sample.

examples/otlptrace/build.gradle

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@ dependencies {
3333
// Provide headers from env variable
3434
// export OTEL_EXPORTER_OTLP_ENDPOINT="http://path/to/yourendpoint:port"
3535
def autoconf_config = [
36-
'-Dotel.resource.attributes=gcp.project_id=your-gcp-project',
37-
'-Dotel.exporter.otlp.headers=X-Goog-User-Project=your-gcp-project',
36+
'-Dotel.resource.attributes=gcp.project_id=<YOUR_PROJECT>',
37+
'-Dotel.exporter.otlp.headers=X-Goog-User-Project=<YOUR_QUOTA_PROJECT>',
3838
'-Dotel.traces.exporter=otlp',
39+
'-Dotel.exporter.otlp.protocol=http/protobuf',
3940
'-Dotel.java.global-autoconfigure.enabled=true',
4041
]
4142

examples/otlptrace/src/main/java/com/google/cloud/opentelemetry/example/otlptrace/OTLPTraceExample.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.google.cloud.opentelemetry.example.otlptrace;
1717

1818
import com.google.auth.oauth2.GoogleCredentials;
19+
import com.google.auth.oauth2.AccessToken;
1920
import io.opentelemetry.api.trace.Span;
2021
import io.opentelemetry.context.Scope;
2122
import io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporter;
@@ -24,6 +25,7 @@
2425
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
2526
import io.opentelemetry.sdk.common.CompletableResultCode;
2627
import io.opentelemetry.sdk.trace.export.SpanExporter;
28+
import java.io.*;
2729
import java.io.IOException;
2830
import java.util.Random;
2931
import java.util.concurrent.TimeUnit;
@@ -50,11 +52,17 @@ private static OpenTelemetrySdk setupTraceExporter() throws IOException {
5052
private static SpanExporter addAuthorizationHeaders(
5153
SpanExporter exporter, GoogleCredentials credentials) {
5254
if (exporter instanceof OtlpHttpSpanExporter) {
53-
OtlpHttpSpanExporterBuilder builder =
54-
((OtlpHttpSpanExporter) exporter)
55-
.toBuilder().addHeader("Authorization", "Bearer " + credentials.getAccessToken());
55+
try {
56+
credentials.refreshIfExpired();
57+
OtlpHttpSpanExporterBuilder builder =
58+
((OtlpHttpSpanExporter) exporter)
59+
.toBuilder().addHeader("Authorization", "Bearer " + credentials.getAccessToken().getTokenValue());
5660

57-
return builder.build();
61+
return builder.build();
62+
}
63+
catch(IOException e) {
64+
System.out.println("error");
65+
}
5866
}
5967
return exporter;
6068
}

0 commit comments

Comments
 (0)