Skip to content

Commit 396b54e

Browse files
psx95dashpole
andauthored
Add auth support for gRPC (#323)
* Add auth support for gRPC * Add comment on static headers * Update examples/otlptrace/src/main/java/com/google/cloud/opentelemetry/example/otlptrace/OTLPTraceExample.java Co-authored-by: David Ashpole <[email protected]> --------- Co-authored-by: David Ashpole <[email protected]>
1 parent 91ce992 commit 396b54e

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

examples/otlptrace/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ Next, update [`build.gradle`](build.grade) to set the following:
2222
```
2323
'-Dotel.resource.attributes=gcp.project_id=<YOUR_PROJECT_ID>,
2424
'-Dotel.exporter.otlp.headers=X-Goog-User-Project=<YOUR_QUOTA_PROJECT>',
25+
# Optional - if you want to export using gRPC protocol
26+
'-Dotel.exporter.otlp.protocol=grpc',
2527
```
2628

2729
Finally, to run the sample from the project root:

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@
2020
import io.opentelemetry.context.Scope;
2121
import io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporter;
2222
import io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporterBuilder;
23+
import io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter;
24+
import io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporterBuilder;
2325
import io.opentelemetry.sdk.OpenTelemetrySdk;
2426
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
2527
import io.opentelemetry.sdk.common.CompletableResultCode;
2628
import io.opentelemetry.sdk.trace.export.SpanExporter;
27-
import java.io.*;
2829
import java.io.IOException;
2930
import java.util.Random;
3031
import java.util.concurrent.TimeUnit;
@@ -48,6 +49,8 @@ private static OpenTelemetrySdk setupTraceExporter() throws IOException {
4849
}
4950

5051
// Modifies the span exporter initially auto-configured using environment variables
52+
// Note: This adds static authorization headers which are set only at initialization time.
53+
// This will stop working after the token expires, since the token is not refreshed.
5154
private static SpanExporter addAuthorizationHeaders(
5255
SpanExporter exporter, GoogleCredentials credentials) {
5356
if (exporter instanceof OtlpHttpSpanExporter) {
@@ -63,6 +66,18 @@ private static SpanExporter addAuthorizationHeaders(
6366
} catch (IOException e) {
6467
System.out.println("error:" + e.getMessage());
6568
}
69+
} else if (exporter instanceof OtlpGrpcSpanExporter) {
70+
try {
71+
credentials.refreshIfExpired();
72+
OtlpGrpcSpanExporterBuilder builder =
73+
((OtlpGrpcSpanExporter) exporter)
74+
.toBuilder()
75+
.addHeader(
76+
"Authorization", "Bearer " + credentials.getAccessToken().getTokenValue());
77+
return builder.build();
78+
} catch (IOException e) {
79+
throw new RuntimeException(e);
80+
}
6681
}
6782
return exporter;
6883
}

0 commit comments

Comments
 (0)