|
| 1 | +package io.temporal.common.interceptors; |
| 2 | + |
| 3 | +import io.temporal.api.common.v1.WorkflowExecution; |
| 4 | +import io.temporal.client.WorkflowOptions; |
| 5 | +import io.temporal.client.WorkflowStub; |
| 6 | + |
| 7 | +import java.lang.reflect.Type; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.Map; |
| 10 | +import java.util.Optional; |
| 11 | +import java.util.concurrent.CompletableFuture; |
| 12 | +import java.util.concurrent.TimeUnit; |
| 13 | +import java.util.concurrent.TimeoutException; |
| 14 | + |
| 15 | +/** |
| 16 | + * This is a "better" implementation, that will give right behavior. |
| 17 | + * Requires getOptions setOptions on stub to hack it right before the call. |
| 18 | + * Check out {@link SampleOpenTracingLikeInterceptor} before this class. |
| 19 | + */ |
| 20 | +public class SampleOpenTracingLikeInterceptor2 extends WorkflowClientInterceptorBase { |
| 21 | + @Override |
| 22 | + public WorkflowStub newUntypedWorkflowStub( |
| 23 | + String workflowType, WorkflowOptions options, WorkflowStub next) { |
| 24 | + return new WorkflowStub() { |
| 25 | + private void hackHeaders() { |
| 26 | + //We shouldn't use headers that are passed as a parameter to newUntypedWorkflowStub, because |
| 27 | + //they can be outdated at a time of call because of the exposed setOptions |
| 28 | + WorkflowOptions options = next.getOptions().orElse(null); |
| 29 | + Map<String, Object> originalHeaders = options != null ? options.getHeaders() : null; |
| 30 | + Map<String, Object> newHeaders; |
| 31 | + |
| 32 | + if (originalHeaders == null) { |
| 33 | + newHeaders = new HashMap<>(); |
| 34 | + } else { |
| 35 | + newHeaders = new HashMap<>(originalHeaders); |
| 36 | + } |
| 37 | + newHeaders.put("opentracing", new Object()); |
| 38 | + WorkflowOptions modifiedOptions = |
| 39 | + WorkflowOptions.newBuilder(options).setHeaders(newHeaders).build(); |
| 40 | + next.setOptions(modifiedOptions); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public WorkflowExecution start(Object... args) { |
| 45 | + hackHeaders(); |
| 46 | + return next.start(args); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public WorkflowExecution signalWithStart(String signalName, Object[] signalArgs, Object[] startArgs) { |
| 51 | + hackHeaders(); |
| 52 | + return next.signalWithStart(signalName, signalArgs, startArgs); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public void signal(String signalName, Object... args) { |
| 57 | + next.signal(signalName, args); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public Optional<String> getWorkflowType() { |
| 62 | + return next.getWorkflowType(); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public WorkflowExecution getExecution() { |
| 67 | + return next.getExecution(); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public <R> R getResult(Class<R> resultClass, Type resultType) { |
| 72 | + return next.getResult(resultClass, resultType); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public <R> CompletableFuture<R> getResultAsync(Class<R> resultClass, Type resultType) { |
| 77 | + return next.getResultAsync(resultClass, resultType); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public <R> R getResult(Class<R> resultClass) { |
| 82 | + return next.getResult(resultClass); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public <R> CompletableFuture<R> getResultAsync(Class<R> resultClass) { |
| 87 | + return next.getResultAsync(resultClass); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public <R> R getResult(long timeout, TimeUnit unit, Class<R> resultClass, Type resultType) throws TimeoutException { |
| 92 | + return next.getResult(timeout, unit, resultClass, resultType); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public <R> R getResult(long timeout, TimeUnit unit, Class<R> resultClass) throws TimeoutException { |
| 97 | + return next.getResult(timeout, unit, resultClass); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public <R> CompletableFuture<R> getResultAsync(long timeout, TimeUnit unit, Class<R> resultClass, Type resultType) { |
| 102 | + return next.getResultAsync(timeout, unit, resultClass, resultType); |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public <R> CompletableFuture<R> getResultAsync(long timeout, TimeUnit unit, Class<R> resultClass) { |
| 107 | + return next.getResultAsync(timeout, unit, resultClass); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public <R> R query(String queryType, Class<R> resultClass, Object... args) { |
| 112 | + return next.query(queryType, resultClass, args); |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public <R> R query(String queryType, Class<R> resultClass, Type resultType, Object... args) { |
| 117 | + return next.query(queryType, resultClass, resultType, args); |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public void cancel() { |
| 122 | + next.cancel(); |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public void terminate(String reason, Object... details) { |
| 127 | + next.terminate(reason, details); |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public Optional<WorkflowOptions> getOptions() { |
| 132 | + return next.getOptions(); |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public void setOptions(WorkflowOptions options) { |
| 137 | + next.setOptions(options); |
| 138 | + } |
| 139 | + }; |
| 140 | + } |
| 141 | +} |
0 commit comments