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

feat(gax): add protobuf version tracking to headers #3199

Open
wants to merge 26 commits into
base: main
Choose a base branch
from

Conversation

ldetmer
Copy link
Contributor

@ldetmer ldetmer commented Sep 13, 2024

Update the Java client libraries to report the runtime version of Protobuf as part of the existing x-goog-api-client request header.

Tested: java-cloud-library api (billing) and hand written api (storage)

@product-auto-label product-auto-label bot added the size: m Pull request size is medium. label Sep 13, 2024
@ldetmer ldetmer changed the title Protobuf metric feat(gax): add protobuf version to headers to track Sep 13, 2024
@ldetmer ldetmer changed the title feat(gax): add protobuf version to headers to track feat(gax): add protobuf version tracking to headers Sep 16, 2024
@ldetmer ldetmer marked this pull request as ready for review September 16, 2024 13:39
@@ -41,8 +43,11 @@
public class ApiClientHeaderProvider implements HeaderProvider, Serializable {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we have two tests added to Showcase that show the token being added in both grpc + httpjson clients?

Copy link
Contributor Author

@ldetmer ldetmer Sep 18, 2024

Choose a reason for hiding this comment

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

added, but i notice that native-showcase is failing, would that be because the sdk-platform-java lib is not included in the image yet?

Copy link
Contributor

Choose a reason for hiding this comment

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

No - the native tests install the repo's modules first: https://github.com/googleapis/sdk-platform-java/blob/main/.github/workflows/ci.yaml#L272

Then starts the Showcase server, and finally runs the mvn test command: https://github.com/googleapis/sdk-platform-java/blob/main/.github/workflows/ci.yaml#L287

@burkedavison
Copy link
Contributor

LGTM w/ addition of Showcase tests showing gRPC + httpjson clients are both providing the new headers.

Looking for approval from @blakeli0

private static final String PROTOBUF_APPENDER = "--" + PROTOBUF_HEADER_VERSION_KEY + "-.*";
private static final String FULL_HEADER_MATCH =
"^gl-java/.* gapic/4\\.5\\.6"
+ PROTOBUF_APPENDER
Copy link
Collaborator

Choose a reason for hiding this comment

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

I feel these static strings add additional complexity in reading the code, can we try to inline most of them per style guide?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

File file = new File(clazz.getProtectionDomain().getCodeSource().getLocation().toURI());
try (JarFile jar = new JarFile(file.getPath())) {
Attributes attributes = jar.getManifest().getMainAttributes();
return Optional.ofNullable(attributes.getValue(BUNDLE_VERSION_KEY));
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we can inline BUNDLE_VERSION_KEY if it is not expected to be reused.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@@ -43,6 +49,9 @@ public class GaxProperties {
private static final String DEFAULT_VERSION = "";
private static final String GAX_VERSION = getLibraryVersion(GaxProperties.class, "version.gax");
private static final String JAVA_VERSION = getRuntimeVersion();
private static final String PROTOBUF_VERSION =
getBundleVersion(Any.class).orElse(DEFAULT_VERSION);
static final String BUNDLE_VERSION_KEY = "Bundle-Version";
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do you mind adding a comment regarding where do we get this magic string "Bundle-Version"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added it to the getBundleVersion JavaDoc

@@ -224,6 +247,10 @@ public Builder setApiVersionToken(String apiVersionToken) {
return this;
}

public String getProtobufRuntimeToken() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I know other fields have a public getter, but I don't think they have to be. Since this method is only used within Gax, can we change this to package private?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Matcher matcher = pattern.matcher(apiClientHeaderValue);
if (matcher.find()) {
return apiClientHeaderValue.substring(0, matcher.end())
+ protobufVersionAppendValue
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this field can be inlined too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Pattern.compile("gl-java/.+ gapic/.+--protobuf-.+ gax/.+ rest/ protobuf/.*");
httpJsonClient.echo(EchoRequest.newBuilder().build());
ArrayList headerValues =
(ArrayList) httpJsonInterceptor.metadata.getHeaders().get(HTTP_CLIENT_API_HEADER_KEY);
Copy link
Contributor

Choose a reason for hiding this comment

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

Rather than using raw-types, use ArrayList<?> if you don't know the type. In this case, we're casting to String in the next line so we might as well cast to ArrayList<String>

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@burkedavison
Copy link
Contributor

Thank you for the showcase tests - LGTM.

Copy link

sonarcloud bot commented Sep 18, 2024

Copy link

sonarcloud bot commented Sep 18, 2024

Quality Gate Passed Quality Gate passed for 'java_showcase_integration_tests'

Issues
14 New issues
0 Accepted issues

Measures
0 Security Hotspots
84.0% Coverage on New Code
0.0% Duplication on New Code

See analysis details on SonarCloud

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: m Pull request size is medium.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants