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
Update User Agent methods and format #225
Conversation
…ic to append to string
@@ -31,8 +31,24 @@ | |||
|
|||
private Socket socket; | |||
|
|||
private static final String APPLICATION_NAME = "mysql-socket-factory-connector-j-5"; | |||
|
|||
private static String getUserAgentString() { |
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.
Rather than run this each time connect is called, can we use a static block so it's only called once? Otherwise this method has to run on each connect.
It would also be convient to try to keep as much of this logic in the core factory as possible, so it doesn't need to be repeated.
static {
SocketFactory.setArtifactNameOrSomething("mysql-socket-factory-connector-j-5/" + VERSION );
}
try { | ||
Properties packageInfo = new Properties(); | ||
packageInfo | ||
.load(SocketFactory.class.getClassLoader().getResourceAsStream("project.properties")); |
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 gcp-spring folks are getting the artifact version without having to read from a file (example). Can we do something similar here?
Looks like there is a getImplementationtitle
that might be helpful as well.
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.
I tried that earlier but kept getting null values for the version. From looking it up, I gathered it was because Maven jar builds don't include that information in the jar manifest by default. I added a config option to the parent pom.xml which added that metadata to the jar, but it still showed up as null when getting the value.
@@ -290,7 +296,8 @@ private static SQLAdmin createAdminApiClient(HttpRequestInitializer requestIniti | |||
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); | |||
SQLAdmin.Builder adminApiBuilder = | |||
new Builder(httpTransport, jsonFactory, requestInitializer) | |||
.setApplicationName(getApplicationName()); | |||
.setApplicationName( | |||
String.format("%s %s", getDefaultUserAgent(), getApplicationName())); |
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.
What if this is null?
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.
so right now, getDefaultUserAgent
defaults to "cloud-sql-java-connector" and getApplicationName
defaults to an empty string. So there shouldn't be any cases of either of those returning null.
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.
I'll probably get rid of the getDefaultUserAgent method in the next commit though, since we don't need to set a system property. In that case I'll handle if the connector name is null.
@@ -333,6 +340,24 @@ private static KeyPair generateRsaKeyPair() { | |||
return generator.generateKeyPair(); | |||
} | |||
|
|||
/** Sets the default string which is appended to the SQLAdmin API client User-Agent header. */ | |||
private static void setDefaultUserAgent(String userAgent) { | |||
System.setProperty(APPLICATION_NAME_PROPERTY, userAgent); |
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.
I don't think we need to store this in a system property - is only done for getApplicationName()
for legacy reasons.
For the postgres connector, the /version seems to be getting logged as /null instead of the expected /1.0.17 |
This might have been because of early testing. I tried using another method to get the version and it kept returning null. There should also be some that log the version correctly |
Got it. LGTM. Logged format postgres-socket-factory/1.0.17-SNAPSHOT |
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.
Left a few comments, but apparently didn't attach them to a review correctly (sorry!)
…ties in connectors
I think I do have a few questions:
|
Yes - raw number of API calls for each db isn't (IMO) a useful metric, but number of users is.
It should always set at least one, but I'm open to suggestions on improving to ensure both are entered. I'm not too terribly worried since this is an edge case.
I think leaving |
private static final String DEFAULT_APPLICATION_NAME = "mysql-socket-factory-connector-j-5"; | ||
|
||
static { | ||
CoreSocketFactory.addUserAgent(DEFAULT_APPLICATION_NAME); |
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.
nit: I think at this point you should just inline these.
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.
fixed
|
||
static { | ||
try { | ||
Properties packageInfo = new Properties(); |
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.
Can we move this logic into a function and just call it here so it's a bit more contained? This way we can return "unknown" over null on exception (you may want to check with @jsimonweb if we need to use anything special instead of "unknown")
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.
moved to separate function in latest commit
/** Sets the default string which is appended to the SQLAdmin API client User-Agent header. */ | ||
public static void addUserAgent(String artifactId) { | ||
if (version != null) { | ||
userAgents.add(artifactId + "/" + version); |
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.
we should avoid letting version
be null and just default it to some safe value (even if empty string). Also, we discussed adding a quick check to avoid repeats.
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.
it's defaulted to "unknown" in the latest commit
@@ -0,0 +1 @@ | |||
version=${project.version} |
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.
nit: should the path just be com.google.cloud.sql
since we're using the same version for all of the artifacts?
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.
fixed
@@ -333,6 +350,28 @@ private static KeyPair generateRsaKeyPair() { | |||
return generator.generateKeyPair(); | |||
} | |||
|
|||
/** Sets the default string which is appended to the SQLAdmin API client User-Agent header. */ | |||
public static void addUserAgent(String artifactId) { |
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.
nit: Should this be addArtifactId
since we're only setting a portion of the intended UserAgent?
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.
fixed
} | ||
|
||
/** Returns the default string which is appended to the SQLAdmin API client User-Agent header. */ | ||
public static String getUserAgentString() { |
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.
public static String getUserAgentString() { | |
private static String getUserAgents() { |
|
||
/** Returns the default string which is appended to the SQLAdmin API client User-Agent header. */ | ||
public static String getUserAgentString() { | ||
return String.join(" ", userAgents); |
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.
Should this add in USER_TOKEN_PROPERTY_NAME
if set?
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.
Not sure. Right now I'm inlining combining the two strings when creating the SQLAdmin API client.
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.
Never mind, changed this to append the application name in the latest set of commits
Properties packageInfo = new Properties(); | ||
packageInfo.load(CoreSocketFactory.class.getClassLoader().getResourceAsStream( | ||
"com.google.cloud.sql/project.properties")); | ||
return packageInfo.getProperty("version"); |
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.
nit: Should this have a default?
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.
fixed
Change Description
Updated the
SocketFactory
classes to set the user agent inCoreSocketFactory
so that it is specific to the connector. Also updated the logic inCoreSocketFactory.setApplicationName
to append to the existing user agent string if it has already been set, allowing third party tools like spring-cloud-gcp to add custom names.Checklist
bug/issue
before writing your code! That way we can discuss the change, evaluate
designs, and agree on the general idea.
Relevant issues: