fix(spanner): derive built-in metrics project from database client by rahul2393 · Pull Request #13262 · googleapis/google-cloud-java · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.google.cloud.opentelemetry.detection.AttributeKeys;
import com.google.cloud.opentelemetry.detection.DetectedPlatform;
import com.google.cloud.opentelemetry.detection.GCPPlatformDetector;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;
import com.google.common.hash.HashFunction;
import com.google.common.hash.Hashing;
Expand Down Expand Up @@ -75,10 +76,13 @@ final class BuiltInMetricsProvider {
private static final String default_location = "global";

private OpenTelemetry openTelemetry;
private String projectId;
private boolean mismatchedProjectIdLogged;
private Thread shutdownHook;

private BuiltInMetricsProvider() {}

OpenTelemetry getOrCreateOpenTelemetry(
synchronized OpenTelemetry getOrCreateOpenTelemetry(
String projectId,
@Nullable Credentials credentials,
@Nullable String monitoringHost,
Expand All @@ -88,12 +92,13 @@ OpenTelemetry getOrCreateOpenTelemetry(
SdkMeterProviderBuilder sdkMeterProviderBuilder = SdkMeterProvider.builder();
BuiltInMetricsView.registerBuiltinMetrics(
SpannerCloudMonitoringExporter.create(
projectId, credentials, monitoringHost, universeDomain),
this::getProjectId, credentials, monitoringHost, universeDomain),

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.

@rahul2393 I did not understand this solution. getOrCreateOpenTelemetry is called from GapicSpannerRPC while creating SpannerClient. At the time projectId shared here could be the projectId of GKE instance for example.

So in this case we will be initialising SpannerCloudMonitoringExporter with null projectId ? As by this time setProjectIdIfAbsent won't be called, it is called later during database init.

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.

So flow is:

  1. SpannerClient init → OpenTelemetry/exporter may be created, project supplier returns null
  2. no export happens yet because no database project is known
  3. getDatabaseClient(DatabaseId) → database project is set once
  4. future metric exports use that database project

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.

We are also passing the projectId in next line to create OpenTelemetry Resource

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.

We also do

monitoredResourceBuilder.putLabels(PROJECT_ID_KEY.getKey(), projectId);

So the resource created during SDK initialization may contain the early/default project, but before sending
createServiceTimeSeries, we overwrite the monitored resource label with the database project from the exporter supplier.

sdkMeterProviderBuilder);
sdkMeterProviderBuilder.setResource(Resource.create(createResourceAttributes(projectId)));
SdkMeterProvider sdkMeterProvider = sdkMeterProviderBuilder.build();
this.openTelemetry = OpenTelemetrySdk.builder().setMeterProvider(sdkMeterProvider).build();
Runtime.getRuntime().addShutdownHook(new Thread(sdkMeterProvider::close));
this.shutdownHook = new Thread(sdkMeterProvider::close);
Runtime.getRuntime().addShutdownHook(this.shutdownHook);
}
return this.openTelemetry;
} catch (IOException ex) {
Expand All @@ -106,6 +111,47 @@ OpenTelemetry getOrCreateOpenTelemetry(
}
}

synchronized void setProjectIdIfAbsent(String projectId) {
if (this.projectId == null) {
this.projectId = projectId;
} else if (!this.projectId.equals(projectId) && !mismatchedProjectIdLogged) {
mismatchedProjectIdLogged = true;
logger.log(
Level.WARNING,
"Built-in metrics fallback project is already initialized to project {0}. Non-Spanner"
+ " metrics without project information will be exported using that project instead"
+ " of project {1}.",
new Object[] {this.projectId, projectId});
}
}

@Nullable
synchronized OpenTelemetry getOpenTelemetry() {
return this.openTelemetry;
}

synchronized String getProjectId() {
return this.projectId;
}

@VisibleForTesting
synchronized void reset() {
if (this.openTelemetry instanceof OpenTelemetrySdk) {
((OpenTelemetrySdk) this.openTelemetry).getSdkMeterProvider().close();
}
if (this.shutdownHook != null) {
try {
Runtime.getRuntime().removeShutdownHook(this.shutdownHook);
} catch (IllegalStateException ignored) {
// The JVM is already shutting down.
}
}
this.openTelemetry = null;
this.projectId = null;
this.mismatchedProjectIdLogged = false;
this.shutdownHook = null;
}

// TODO: Remove when
// https://github.com/GoogleCloudPlatform/opentelemetry-operations-java/issues/421
// has been fixed.
Expand Down
Loading
Loading