File tree Expand file tree Collapse file tree 3 files changed +24
-0
lines changed
hypertrace-core-graphql-grpc-utils/src
main/java/org/hypertrace/core/graphql/utils/grpc
test/java/org/hypertrace/core/graphql/utils/grpc Expand file tree Collapse file tree 3 files changed +24
-0
lines changed Original file line number Diff line number Diff line change 1
1
package org .hypertrace .core .graphql .utils .grpc ;
2
2
3
+ import java .util .Optional ;
3
4
import org .hypertrace .core .graphql .context .GraphQlRequestContext ;
4
5
import org .hypertrace .core .grpcutils .context .RequestContext ;
5
6
6
7
public interface GrpcContextBuilder {
7
8
RequestContext build (GraphQlRequestContext requestContext );
9
+
10
+ Optional <GraphQlRequestContext > tryRestore (RequestContext requestContext );
8
11
}
Original file line number Diff line number Diff line change 4
4
import static org .hypertrace .core .grpcutils .context .RequestContextConstants .REQUEST_ID_HEADER_KEY ;
5
5
import static org .hypertrace .core .grpcutils .context .RequestContextConstants .TENANT_ID_HEADER_KEY ;
6
6
7
+ import com .google .common .cache .Cache ;
8
+ import com .google .common .cache .CacheBuilder ;
9
+ import java .time .Duration ;
7
10
import java .util .Arrays ;
8
11
import java .util .Collection ;
9
12
import java .util .Map ;
16
19
17
20
class PlatformGrpcContextBuilder implements GrpcContextBuilder {
18
21
22
+ private final Cache <String , GraphQlRequestContext > contextCache =
23
+ CacheBuilder .newBuilder ().expireAfterAccess (Duration .ofMinutes (1 )).maximumSize (1000 ).build ();
24
+
19
25
@ Override
20
26
public RequestContext build (GraphQlRequestContext requestContext ) {
27
+ this .contextCache .put (requestContext .getRequestId (), requestContext );
21
28
Map <String , String > grpcHeaders =
22
29
this .mergeMaps (
23
30
requestContext .getTracingContextHeaders (),
@@ -30,6 +37,11 @@ public RequestContext build(GraphQlRequestContext requestContext) {
30
37
return this .build (grpcHeaders );
31
38
}
32
39
40
+ @ Override
41
+ public Optional <GraphQlRequestContext > tryRestore (RequestContext requestContext ) {
42
+ return requestContext .getRequestId ().map (this .contextCache ::getIfPresent );
43
+ }
44
+
33
45
private RequestContext build (@ Nonnull Map <String , String > headers ) {
34
46
RequestContext platformContext = new RequestContext ();
35
47
headers .forEach (platformContext ::add );
Original file line number Diff line number Diff line change 9
9
import java .util .Map ;
10
10
import java .util .Optional ;
11
11
import org .hypertrace .core .graphql .context .GraphQlRequestContext ;
12
+ import org .hypertrace .core .grpcutils .context .RequestContext ;
12
13
import org .junit .jupiter .api .Test ;
13
14
import org .junit .jupiter .api .extension .ExtendWith ;
14
15
import org .mockito .Mock ;
@@ -61,4 +62,12 @@ void passesAuthHeaderToPlatformContextIfPresent() {
61
62
"auth header" ,
62
63
this .builder .build (this .mockRequestContext ).getRequestHeaders ().get ("authorization" ));
63
64
}
65
+
66
+ @ Test
67
+ void testRestoreContext () {
68
+ when (this .mockRequestContext .getRequestId ()).thenReturn ("request id" );
69
+ RequestContext resultContext = this .builder .build (this .mockRequestContext );
70
+ assertEquals (Optional .empty (), this .builder .tryRestore (RequestContext .forTenantId ("other" )));
71
+ assertEquals (Optional .of (this .mockRequestContext ), this .builder .tryRestore (resultContext ));
72
+ }
64
73
}
You can’t perform that action at this time.
0 commit comments