Skip to content

Commit bdcf525

Browse files
committed
feat(spanner): Update OpenTelemetry OTLP trace sample to use Google Cloud OTLP endpoint
1 parent d70946d commit bdcf525

1 file changed

Lines changed: 36 additions & 13 deletions

File tree

spanner/opentelemetry_traces/src/main/java/com/example/spanner/OpenTelemetryUsage.java

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.example.spanner;
1818

19+
import com.google.auth.oauth2.GoogleCredentials;
1920
import com.google.cloud.opentelemetry.trace.TraceConfiguration;
2021
import com.google.cloud.opentelemetry.trace.TraceExporter;
2122
import com.google.cloud.spanner.DatabaseClient;
@@ -33,6 +34,12 @@
3334
import io.opentelemetry.sdk.trace.export.SpanExporter;
3435
import io.opentelemetry.sdk.trace.samplers.Sampler;
3536

37+
import java.io.IOException;
38+
import java.util.Collections;
39+
import java.util.HashMap;
40+
import java.util.List;
41+
import java.util.Map;
42+
3643
/**
3744
* This sample demonstrates how to configure OpenTelemetry and inject via Spanner Options.
3845
*/
@@ -42,15 +49,14 @@ public class OpenTelemetryUsage {
4249
static Spanner spanner;
4350

4451
// TODO(developer): Replace these variables before running the sample.
45-
static String projectId = "my-project";
46-
static String instanceId = "my-instance";
47-
static String databaseId = "my-database";
52+
static String projectId = "span-cloud-testing";
53+
static String instanceId = "gargsurbhi-testing";
54+
static String databaseId = "test-db";
4855

49-
// Replace these variables to use OTLP Exporter
50-
static boolean useCloudTraceExporter = true; // Replace to false for OTLP
51-
static String otlpEndpoint = "http://localhost:4317"; // Replace with your OTLP endpoint
56+
static boolean useCloudTraceExporter = false; // Replace to true for Cloud Trace exporter
57+
static String otlpEndpoint = "https://telemetry.googleapis.com"; // Replace with your OTLP endpoint
5258

53-
public static void main(String[] args) {
59+
public static void main(String[] args) throws IOException {
5460

5561
if (useCloudTraceExporter) {
5662
spanner = getSpannerWithCloudTraceExporter();
@@ -64,25 +70,42 @@ public static void main(String[] args) {
6470
try (ResultSet resultSet =
6571
dbClient
6672
.singleUse() // Execute a single read or query against Cloud Spanner.
67-
.executeQuery(Statement.of("SELECT SingerId, AlbumId, AlbumTitle FROM Albums"))) {
73+
.executeQuery(Statement.of("SELECT 1"))) {
6874
while (resultSet.next()) {
6975
System.out.printf(
70-
"%d %d %s", resultSet.getLong(0), resultSet.getLong(1), resultSet.getString(2));
76+
"%d", resultSet.getLong(0));
7177
}
7278
}
7379

7480
sdkTracerProvider.forceFlush();
7581
}
7682

77-
public static Spanner getSpannerWithOtlpExporter() {
83+
public static Spanner getSpannerWithOtlpExporter() throws IOException {
7884
// [START spanner_opentelemetry_traces_otlp_usage]
7985
Resource resource = Resource
80-
.getDefault().merge(Resource.builder().put("service.name", "My App").build());
86+
.getDefault().merge(Resource.builder().put("service.name", "Surbhi App").put("gcp.project_id", projectId).build());
8187

88+
GoogleCredentials credentials = GoogleCredentials.getApplicationDefault()
89+
.createScoped(Collections.singleton("https://www.googleapis.com/auth/trace.append"));
8290
OtlpGrpcSpanExporter otlpGrpcSpanExporter =
8391
OtlpGrpcSpanExporter
8492
.builder()
85-
.setEndpoint(otlpEndpoint) // Replace with your OTLP endpoint
93+
.setEndpoint(otlpEndpoint)
94+
.setHeaders(() -> {
95+
try {
96+
credentials.refreshIfExpired();
97+
Map<String, List<String>> metadata = credentials.getRequestMetadata();
98+
Map<String, String> headers = new HashMap<>();
99+
if (metadata != null) {
100+
metadata.forEach((key, values) ->
101+
headers.put(key, String.join(",", values)));
102+
}
103+
return headers;
104+
} catch (Exception e) {
105+
// Handle error fetching credentials
106+
return Collections.emptyMap();
107+
}
108+
})// Replace with your OTLP endpoint
86109
.build();
87110

88111
// Using a batch span processor
@@ -96,7 +119,7 @@ public static Spanner getSpannerWithOtlpExporter() {
96119
// Use Otlp exporter or any other exporter of your choice.
97120
.addSpanProcessor(otlpGrpcSpanProcessor)
98121
.setResource(resource)
99-
.setSampler(Sampler.traceIdRatioBased(0.1))
122+
.setSampler(Sampler.traceIdRatioBased(1.0))
100123
.build();
101124

102125
// Export to a collector that is expecting OTLP using gRPC.

0 commit comments

Comments
 (0)