Skip to content

Commit fd5b13d

Browse files
refactor: add request id, change how registry is provided (#107)
* refactor: add request id, change how registry is provided * test: update tests, style
1 parent b3f39e7 commit fd5b13d

File tree

13 files changed

+98
-61
lines changed

13 files changed

+98
-61
lines changed

build.gradle.kts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
plugins {
2-
id("org.hypertrace.repository-plugin") version "0.4.0"
2+
id("org.hypertrace.repository-plugin") version "0.4.1"
33
id("org.hypertrace.ci-utils-plugin") version "0.3.0"
44
id("org.hypertrace.jacoco-report-plugin") version "0.2.0" apply false
5-
id("org.hypertrace.docker-java-application-plugin") version "0.9.0" apply false
6-
id("org.hypertrace.docker-publish-plugin") version "0.9.0" apply false
7-
id("org.hypertrace.code-style-plugin") version "1.1.0" apply false
5+
id("org.hypertrace.docker-java-application-plugin") version "0.9.5" apply false
6+
id("org.hypertrace.docker-publish-plugin") version "0.9.5" apply false
7+
id("org.hypertrace.code-style-plugin") version "1.1.2" apply false
88
}
99

1010
subprojects {

hypertrace-core-graphql-context/src/main/java/org/hypertrace/core/graphql/context/DefaultGraphQlRequestContextBuilder.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.Map;
1010
import java.util.Optional;
1111
import java.util.Set;
12+
import java.util.UUID;
1213
import java.util.concurrent.CompletableFuture;
1314
import java.util.stream.Collectors;
1415
import javax.annotation.Nonnull;
@@ -46,6 +47,8 @@ private final class DefaultGraphQlRequestContext implements GraphQlRequestContex
4647
private final GraphQLServletContext servletContext;
4748
private final ContextualCachingKey cachingKey;
4849

50+
private final String requestId = UUID.randomUUID().toString();
51+
4952
private DefaultGraphQlRequestContext(HttpServletRequest request, HttpServletResponse response) {
5053
this.servletContext =
5154
DefaultGraphQLServletContext.createServletContext().with(request).with(response).build();
@@ -102,6 +105,12 @@ public Map<String, String> getTracingContextHeaders() {
102105
public ContextualCachingKey getCachingKey() {
103106
return this.cachingKey;
104107
}
108+
109+
@Nonnull
110+
@Override
111+
public String getRequestId() {
112+
return this.requestId;
113+
}
105114
}
106115

107116
private static class DefaultContextualCacheKey implements ContextualCachingKey {

hypertrace-core-graphql-context/src/main/java/org/hypertrace/core/graphql/context/GraphQlRequestContext.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,6 @@ <T> DataFetcher<CompletableFuture<T>> constructDataFetcher(
2424

2525
@Nonnull
2626
ContextualCachingKey getCachingKey();
27+
28+
String getRequestId();
2729
}

hypertrace-core-graphql-grpc-utils/src/main/java/org/hypertrace/core/graphql/utils/grpc/DefaultGrpcChannelRegistry.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package org.hypertrace.core.graphql.utils.grpc;
22

3+
import io.grpc.Channel;
34
import io.grpc.ManagedChannel;
5+
import javax.inject.Inject;
46
import org.hypertrace.core.grpcutils.client.GrpcChannelConfig;
57

68
class DefaultGrpcChannelRegistry implements GrpcChannelRegistry {
79

810
private final org.hypertrace.core.grpcutils.client.GrpcChannelRegistry delegate;
911

12+
@Inject
1013
DefaultGrpcChannelRegistry(org.hypertrace.core.grpcutils.client.GrpcChannelRegistry delegate) {
1114
this.delegate = delegate;
1215
}
@@ -17,7 +20,7 @@ public ManagedChannel forAddress(String host, int port) {
1720
}
1821

1922
@Override
20-
public ManagedChannel forAddress(String host, int port, GrpcChannelConfig channelConfig) {
23+
public Channel forAddress(String host, int port, GrpcChannelConfig channelConfig) {
2124
return this.delegate.forPlaintextAddress(host, port, channelConfig);
2225
}
2326
}

hypertrace-core-graphql-grpc-utils/src/main/java/org/hypertrace/core/graphql/utils/grpc/GraphQlGrpcContextBuilder.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import org.hypertrace.core.graphql.context.GraphQlRequestContext;
44

5-
/** @deprecated Use {@link GrpcContextBuilder#build(GraphQlRequestContext)} instead */
5+
/**
6+
* @deprecated Use {@link GrpcContextBuilder#build(GraphQlRequestContext)} instead
7+
*/
68
@Deprecated
79
public interface GraphQlGrpcContextBuilder {
810
GraphQlGrpcContext build(GraphQlRequestContext requestContext);

hypertrace-core-graphql-grpc-utils/src/main/java/org/hypertrace/core/graphql/utils/grpc/GraphQlGrpcModule.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,11 @@
77

88
public class GraphQlGrpcModule extends AbstractModule {
99

10-
private final org.hypertrace.core.grpcutils.client.GrpcChannelRegistry channelRegistry;
11-
12-
public GraphQlGrpcModule(
13-
org.hypertrace.core.grpcutils.client.GrpcChannelRegistry channelRegistry) {
14-
this.channelRegistry = channelRegistry;
15-
}
16-
1710
@Override
1811
protected void configure() {
1912
bind(CallCredentials.class).toInstance(getClientCallCredsProvider().get());
2013
bind(GraphQlGrpcContextBuilder.class).to(DefaultGraphQlGrpcContextBuilder.class);
21-
bind(GrpcChannelRegistry.class)
22-
.toInstance(new DefaultGrpcChannelRegistry(this.channelRegistry));
14+
bind(GrpcChannelRegistry.class).to(DefaultGrpcChannelRegistry.class);
2315
bind(GrpcContextBuilder.class).to(PlatformGrpcContextBuilder.class);
2416
}
2517
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package org.hypertrace.core.graphql.utils.grpc;
22

33
import io.grpc.Channel;
4-
import io.grpc.ManagedChannel;
54
import org.hypertrace.core.grpcutils.client.GrpcChannelConfig;
65

76
public interface GrpcChannelRegistry {
87
Channel forAddress(String host, int port);
98

10-
ManagedChannel forAddress(String host, int port, GrpcChannelConfig channelConfig);
9+
Channel forAddress(String host, int port, GrpcChannelConfig channelConfig);
1110
}

hypertrace-core-graphql-grpc-utils/src/main/java/org/hypertrace/core/graphql/utils/grpc/PlatformGrpcContextBuilder.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.hypertrace.core.graphql.utils.grpc;
22

33
import static org.hypertrace.core.grpcutils.context.RequestContextConstants.AUTHORIZATION_HEADER;
4+
import static org.hypertrace.core.grpcutils.context.RequestContextConstants.REQUEST_ID_HEADER_KEY;
45
import static org.hypertrace.core.grpcutils.context.RequestContextConstants.TENANT_ID_HEADER_KEY;
56

67
import java.util.Arrays;
@@ -23,7 +24,8 @@ public RequestContext build(GraphQlRequestContext requestContext) {
2324
this.flattenOptionalMap(
2425
Map.of(
2526
AUTHORIZATION_HEADER, this.extractAuthorizationHeader(requestContext),
26-
TENANT_ID_HEADER_KEY, this.extractTenantId(requestContext))));
27+
TENANT_ID_HEADER_KEY, this.extractTenantId(requestContext),
28+
REQUEST_ID_HEADER_KEY, Optional.of(requestContext.getRequestId()))));
2729

2830
return this.build(grpcHeaders);
2931
}

hypertrace-core-graphql-grpc-utils/src/test/java/org/hypertrace/core/graphql/utils/grpc/PlatformGrpcContextBuilderTest.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package org.hypertrace.core.graphql.utils.grpc;
22

3-
import static java.util.Collections.emptyMap;
3+
import static org.hypertrace.core.grpcutils.context.RequestContextConstants.REQUEST_ID_HEADER_KEY;
44
import static org.hypertrace.core.grpcutils.context.RequestContextConstants.TENANT_ID_HEADER_KEY;
55
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
import static org.junit.jupiter.api.Assertions.assertFalse;
67
import static org.mockito.Mockito.when;
78

89
import java.util.Map;
@@ -23,28 +24,41 @@ class PlatformGrpcContextBuilderTest {
2324
@Test
2425
void addsTenantIdToContext() {
2526
when(this.mockRequestContext.getTenantId()).thenReturn(Optional.of("tenant id"));
27+
when(this.mockRequestContext.getRequestId()).thenReturn("request id");
2628
assertEquals(
2729
Optional.of("tenant id"), this.builder.build(this.mockRequestContext).getTenantId());
2830
}
2931

3032
@Test
3133
void addsTracingHeadersToContext() {
3234
when(this.mockRequestContext.getTenantId()).thenReturn(Optional.of("tenant id"));
35+
when(this.mockRequestContext.getRequestId()).thenReturn("request id");
3336
when(this.mockRequestContext.getTracingContextHeaders())
3437
.thenReturn(Map.of("traceid", "traceid value"));
3538
this.builder.build(this.mockRequestContext);
3639
assertEquals(
37-
Map.of(TENANT_ID_HEADER_KEY, "tenant id", "traceid", "traceid value"),
40+
Map.of(
41+
TENANT_ID_HEADER_KEY,
42+
"tenant id",
43+
REQUEST_ID_HEADER_KEY,
44+
"request id",
45+
"traceid",
46+
"traceid value"),
3847
this.builder.build(this.mockRequestContext).getRequestHeaders());
3948
}
4049

4150
@Test
4251
void passesAuthHeaderToPlatformContextIfPresent() {
43-
assertEquals(emptyMap(), this.builder.build(this.mockRequestContext).getRequestHeaders());
52+
when(this.mockRequestContext.getRequestId()).thenReturn("request id");
53+
assertFalse(
54+
this.builder
55+
.build(this.mockRequestContext)
56+
.getRequestHeaders()
57+
.containsKey("authorization"));
4458

4559
when(this.mockRequestContext.getAuthorizationHeader()).thenReturn(Optional.of("auth header"));
4660
assertEquals(
47-
Map.of("authorization", "auth header"),
48-
this.builder.build(this.mockRequestContext).getRequestHeaders());
61+
"auth header",
62+
this.builder.build(this.mockRequestContext).getRequestHeaders().get("authorization"));
4963
}
5064
}

hypertrace-core-graphql-impl/src/main/java/org/hypertrace/core/graphql/impl/GraphQlModule.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class GraphQlModule extends AbstractModule {
2525

2626
private final GraphQlServiceConfig config;
2727
private final GraphQlServiceLifecycle lifecycle;
28-
private final org.hypertrace.core.grpcutils.client.GrpcChannelRegistry grpcChannelRegistry;
28+
private final GrpcChannelRegistry grpcChannelRegistry;
2929

3030
public GraphQlModule(
3131
final GraphQlServiceConfig config,
@@ -40,8 +40,9 @@ public GraphQlModule(
4040
protected void configure() {
4141
bind(GraphQlServiceConfig.class).toInstance(this.config);
4242
bind(GraphQlServiceLifecycle.class).toInstance(this.lifecycle);
43+
bind(GrpcChannelRegistry.class).toInstance(this.grpcChannelRegistry);
4344
install(new GraphQlRequestContextModule());
44-
install(new GraphQlGrpcModule(this.grpcChannelRegistry));
45+
install(new GraphQlGrpcModule());
4546
install(new GraphQlSchemaRegistryModule());
4647
install(new GraphQlDeserializationRegistryModule());
4748
install(new CommonSchemaModule());

0 commit comments

Comments
 (0)