Add support for custom static headers#2690
Conversation
Dependent on googleapis/gax-java#432 Usage: // For manual/semi-manual clients: // Resource Manager // Translate // Compute // Storage // Datastore // Dns // BigQuery // // Logging // Firestore // Spanner HeaderProvider headerProvider = FixedHeaderProvider.create("user-agent", "my-client-name", "custom-header", "custom stuff"); LanguageServiceSettings settings = LanguageServiceSettings.newBuilder().setHeaderProvider(headerProvider).build(); LanguageServiceClient client = LanguageServiceClient.create(settings); // For generated clients: // All others, except Pub/Sub HeaderProvider headerProvider = FixedHeaderProvider.create("user-agent", "my-client-name", "custom-header", "custom stuff"); DatastoreOptions options = DatastoreOptions.newBuilder().setHeaderProvider(headerProvider).build(); Datastore datastore = options.getService(); // Note, PubSub is supported only partially. Full support is dependent on migration of PubSub from Grpc stubs to Gapic stubs
garrettjonesgoogle
left a comment
There was a problem hiding this comment.
One initial comment
| public static void main(String... args) { | ||
| Datastore datastore = DatastoreOptions.getDefaultInstance().getService(); | ||
| HeaderProvider headerProvider = | ||
| FixedHeaderProvider.create("user-agent", "my-client-name", "custom-header", "custom stuff"); |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
| </goals> | ||
| </execution> | ||
| </executions> | ||
| </plugin> |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
| private SpannerOptions(Builder builder) { | ||
| super(SpannerFactory.class, SpannerRpcFactory.class, builder, new SpannerDefaults()); | ||
| numChannels = builder.numChannels; | ||
| userAgent = builder.userAgentPrefix; |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
| Pattern.compile("^(?<headerValue>projects/[^/]*/instances/[^/]*/databases/[^/]*)(.*)?"); | ||
| private static final Pattern INSTANCE_PATTERN = | ||
| Pattern.compile("^(?<instance>projects/[^/]*/instances/[^/]*)(.*)?"); | ||
| Pattern.compile("^(?<headerValue>projects/[^/]*/instances/[^/]*)(.*)?"); |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
| return headersAsMetadata; | ||
| } | ||
|
|
||
| Map<Key<String>, String> getResourceHeadersAsMetadata( |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| class SpannerClientHeaderProvider implements HeaderProvider { |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
|
@vkedia PTAL |
|
|
||
| HeaderProvider mergedHeaderProvider = options.getMergedHeaderProvider(internalHeaderProvider); | ||
| this.headerProvider = SpannerHeaderProvider.create(mergedHeaderProvider.getHeaders(), | ||
| internalHeaderProviderBuilder.getResourceHeaderKey()); |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| class SpannerHeaderProvider implements HeaderProvider { |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
| } | ||
|
|
||
| return ImmutableMap.of( | ||
| Key.of(resourceHeaderKey, Metadata.ASCII_STRING_MARSHALLER), resourceToken); |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
| Matcher m = DATABASE_PATTERN.matcher(resource); | ||
| if (m.matches()) { | ||
| return m.group("database"); | ||
| Map<Metadata.Key<String>, String> headers = headerProvider.getHeadersAsMetadata(); |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
|
@garrettjonesgoogle PTAL Note that the header testing changes are additive, and thus are reflected here only in |
google-cloud-bom/pom.xml
Outdated
| <gax-grpc.version>1.15.0</gax-grpc.version> | ||
| <gax.version>1.15.1-SNAPSHOT</gax.version> | ||
| <gax-grpc.version>1.15.1-SNAPSHOT</gax-grpc.version> | ||
| <gax-httpjson.version>0.32.1-SNAPSHOT</gax-httpjson.version> |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
| @Test | ||
| public void testGetHeadersAsMetadata() { | ||
| Map<String, String> headers = ImmutableMap.of("header1", "value1", "header2", "value2"); | ||
| SpannerMetadataProvider headerProvider = SpannerMetadataProvider.create(headers, "header3"); |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| class SpannerMetadataProvider { |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
| FixedHeaderProvider.create("user-agent", "my-client-name", "custom-header", "custom stuff"); | ||
|
|
||
| LoggingOptions options = | ||
| LoggingOptions.newBuilder().setHeaderProvider(headerProvider).build(); |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
|
|
||
| // Create a dataset | ||
| String datasetId = "my_dataset_id"; | ||
| String datasetId = "my_dataset_id_1"; |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
| return libraryVersion == null ? LIBRARY_NAME : LIBRARY_NAME + "/" + libraryVersion; | ||
| } | ||
|
|
||
| // We have to do it, since underlying layers often do not appreciate User-Agent provided as a |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
|
@garrettjonesgoogle PTAL |
gax: 1.16.0 gax-grpc: 1.16.0 gax-httpjson: 0.33.0
|
@garrettjonesgoogle PTAL |
…#2690) * chore: Update generation configuration at Tue Oct 8 02:26:28 UTC 2024 * chore: generate libraries at Tue Oct 8 02:26:50 UTC 2024
Dependent on googleapis/gax-java#432 (will fail build until that is released)
Note, PubSub is supported only partially. Full support is dependent on migration of PubSub from Grpc stubs to Gapic stubs.
The spanner's
SpannerOptions.Builder.setUserAgentPrefix()method is replaced withServiceOptions.Builder.setHeaderProvider()and providing "user-agent" value there (it will be prefixing the header provider as explained inServiceOptions.Builder.setHeaderProvider()documentation, see example below).Usage: