Skip to content
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

fix: update version resolution logic to be more resilient #2169

Merged
merged 2 commits into from Aug 23, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -23,7 +23,6 @@
import com.google.api.core.InternalApi;
import com.google.api.gax.core.CredentialsProvider;
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.api.gax.core.GaxProperties;
import com.google.api.gax.core.NoCredentialsProvider;
import com.google.api.gax.grpc.GrpcInterceptorProvider;
import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider;
Expand Down Expand Up @@ -210,9 +209,7 @@ private Tuple<StorageSettings, Opts<UserProject>> resolveSettingsAndOpts() throw

HeaderProvider internalHeaderProvider =
StorageSettings.defaultApiClientHeaderProviderBuilder()
.setClientLibToken(
ServiceOptions.getGoogApiClientLibName(),
GaxProperties.getLibraryVersion(this.getClass()))
.setClientLibToken(ServiceOptions.getGoogApiClientLibName(), getLibraryVersion())
.build();

StorageSettings.Builder builder =
Expand Down
Expand Up @@ -17,6 +17,7 @@
package com.google.cloud.storage;

import com.google.api.core.BetaApi;
import com.google.api.core.InternalApi;
import com.google.cloud.NoCredentials;
import com.google.cloud.ServiceDefaults;
import com.google.cloud.ServiceOptions;
Expand All @@ -26,10 +27,38 @@
import com.google.cloud.storage.HttpStorageOptions.HttpStorageRpcFactory;
import com.google.cloud.storage.TransportCompatibility.Transport;
import com.google.cloud.storage.spi.StorageRpcFactory;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public abstract class StorageOptions extends ServiceOptions<Storage, StorageOptions> {

private static final long serialVersionUID = -7295846567928013233L;
private static final String VERSION;

static {
String tmp = "unresolved";
final Properties props = new Properties();
try {
String resourcePath =
String.format(
"/META-INF/maven/%s/%s/pom.properties", "com.google.cloud", "google-cloud-storage");
InputStream resourceAsStream = StorageOptions.class.getResourceAsStream(resourcePath);
if (resourceAsStream == null) {
// some classloaders don't like a leading slash
resourceAsStream = StorageOptions.class.getResourceAsStream(resourcePath.substring(1));
}
if (resourceAsStream != null) {
props.load(resourceAsStream);
resourceAsStream.close();

tmp = props.getProperty("version", "unknown-version");
}
} catch (IOException ignore) {
// ignored
}
VERSION = tmp;
}

/** @deprecated Use {@link HttpStorageFactory} */
@Deprecated
Expand Down Expand Up @@ -86,6 +115,17 @@ protected boolean projectIdRequired() {
return false;
}

@Override
public String getLibraryVersion() {
return VERSION;
}

/* This can break at any time, the value produce is intended to be informative not authoritative */
@InternalApi
public static String version() {
return VERSION;
}

@SuppressWarnings("unchecked")
@Override
public abstract StorageOptions.Builder toBuilder();
Expand Down
Expand Up @@ -20,7 +20,6 @@
import com.google.api.core.ApiFutures;
import com.google.api.core.BetaApi;
import com.google.api.core.ListenableFutureToApiFuture;
import com.google.api.gax.core.GaxProperties;
import com.google.api.gax.rpc.FixedHeaderProvider;
import com.google.cloud.storage.BlobId;
import com.google.cloud.storage.BlobInfo;
Expand All @@ -45,8 +44,7 @@
final class TransferManagerImpl implements TransferManager {

private static final String USER_AGENT_ENTRY = "gcloud-tm/";
private static final String LIBRARY_VERSION =
GaxProperties.getLibraryVersion(TransferManagerConfig.class);
private static final String LIBRARY_VERSION = StorageOptions.version();
private final TransferManagerConfig transferManagerConfig;
private final ListeningExecutorService executor;
private final Qos qos;
Expand Down