Skip to content

Commit

Permalink
fix: ensure user agent is applied to gRPC channel
Browse files Browse the repository at this point in the history
  • Loading branch information
jschneidereit committed Nov 27, 2024
1 parent 39dad57 commit 045eeb9
Show file tree
Hide file tree
Showing 19 changed files with 476 additions and 191 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@
import com.salesforce.datacloud.jdbc.http.ClientBuilder;
import com.salesforce.datacloud.jdbc.interceptor.AuthorizationHeaderInterceptor;
import com.salesforce.datacloud.jdbc.interceptor.DataspaceHeaderInterceptor;
import com.salesforce.datacloud.jdbc.interceptor.HyperDefaultsHeaderInterceptor;
import com.salesforce.datacloud.jdbc.interceptor.HyperExternalClientContextHeaderInterceptor;
import com.salesforce.datacloud.jdbc.interceptor.HyperWorkloadHeaderInterceptor;
import com.salesforce.datacloud.jdbc.interceptor.MaxMetadataSizeHeaderInterceptor;
import com.salesforce.datacloud.jdbc.interceptor.TracingHeadersInterceptor;
import com.salesforce.datacloud.jdbc.interceptor.UserAgentHeaderInterceptor;
import com.salesforce.datacloud.jdbc.util.Messages;
import io.grpc.ClientInterceptor;
import io.grpc.ManagedChannelBuilder;
Expand Down Expand Up @@ -125,9 +126,10 @@ static List<ClientInterceptor> getClientInterceptors(
AuthorizationHeaderInterceptor authInterceptor, Properties properties) {
return Stream.of(
authInterceptor,
new HyperDefaultsHeaderInterceptor(),
new MaxMetadataSizeHeaderInterceptor(),
TracingHeadersInterceptor.of(),
UserAgentHeaderInterceptor.of(properties),
HyperExternalClientContextHeaderInterceptor.of(properties),
HyperWorkloadHeaderInterceptor.of(properties),
DataspaceHeaderInterceptor.of(properties))
.filter(Objects::nonNull)
.peek(t -> log.info("Registering interceptor. interceptor={}", t))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.salesforce.datacloud.jdbc.core;

import com.salesforce.datacloud.jdbc.config.DriverVersion;
import com.salesforce.datacloud.jdbc.interceptor.QueryIdHeaderInterceptor;
import com.salesforce.datacloud.jdbc.util.PropertiesExtensions;
import com.salesforce.hyperdb.grpc.ExecuteQueryResponse;
Expand Down Expand Up @@ -78,8 +79,9 @@ public static HyperGrpcClientExecutor of(@NonNull ManagedChannelBuilder<?> build
.defaultServiceConfig(retryPolicy(maxRetryAttempts));
}

val channel =
builder.maxInboundMessageSize(GRPC_INBOUND_MESSAGE_MAX_SIZE).build();
val channel = builder.maxInboundMessageSize(GRPC_INBOUND_MESSAGE_MAX_SIZE)
.userAgent(DriverVersion.formatDriverInfo())
.build();
return client.channel(channel).build();
}

Expand Down Expand Up @@ -202,7 +204,7 @@ private HyperServiceGrpc.HyperServiceBlockingStub lazyStub() {
}
}

private HyperServiceGrpc.HyperServiceBlockingStub getStub(String queryId) {
private HyperServiceGrpc.HyperServiceBlockingStub getStub(@NonNull String queryId) {
val queryIdHeaderInterceptor = new QueryIdHeaderInterceptor(queryId);
return getStub().withInterceptors(queryIdHeaderInterceptor);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package com.salesforce.datacloud.jdbc.interceptor;

import static io.grpc.Metadata.ASCII_STRING_MARSHALLER;
import static com.salesforce.datacloud.jdbc.interceptor.MetadataUtilities.keyOf;

import com.salesforce.datacloud.jdbc.auth.TokenProcessor;
import io.grpc.Metadata;
Expand Down Expand Up @@ -52,8 +52,8 @@ public static AuthorizationHeaderInterceptor of(TokenSupplier supplier) {
private static final String AUTH = "Authorization";
private static final String AUD = "audience";

private static final Metadata.Key<String> AUTH_KEY = Metadata.Key.of(AUTH, ASCII_STRING_MARSHALLER);
private static final Metadata.Key<String> AUD_KEY = Metadata.Key.of(AUD, ASCII_STRING_MARSHALLER);
private static final Metadata.Key<String> AUTH_KEY = keyOf(AUTH);
private static final Metadata.Key<String> AUD_KEY = keyOf(AUD);

@ToString.Exclude
private final TokenSupplier tokenSupplier;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,30 @@
*/
package com.salesforce.datacloud.jdbc.interceptor;

import static com.salesforce.datacloud.jdbc.interceptor.MetadataUtilities.keyOf;
import static com.salesforce.datacloud.jdbc.util.PropertiesExtensions.optional;
import static io.grpc.Metadata.ASCII_STRING_MARSHALLER;

import io.grpc.Metadata;
import java.util.Properties;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import lombok.Getter;
import lombok.ToString;

@Slf4j
@Getter
@ToString
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class DataspaceHeaderInterceptor implements HeaderMutatingClientInterceptor {
public class DataspaceHeaderInterceptor implements SingleHeaderMutatingClientInterceptor {
public static DataspaceHeaderInterceptor of(Properties properties) {
return optional(properties, DATASPACE)
return optional(properties, PROPERTY)
.map(DataspaceHeaderInterceptor::new)
.orElse(null);
}

@NonNull private final String dataspace;
static final String PROPERTY = "dataspace";

static final String DATASPACE = "dataspace";
@ToString.Exclude
private final Metadata.Key<String> key = keyOf(PROPERTY);

private static final Metadata.Key<String> DATASPACE_KEY = Metadata.Key.of(DATASPACE, ASCII_STRING_MARSHALLER);

@Override
public void mutate(final Metadata headers) {
headers.put(DATASPACE_KEY, dataspace);
}

@Override
public String toString() {
return ("DataspaceHeaderInterceptor(dataspace=" + dataspace + ")");
}
private final String value;
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2024, Salesforce, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.salesforce.datacloud.jdbc.interceptor;

import static com.salesforce.datacloud.jdbc.interceptor.MetadataUtilities.keyOf;
import static com.salesforce.datacloud.jdbc.util.PropertiesExtensions.optional;

import io.grpc.Metadata;
import java.util.Properties;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;

@Getter
@ToString
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class HyperExternalClientContextHeaderInterceptor implements SingleHeaderMutatingClientInterceptor {
public static HyperExternalClientContextHeaderInterceptor of(Properties properties) {
return optional(properties, PROPERTY)
.map(HyperExternalClientContextHeaderInterceptor::new)
.orElse(null);
}

static final String PROPERTY = "external-client-context";

@ToString.Exclude
private final Metadata.Key<String> key = keyOf("x-hyperdb-external-client-context");

private final String value;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2024, Salesforce, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.salesforce.datacloud.jdbc.interceptor;

import static com.salesforce.datacloud.jdbc.interceptor.MetadataUtilities.keyOf;
import static com.salesforce.datacloud.jdbc.util.PropertiesExtensions.optional;

import io.grpc.Metadata;
import java.util.Properties;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;
import lombok.val;

@Getter
@ToString
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class HyperWorkloadHeaderInterceptor implements SingleHeaderMutatingClientInterceptor {
public static HyperWorkloadHeaderInterceptor of(Properties properties) {
val value = optional(properties, PROPERTY).orElse(DEFAULT);
return new HyperWorkloadHeaderInterceptor(value);
}

private static final String PROPERTY = "workload";

private static final String DEFAULT = "jdbcv3";

@ToString.Exclude
private final Metadata.Key<String> key = keyOf("x-hyperdb-workload");

private final String value;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2024, Salesforce, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.salesforce.datacloud.jdbc.interceptor;

import static com.salesforce.datacloud.jdbc.interceptor.MetadataUtilities.keyOf;

import io.grpc.Metadata;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Getter
@ToString
@NoArgsConstructor
public class MaxMetadataSizeHeaderInterceptor implements SingleHeaderMutatingClientInterceptor {
private final String value = String.valueOf(1024 * 1024);

@ToString.Exclude
private final Metadata.Key<String> key = keyOf("grpc.max_metadata_size");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2024, Salesforce, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.salesforce.datacloud.jdbc.interceptor;

import static io.grpc.Metadata.ASCII_STRING_MARSHALLER;

import io.grpc.Metadata;
import lombok.experimental.UtilityClass;

@UtilityClass
public class MetadataUtilities {
public static Metadata.Key<String> keyOf(String key) {
return Metadata.Key.of(key, ASCII_STRING_MARSHALLER);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,29 @@
*/
package com.salesforce.datacloud.jdbc.interceptor;

import static io.grpc.Metadata.ASCII_STRING_MARSHALLER;
import static com.salesforce.datacloud.jdbc.interceptor.MetadataUtilities.keyOf;

import io.grpc.Metadata;
import lombok.Getter;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.ToString;

@Getter
@ToString
@RequiredArgsConstructor
public class QueryIdHeaderInterceptor implements HeaderMutatingClientInterceptor {
static final String HYPER_QUERY_ID = "x-hyperdb-query-id";
public class QueryIdHeaderInterceptor implements SingleHeaderMutatingClientInterceptor {
@ToString.Exclude
public final Metadata.Key<String> key = keyOf("x-hyperdb-query-id");

public static final Metadata.Key<String> HYPER_QUERY_ID_KEY =
Metadata.Key.of(HYPER_QUERY_ID, ASCII_STRING_MARSHALLER);

private final String queryId;
@NonNull private final String value;

@Override
public void mutate(final Metadata headers) {
headers.put(HYPER_QUERY_ID_KEY, queryId);
if (value == null || value.isBlank()) {
return;
}

headers.put(key, value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2024, Salesforce, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.salesforce.datacloud.jdbc.interceptor;

import io.grpc.Metadata;
import lombok.NonNull;

interface SingleHeaderMutatingClientInterceptor extends HeaderMutatingClientInterceptor {
@NonNull Metadata.Key<String> getKey();

@NonNull String getValue();

default void mutate(final Metadata headers) {
headers.put(getKey(), getValue());
}
}
Loading

0 comments on commit 045eeb9

Please sign in to comment.