Skip to content

Commit

Permalink
chore: Add X-Goog-Api-Client metric header to outgoing requests
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanedey committed Nov 18, 2024
1 parent feaa092 commit 7cc2988
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public void sendAndParse(HttpRequestInfo requestInfo, Object destination) throws
}

public IncomingHttpResponse send(HttpRequestInfo requestInfo) throws T {
requestInfo.addHeader("X-Goog-Api-Client", SdkUtils.getMetricsHeader());
HttpRequest request = createHttpRequest(requestInfo);

HttpResponse response = null;
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/google/firebase/internal/SdkUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ public static String getVersion() {
return SDK_VERSION;
}

public static String getJavaVersion() {
return System.getProperty("java.version");
}

public static String getMetricsHeader() {
return String.format("gl-java/%s fire-admin/%s", getJavaVersion(), getVersion());
}

private static String loadSdkVersion() {
try (InputStream in = SdkUtils.class.getClassLoader()
.getResourceAsStream(ADMIN_SDK_PROPERTIES)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3026,6 +3026,8 @@ private static void checkRequestHeaders(TestResponseInterceptor interceptor) {

String clientVersion = "Java/Admin/" + SdkUtils.getVersion();
assertEquals(clientVersion, headers.getFirstHeaderStringValue("X-Client-Version"));
assertEquals(SdkUtils.getMetricsHeader(), headers.get("X-Goog-Api-Client"));

}

private static void checkUrl(TestResponseInterceptor interceptor, String method, String url) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import com.google.firebase.auth.FirebaseAuthException;
import com.google.firebase.auth.MockGoogleCredentials;
import com.google.firebase.internal.ApiClientUtils;
import com.google.firebase.internal.SdkUtils;
import com.google.firebase.testing.MultiRequestMockHttpTransport;
import com.google.firebase.testing.ServiceAccount;
import com.google.firebase.testing.TestResponseInterceptor;
Expand Down Expand Up @@ -87,6 +88,8 @@ public void testIAMCryptoSigner() throws Exception {
final String url = "https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/"
+ "[email protected]:signBlob";
assertEquals(url, interceptor.getResponse().getRequest().getUrl().toString());
HttpRequest request = interceptor.getResponse().getRequest();
assertEquals(SdkUtils.getMetricsHeader(), request.getHeaders().get("X-Goog-Api-Client"));
}

@Test
Expand Down Expand Up @@ -173,6 +176,7 @@ public void testMetadataService() throws Exception {
HttpRequest request = interceptor.getResponse().getRequest();
assertEquals(url, request.getUrl().toString());
assertEquals("Bearer test-token", request.getHeaders().getAuthorization());
assertEquals(SdkUtils.getMetricsHeader(), request.getHeaders().get("X-Goog-Api-Client"));
}

@Test
Expand Down Expand Up @@ -203,6 +207,7 @@ public void testExplicitServiceAccountEmail() throws Exception {
HttpRequest request = interceptor.getResponse().getRequest();
assertEquals(url, request.getUrl().toString());
assertEquals("Bearer test-token", request.getHeaders().getAuthorization());
assertEquals(SdkUtils.getMetricsHeader(), request.getHeaders().get("X-Goog-Api-Client"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ private static void checkRequestHeaders(TestResponseInterceptor interceptor) {

String clientVersion = "Java/Admin/" + SdkUtils.getVersion();
assertEquals(clientVersion, headers.getFirstHeaderStringValue("X-Client-Version"));
assertEquals(SdkUtils.getMetricsHeader(), headers.get("X-Goog-Api-Client"));
}

private static void checkUrl(TestResponseInterceptor interceptor, String method, String url) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import com.google.firebase.auth.MockTokenVerifier;
import com.google.firebase.auth.SessionCookieOptions;
import com.google.firebase.internal.ApiClientUtils;
import com.google.firebase.internal.SdkUtils;
import com.google.firebase.testing.TestResponseInterceptor;
import com.google.firebase.testing.TestUtils;
import java.io.ByteArrayOutputStream;
Expand Down Expand Up @@ -85,6 +86,7 @@ public void testCreateSessionCookieAsync() throws Exception {
assertEquals("testToken", parsed.get("idToken"));
assertEquals(new BigDecimal(3600), parsed.get("validDuration"));
checkUrl(interceptor, AUTH_BASE_URL + ":createSessionCookie");
checkHeaders(interceptor);
}

@Test
Expand Down Expand Up @@ -121,6 +123,7 @@ public void testCreateSessionCookie() throws Exception {
assertEquals("testToken", parsed.get("idToken"));
assertEquals(new BigDecimal(3600), parsed.get("validDuration"));
checkUrl(interceptor, AUTH_BASE_URL + ":createSessionCookie");
checkHeaders(interceptor);
}

@Test
Expand Down Expand Up @@ -212,6 +215,7 @@ public void testVerifySessionCookieWithCheckRevoked() throws FirebaseAuthExcepti
assertEquals("uid", token.getUid());
assertEquals("cookie", tokenVerifier.getLastTokenString());
checkUrl(interceptor, AUTH_BASE_URL + "/accounts:lookup");
checkHeaders(interceptor);
}

@Test
Expand Down Expand Up @@ -290,4 +294,9 @@ private static void checkUrl(TestResponseInterceptor interceptor, String url) {
assertEquals(HttpMethods.POST, request.getRequestMethod());
assertEquals(url, request.getUrl().getRawPath());
}

private static void checkHeaders(TestResponseInterceptor interceptor) {
HttpRequest request = interceptor.getResponse().getRequest();
assertEquals(SdkUtils.getMetricsHeader(), request.getHeaders().get("X-Goog-Api-Client"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import com.google.firebase.OutgoingHttpRequest;
import com.google.firebase.TestOnlyImplFirebaseTrampolines;
import com.google.firebase.auth.MockGoogleCredentials;
import com.google.firebase.internal.SdkUtils;
import com.google.firebase.testing.GenericFunction;
import com.google.firebase.testing.TestResponseInterceptor;
import com.google.firebase.testing.TestUtils;
Expand Down Expand Up @@ -174,6 +175,7 @@ public Void call(Object... args) throws Exception {
assertEquals(HttpMethods.DELETE, request.getRequestMethod());
assertEquals(TEST_URL, request.getUrl().toString());
assertEquals("Bearer test-token", request.getHeaders().getAuthorization());
assertEquals(SdkUtils.getMetricsHeader(), request.getHeaders().get("X-Goog-Api-Client"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ public void testSuccessfulRequestWithHeadersAndBody() throws FirebaseException,
assertEquals("v1", last.getHeaders().get("h1"));
assertEquals("v2", last.getHeaders().get("h2"));
assertEquals("v3", last.getHeaders().get("h3"));
assertEquals(SdkUtils.getMetricsHeader(), last.getHeaders().get("x-goog-api-client"));

ByteArrayOutputStream out = new ByteArrayOutputStream();
last.getContent().writeTo(out);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ private void checkRequestHeader(HttpRequest request) {
HttpHeaders headers = request.getHeaders();
assertEquals("2", headers.get("X-GOOG-API-FORMAT-VERSION"));
assertEquals("fire-admin-java/" + SdkUtils.getVersion(), headers.get("X-Firebase-Client"));
assertEquals(SdkUtils.getMetricsHeader(), headers.get("X-Goog-Api-Client"));
}

private void checkRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import com.google.firebase.TestOnlyImplFirebaseTrampolines;
import com.google.firebase.auth.MockGoogleCredentials;
import com.google.firebase.internal.ApiClientUtils;
import com.google.firebase.internal.SdkUtils;
import com.google.firebase.testing.TestResponseInterceptor;
import com.google.firebase.testing.TestUtils;
import java.io.ByteArrayOutputStream;
Expand Down Expand Up @@ -447,6 +448,8 @@ private void checkTopicManagementRequestHeader(
HttpRequest request, String expectedUrl) {
assertEquals("POST", request.getRequestMethod());
assertEquals(expectedUrl, request.getUrl().toString());
assertEquals(SdkUtils.getMetricsHeader(), request.getHeaders().get("X-Goog-Api-Client"));

}

private void checkExceptionFromHttpResponse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,7 @@ private void checkRequestHeader(int index, String expectedUrl, HttpMethod httpMe
assertEquals(expectedUrl, request.getUrl().toString());
assertEquals("Bearer test-token", request.getHeaders().getAuthorization());
assertEquals(CLIENT_VERSION, request.getHeaders().get("X-Client-Version"));
assertEquals(SdkUtils.getMetricsHeader(), request.getHeaders().get("X-Goog-Api-Client"));
}

private void checkRequestPayload(Map<String, String> expected) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1185,6 +1185,7 @@ private void checkGetRequestHeader(HttpRequest request, String urlSuffix) {
assertEquals(TEST_REMOTE_CONFIG_URL + urlSuffix, request.getUrl().toString());
HttpHeaders headers = request.getHeaders();
assertEquals("fire-admin-java/" + SdkUtils.getVersion(), headers.get("X-Firebase-Client"));
assertEquals(SdkUtils.getMetricsHeader(), request.getHeaders().get("X-Goog-Api-Client"));
assertEquals("gzip", headers.getAcceptEncoding());
}

Expand All @@ -1197,6 +1198,7 @@ private void checkPutRequestHeader(HttpRequest request, String urlSuffix, String
assertEquals(TEST_REMOTE_CONFIG_URL + urlSuffix, request.getUrl().toString());
HttpHeaders headers = request.getHeaders();
assertEquals("fire-admin-java/" + SdkUtils.getVersion(), headers.get("X-Firebase-Client"));
assertEquals(SdkUtils.getMetricsHeader(), request.getHeaders().get("X-Goog-Api-Client"));
assertEquals("gzip", headers.getAcceptEncoding());
assertEquals(ifMatch, headers.getIfMatch());
}
Expand All @@ -1206,6 +1208,7 @@ private void checkPostRequestHeader(HttpRequest request, String urlSuffix) {
assertEquals(TEST_REMOTE_CONFIG_URL + urlSuffix, request.getUrl().toString());
HttpHeaders headers = request.getHeaders();
assertEquals("fire-admin-java/" + SdkUtils.getVersion(), headers.get("X-Firebase-Client"));
assertEquals(SdkUtils.getMetricsHeader(), request.getHeaders().get("X-Goog-Api-Client"));
assertEquals("gzip", headers.getAcceptEncoding());
}

Expand Down

0 comments on commit 7cc2988

Please sign in to comment.