-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(spanner): add setMetricsProjectId to fix metrics export #13241
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
RRap0so
wants to merge
2
commits into
googleapis:main
Choose a base branch
from
RRap0so:fix/metrics-project-default
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+42
−3
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -306,6 +306,7 @@ static GcpChannelPoolOptions mergeWithDefaultChannelPoolOptions( | |
| private final boolean enableExtendedTracing; | ||
| private final boolean enableEndToEndTracing; | ||
| private final String monitoringHost; | ||
| private final String metricsProjectId; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| private final TransactionOptions defaultTransactionOptions; | ||
| private final RequestOptions.ClientContext clientContext; | ||
|
|
||
|
|
@@ -991,6 +992,7 @@ protected SpannerOptions(Builder builder) { | |
| } | ||
| enableEndToEndTracing = builder.enableEndToEndTracing; | ||
| monitoringHost = builder.monitoringHost; | ||
| metricsProjectId = builder.metricsProjectId; | ||
| defaultTransactionOptions = builder.defaultTransactionOptions; | ||
| clientContext = builder.clientContext; | ||
| } | ||
|
|
@@ -1251,6 +1253,7 @@ public static class Builder | |
| private boolean enableBuiltInMetrics = SpannerOptions.environment.isEnableBuiltInMetrics(); | ||
| private boolean enableLocationApi = SpannerOptions.environment.isEnableLocationApi(); | ||
| private String monitoringHost = SpannerOptions.environment.getMonitoringHost(); | ||
| private String metricsProjectId; | ||
| private SslContext mTLSContext = null; | ||
| private String experimentalHost = null; | ||
| private boolean usePlainText = false; | ||
|
|
@@ -1360,6 +1363,7 @@ protected Builder() { | |
| this.enableLocationApi = options.enableLocationApi; | ||
| this.enableEndToEndTracing = options.enableEndToEndTracing; | ||
| this.monitoringHost = options.monitoringHost; | ||
| this.metricsProjectId = options.metricsProjectId; | ||
| this.defaultTransactionOptions = options.defaultTransactionOptions; | ||
| this.clientContext = options.clientContext; | ||
| } | ||
|
|
@@ -2033,6 +2037,17 @@ public Builder setMonitoringHost(String monitoringHost) { | |
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Sets the GCP project ID for exporting client-side built-in metrics. Defaults to {@link | ||
| * SpannerOptions#getProjectId()} when not set. On GKE with shared VPC, the default project may | ||
| * resolve to the host project instead of the application project; set this explicitly to avoid | ||
| * {@code monitoring.metricWriter} permission errors. | ||
| */ | ||
| public Builder setMetricsProjectId(String metricsProjectId) { | ||
| this.metricsProjectId = metricsProjectId; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Sets whether to enable extended OpenTelemetry tracing. Enabling this option will add the | ||
| * following additional attributes to the traces that are generated by the client: | ||
|
|
@@ -2443,14 +2458,14 @@ public ApiTracerFactory getApiTracerFactory() { | |
| @InternalApi | ||
| public OpenTelemetry getBuiltInOpenTelemetry() { | ||
| return this.builtInMetricsProvider.getOrCreateOpenTelemetry( | ||
| this.getProjectId(), getCredentials(), this.monitoringHost, getUniverseDomain()); | ||
| this.resolveMetricsProjectId(), getCredentials(), this.monitoringHost, getUniverseDomain()); | ||
| } | ||
|
|
||
| public void enablegRPCMetrics(InstantiatingGrpcChannelProvider.Builder channelProviderBuilder) { | ||
| if (isEnableBuiltInMetrics() && SpannerOptions.environment.isEnableGRPCBuiltInMetrics()) { | ||
| this.builtInMetricsProvider.enableGrpcMetrics( | ||
| channelProviderBuilder, | ||
| this.getProjectId(), | ||
| this.resolveMetricsProjectId(), | ||
| getCredentials(), | ||
| this.monitoringHost, | ||
| getUniverseDomain()); | ||
|
|
@@ -2499,7 +2514,7 @@ private ApiTracerFactory getDefaultApiTracerFactory() { | |
| private ApiTracerFactory createMetricsApiTracerFactory() { | ||
| OpenTelemetry openTelemetry = | ||
| this.builtInMetricsProvider.getOrCreateOpenTelemetry( | ||
| this.getProjectId(), getCredentials(), this.monitoringHost, getUniverseDomain()); | ||
| this.resolveMetricsProjectId(), getCredentials(), this.monitoringHost, getUniverseDomain()); | ||
|
|
||
| return openTelemetry != null | ||
| ? new BuiltInMetricsTracerFactory( | ||
|
|
@@ -2543,6 +2558,19 @@ String getMonitoringHost() { | |
| return monitoringHost; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the GCP project ID for client-side metrics export. Returns the explicit value if set | ||
| * via {@link Builder#setMetricsProjectId}, otherwise falls back to {@link #getProjectId()}. | ||
| */ | ||
| String resolveMetricsProjectId() { | ||
| return metricsProjectId != null ? metricsProjectId : getProjectId(); | ||
| } | ||
|
|
||
| /** Returns the explicitly configured metrics project ID, or null if not set. */ | ||
| public String getMetricsProjectId() { | ||
| return metricsProjectId; | ||
| } | ||
|
|
||
| public TransactionOptions getDefaultTransactionOptions() { | ||
| return defaultTransactionOptions; | ||
| } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The warning for project ID mismatch is currently logged on every call to
getDatabaseClient. Since this method is frequently called during normal operation, this can lead to excessive log spam and unnecessary performance overhead. It is recommended to move this check inside the conditional block where a newDatabaseClientis actually created (i.e., when!dbClients.containsKey(db)). This ensures the warning is logged at most once perDatabaseIdfor the lifetime of theSpannerinstance.