Skip to content

feat(spanner): Update OpenTelemetry OTLP trace sample to use Google C…#10251

Open
surbhigarg92 wants to merge 1 commit intomainfrom
otlp_sample_spanner
Open

feat(spanner): Update OpenTelemetry OTLP trace sample to use Google C…#10251
surbhigarg92 wants to merge 1 commit intomainfrom
otlp_sample_spanner

Conversation

@surbhigarg92
Copy link
Copy Markdown
Contributor

…loud OTLP endpoint

Description

@surbhigarg92 surbhigarg92 requested review from a team and yoshi-approver as code owners April 20, 2026 09:01
@product-auto-label product-auto-label bot added api: spanner Issues related to the Spanner API. samples Issues that are directly related to samples. labels Apr 20, 2026
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the OpenTelemetry Spanner sample to include authentication headers for the OTLP exporter and modifies the default configuration to use the OTLP endpoint with a full sampling rate. Feedback suggests reverting specific testing values, such as project IDs and service names, to generic placeholders to ensure the code sample remains professional and reusable for a public audience.

Comment on lines +52 to +54
static String projectId = "span-cloud-testing";
static String instanceId = "gargsurbhi-testing";
static String databaseId = "test-db";
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The project, instance, and database IDs have been updated to specific testing values. For a public code sample, it is best practice to use generic placeholders (e.g., 'my-project') as originally used, especially since the TODO(developer) comment remains.

Suggested change
static String projectId = "span-cloud-testing";
static String instanceId = "gargsurbhi-testing";
static String databaseId = "test-db";
static String projectId = "my-project";
static String instanceId = "my-instance";
static String databaseId = "my-database";
References
  1. Code samples may prioritize simplicity and clarity over production-ready practices.

// [START spanner_opentelemetry_traces_otlp_usage]
Resource resource = Resource
.getDefault().merge(Resource.builder().put("service.name", "My App").build());
.getDefault().merge(Resource.builder().put("service.name", "Surbhi App").put("gcp.project_id", projectId).build());
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The 'service.name' resource attribute is set to a personalized value ('Surbhi App'). Please use a generic name like 'My App' or 'spanner-otlp-sample' to ensure the sample is professional and reusable by others.

Suggested change
.getDefault().merge(Resource.builder().put("service.name", "Surbhi App").put("gcp.project_id", projectId).build());
.getDefault().merge(Resource.builder().put("service.name", "My App").put("gcp.project_id", projectId).build());
References
  1. Code samples may prioritize simplicity and clarity over production-ready practices.

static boolean useCloudTraceExporter = true; // Replace to false for OTLP
static String otlpEndpoint = "http://localhost:4317"; // Replace with your OTLP endpoint
static boolean useCloudTraceExporter = false; // Replace to true for Cloud Trace exporter
static String otlpEndpoint =
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The customer's OTLP configuration should be respected (see relevant OTel environment variables configuring the exporter). However, auth and project ID should get automatically injected/added if the endpoint is the Telemetry API or one of its regional variants.

Resource.getDefault()
.merge(
Resource.builder()
.put("service.name", "My App")
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why "My App"? The service name really ought to be supplied by the program, either through an explicit parameter or through environment variables.

Note that there are standard OTel environment variables that are typically used to supply this.

Comment on lines +91 to +96
GoogleCredentials credentials =
GoogleCredentials.getApplicationDefault()
.createScoped(Collections.singleton("https://www.googleapis.com/auth/trace.append"));
OtlpGrpcSpanExporter otlpGrpcSpanExporter =
OtlpGrpcSpanExporter
.builder()
.setEndpoint(otlpEndpoint) // Replace with your OTLP endpoint
OtlpGrpcSpanExporter.builder()
.setEndpoint(otlpEndpoint)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The credentials and header logic should be conditioned on the endpoint matching the GCP Telemetry API. But that endpoint should not be hardcoded. It should be easy for customers to use the library with a collector of their choice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: spanner Issues related to the Spanner API. samples Issues that are directly related to samples.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants