Skip to content

Commit 5d48c7b

Browse files
refactor: deprecate graphql request context (#68)
* refactor: deprecate graphql request context * refactor: migrated all usages in core
1 parent ae05224 commit 5d48c7b

File tree

22 files changed

+174
-226
lines changed

22 files changed

+174
-226
lines changed

hypertrace-core-graphql-attribute-store/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ dependencies {
1414
implementation("com.google.guava:guava")
1515

1616
implementation("org.hypertrace.core.attribute.service:attribute-service-api")
17+
implementation("org.hypertrace.core.grpcutils:grpc-client-rx-utils")
1718
implementation(project(":hypertrace-core-graphql-grpc-utils"))
1819
implementation(project(":hypertrace-core-graphql-rx-utils"))
1920

hypertrace-core-graphql-attribute-store/src/main/java/org/hypertrace/core/graphql/attributes/AttributeClient.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,21 @@
1212
import org.hypertrace.core.attribute.service.v1.Empty;
1313
import org.hypertrace.core.graphql.context.GraphQlRequestContext;
1414
import org.hypertrace.core.graphql.spi.config.GraphQlServiceConfig;
15-
import org.hypertrace.core.graphql.utils.grpc.GraphQlGrpcContextBuilder;
1615
import org.hypertrace.core.graphql.utils.grpc.GrpcChannelRegistry;
16+
import org.hypertrace.core.graphql.utils.grpc.GrpcContextBuilder;
17+
import org.hypertrace.core.grpcutils.client.rx.GrpcRxExecutionContext;
1718

1819
@Singleton
1920
class AttributeClient {
2021
private static final int DEFAULT_DEADLINE_SEC = 10;
2122
private final AttributeServiceStub attributeServiceClient;
22-
private final GraphQlGrpcContextBuilder grpcContextBuilder;
23+
private final GrpcContextBuilder grpcContextBuilder;
2324
private final AttributeModelTranslator translator;
2425

2526
@Inject
2627
AttributeClient(
2728
GraphQlServiceConfig serviceConfig,
28-
GraphQlGrpcContextBuilder grpcContextBuilder,
29+
GrpcContextBuilder grpcContextBuilder,
2930
GrpcChannelRegistry grpcChannelRegistry,
3031
CallCredentials credentials,
3132
AttributeModelTranslator translator) {
@@ -41,9 +42,8 @@ class AttributeClient {
4142
}
4243

4344
public Observable<AttributeModel> queryAll(GraphQlRequestContext requestContext) {
44-
return this.grpcContextBuilder
45-
.build(requestContext)
46-
.<AttributeMetadata>streamInContext(
45+
return GrpcRxExecutionContext.forContext(this.grpcContextBuilder.build(requestContext))
46+
.<AttributeMetadata>stream(
4747
streamObserver ->
4848
this.attributeServiceClient
4949
.withDeadlineAfter(DEFAULT_DEADLINE_SEC, TimeUnit.SECONDS)

hypertrace-core-graphql-attribute-store/src/main/java/org/hypertrace/core/graphql/attributes/AttributeStoreModule.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
import io.reactivex.rxjava3.core.Scheduler;
88
import org.hypertrace.core.graphql.rx.BoundedIoScheduler;
99
import org.hypertrace.core.graphql.spi.config.GraphQlServiceConfig;
10-
import org.hypertrace.core.graphql.utils.grpc.GraphQlGrpcContextBuilder;
1110
import org.hypertrace.core.graphql.utils.grpc.GrpcChannelRegistry;
11+
import org.hypertrace.core.graphql.utils.grpc.GrpcContextBuilder;
1212

1313
public class AttributeStoreModule extends AbstractModule {
1414

@@ -18,7 +18,7 @@ protected void configure() {
1818
Multibinder.newSetBinder(binder(), IdMapping.class);
1919
Multibinder.newSetBinder(binder(), IdMappingLoader.class);
2020
requireBinding(GraphQlServiceConfig.class);
21-
requireBinding(GraphQlGrpcContextBuilder.class);
21+
requireBinding(GrpcContextBuilder.class);
2222
requireBinding(CallCredentials.class);
2323
requireBinding(GrpcChannelRegistry.class);
2424
requireBinding(Key.get(Scheduler.class, BoundedIoScheduler.class));

hypertrace-core-graphql-grpc-utils/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ dependencies {
1111
api("io.grpc:grpc-core")
1212
api("io.grpc:grpc-stub")
1313
api(project(":hypertrace-core-graphql-context"))
14+
api("org.hypertrace.core.grpcutils:grpc-context-utils")
1415

15-
implementation("org.hypertrace.core.grpcutils:grpc-context-utils")
1616
implementation("org.hypertrace.core.grpcutils:grpc-client-utils")
1717
implementation("io.grpc:grpc-context")
1818
implementation("io.reactivex.rxjava3:rxjava")
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,40 @@
11
package org.hypertrace.core.graphql.utils.grpc;
22

3-
import static org.hypertrace.core.grpcutils.context.RequestContextConstants.AUTHORIZATION_HEADER;
4-
import static org.hypertrace.core.grpcutils.context.RequestContextConstants.TENANT_ID_HEADER_KEY;
5-
6-
import io.grpc.Context;
73
import io.grpc.stub.StreamObserver;
84
import io.reactivex.rxjava3.core.Observable;
9-
import java.util.Arrays;
10-
import java.util.Collection;
11-
import java.util.Map;
12-
import java.util.Map.Entry;
13-
import java.util.Optional;
145
import java.util.concurrent.Callable;
156
import java.util.function.Consumer;
16-
import java.util.stream.Collectors;
177
import javax.inject.Inject;
188
import org.hypertrace.core.graphql.context.GraphQlRequestContext;
199
import org.hypertrace.core.grpcutils.context.RequestContext;
2010

2111
class DefaultGraphQlGrpcContextBuilder implements GraphQlGrpcContextBuilder {
2212

23-
private final PlatformRequestContextBuilder platformRequestContextBuilder;
13+
private final PlatformGrpcContextBuilder platformGrpcContextBuilder;
2414

2515
@Inject
26-
DefaultGraphQlGrpcContextBuilder(PlatformRequestContextBuilder platformRequestContextBuilder) {
27-
this.platformRequestContextBuilder = platformRequestContextBuilder;
16+
DefaultGraphQlGrpcContextBuilder(PlatformGrpcContextBuilder platformGrpcContextBuilder) {
17+
this.platformGrpcContextBuilder = platformGrpcContextBuilder;
2818
}
2919

3020
@Override
3121
public GraphQlGrpcContext build(GraphQlRequestContext requestContext) {
32-
return new DefaultGraphQlGrpcContext(requestContext, this.platformRequestContextBuilder);
22+
return new DefaultGraphQlGrpcContext(requestContext, this.platformGrpcContextBuilder);
3323
}
3424

3525
private static final class DefaultGraphQlGrpcContext implements GraphQlGrpcContext {
36-
private final Context grpcContext;
26+
27+
private final RequestContext grpcContext;
3728

3829
private DefaultGraphQlGrpcContext(
3930
GraphQlRequestContext requestContext,
40-
PlatformRequestContextBuilder platformRequestContextBuilder) {
41-
Map<String, String> grpcHeaders =
42-
this.mergeMaps(
43-
requestContext.getTracingContextHeaders(),
44-
this.flattenOptionalMap(
45-
Map.of(
46-
AUTHORIZATION_HEADER, this.extractAuthorizationHeader(requestContext),
47-
TENANT_ID_HEADER_KEY, this.extractTenantId(requestContext))));
48-
49-
RequestContext platformContext = platformRequestContextBuilder.build(grpcHeaders);
50-
this.grpcContext = this.buildGrpcContext(platformContext);
51-
}
52-
53-
private Optional<String> extractAuthorizationHeader(GraphQlRequestContext requestContext) {
54-
return requestContext.getAuthorizationHeader().filter(header -> header.length() > 0);
55-
}
56-
57-
private Optional<String> extractTenantId(GraphQlRequestContext requestContext) {
58-
return requestContext.getTenantId();
31+
PlatformGrpcContextBuilder platformGrpcContextBuilder) {
32+
this.grpcContext = platformGrpcContextBuilder.build(requestContext);
5933
}
6034

6135
@Override
6236
public <TResp> TResp callInContext(Callable<TResp> callable) {
63-
try {
64-
return this.grpcContext.call(callable);
65-
} catch (Exception e) {
66-
throw new RuntimeException(e);
67-
}
37+
return this.grpcContext.call(callable);
6838
}
6939

7040
@Override
@@ -80,24 +50,5 @@ public <TResponse> Observable<TResponse> streamInContext(
8050
this.runInContext(
8151
() -> requestExecutor.accept(new StreamingClientResponseObserver<>(emitter))));
8252
}
83-
84-
private Context buildGrpcContext(RequestContext platformContext) {
85-
return Context.current().withValue(RequestContext.CURRENT, platformContext);
86-
}
87-
88-
private Map<String, String> flattenOptionalMap(Map<String, Optional<String>> optionalMap) {
89-
return optionalMap.entrySet().stream()
90-
.map(entry -> entry.getValue().map(value -> Map.entry(entry.getKey(), value)))
91-
.flatMap(Optional::stream)
92-
.collect(Collectors.toUnmodifiableMap(Entry::getKey, Entry::getValue));
93-
}
94-
95-
@SafeVarargs
96-
private Map<String, String> mergeMaps(Map<String, String>... maps) {
97-
return Arrays.stream(maps)
98-
.map(Map::entrySet)
99-
.flatMap(Collection::stream)
100-
.collect(Collectors.toUnmodifiableMap(Entry::getKey, Entry::getValue));
101-
}
10253
}
10354
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.concurrent.Callable;
66
import java.util.function.Consumer;
77

8+
@Deprecated
89
public interface GraphQlGrpcContext {
910

1011
<TResp> TResp callInContext(Callable<TResp> callable);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

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

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

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ protected void configure() {
1212
bind(CallCredentials.class).toInstance(getClientCallCredsProvider().get());
1313
bind(GraphQlGrpcContextBuilder.class).to(DefaultGraphQlGrpcContextBuilder.class);
1414
bind(GrpcChannelRegistry.class).to(DefaultGrpcChannelRegistry.class);
15+
bind(GrpcContextBuilder.class).to(PlatformGrpcContextBuilder.class);
1516
}
1617
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.hypertrace.core.graphql.utils.grpc;
2+
3+
import org.hypertrace.core.graphql.context.GraphQlRequestContext;
4+
import org.hypertrace.core.grpcutils.context.RequestContext;
5+
6+
public interface GrpcContextBuilder {
7+
RequestContext build(GraphQlRequestContext requestContext);
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package org.hypertrace.core.graphql.utils.grpc;
2+
3+
import static org.hypertrace.core.grpcutils.context.RequestContextConstants.AUTHORIZATION_HEADER;
4+
import static org.hypertrace.core.grpcutils.context.RequestContextConstants.TENANT_ID_HEADER_KEY;
5+
6+
import java.util.Arrays;
7+
import java.util.Collection;
8+
import java.util.Map;
9+
import java.util.Map.Entry;
10+
import java.util.Optional;
11+
import java.util.stream.Collectors;
12+
import javax.annotation.Nonnull;
13+
import org.hypertrace.core.graphql.context.GraphQlRequestContext;
14+
import org.hypertrace.core.grpcutils.context.RequestContext;
15+
16+
class PlatformGrpcContextBuilder implements GrpcContextBuilder {
17+
18+
@Override
19+
public RequestContext build(GraphQlRequestContext requestContext) {
20+
Map<String, String> grpcHeaders =
21+
this.mergeMaps(
22+
requestContext.getTracingContextHeaders(),
23+
this.flattenOptionalMap(
24+
Map.of(
25+
AUTHORIZATION_HEADER, this.extractAuthorizationHeader(requestContext),
26+
TENANT_ID_HEADER_KEY, this.extractTenantId(requestContext))));
27+
28+
return this.build(grpcHeaders);
29+
}
30+
31+
private RequestContext build(@Nonnull Map<String, String> headers) {
32+
RequestContext platformContext = new RequestContext();
33+
headers.forEach(platformContext::add);
34+
return platformContext;
35+
}
36+
37+
private Optional<String> extractAuthorizationHeader(GraphQlRequestContext requestContext) {
38+
return requestContext.getAuthorizationHeader().filter(header -> header.length() > 0);
39+
}
40+
41+
private Optional<String> extractTenantId(GraphQlRequestContext requestContext) {
42+
return requestContext.getTenantId();
43+
}
44+
45+
private Map<String, String> flattenOptionalMap(Map<String, Optional<String>> optionalMap) {
46+
return optionalMap.entrySet().stream()
47+
.map(entry -> entry.getValue().map(value -> Map.entry(entry.getKey(), value)))
48+
.flatMap(Optional::stream)
49+
.collect(Collectors.toUnmodifiableMap(Entry::getKey, Entry::getValue));
50+
}
51+
52+
@SafeVarargs
53+
private Map<String, String> mergeMaps(Map<String, String>... maps) {
54+
return Arrays.stream(maps)
55+
.map(Map::entrySet)
56+
.flatMap(Collection::stream)
57+
.collect(Collectors.toUnmodifiableMap(Entry::getKey, Entry::getValue));
58+
}
59+
}

0 commit comments

Comments
 (0)