Skip to content

Commit

Permalink
feat: onetime headers for agent and runtime-version (#359)
Browse files Browse the repository at this point in the history
  • Loading branch information
rishtigupta authored Jun 28, 2024
1 parent e6f41a4 commit 7096929
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private static ManagedChannel setupConnection(
GrpcChannelOptions.applyGrpcConfigurationToChannelBuilder(controlConfig, channelBuilder);

final List<ClientInterceptor> clientInterceptors = new ArrayList<>();
clientInterceptors.add(new UserHeaderInterceptor(credentialProvider.getAuthToken()));
clientInterceptors.add(new UserHeaderInterceptor(credentialProvider.getAuthToken(), "cache"));
channelBuilder.intercept(clientInterceptors);
return channelBuilder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ private ManagedChannel setupChannel(
configuration.getTransportStrategy().getGrpcConfiguration(), channelBuilder);

final List<ClientInterceptor> clientInterceptors = new ArrayList<>();
clientInterceptors.add(new UserHeaderInterceptor(credentialProvider.getAuthToken()));
clientInterceptors.add(new UserHeaderInterceptor(credentialProvider.getAuthToken(), "cache"));
clientInterceptors.add(
new RetryClientInterceptor(
configuration.getRetryStrategy(), retryScheduler, retryExecutor));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private static ManagedChannel setupConnection(CredentialProvider credentialProvi
GrpcChannelOptions.applyGrpcConfigurationToChannelBuilder(grpcConfig, channelBuilder);

final List<ClientInterceptor> clientInterceptors = new ArrayList<>();
clientInterceptors.add(new UserHeaderInterceptor(credentialProvider.getAuthToken()));
clientInterceptors.add(new UserHeaderInterceptor(credentialProvider.getAuthToken(), "auth"));
channelBuilder.intercept(clientInterceptors);
return channelBuilder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private static ManagedChannel setupConnection(
configuration.getTransportStrategy().getGrpcConfiguration(), channelBuilder);

final List<ClientInterceptor> clientInterceptors = new ArrayList<>();
clientInterceptors.add(new UserHeaderInterceptor(credentialProvider.getAuthToken()));
clientInterceptors.add(new UserHeaderInterceptor(credentialProvider.getAuthToken(), "topic"));
channelBuilder.intercept(clientInterceptors);
return channelBuilder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private static ManagedChannel setupConnection(
GrpcChannelOptions.applyGrpcConfigurationToChannelBuilder(controlConfig, channelBuilder);

final List<ClientInterceptor> clientInterceptors = new ArrayList<>();
clientInterceptors.add(new UserHeaderInterceptor(credentialProvider.getAuthToken()));
clientInterceptors.add(new UserHeaderInterceptor(credentialProvider.getAuthToken(), "store"));
channelBuilder.intercept(clientInterceptors);
return channelBuilder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ private ManagedChannel setupChannel(
configuration.getTransportStrategy().getGrpcConfiguration(), channelBuilder);

final List<ClientInterceptor> clientInterceptors = new ArrayList<>();
clientInterceptors.add(new UserHeaderInterceptor(credentialProvider.getAuthToken()));
clientInterceptors.add(new UserHeaderInterceptor(credentialProvider.getAuthToken(), "store"));
channelBuilder.intercept(clientInterceptors);

return channelBuilder.build();
Expand Down
19 changes: 13 additions & 6 deletions momento-sdk/src/main/java/momento/sdk/UserHeaderInterceptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,22 @@
final class UserHeaderInterceptor implements ClientInterceptor {

private static final Metadata.Key<String> AUTH_HEADER_KEY =
Metadata.Key.of("Authorization", ASCII_STRING_MARSHALLER);
Metadata.Key.of("authorization", ASCII_STRING_MARSHALLER);
private static final Metadata.Key<String> SDK_AGENT_KEY =
Metadata.Key.of("Agent", ASCII_STRING_MARSHALLER);
Metadata.Key.of("agent", ASCII_STRING_MARSHALLER);
private static final Metadata.Key<String> RUNTIME_VERSION_KEY =
Metadata.Key.of("runtime-version", ASCII_STRING_MARSHALLER);
private final String tokenValue;
private final String sdkVersion =
String.format("java:%s", this.getClass().getPackage().getImplementationVersion());
private static volatile boolean isUserAgentSent = false;
private final String sdkVersion;
private final String runtimeVersion;
private boolean isUserAgentSent = false;

UserHeaderInterceptor(String token) {
UserHeaderInterceptor(String token, String clientType) {
tokenValue = token;
sdkVersion =
String.format(
"java:%s:%s", clientType, this.getClass().getPackage().getImplementationVersion());
runtimeVersion = System.getProperty("java.vendor") + ", " + System.getProperty("java.version");
}

@Override
Expand All @@ -35,6 +41,7 @@ public void start(Listener<RespT> listener, Metadata metadata) {
metadata.put(AUTH_HEADER_KEY, tokenValue);
if (!isUserAgentSent) {
metadata.put(SDK_AGENT_KEY, sdkVersion);
metadata.put(RUNTIME_VERSION_KEY, runtimeVersion);
isUserAgentSent = true;
}
super.start(listener, metadata);
Expand Down

0 comments on commit 7096929

Please sign in to comment.