-
Notifications
You must be signed in to change notification settings - Fork 53
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): append cred-type header for auth metrics #3186
Changes from all commits
2dbd2d8
ee2624c
fe3ed54
0cd0ad8
b1dd5b0
781f161
01adbde
32f8934
51bed23
0424fd2
0e69f72
b50896a
6e60633
33e24d9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,7 @@ | |
import com.google.api.gax.tracing.ApiTracerFactory; | ||
import com.google.api.gax.tracing.BaseApiTracerFactory; | ||
import com.google.auth.ApiKeyCredentials; | ||
import com.google.auth.CredentialTypeForMetrics; | ||
import com.google.auth.Credentials; | ||
import com.google.auth.oauth2.GdchCredentials; | ||
import com.google.auto.value.AutoValue; | ||
|
@@ -210,7 +211,8 @@ public static ClientContext create(StubSettings settings) throws IOException { | |
if (transportChannelProvider.needsExecutor() && settings.getExecutorProvider() != null) { | ||
transportChannelProvider = transportChannelProvider.withExecutor(backgroundExecutor); | ||
} | ||
Map<String, String> headers = getHeadersFromSettings(settings); | ||
|
||
Map<String, String> headers = getHeaders(settings, credentials); | ||
if (transportChannelProvider.needsHeaders()) { | ||
transportChannelProvider = transportChannelProvider.withHeaders(headers); | ||
} | ||
|
@@ -318,8 +320,11 @@ static GdchCredentials getGdchCredentials( | |
/** | ||
* Getting a header map from HeaderProvider and InternalHeaderProvider from settings with Quota | ||
* Project Id. | ||
* | ||
* <p>Then if credentials is present and its type for metrics is not {@code | ||
* CredentialTypeForMetrics.DO_NOT_SEND}, append this type info to x-goog-api-client header. | ||
*/ | ||
private static Map<String, String> getHeadersFromSettings(StubSettings settings) { | ||
private static Map<String, String> getHeaders(StubSettings settings, Credentials credentials) { | ||
// Resolve conflicts when merging headers from multiple sources | ||
Map<String, String> userHeaders = settings.getHeaderProvider().getHeaders(); | ||
Map<String, String> internalHeaders = settings.getInternalHeaderProvider().getHeaders(); | ||
|
@@ -346,6 +351,20 @@ private static Map<String, String> getHeadersFromSettings(StubSettings settings) | |
effectiveHeaders.putAll(userHeaders); | ||
effectiveHeaders.putAll(conflictResolution); | ||
|
||
return appendCredentialTypeToHeaderIfPresent(effectiveHeaders, credentials); | ||
} | ||
|
||
private static Map<String, String> appendCredentialTypeToHeaderIfPresent( | ||
Map<String, String> effectiveHeaders, Credentials credentials) { | ||
CredentialTypeForMetrics credentialTypeForMetrics = | ||
credentials == null | ||
? CredentialTypeForMetrics.DO_NOT_SEND | ||
: credentials.getMetricsCredentialType(); | ||
if (credentialTypeForMetrics != CredentialTypeForMetrics.DO_NOT_SEND) { | ||
effectiveHeaders.computeIfPresent( | ||
ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it decided to append There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
yes, this design is already implemented for other some languages. See details in design go/googleapis-auth-metric-design section 2.3.2. I don't think I saw discussion on using a different header specifically though.
IIUC, this header is eventually sent per request, at that time, I don't see credentials type much different than other client lib info? Do you have specific concerns about this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The concern is that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see your point. |
||
(key, value) -> value + " cred-type/" + credentialTypeForMetrics.getLabel()); | ||
} | ||
return ImmutableMap.copyOf(effectiveHeaders); | ||
} | ||
|
||
|
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: extract this to a private method for better readability.