Skip to content

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

Open
surbhigarg92 wants to merge 2 commits intomainfrom
otlp_sample_spanner
Open

feat(spanner): Update OpenTelemetry OTLP trace sample to use Google C…#10251
surbhigarg92 wants to merge 2 commits 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.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sure changed it to fetch from OTEL_EXPORTER_OTLP_ENDPOINT/OTEL_EXPORTER_OTLP_TRACES_ENDPOINT

Have used google telemetry endpoint as default.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removed

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added this as a conditional logic for "telemetry.googleapis.com"

SpannerOptions options = SpannerOptions.newBuilder()
.setOpenTelemetry(openTelemetry)
.build();
SpannerOptions options = SpannerOptions.newBuilder().setOpenTelemetry(openTelemetry).build();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Can we remove this change? This looks more cleaner

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

similarly in other places.

if (otlpEndpoint == null) {
otlpEndpoint = System.getenv("OTEL_EXPORTER_OTLP_ENDPOINT");
}
if (otlpEndpoint == null) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I think if "OTEL_EXPORTER" or "OTEL_TRACES_EXPORTER" are set to "gcp" or if environment variable "GOOGLE_CLOUD_TRACING_ENABLED" is set to "true", then you can fallback like this.

Otherwise, I think you should fallback to the default behavior of Open Telemetry. If "OTEL_EXPORTER" or "OTEL_TRACES_EXPORTER" is set to "otlp" and neither "OTEL_EXPORTER_OTLP_ENDPOINT" nor "OTEL_OTLP_EXPORTER_TRACES_ENDPOINT" are set, then the default is to use "http://localhost:4317" for gRPC and "http://localhost:4318" for HTTP. See:

https://opentelemetry.io/docs/languages/sdk-configuration/otlp-exporter/


// Add authentication only if the endpoint is the Google Cloud Telemetry API.
// The standard endpoint is telemetry.googleapis.com
if (otlpEndpoint.contains("telemetry.googleapis.com")) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Note that you also need to match regionalized addresses:

telemetry.googleapis.com (global)
telemetry.us-central1.rep.googleapis.com (regionalized in cloud region us-central1)
telemetry.us-east1.rep.googleapis.com (regionalized in us-east1)
...

Prefixed with "https://" or suffixed with ":443" (optionally with "dns:// " as a prefix). Ex:

telemetry.googleapis.com
https://telemetry.googleapis.com
telemetry.googleapis.com:443
dns://telemetry.googleapis.com:443
https://telemetry.us-central1.rep.googleapis.com
telemetry.us-central1.rep.googleapis.com:443
...

However, you also want this match to apply only to the domain (it would be a mistake to match "otherdomain.com/proxy/telemetry.googleapis.com").

.addSpanProcessor(otlpGrpcSpanProcessor)
.addSpanProcessor(BatchSpanProcessor.builder(exporterBuilder.build()).build())
.setResource(resource)
.setSampler(Sampler.traceIdRatioBased(0.1))
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Use the OTEL_TRACES_SAMPLER flag to control sampling behavior?

https://opentelemetry.io/docs/languages/sdk-configuration/general/#otel_traces_sampler

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